blob: 94735b0871894ab9c2a95ce253c3981f90ed6c87 [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"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000019#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000021#include "clang/AST/DeclObjC.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000022#include "clang/AST/RecordLayout.h"
Chris Lattner16f00492009-04-26 01:32:48 +000023#include "clang/AST/StmtObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000024#include "clang/Basic/LangOptions.h"
Daniel Dunbar2feefe82010-02-01 21:07:33 +000025#include "clang/CodeGen/CodeGenOptions.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000026
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +000027#include "llvm/Intrinsics.h"
Owen Anderson69243822009-07-13 04:10:07 +000028#include "llvm/LLVMContext.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000029#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000030#include "llvm/ADT/DenseSet.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000031#include "llvm/ADT/SetVector.h"
32#include "llvm/ADT/SmallString.h"
Fariborz Jahanian191dcd72009-12-12 21:26:21 +000033#include "llvm/ADT/SmallPtrSet.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000034#include "llvm/Support/raw_ostream.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000035#include "llvm/Target/TargetData.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000036#include <cstdio>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000037
38using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000039using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000040
Daniel Dunbar97776872009-04-22 07:32:20 +000041// Common CGObjCRuntime functions, these don't belong here, but they
42// don't belong in CGObjCRuntime either so we will live with it for
43// now.
44
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000045static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM,
46 const ObjCInterfaceDecl *OID,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000047 const ObjCImplementationDecl *ID,
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000048 const ObjCIvarDecl *Ivar) {
Daniel Dunbare83be122010-04-02 21:14:02 +000049 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar532d4da2009-05-03 13:15:50 +000050
Daniel Dunbar61ac1d22010-04-02 22:29:40 +000051 // FIXME: We should eliminate the need to have ObjCImplementationDecl passed
52 // in here; it should never be necessary because that should be the lexical
53 // decl context for the ivar.
Daniel Dunbar3a2c80f2010-04-02 15:43:29 +000054
Daniel Dunbar532d4da2009-05-03 13:15:50 +000055 // If we know have an implementation (and the ivar is in it) then
56 // look up in the implementation layout.
Daniel Dunbar6bff2512009-08-03 17:06:42 +000057 const ASTRecordLayout *RL;
Daniel Dunbar532d4da2009-05-03 13:15:50 +000058 if (ID && ID->getClassInterface() == Container)
59 RL = &CGM.getContext().getASTObjCImplementationLayout(ID);
60 else
61 RL = &CGM.getContext().getASTObjCInterfaceLayout(Container);
Daniel Dunbare83be122010-04-02 21:14:02 +000062
63 // Compute field index.
64 //
65 // FIXME: The index here is closely tied to how ASTContext::getObjCLayout is
66 // implemented. This should be fixed to get the information from the layout
67 // directly.
68 unsigned Index = 0;
69 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
70 CGM.getContext().ShallowCollectObjCIvars(Container, Ivars);
71 for (unsigned k = 0, e = Ivars.size(); k != e; ++k) {
72 if (Ivar == Ivars[k])
73 break;
74 ++Index;
75 }
76 assert(Index != Ivars.size() && "Ivar is not inside container!");
77
Daniel Dunbar532d4da2009-05-03 13:15:50 +000078 return RL->getFieldOffset(Index);
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000079}
80
81uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
82 const ObjCInterfaceDecl *OID,
83 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000084 return LookupFieldBitOffset(CGM, OID, 0, Ivar) / 8;
85}
86
87uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
88 const ObjCImplementationDecl *OID,
89 const ObjCIvarDecl *Ivar) {
90 return LookupFieldBitOffset(CGM, OID->getClassInterface(), OID, Ivar) / 8;
Daniel Dunbar97776872009-04-22 07:32:20 +000091}
92
93LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
94 const ObjCInterfaceDecl *OID,
95 llvm::Value *BaseValue,
96 const ObjCIvarDecl *Ivar,
97 unsigned CVRQualifiers,
98 llvm::Value *Offset) {
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000099 // Compute (type*) ( (char *) BaseValue + Offset)
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000100 const llvm::Type *I8Ptr = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000101 QualType IvarTy = Ivar->getType();
102 const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy);
Daniel Dunbar97776872009-04-22 07:32:20 +0000103 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr);
Daniel Dunbar97776872009-04-22 07:32:20 +0000104 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000105 V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000106
John McCall0953e762009-09-24 19:53:00 +0000107 Qualifiers Quals = CGF.MakeQualifiers(IvarTy);
108 Quals.addCVRQualifiers(CVRQualifiers);
109
Daniel Dunbar56229f52010-04-05 16:20:33 +0000110 if (!Ivar->isBitField())
111 return LValue::MakeAddr(V, Quals);
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +0000112
Daniel Dunbar56229f52010-04-05 16:20:33 +0000113 // We need to compute the bit offset for the bit-field, the offset is to the
114 // byte. Note, there is a subtle invariant here: we can only call this routine
115 // on non-synthesized ivars but we may be called for synthesized ivars.
116 // However, a synthesized ivar can never be a bit-field, so this is safe.
117 uint64_t BitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar) % 8;
118 uint64_t BitFieldSize =
119 Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue();
Daniel Dunbar97776872009-04-22 07:32:20 +0000120
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000121 // Allocate a new CGBitFieldInfo object to describe this access.
122 //
123 // FIXME: This is incredibly wasteful, these should be uniqued or part of some
124 // layout object. However, this is blocked on other cleanups to the
125 // Objective-C code, so for now we just live with allocating a bunch of these
126 // objects.
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000127
Daniel Dunbarab970f92010-04-13 20:58:55 +0000128 // We always construct a single, possibly unaligned, access for this case.
Daniel Dunbar2df25692010-04-15 05:09:32 +0000129 CGBitFieldInfo::AccessInfo AI;
Daniel Dunbarab970f92010-04-13 20:58:55 +0000130 AI.FieldIndex = 0;
131 AI.FieldByteOffset = 0;
132 AI.FieldBitStart = BitOffset;
133 AI.AccessWidth = CGF.CGM.getContext().getTypeSize(IvarTy);
134 AI.AccessAlignment = 0;
135 AI.TargetBitOffset = 0;
136 AI.TargetBitWidth = BitFieldSize;
137
Daniel Dunbar2df25692010-04-15 05:09:32 +0000138 CGBitFieldInfo *Info =
139 new (CGF.CGM.getContext()) CGBitFieldInfo(BitFieldSize, 1, &AI,
140 IvarTy->isSignedIntegerType());
141
Daniel Dunbar56229f52010-04-05 16:20:33 +0000142 // FIXME: We need to set a very conservative alignment on this, or make sure
143 // that the runtime is doing the right thing.
Daniel Dunbarefbf4872010-04-06 01:07:44 +0000144 return LValue::MakeBitfield(V, *Info, Quals.getCVRQualifiers());
Daniel Dunbar97776872009-04-22 07:32:20 +0000145}
146
147///
148
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000149namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000150
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000151typedef std::vector<llvm::Constant*> ConstantVector;
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000152
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000153// FIXME: We should find a nicer way to make the labels for metadata, string
154// concatenation is lame.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000155
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000156class ObjCCommonTypesHelper {
Owen Andersona1cf15f2009-07-14 23:10:40 +0000157protected:
158 llvm::LLVMContext &VMContext;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000159
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000160private:
161 llvm::Constant *getMessageSendFn() const {
162 // id objc_msgSend (id, SEL, ...)
163 std::vector<const llvm::Type*> Params;
164 Params.push_back(ObjectPtrTy);
165 Params.push_back(SelectorPtrTy);
166 return
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000167 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
168 Params, true),
169 "objc_msgSend");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000170 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000171
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000172 llvm::Constant *getMessageSendStretFn() const {
173 // id objc_msgSend_stret (id, SEL, ...)
174 std::vector<const llvm::Type*> Params;
175 Params.push_back(ObjectPtrTy);
176 Params.push_back(SelectorPtrTy);
177 return
Owen Anderson0032b272009-08-13 21:57:51 +0000178 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000179 Params, true),
180 "objc_msgSend_stret");
181
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000182 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000183
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000184 llvm::Constant *getMessageSendFpretFn() const {
185 // FIXME: This should be long double on x86_64?
186 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
187 std::vector<const llvm::Type*> Params;
188 Params.push_back(ObjectPtrTy);
189 Params.push_back(SelectorPtrTy);
190 return
Owen Anderson0032b272009-08-13 21:57:51 +0000191 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
192 llvm::Type::getDoubleTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000193 Params,
194 true),
195 "objc_msgSend_fpret");
196
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000197 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000198
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000199 llvm::Constant *getMessageSendSuperFn() const {
200 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
201 const char *SuperName = "objc_msgSendSuper";
202 std::vector<const llvm::Type*> Params;
203 Params.push_back(SuperPtrTy);
204 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000205 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000206 Params, true),
207 SuperName);
208 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000209
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000210 llvm::Constant *getMessageSendSuperFn2() const {
211 // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
212 const char *SuperName = "objc_msgSendSuper2";
213 std::vector<const llvm::Type*> Params;
214 Params.push_back(SuperPtrTy);
215 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000216 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000217 Params, true),
218 SuperName);
219 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000220
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000221 llvm::Constant *getMessageSendSuperStretFn() const {
222 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
223 // SEL op, ...)
224 std::vector<const llvm::Type*> Params;
225 Params.push_back(Int8PtrTy);
226 Params.push_back(SuperPtrTy);
227 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000228 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000229 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000230 Params, true),
231 "objc_msgSendSuper_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000232 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000233
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000234 llvm::Constant *getMessageSendSuperStretFn2() const {
235 // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
236 // SEL op, ...)
237 std::vector<const llvm::Type*> Params;
238 Params.push_back(Int8PtrTy);
239 Params.push_back(SuperPtrTy);
240 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000241 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000242 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000243 Params, true),
244 "objc_msgSendSuper2_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000245 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000246
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000247 llvm::Constant *getMessageSendSuperFpretFn() const {
248 // There is no objc_msgSendSuper_fpret? How can that work?
249 return getMessageSendSuperFn();
250 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000251
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000252 llvm::Constant *getMessageSendSuperFpretFn2() const {
253 // There is no objc_msgSendSuper_fpret? How can that work?
254 return getMessageSendSuperFn2();
255 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000256
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000257protected:
258 CodeGen::CodeGenModule &CGM;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000259
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000260public:
Fariborz Jahanian0a855d02009-03-23 19:10:40 +0000261 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000262 const llvm::Type *Int8PtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000263
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000264 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
265 const llvm::Type *ObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000266
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000267 /// PtrObjectPtrTy - LLVM type for id *
268 const llvm::Type *PtrObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000269
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000270 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000271 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000272 /// ProtocolPtrTy - LLVM type for external protocol handles
273 /// (typeof(Protocol))
274 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000275
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000276 // SuperCTy - clang type for struct objc_super.
277 QualType SuperCTy;
278 // SuperPtrCTy - clang type for struct objc_super *.
279 QualType SuperPtrCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000280
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000281 /// SuperTy - LLVM type for struct objc_super.
282 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000283 /// SuperPtrTy - LLVM type for struct objc_super *.
284 const llvm::Type *SuperPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000285
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000286 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
287 /// in GCC parlance).
288 const llvm::StructType *PropertyTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000289
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000290 /// PropertyListTy - LLVM type for struct objc_property_list
291 /// (_prop_list_t in GCC parlance).
292 const llvm::StructType *PropertyListTy;
293 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
294 const llvm::Type *PropertyListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000295
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000296 // MethodTy - LLVM type for struct objc_method.
297 const llvm::StructType *MethodTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000298
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000299 /// CacheTy - LLVM type for struct objc_cache.
300 const llvm::Type *CacheTy;
301 /// CachePtrTy - LLVM type for struct objc_cache *.
302 const llvm::Type *CachePtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000303
Chris Lattner72db6c32009-04-22 02:44:54 +0000304 llvm::Constant *getGetPropertyFn() {
305 CodeGen::CodeGenTypes &Types = CGM.getTypes();
306 ASTContext &Ctx = CGM.getContext();
307 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
John McCallead608a2010-02-26 00:48:12 +0000308 llvm::SmallVector<CanQualType,4> Params;
309 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
310 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000311 Params.push_back(IdType);
312 Params.push_back(SelType);
313 Params.push_back(Ctx.LongTy);
314 Params.push_back(Ctx.BoolTy);
315 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000316 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000317 FunctionType::ExtInfo()),
318 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000319 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
320 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000321
Chris Lattner72db6c32009-04-22 02:44:54 +0000322 llvm::Constant *getSetPropertyFn() {
323 CodeGen::CodeGenTypes &Types = CGM.getTypes();
324 ASTContext &Ctx = CGM.getContext();
325 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
John McCallead608a2010-02-26 00:48:12 +0000326 llvm::SmallVector<CanQualType,6> Params;
327 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
328 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000329 Params.push_back(IdType);
330 Params.push_back(SelType);
331 Params.push_back(Ctx.LongTy);
332 Params.push_back(IdType);
333 Params.push_back(Ctx.BoolTy);
334 Params.push_back(Ctx.BoolTy);
335 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000336 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000337 FunctionType::ExtInfo()),
338 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000339 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
340 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000341
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000342
343 llvm::Constant *getCopyStructFn() {
344 CodeGen::CodeGenTypes &Types = CGM.getTypes();
345 ASTContext &Ctx = CGM.getContext();
346 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
347 llvm::SmallVector<CanQualType,5> Params;
348 Params.push_back(Ctx.VoidPtrTy);
349 Params.push_back(Ctx.VoidPtrTy);
350 Params.push_back(Ctx.LongTy);
351 Params.push_back(Ctx.BoolTy);
352 Params.push_back(Ctx.BoolTy);
353 const llvm::FunctionType *FTy =
354 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
355 FunctionType::ExtInfo()),
356 false);
357 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
358 }
359
Chris Lattner72db6c32009-04-22 02:44:54 +0000360 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000361 CodeGen::CodeGenTypes &Types = CGM.getTypes();
362 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000363 // void objc_enumerationMutation (id)
John McCallead608a2010-02-26 00:48:12 +0000364 llvm::SmallVector<CanQualType,1> Params;
365 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000366 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000367 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000368 FunctionType::ExtInfo()),
369 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000370 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
371 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000372
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000373 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000374 llvm::Constant *getGcReadWeakFn() {
375 // id objc_read_weak (id *)
376 std::vector<const llvm::Type*> Args;
377 Args.push_back(ObjectPtrTy->getPointerTo());
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000378 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000379 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000380 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000381 }
382
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000383 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000384 llvm::Constant *getGcAssignWeakFn() {
385 // id objc_assign_weak (id, id *)
386 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
387 Args.push_back(ObjectPtrTy->getPointerTo());
388 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000389 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner96508e12009-04-17 22:12:36 +0000390 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
391 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000392
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000393 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000394 llvm::Constant *getGcAssignGlobalFn() {
395 // id objc_assign_global(id, id *)
396 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
397 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000398 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000399 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000400 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
401 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000402
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000403 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000404 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000405 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattnerbbccd612009-04-22 02:38:11 +0000406 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
407 Args.push_back(ObjectPtrTy->getPointerTo());
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000408 Args.push_back(LongTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000409 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000410 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000411 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
412 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000413
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000414 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
415 llvm::Constant *GcMemmoveCollectableFn() {
416 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
417 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
418 Args.push_back(Int8PtrTy);
419 Args.push_back(LongTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000420 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000421 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
422 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000423
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000424 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000425 llvm::Constant *getGcAssignStrongCastFn() {
426 // id objc_assign_global(id, id *)
427 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
428 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000429 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000430 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000431 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
432 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000433
434 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000435 llvm::Constant *getExceptionThrowFn() {
436 // void objc_exception_throw(id)
437 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
438 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000439 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000440 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
441 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000442
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000443 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
444 llvm::Constant *getExceptionRethrowFn() {
445 // void objc_exception_rethrow(void)
446 std::vector<const llvm::Type*> Args;
447 llvm::FunctionType *FTy =
448 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, true);
449 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
450 }
451
Daniel Dunbar1c566672009-02-24 01:43:46 +0000452 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000453 llvm::Constant *getSyncEnterFn() {
454 // void objc_sync_enter (id)
455 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
456 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000457 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000458 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
459 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000460
Daniel Dunbar1c566672009-02-24 01:43:46 +0000461 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000462 llvm::Constant *getSyncExitFn() {
463 // void objc_sync_exit (id)
464 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
465 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000466 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000467 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
468 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000469
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000470 llvm::Constant *getSendFn(bool IsSuper) const {
471 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
472 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000473
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000474 llvm::Constant *getSendFn2(bool IsSuper) const {
475 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
476 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000477
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000478 llvm::Constant *getSendStretFn(bool IsSuper) const {
479 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
480 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000481
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000482 llvm::Constant *getSendStretFn2(bool IsSuper) const {
483 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
484 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000485
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000486 llvm::Constant *getSendFpretFn(bool IsSuper) const {
487 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
488 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000489
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000490 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
491 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
492 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000493
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000494 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
495 ~ObjCCommonTypesHelper(){}
496};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000497
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000498/// ObjCTypesHelper - Helper class that encapsulates lazy
499/// construction of varies types used during ObjC generation.
500class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000501public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000502 /// SymtabTy - LLVM type for struct objc_symtab.
503 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000504 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
505 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000506 /// ModuleTy - LLVM type for struct objc_module.
507 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000508
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000509 /// ProtocolTy - LLVM type for struct objc_protocol.
510 const llvm::StructType *ProtocolTy;
511 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
512 const llvm::Type *ProtocolPtrTy;
513 /// ProtocolExtensionTy - LLVM type for struct
514 /// objc_protocol_extension.
515 const llvm::StructType *ProtocolExtensionTy;
516 /// ProtocolExtensionTy - LLVM type for struct
517 /// objc_protocol_extension *.
518 const llvm::Type *ProtocolExtensionPtrTy;
519 /// MethodDescriptionTy - LLVM type for struct
520 /// objc_method_description.
521 const llvm::StructType *MethodDescriptionTy;
522 /// MethodDescriptionListTy - LLVM type for struct
523 /// objc_method_description_list.
524 const llvm::StructType *MethodDescriptionListTy;
525 /// MethodDescriptionListPtrTy - LLVM type for struct
526 /// objc_method_description_list *.
527 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000528 /// ProtocolListTy - LLVM type for struct objc_property_list.
529 const llvm::Type *ProtocolListTy;
530 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
531 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000532 /// CategoryTy - LLVM type for struct objc_category.
533 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000534 /// ClassTy - LLVM type for struct objc_class.
535 const llvm::StructType *ClassTy;
536 /// ClassPtrTy - LLVM type for struct objc_class *.
537 const llvm::Type *ClassPtrTy;
538 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
539 const llvm::StructType *ClassExtensionTy;
540 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
541 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000542 // IvarTy - LLVM type for struct objc_ivar.
543 const llvm::StructType *IvarTy;
544 /// IvarListTy - LLVM type for struct objc_ivar_list.
545 const llvm::Type *IvarListTy;
546 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
547 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000548 /// MethodListTy - LLVM type for struct objc_method_list.
549 const llvm::Type *MethodListTy;
550 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
551 const llvm::Type *MethodListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000552
Anders Carlsson124526b2008-09-09 10:10:21 +0000553 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
554 const llvm::Type *ExceptionDataTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000555
Anders Carlsson124526b2008-09-09 10:10:21 +0000556 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000557 llvm::Constant *getExceptionTryEnterFn() {
558 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000559 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +0000560 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000561 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000562 Params, false),
563 "objc_exception_try_enter");
Chris Lattner34b02a12009-04-22 02:26:14 +0000564 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000565
566 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000567 llvm::Constant *getExceptionTryExitFn() {
568 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000569 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +0000570 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000571 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000572 Params, false),
573 "objc_exception_try_exit");
Chris Lattner34b02a12009-04-22 02:26:14 +0000574 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000575
576 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000577 llvm::Constant *getExceptionExtractFn() {
578 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000579 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
580 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner34b02a12009-04-22 02:26:14 +0000581 Params, false),
582 "objc_exception_extract");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000583
Chris Lattner34b02a12009-04-22 02:26:14 +0000584 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000585
Anders Carlsson124526b2008-09-09 10:10:21 +0000586 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000587 llvm::Constant *getExceptionMatchFn() {
588 std::vector<const llvm::Type*> Params;
589 Params.push_back(ClassPtrTy);
590 Params.push_back(ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000591 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000592 llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000593 Params, false),
594 "objc_exception_match");
595
Chris Lattner34b02a12009-04-22 02:26:14 +0000596 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000597
Anders Carlsson124526b2008-09-09 10:10:21 +0000598 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000599 llvm::Constant *getSetJmpFn() {
600 std::vector<const llvm::Type*> Params;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000601 Params.push_back(llvm::Type::getInt32PtrTy(VMContext));
Chris Lattner34b02a12009-04-22 02:26:14 +0000602 return
Owen Anderson0032b272009-08-13 21:57:51 +0000603 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattner34b02a12009-04-22 02:26:14 +0000604 Params, false),
605 "_setjmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000606
Chris Lattner34b02a12009-04-22 02:26:14 +0000607 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000608
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000609public:
610 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000611 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000612};
613
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000614/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000615/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000616class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000617public:
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000618
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000619 // MethodListnfABITy - LLVM for struct _method_list_t
620 const llvm::StructType *MethodListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000621
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000622 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
623 const llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000624
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000625 // ProtocolnfABITy = LLVM for struct _protocol_t
626 const llvm::StructType *ProtocolnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000627
Daniel Dunbar948e2582009-02-15 07:36:20 +0000628 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
629 const llvm::Type *ProtocolnfABIPtrTy;
630
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000631 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
632 const llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000633
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000634 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
635 const llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000636
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000637 // ClassnfABITy - LLVM for struct _class_t
638 const llvm::StructType *ClassnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000639
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000640 // ClassnfABIPtrTy - LLVM for struct _class_t*
641 const llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000642
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000643 // IvarnfABITy - LLVM for struct _ivar_t
644 const llvm::StructType *IvarnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000645
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000646 // IvarListnfABITy - LLVM for struct _ivar_list_t
647 const llvm::StructType *IvarListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000648
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000649 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
650 const llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000651
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000652 // ClassRonfABITy - LLVM for struct _class_ro_t
653 const llvm::StructType *ClassRonfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000654
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000655 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
656 const llvm::Type *ImpnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000657
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000658 // CategorynfABITy - LLVM for struct _category_t
659 const llvm::StructType *CategorynfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000660
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000661 // New types for nonfragile abi messaging.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000662
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000663 // MessageRefTy - LLVM for:
664 // struct _message_ref_t {
665 // IMP messenger;
666 // SEL name;
667 // };
668 const llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000669 // MessageRefCTy - clang type for struct _message_ref_t
670 QualType MessageRefCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000671
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000672 // MessageRefPtrTy - LLVM for struct _message_ref_t*
673 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000674 // MessageRefCPtrTy - clang type for struct _message_ref_t*
675 QualType MessageRefCPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000676
Fariborz Jahanianef163782009-02-05 01:13:09 +0000677 // MessengerTy - Type of the messenger (shown as IMP above)
678 const llvm::FunctionType *MessengerTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000679
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000680 // SuperMessageRefTy - LLVM for:
681 // struct _super_message_ref_t {
682 // SUPER_IMP messenger;
683 // SEL name;
684 // };
685 const llvm::StructType *SuperMessageRefTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000686
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000687 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
688 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000689
Chris Lattner1c02f862009-04-22 02:53:24 +0000690 llvm::Constant *getMessageSendFixupFn() {
691 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
692 std::vector<const llvm::Type*> Params;
693 Params.push_back(ObjectPtrTy);
694 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000695 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000696 Params, true),
697 "objc_msgSend_fixup");
698 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000699
Chris Lattner1c02f862009-04-22 02:53:24 +0000700 llvm::Constant *getMessageSendFpretFixupFn() {
701 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
702 std::vector<const llvm::Type*> Params;
703 Params.push_back(ObjectPtrTy);
704 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000705 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000706 Params, true),
707 "objc_msgSend_fpret_fixup");
708 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000709
Chris Lattner1c02f862009-04-22 02:53:24 +0000710 llvm::Constant *getMessageSendStretFixupFn() {
711 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
712 std::vector<const llvm::Type*> Params;
713 Params.push_back(ObjectPtrTy);
714 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000715 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000716 Params, true),
717 "objc_msgSend_stret_fixup");
718 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000719
Chris Lattner1c02f862009-04-22 02:53:24 +0000720 llvm::Constant *getMessageSendIdFixupFn() {
721 // id objc_msgSendId_fixup(id, struct message_ref_t*, ...)
722 std::vector<const llvm::Type*> Params;
723 Params.push_back(ObjectPtrTy);
724 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000725 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000726 Params, true),
727 "objc_msgSendId_fixup");
728 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000729
Chris Lattner1c02f862009-04-22 02:53:24 +0000730 llvm::Constant *getMessageSendIdStretFixupFn() {
731 // id objc_msgSendId_stret_fixup(id, struct message_ref_t*, ...)
732 std::vector<const llvm::Type*> Params;
733 Params.push_back(ObjectPtrTy);
734 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000735 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000736 Params, true),
737 "objc_msgSendId_stret_fixup");
738 }
739 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000740 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000741 // struct _super_message_ref_t*, ...)
742 std::vector<const llvm::Type*> Params;
743 Params.push_back(SuperPtrTy);
744 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000745 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000746 Params, true),
747 "objc_msgSendSuper2_fixup");
748 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000749
Chris Lattner1c02f862009-04-22 02:53:24 +0000750 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000751 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000752 // struct _super_message_ref_t*, ...)
753 std::vector<const llvm::Type*> Params;
754 Params.push_back(SuperPtrTy);
755 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000756 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000757 Params, true),
758 "objc_msgSendSuper2_stret_fixup");
759 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000760
761
762
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000763 /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C
764 /// exception personality function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000765 llvm::Value *getEHPersonalityPtr() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000766 llvm::Constant *Personality =
Owen Anderson0032b272009-08-13 21:57:51 +0000767 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000768 true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000769 "__objc_personality_v0");
Owen Anderson3c4972d2009-07-29 18:54:39 +0000770 return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000771 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000772
Chris Lattner8a569112009-04-22 02:15:23 +0000773 llvm::Constant *getUnwindResumeOrRethrowFn() {
774 std::vector<const llvm::Type*> Params;
775 Params.push_back(Int8PtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000776 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000777 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000778 Params, false),
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000779 (CGM.getLangOptions().SjLjExceptions ? "_Unwind_SjLj_Resume" :
780 "_Unwind_Resume_or_Rethrow"));
Chris Lattner8a569112009-04-22 02:15:23 +0000781 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000782
Chris Lattner8a569112009-04-22 02:15:23 +0000783 llvm::Constant *getObjCEndCatchFn() {
Owen Anderson0032b272009-08-13 21:57:51 +0000784 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Chris Lattnerb59761b2009-07-01 04:13:52 +0000785 false),
Chris Lattner8a569112009-04-22 02:15:23 +0000786 "objc_end_catch");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000787
Chris Lattner8a569112009-04-22 02:15:23 +0000788 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000789
Chris Lattner8a569112009-04-22 02:15:23 +0000790 llvm::Constant *getObjCBeginCatchFn() {
791 std::vector<const llvm::Type*> Params;
792 Params.push_back(Int8PtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000793 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
Chris Lattner8a569112009-04-22 02:15:23 +0000794 Params, false),
795 "objc_begin_catch");
796 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000797
798 const llvm::StructType *EHTypeTy;
799 const llvm::Type *EHTypePtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000800
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000801 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
802 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000803};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000804
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000805class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000806public:
807 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000808 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000809 public:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000810 unsigned ivar_bytepos;
811 unsigned ivar_size;
812 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000813 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000814
815 // Allow sorting based on byte pos.
816 bool operator<(const GC_IVAR &b) const {
817 return ivar_bytepos < b.ivar_bytepos;
818 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000819 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000820
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000821 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000822 public:
823 unsigned skip;
824 unsigned scan;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000825 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000826 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000827 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000828
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000829protected:
830 CodeGen::CodeGenModule &CGM;
Owen Anderson69243822009-07-13 04:10:07 +0000831 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000832 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000833 unsigned ObjCABI;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000834
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000835 // gc ivar layout bitmap calculation helper caches.
836 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
837 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000838
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000839 /// LazySymbols - Symbols to generate a lazy reference for. See
840 /// DefinedSymbols and FinishModule().
Daniel Dunbar33063492009-09-07 00:20:42 +0000841 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000842
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000843 /// DefinedSymbols - External symbols which are defined by this
844 /// module. The symbols in this list and LazySymbols are used to add
845 /// special linker symbols which ensure that Objective-C modules are
846 /// linked properly.
Daniel Dunbar33063492009-09-07 00:20:42 +0000847 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000848
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000849 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000850 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000851
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000852 /// MethodVarNames - uniqued method variable names.
853 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000854
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000855 /// MethodVarTypes - uniqued method type signatures. We have to use
856 /// a StringMap here because have no other unique reference.
857 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000858
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000859 /// MethodDefinitions - map of methods which have been defined in
860 /// this translation unit.
861 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000862
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000863 /// PropertyNames - uniqued method variable names.
864 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000865
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000866 /// ClassReferences - uniqued class references.
867 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000868
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000869 /// SelectorReferences - uniqued selector references.
870 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000871
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000872 /// Protocols - Protocols for which an objc_protocol structure has
873 /// been emitted. Forward declarations are handled by creating an
874 /// empty structure whose initializer is filled in when/if defined.
875 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000876
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000877 /// DefinedProtocols - Protocols which have actually been
878 /// defined. We should not need this, see FIXME in GenerateProtocol.
879 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000880
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000881 /// DefinedClasses - List of defined classes.
882 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000883
884 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
885 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000886
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000887 /// DefinedCategories - List of defined categories.
888 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000889
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000890 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
891 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000892
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000893 /// GetNameForMethod - Return a name for the given method.
894 /// \param[out] NameOut - The return value.
895 void GetNameForMethod(const ObjCMethodDecl *OMD,
896 const ObjCContainerDecl *CD,
Daniel Dunbarc575ce72009-10-19 01:21:19 +0000897 llvm::SmallVectorImpl<char> &NameOut);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000898
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000899 /// GetMethodVarName - Return a unique constant for the given
900 /// selector's name. The return value has type char *.
901 llvm::Constant *GetMethodVarName(Selector Sel);
902 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
903 llvm::Constant *GetMethodVarName(const std::string &Name);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000904
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000905 /// GetMethodVarType - Return a unique constant for the given
906 /// selector's name. The return value has type char *.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000907
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000908 // FIXME: This is a horrible name.
909 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000910 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000911
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000912 /// GetPropertyName - Return a unique constant for the given
913 /// name. The return value has type char *.
914 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000915
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000916 // FIXME: This can be dropped once string functions are unified.
917 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
918 const Decl *Container);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000919
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000920 /// GetClassName - Return a unique constant for the given selector's
921 /// name. The return value has type char *.
922 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000923
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000924 /// BuildIvarLayout - Builds ivar layout bitmap for the class
925 /// implementation for the __strong or __weak case.
926 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000927 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
928 bool ForStrongLayout);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000929
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000930 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000931 unsigned int BytePos, bool ForStrongLayout,
932 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000933 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000934 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000935 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +0000936 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000937 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000938 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000939
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000940 /// GetIvarLayoutName - Returns a unique constant for the given
941 /// ivar layout bitmap.
942 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
943 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000944
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000945 /// EmitPropertyList - Emit the given property list. The return
946 /// value has type PropertyListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +0000947 llvm::Constant *EmitPropertyList(llvm::Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000948 const Decl *Container,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000949 const ObjCContainerDecl *OCD,
950 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000951
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000952 /// PushProtocolProperties - Push protocol's property on the input stack.
953 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
954 std::vector<llvm::Constant*> &Properties,
955 const Decl *Container,
956 const ObjCProtocolDecl *PROTO,
957 const ObjCCommonTypesHelper &ObjCTypes);
958
Fariborz Jahanianda320092009-01-29 19:24:30 +0000959 /// GetProtocolRef - Return a reference to the internal protocol
960 /// description, creating an empty one if it has not been
961 /// defined. The return value has type ProtocolPtrTy.
962 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000963
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000964 /// CreateMetadataVar - Create a global variable with internal
965 /// linkage for use by the Objective-C runtime.
966 ///
967 /// This is a convenience wrapper which not only creates the
968 /// variable, but also sets the section and alignment and adds the
Chris Lattnerad64e022009-07-17 23:57:13 +0000969 /// global to the "llvm.used" list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000970 ///
971 /// \param Name - The variable name.
972 /// \param Init - The variable initializer; this is also used to
973 /// define the type of the variable.
974 /// \param Section - The section the variable should go into, or 0.
975 /// \param Align - The alignment for the variable, or 0.
976 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000977 /// "llvm.used".
Daniel Dunbar9c29bf52009-10-18 20:48:59 +0000978 llvm::GlobalVariable *CreateMetadataVar(llvm::Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000979 llvm::Constant *Init,
980 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000981 unsigned Align,
982 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000983
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000984 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +0000985 ReturnValueSlot Return,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000986 QualType ResultType,
987 llvm::Value *Sel,
988 llvm::Value *Arg0,
989 QualType Arg0Ty,
990 bool IsSuper,
991 const CallArgList &CallArgs,
Daniel Dunbard6c93d72009-09-17 04:01:22 +0000992 const ObjCMethodDecl *OMD,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000993 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000994
Daniel Dunbarfce176b2010-04-25 20:39:01 +0000995 /// EmitImageInfo - Emit the image info marker used to encode some module
996 /// level information.
997 void EmitImageInfo();
998
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000999public:
Owen Anderson69243822009-07-13 04:10:07 +00001000 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump1eb44332009-09-09 15:08:12 +00001001 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001002
David Chisnall0d13f6f2010-01-23 02:40:42 +00001003 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001004
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001005 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
1006 const ObjCContainerDecl *CD=0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001007
Fariborz Jahanianda320092009-01-29 19:24:30 +00001008 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001009
Fariborz Jahanianda320092009-01-29 19:24:30 +00001010 /// GetOrEmitProtocol - Get the protocol object for the given
1011 /// declaration, emitting it if necessary. The return value has type
1012 /// ProtocolPtrTy.
1013 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001014
Fariborz Jahanianda320092009-01-29 19:24:30 +00001015 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1016 /// object for the given declaration, emitting it if needed. These
1017 /// forward references will be filled in with empty bodies if no
1018 /// definition is seen. The return value has type ProtocolPtrTy.
1019 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001020};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001021
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001022class CGObjCMac : public CGObjCCommonMac {
1023private:
1024 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001025
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001026 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001027 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001028 void EmitModuleInfo();
1029
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001030 /// EmitModuleSymols - Emit module symbols, the list of defined
1031 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001032 llvm::Constant *EmitModuleSymbols();
1033
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001034 /// FinishModule - Write out global data structures at the end of
1035 /// processing a translation unit.
1036 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001037
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001038 /// EmitClassExtension - Generate the class extension structure used
1039 /// to store the weak ivar layout and properties. The return value
1040 /// has type ClassExtensionPtrTy.
1041 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
1042
1043 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1044 /// for the given class.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001045 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001046 const ObjCInterfaceDecl *ID);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001047
1048 /// EmitSuperClassRef - Emits reference to class's main metadata class.
1049 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001050
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001051 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001052 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001053 QualType ResultType,
1054 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001055 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001056 QualType Arg0Ty,
1057 bool IsSuper,
1058 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001059
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001060 /// EmitIvarList - Emit the ivar list for the given
1061 /// implementation. If ForClass is true the list of class ivars
1062 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1063 /// interface ivars will be emitted. The return value has type
1064 /// IvarListPtrTy.
1065 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001066 bool ForClass);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001067
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001068 /// EmitMetaClass - Emit a forward reference to the class structure
1069 /// for the metaclass of the given interface. The return value has
1070 /// type ClassPtrTy.
1071 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1072
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001073 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001074 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001075 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1076 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001077 const ConstantVector &Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001078
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001079 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001080
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001081 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001082
1083 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001084 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001085 llvm::Constant *EmitMethodList(llvm::Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001086 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001087 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001088
1089 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001090 /// method declarations.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001091 /// - TypeName: The name for the type containing the methods.
1092 /// - IsProtocol: True iff these methods are for a protocol.
1093 /// - ClassMethds: True iff these are class methods.
1094 /// - Required: When true, only "required" methods are
1095 /// listed. Similarly, when false only "optional" methods are
1096 /// listed. For classes this should always be true.
1097 /// - begin, end: The method list to output.
1098 ///
1099 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001100 llvm::Constant *EmitMethodDescList(llvm::Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001101 const char *Section,
1102 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001103
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001104 /// GetOrEmitProtocol - Get the protocol object for the given
1105 /// declaration, emitting it if necessary. The return value has type
1106 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001107 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001108
1109 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1110 /// object for the given declaration, emitting it if needed. These
1111 /// forward references will be filled in with empty bodies if no
1112 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001113 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001114
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001115 /// EmitProtocolExtension - Generate the protocol extension
1116 /// structure used to store optional instance and class methods, and
1117 /// protocol properties. The return value has type
1118 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001119 llvm::Constant *
1120 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1121 const ConstantVector &OptInstanceMethods,
1122 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001123
1124 /// EmitProtocolList - Generate the list of referenced
1125 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001126 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00001127 ObjCProtocolDecl::protocol_iterator begin,
1128 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001129
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001130 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1131 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001132 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001133
1134public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001135 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001136
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001137 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001138
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001139 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001140 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001141 QualType ResultType,
1142 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001143 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001144 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001145 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001146 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001147
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001148 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001149 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001150 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001151 QualType ResultType,
1152 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001153 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001154 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001155 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001156 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001157 const CallArgList &CallArgs,
1158 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001159
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001160 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001161 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001162
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001163 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001164
1165 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1166 /// untyped one.
1167 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1168 const ObjCMethodDecl *Method);
1169
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001170 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001171
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001172 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001173
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001174 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001175 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001176
Chris Lattner74391b42009-03-22 21:03:39 +00001177 virtual llvm::Constant *GetPropertyGetFunction();
1178 virtual llvm::Constant *GetPropertySetFunction();
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001179 virtual llvm::Constant *GetCopyStructFunction();
Chris Lattner74391b42009-03-22 21:03:39 +00001180 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001181
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001182 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1183 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001184 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1185 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001186 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001187 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001188 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001189 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001190 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1191 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001192 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001193 llvm::Value *src, llvm::Value *dest,
1194 llvm::Value *ivarOffset);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001195 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1196 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001197 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1198 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian08c32132009-08-31 19:33:16 +00001199 QualType Ty);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001200
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001201 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1202 QualType ObjectTy,
1203 llvm::Value *BaseValue,
1204 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001205 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001206 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001207 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001208 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001209};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001210
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001211class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001212private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001213 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001214 llvm::GlobalVariable* ObjCEmptyCacheVar;
1215 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001216
Daniel Dunbar11394522009-04-18 08:51:00 +00001217 /// SuperClassReferences - uniqued super class references.
1218 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001219
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001220 /// MetaClassReferences - uniqued meta class references.
1221 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001222
1223 /// EHTypeReferences - uniqued class ehtype references.
1224 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001225
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001226 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001227 /// legacy messaging dispatch.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001228 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001229
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00001230 /// DefinedMetaClasses - List of defined meta-classes.
1231 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1232
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001233 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001234 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001235 bool LegacyDispatchedSelector(Selector Sel);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001236
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001237 /// FinishNonFragileABIModule - Write out global data structures at the end of
1238 /// processing a translation unit.
1239 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001240
Daniel Dunbar463b8762009-05-15 21:48:48 +00001241 /// AddModuleClassList - Add the given list of class pointers to the
1242 /// module with the provided symbol and section names.
1243 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1244 const char *SymbolName,
1245 const char *SectionName);
1246
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001247 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1248 unsigned InstanceStart,
1249 unsigned InstanceSize,
1250 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001251 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001252 llvm::Constant *IsAGV,
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001253 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001254 llvm::Constant *ClassRoGV,
1255 bool HiddenVisibility);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001256
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001257 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001258
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001259 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001260
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001261 /// EmitMethodList - Emit the method list for the given
1262 /// implementation. The return value has type MethodListnfABITy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001263 llvm::Constant *EmitMethodList(llvm::Twine Name,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001264 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001265 const ConstantVector &Methods);
1266 /// EmitIvarList - Emit the ivar list for the given
1267 /// implementation. If ForClass is true the list of class ivars
1268 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1269 /// interface ivars will be emitted. The return value has type
1270 /// IvarListnfABIPtrTy.
1271 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001272
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001273 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001274 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001275 unsigned long int offset);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001276
Fariborz Jahanianda320092009-01-29 19:24:30 +00001277 /// GetOrEmitProtocol - Get the protocol object for the given
1278 /// declaration, emitting it if necessary. The return value has type
1279 /// ProtocolPtrTy.
1280 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001281
Fariborz Jahanianda320092009-01-29 19:24:30 +00001282 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1283 /// object for the given declaration, emitting it if needed. These
1284 /// forward references will be filled in with empty bodies if no
1285 /// definition is seen. The return value has type ProtocolPtrTy.
1286 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001287
Fariborz Jahanianda320092009-01-29 19:24:30 +00001288 /// EmitProtocolList - Generate the list of referenced
1289 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001290 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Fariborz Jahanianda320092009-01-29 19:24:30 +00001291 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001292 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001293
Fariborz Jahanian46551122009-02-04 00:22:57 +00001294 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001295 ReturnValueSlot Return,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001296 QualType ResultType,
1297 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00001298 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001299 QualType Arg0Ty,
1300 bool IsSuper,
1301 const CallArgList &CallArgs);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001302
1303 /// GetClassGlobal - Return the global variable for the Objective-C
1304 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001305 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001306
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001307 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001308 /// for the given class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001309 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001310 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001311
Daniel Dunbar11394522009-04-18 08:51:00 +00001312 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1313 /// for the given super class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001314 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1315 const ObjCInterfaceDecl *ID);
1316
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001317 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1318 /// meta-data
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001319 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001320 const ObjCInterfaceDecl *ID);
1321
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001322 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1323 /// the given ivar.
1324 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001325 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001326 const ObjCInterfaceDecl *ID,
1327 const ObjCIvarDecl *Ivar);
1328
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001329 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1330 /// for the given selector.
1331 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbare588b992009-03-01 04:46:24 +00001332
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001333 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001334 /// interface. The return value has type EHTypePtrTy.
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001335 llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
1336 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001337
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001338 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001339 return "OBJC_METACLASS_$_";
1340 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001341
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001342 const char *getClassSymbolPrefix() const {
1343 return "OBJC_CLASS_$_";
1344 }
1345
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001346 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001347 uint32_t &InstanceStart,
1348 uint32_t &InstanceSize);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001349
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001350 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001351 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001352 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1353 return CGM.getContext().Selectors.getSelector(0, &II);
1354 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001355
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001356 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001357 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1358 return CGM.getContext().Selectors.getSelector(1, &II);
1359 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001360
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001361 /// ImplementationIsNonLazy - Check whether the given category or
1362 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001363 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001364
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001365public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001366 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001367 // FIXME. All stubs for now!
1368 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001369
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001370 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001371 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001372 QualType ResultType,
1373 Selector Sel,
1374 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001375 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001376 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001377 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001378
1379 virtual CodeGen::RValue
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001380 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001381 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001382 QualType ResultType,
1383 Selector Sel,
1384 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001385 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001386 llvm::Value *Receiver,
1387 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001388 const CallArgList &CallArgs,
1389 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001390
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001391 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001392 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001393
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001394 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001395 { return EmitSelector(Builder, Sel); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001396
1397 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1398 /// untyped one.
1399 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1400 const ObjCMethodDecl *Method)
1401 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001402
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001403 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001404
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001405 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001406 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001407 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001408
1409 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001410 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001411 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001412 virtual llvm::Constant *GetPropertySetFunction() {
1413 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001414 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001415
1416 virtual llvm::Constant *GetCopyStructFunction() {
1417 return ObjCTypes.getCopyStructFn();
1418 }
1419
Chris Lattner74391b42009-03-22 21:03:39 +00001420 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001421 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001422 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001423
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001424 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00001425 const Stmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001426 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001427 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001428 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001429 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001430 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001431 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001432 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001433 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001434 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001435 llvm::Value *src, llvm::Value *dest,
1436 llvm::Value *ivarOffset);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001437 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001438 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001439 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1440 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian08c32132009-08-31 19:33:16 +00001441 QualType Ty);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001442 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1443 QualType ObjectTy,
1444 llvm::Value *BaseValue,
1445 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001446 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001447 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001448 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001449 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001450};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001451
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001452} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001453
1454/* *** Helper Functions *** */
1455
1456/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001457static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001458 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001459 unsigned idx0,
1460 unsigned idx1) {
1461 llvm::Value *Idxs[] = {
Owen Anderson0032b272009-08-13 21:57:51 +00001462 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1463 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001464 };
Owen Anderson3c4972d2009-07-29 18:54:39 +00001465 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001466}
1467
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001468/// hasObjCExceptionAttribute - Return true if this class or any super
1469/// class has the __objc_exception__ attribute.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001470static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor68584ed2009-06-18 16:11:24 +00001471 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001472 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001473 return true;
1474 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001475 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001476 return false;
1477}
1478
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001479/* *** CGObjCMac Public Interface *** */
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001480
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001481CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00001482 ObjCTypes(cgm) {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001483 ObjCABI = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001484 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001485}
1486
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001487/// GetClass - Return a reference to the class for the given interface
1488/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001489llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001490 const ObjCInterfaceDecl *ID) {
1491 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001492}
1493
1494/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001495llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001496 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001497}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001498llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001499 *Method) {
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001500 return EmitSelector(Builder, Method->getSelector());
1501}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001502
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001503/// Generate a constant CFString object.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001504/*
1505 struct __builtin_CFString {
1506 const int *isa; // point to __CFConstantStringClassReference
1507 int flags;
1508 const char *str;
1509 long length;
1510 };
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001511*/
1512
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001513/// or Generate a constant NSString object.
1514/*
1515 struct __builtin_NSString {
1516 const int *isa; // point to __NSConstantStringClassReference
1517 const char *str;
1518 unsigned int length;
1519 };
1520*/
1521
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001522llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall0d13f6f2010-01-23 02:40:42 +00001523 const StringLiteral *SL) {
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001524 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1525 CGM.GetAddrOfConstantCFString(SL) :
1526 CGM.GetAddrOfConstantNSString(SL));
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001527}
1528
1529/// Generates a message send where the super is the receiver. This is
1530/// a message send to self with special delivery semantics indicating
1531/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001532CodeGen::RValue
1533CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001534 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001535 QualType ResultType,
1536 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001537 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001538 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001539 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001540 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001541 const CodeGen::CallArgList &CallArgs,
1542 const ObjCMethodDecl *Method) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001543 // Create and init a super structure; this is a (receiver, class)
1544 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001545 llvm::Value *ObjCSuper =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001546 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001547 llvm::Value *ReceiverAsObject =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001548 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001549 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001550 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001551
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001552 // If this is a class message the metaclass is passed as the target.
1553 llvm::Value *Target;
1554 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001555 if (isCategoryImpl) {
1556 // Message sent to 'super' in a class method defined in a category
1557 // implementation requires an odd treatment.
1558 // If we are in a class method, we must retrieve the
1559 // _metaclass_ for the current class, pointed at by
1560 // the class's "isa" pointer. The following assumes that
1561 // isa" is the first ivar in a class (which it must be).
1562 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1563 Target = CGF.Builder.CreateStructGEP(Target, 0);
1564 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00001565 } else {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001566 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1567 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1568 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1569 Target = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001570 }
Fariborz Jahanian182f2682009-11-14 02:18:31 +00001571 }
1572 else if (isCategoryImpl)
1573 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1574 else {
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001575 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1576 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1577 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001578 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001579 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1580 // ObjCTypes types.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001581 const llvm::Type *ClassTy =
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001582 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001583 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001584 CGF.Builder.CreateStore(Target,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001585 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCallef072fd2010-05-22 01:48:05 +00001586 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001587 EmitSelector(CGF.Builder, Sel),
1588 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001589 true, CallArgs, Method, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001590}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001591
1592/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001593CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001594 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001595 QualType ResultType,
1596 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001597 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001598 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001599 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001600 const ObjCMethodDecl *Method) {
John McCallef072fd2010-05-22 01:48:05 +00001601 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001602 EmitSelector(CGF.Builder, Sel),
1603 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001604 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001605}
1606
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001607CodeGen::RValue
1608CGObjCCommonMac::EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001609 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001610 QualType ResultType,
1611 llvm::Value *Sel,
1612 llvm::Value *Arg0,
1613 QualType Arg0Ty,
1614 bool IsSuper,
1615 const CallArgList &CallArgs,
1616 const ObjCMethodDecl *Method,
1617 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001618 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001619 if (!IsSuper)
1620 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001621 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001622 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001623 CGF.getContext().getObjCSelType()));
1624 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001625
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001626 CodeGenTypes &Types = CGM.getTypes();
John McCall04a67a62010-02-05 21:31:56 +00001627 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00001628 FunctionType::ExtInfo());
Daniel Dunbar67939662009-09-17 04:01:40 +00001629 const llvm::FunctionType *FTy =
1630 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001631
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001632 llvm::Constant *Fn = NULL;
Daniel Dunbar88b53962009-02-02 22:03:45 +00001633 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001634 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001635 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001636 } else if (ResultType->isFloatingType()) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001637 if (ObjCABI == 2) {
John McCall183700f2009-09-21 23:43:11 +00001638 if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001639 BuiltinType::Kind k = BT->getKind();
1640 Fn = (k == BuiltinType::LongDouble) ? ObjCTypes.getSendFpretFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001641 : ObjCTypes.getSendFn2(IsSuper);
Daniel Dunbar42f963d2009-06-10 04:38:50 +00001642 } else {
1643 Fn = ObjCTypes.getSendFn2(IsSuper);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001644 }
Mike Stumpb3589f42009-07-30 22:28:39 +00001645 } else
Mike Stumpf5408fe2009-05-16 07:57:57 +00001646 // FIXME. This currently matches gcc's API for x86-32. May need to change
1647 // for others if we have their API.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001648 Fn = ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001649 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001650 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001651 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001652 }
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001653 assert(Fn && "EmitLegacyMessageSend - unknown API");
Owen Anderson3c4972d2009-07-29 18:54:39 +00001654 Fn = llvm::ConstantExpr::getBitCast(Fn,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001655 llvm::PointerType::getUnqual(FTy));
John McCallef072fd2010-05-22 01:48:05 +00001656 return CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001657}
1658
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001659llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001660 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001661 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001662 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001663 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1664
Owen Anderson3c4972d2009-07-29 18:54:39 +00001665 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001666 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001667}
1668
Fariborz Jahanianda320092009-01-29 19:24:30 +00001669void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001670 // FIXME: We shouldn't need this, the protocol decl should contain enough
1671 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001672 DefinedProtocols.insert(PD->getIdentifier());
1673
1674 // If we have generated a forward reference to this protocol, emit
1675 // it now. Otherwise do nothing, the protocol objects are lazily
1676 // emitted.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001677 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001678 GetOrEmitProtocol(PD);
1679}
1680
Fariborz Jahanianda320092009-01-29 19:24:30 +00001681llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001682 if (DefinedProtocols.count(PD->getIdentifier()))
1683 return GetOrEmitProtocol(PD);
1684 return GetOrEmitProtocolRef(PD);
1685}
1686
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001687/*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001688// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1689struct _objc_protocol {
1690struct _objc_protocol_extension *isa;
1691char *protocol_name;
1692struct _objc_protocol_list *protocol_list;
1693struct _objc__method_prototype_list *instance_methods;
1694struct _objc__method_prototype_list *class_methods
1695};
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001696
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001697See EmitProtocolExtension().
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001698*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001699llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1700 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1701
1702 // Early exit if a defining object has already been generated.
1703 if (Entry && Entry->hasInitializer())
1704 return Entry;
1705
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001706 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001707 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001708 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1709
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001710 // Construct method lists.
1711 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1712 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001713 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001714 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001715 ObjCMethodDecl *MD = *i;
1716 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1717 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1718 OptInstanceMethods.push_back(C);
1719 } else {
1720 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001721 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001722 }
1723
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001724 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001725 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001726 ObjCMethodDecl *MD = *i;
1727 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1728 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1729 OptClassMethods.push_back(C);
1730 } else {
1731 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001732 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001733 }
1734
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001735 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001736 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001737 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001738 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001739 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00001740 PD->protocol_begin(),
1741 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001742 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001743 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001744 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1745 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001746 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001747 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001748 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1749 ClassMethods);
Owen Anderson08e25242009-07-27 22:29:56 +00001750 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001751 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001752
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001753 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001754 // Already created, fix the linkage and update the initializer.
1755 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001756 Entry->setInitializer(Init);
1757 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001758 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001759 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001760 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001761 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001762 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001763 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001764 // FIXME: Is this necessary? Why only for protocol?
1765 Entry->setAlignment(4);
1766 }
Chris Lattnerad64e022009-07-17 23:57:13 +00001767 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001768
1769 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001770}
1771
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001772llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001773 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1774
1775 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001776 // We use the initializer as a marker of whether this is a forward
1777 // reference or not. At module finalization we add the empty
1778 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001779 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001780 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001781 llvm::GlobalValue::ExternalLinkage,
1782 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001783 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001784 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001785 // FIXME: Is this necessary? Why only for protocol?
1786 Entry->setAlignment(4);
1787 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001788
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001789 return Entry;
1790}
1791
1792/*
1793 struct _objc_protocol_extension {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001794 uint32_t size;
1795 struct objc_method_description_list *optional_instance_methods;
1796 struct objc_method_description_list *optional_class_methods;
1797 struct objc_property_list *instance_properties;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001798 };
1799*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001800llvm::Constant *
1801CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1802 const ConstantVector &OptInstanceMethods,
1803 const ConstantVector &OptClassMethods) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001804 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00001805 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001806 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001807 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001808 Values[1] =
1809 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001810 + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001811 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1812 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001813 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001814 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001815 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1816 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001817 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001818 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001819
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001820 // Return null if no extension bits are used.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001821 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001822 Values[3]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001823 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001824
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001825 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00001826 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001827
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001828 // No special section, but goes in llvm.used
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001829 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001830 Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001831 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001832}
1833
1834/*
1835 struct objc_protocol_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001836 struct objc_protocol_list *next;
1837 long count;
1838 Protocol *list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001839 };
1840*/
Daniel Dunbardbc93372008-08-21 21:57:41 +00001841llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001842CGObjCMac::EmitProtocolList(llvm::Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00001843 ObjCProtocolDecl::protocol_iterator begin,
1844 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001845 std::vector<llvm::Constant*> ProtocolRefs;
1846
Daniel Dunbardbc93372008-08-21 21:57:41 +00001847 for (; begin != end; ++begin)
1848 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001849
1850 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001851 if (ProtocolRefs.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001852 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001853
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001854 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001855 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001856
1857 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001858 // This field is only used by the runtime.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001859 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001860 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001861 ProtocolRefs.size() - 1);
1862 Values[2] =
1863 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1864 ProtocolRefs.size()),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001865 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001866
Nick Lewycky0d36dd22009-09-19 20:00:52 +00001867 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001868 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001869 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001870 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001871 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001872}
1873
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001874void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1875 std::vector<llvm::Constant*> &Properties,
1876 const Decl *Container,
1877 const ObjCProtocolDecl *PROTO,
1878 const ObjCCommonTypesHelper &ObjCTypes) {
1879 std::vector<llvm::Constant*> Prop(2);
1880 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1881 E = PROTO->protocol_end(); P != E; ++P)
1882 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1883 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1884 E = PROTO->prop_end(); I != E; ++I) {
1885 const ObjCPropertyDecl *PD = *I;
1886 if (!PropertySet.insert(PD->getIdentifier()))
1887 continue;
1888 Prop[0] = GetPropertyName(PD->getIdentifier());
1889 Prop[1] = GetPropertyTypeString(PD, Container);
1890 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1891 }
1892}
1893
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001894/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001895 struct _objc_property {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001896 const char * const name;
1897 const char * const attributes;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001898 };
1899
1900 struct _objc_property_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001901 uint32_t entsize; // sizeof (struct _objc_property)
1902 uint32_t prop_count;
1903 struct _objc_property[prop_count];
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001904 };
1905*/
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001906llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001907 const Decl *Container,
1908 const ObjCContainerDecl *OCD,
1909 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001910 std::vector<llvm::Constant*> Properties, Prop(2);
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001911 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001912 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1913 E = OCD->prop_end(); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00001914 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001915 PropertySet.insert(PD->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001916 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001917 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson08e25242009-07-27 22:29:56 +00001918 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001919 Prop));
1920 }
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001921 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD))
1922 for (ObjCInterfaceDecl::protocol_iterator P = OID->protocol_begin(),
1923 E = OID->protocol_end(); P != E; ++P)
1924 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001925
1926 // Return null for empty list.
1927 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001928 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001929
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001930 unsigned PropertySize =
Duncan Sands9408c452009-05-09 07:08:47 +00001931 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001932 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001933 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1934 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001935 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001936 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00001937 Values[2] = llvm::ConstantArray::get(AT, Properties);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00001938 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001939
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001940 llvm::GlobalVariable *GV =
1941 CreateMetadataVar(Name, Init,
1942 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001943 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001944 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001945 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001946 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001947}
1948
1949/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001950 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001951 int count;
1952 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001953 };
1954*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001955llvm::Constant *
1956CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1957 std::vector<llvm::Constant*> Desc(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001958 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001959 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1960 ObjCTypes.SelectorPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001961 Desc[1] = GetMethodVarType(MD);
Owen Anderson08e25242009-07-27 22:29:56 +00001962 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001963 Desc);
1964}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001965
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001966llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001967 const char *Section,
1968 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001969 // Return null for empty list.
1970 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001971 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001972
1973 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001974 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001975 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001976 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00001977 Values[1] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00001978 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001979
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001980 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001981 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001982 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001983}
1984
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001985/*
1986 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001987 char *category_name;
1988 char *class_name;
1989 struct _objc_method_list *instance_methods;
1990 struct _objc_method_list *class_methods;
1991 struct _objc_protocol_list *protocols;
1992 uint32_t size; // <rdar://4585769>
1993 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001994 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001995*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001996void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sands9408c452009-05-09 07:08:47 +00001997 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001998
Mike Stumpf5408fe2009-05-16 07:57:57 +00001999 // FIXME: This is poor design, the OCD should have a pointer to the category
2000 // decl. Additionally, note that Category can be null for the @implementation
2001 // w/o an @interface case. Sema should just create one for us as it does for
2002 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002003 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002004 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002005 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002006
2007 llvm::SmallString<256> ExtName;
2008 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2009 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002010
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002011 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002012 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002013 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002014 // Instance methods should always be defined.
2015 InstanceMethods.push_back(GetMethodConstant(*i));
2016 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002017 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002018 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002019 // Class methods should always be defined.
2020 ClassMethods.push_back(GetMethodConstant(*i));
2021 }
2022
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002023 std::vector<llvm::Constant*> Values(7);
2024 Values[0] = GetClassName(OCD->getIdentifier());
2025 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002026 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002027 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002028 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002029 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002030 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002031 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002032 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002033 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002034 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002035 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002036 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002037 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002038 Category->protocol_begin(),
2039 Category->protocol_end());
2040 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002041 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002042 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002043 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002044
2045 // If there is no category @interface then there can be no properties.
2046 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002047 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002048 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002049 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002050 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002051 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002052
Owen Anderson08e25242009-07-27 22:29:56 +00002053 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002054 Values);
2055
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002056 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002057 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002058 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002059 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002060 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002061}
2062
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002063// FIXME: Get from somewhere?
2064enum ClassFlags {
2065 eClassFlags_Factory = 0x00001,
2066 eClassFlags_Meta = 0x00002,
2067 // <rdr://5142207>
2068 eClassFlags_HasCXXStructors = 0x02000,
2069 eClassFlags_Hidden = 0x20000,
2070 eClassFlags_ABI2_Hidden = 0x00010,
2071 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2072};
2073
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002074/*
2075 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002076 Class isa;
2077 Class super_class;
2078 const char *name;
2079 long version;
2080 long info;
2081 long instance_size;
2082 struct _objc_ivar_list *ivars;
2083 struct _objc_method_list *methods;
2084 struct _objc_cache *cache;
2085 struct _objc_protocol_list *protocols;
2086 // Objective-C 1.0 extensions (<rdr://4585769>)
2087 const char *ivar_layout;
2088 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002089 };
2090
2091 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002092*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002093void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002094 DefinedSymbols.insert(ID->getIdentifier());
2095
Chris Lattner8ec03f52008-11-24 03:54:41 +00002096 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002097 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002098 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002099 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002100 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002101 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00002102 Interface->protocol_begin(),
2103 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002104 unsigned Flags = eClassFlags_Factory;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00002105 if (ID->getNumIvarInitializers())
2106 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002107 unsigned Size =
Daniel Dunbar2bebbf02009-05-03 10:46:44 +00002108 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002109
2110 // FIXME: Set CXX-structors flag.
Daniel Dunbar04d40782009-04-14 06:00:08 +00002111 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002112 Flags |= eClassFlags_Hidden;
2113
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002114 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002115 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002116 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002117 // Instance methods should always be defined.
2118 InstanceMethods.push_back(GetMethodConstant(*i));
2119 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002120 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002121 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002122 // Class methods should always be defined.
2123 ClassMethods.push_back(GetMethodConstant(*i));
2124 }
2125
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002126 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002127 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002128 ObjCPropertyImplDecl *PID = *i;
2129
2130 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2131 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2132
2133 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2134 if (llvm::Constant *C = GetMethodConstant(MD))
2135 InstanceMethods.push_back(C);
2136 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2137 if (llvm::Constant *C = GetMethodConstant(MD))
2138 InstanceMethods.push_back(C);
2139 }
2140 }
2141
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002142 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar5384b092009-05-03 08:56:52 +00002143 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002144 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002145 // Record a reference to the super class.
2146 LazySymbols.insert(Super->getIdentifier());
2147
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002148 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002149 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002150 ObjCTypes.ClassPtrTy);
2151 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002152 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002153 }
2154 Values[ 2] = GetClassName(ID->getIdentifier());
2155 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002156 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2157 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2158 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002159 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002160 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002161 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002162 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002163 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002164 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002165 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002166 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002167 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002168 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00002169 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002170 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002171 std::string Name("\01L_OBJC_CLASS_");
2172 Name += ClassName;
2173 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2174 // Check for a forward reference.
2175 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2176 if (GV) {
2177 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2178 "Forward metaclass reference has incorrect type.");
2179 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2180 GV->setInitializer(Init);
2181 GV->setSection(Section);
2182 GV->setAlignment(4);
2183 CGM.AddUsedGlobal(GV);
2184 }
2185 else
2186 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002187 DefinedClasses.push_back(GV);
2188}
2189
2190llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2191 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002192 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002193 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002194 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002195
Daniel Dunbar04d40782009-04-14 06:00:08 +00002196 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002197 Flags |= eClassFlags_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002198
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002199 std::vector<llvm::Constant*> Values(12);
2200 // The isa for the metaclass is the root of the hierarchy.
2201 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2202 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2203 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002204 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002205 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002206 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002207 // The super class for the metaclass is emitted as the name of the
2208 // super class. The runtime fixes this up to point to the
2209 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002210 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002211 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002212 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002213 ObjCTypes.ClassPtrTy);
2214 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002215 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002216 }
2217 Values[ 2] = GetClassName(ID->getIdentifier());
2218 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002219 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2220 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2221 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002222 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002223 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002224 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002225 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002226 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002227 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002228 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002229 Values[ 9] = Protocols;
2230 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002231 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002232 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002233 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002234 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002235 Values);
2236
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002237 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002238 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002239
2240 // Check for a forward reference.
2241 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2242 if (GV) {
2243 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2244 "Forward metaclass reference has incorrect type.");
2245 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2246 GV->setInitializer(Init);
2247 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002248 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002249 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002250 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002251 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002252 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002253 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002254 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002255
2256 return GV;
2257}
2258
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002259llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002260 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002261
Mike Stumpf5408fe2009-05-16 07:57:57 +00002262 // FIXME: Should we look these up somewhere other than the module. Its a bit
2263 // silly since we only generate these while processing an implementation, so
2264 // exactly one pointer would work if know when we entered/exitted an
2265 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002266
2267 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002268 // Previously, metaclass with internal linkage may have been defined.
2269 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002270 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2271 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002272 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2273 "Forward metaclass reference has incorrect type.");
2274 return GV;
2275 } else {
2276 // Generate as an external reference to keep a consistent
2277 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002278 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002279 llvm::GlobalValue::ExternalLinkage,
2280 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002281 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002282 }
2283}
2284
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002285llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2286 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2287
2288 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2289 true)) {
2290 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2291 "Forward class metadata reference has incorrect type.");
2292 return GV;
2293 } else {
2294 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2295 llvm::GlobalValue::ExternalLinkage,
2296 0,
2297 Name);
2298 }
2299}
2300
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002301/*
2302 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002303 uint32_t size;
2304 const char *weak_ivar_layout;
2305 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002306 };
2307*/
2308llvm::Constant *
2309CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002310 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002311 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002312
2313 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002314 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002315 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002316 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002317 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002318
2319 // Return null if no extension bits are used.
2320 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002321 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002322
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002323 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002324 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002325 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002326 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002327 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002328}
2329
2330/*
2331 struct objc_ivar {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002332 char *ivar_name;
2333 char *ivar_type;
2334 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002335 };
2336
2337 struct objc_ivar_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002338 int ivar_count;
2339 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002340 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002341*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002342llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002343 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002344 std::vector<llvm::Constant*> Ivars, Ivar(3);
2345
2346 // When emitting the root class GCC emits ivar entries for the
2347 // actual class structure. It is not clear if we need to follow this
2348 // behavior; for now lets try and get away with not doing it. If so,
2349 // the cleanest solution would be to make up an ObjCInterfaceDecl
2350 // for the class.
2351 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00002352 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002353
2354 ObjCInterfaceDecl *OID =
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002355 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002356
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002357 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002358 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002359
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002360 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2361 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002362 // Ignore unnamed bit-fields.
2363 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002364 continue;
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002365 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2366 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002367 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002368 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson08e25242009-07-27 22:29:56 +00002369 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002370 }
2371
2372 // Return null for empty list.
2373 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002374 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002375
2376 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002377 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002378 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002379 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002380 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002381 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002382
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002383 llvm::GlobalVariable *GV;
2384 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002385 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002386 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002387 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002388 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002389 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002390 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002391 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002392 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002393}
2394
2395/*
2396 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002397 SEL method_name;
2398 char *method_types;
2399 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002400 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002401
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002402 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002403 struct objc_method_list *obsolete;
2404 int count;
2405 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002406 };
2407*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002408
2409/// GetMethodConstant - Return a struct objc_method constant for the
2410/// given method if it has been defined. The result is null if the
2411/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002412llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002413 // FIXME: Use DenseMap::lookup
2414 llvm::Function *Fn = MethodDefinitions[MD];
2415 if (!Fn)
2416 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002417
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002418 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002419 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002420 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002421 ObjCTypes.SelectorPtrTy);
2422 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002423 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002424 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002425}
2426
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002427llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002428 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002429 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002430 // Return null for empty list.
2431 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002432 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002433
2434 std::vector<llvm::Constant*> Values(3);
Owen Andersonc9c88b42009-07-31 20:28:54 +00002435 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002436 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002437 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002438 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002439 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002440 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002441
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002442 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002443 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002444 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002445}
2446
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002447llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002448 const ObjCContainerDecl *CD) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002449 llvm::SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002450 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002451
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002452 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002453 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002454 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002455 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002456 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002457 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002458 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002459 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002460 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002461
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002462 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002463}
2464
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002465llvm::GlobalVariable *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002466CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002467 llvm::Constant *Init,
2468 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002469 unsigned Align,
2470 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002471 const llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002472 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002473 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002474 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002475 if (Section)
2476 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002477 if (Align)
2478 GV->setAlignment(Align);
2479 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002480 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002481 return GV;
2482}
2483
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002484llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002485 // Abuse this interface function as a place to finalize.
2486 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002487 return NULL;
2488}
2489
Chris Lattner74391b42009-03-22 21:03:39 +00002490llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002491 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002492}
2493
Chris Lattner74391b42009-03-22 21:03:39 +00002494llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002495 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002496}
2497
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00002498llvm::Constant *CGObjCMac::GetCopyStructFunction() {
2499 return ObjCTypes.getCopyStructFn();
2500}
2501
Chris Lattner74391b42009-03-22 21:03:39 +00002502llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002503 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002504}
2505
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002506/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002507
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002508 Objective-C setjmp-longjmp (sjlj) Exception Handling
2509 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002510
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002511 The basic framework for a @try-catch-finally is as follows:
2512 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002513 objc_exception_data d;
2514 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002515 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002516
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002517 objc_exception_try_enter(&d);
2518 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002519 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002520 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002521 // exception path
2522 id _caught = objc_exception_extract(&d);
2523
2524 // enter new try scope for handlers
2525 if (!setjmp(d.jmp_buf)) {
2526 ... match exception and execute catch blocks ...
2527
2528 // fell off end, rethrow.
2529 _rethrow = _caught;
2530 ... jump-through-finally to finally_rethrow ...
2531 } else {
2532 // exception in catch block
2533 _rethrow = objc_exception_extract(&d);
2534 _call_try_exit = false;
2535 ... jump-through-finally to finally_rethrow ...
2536 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002537 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002538 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002539
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002540 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002541 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002542 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002543
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002544 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002545 ... dispatch to finally destination ...
2546
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002547 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00002548 objc_exception_throw(_rethrow);
2549
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002550 finally_end:
2551 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002552
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002553 This framework differs slightly from the one gcc uses, in that gcc
2554 uses _rethrow to determine if objc_exception_try_exit should be called
2555 and if the object should be rethrown. This breaks in the face of
2556 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002557
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002558 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002559
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002560 - If there are no catch blocks, then we avoid emitting the second
2561 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002562
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002563 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2564 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002565
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002566 - FIXME: If there is no @finally block we can do a few more
2567 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002568
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002569 Rethrows and Jumps-Through-Finally
2570 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002571
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002572 Support for implicit rethrows and jumping through the finally block is
2573 handled by storing the current exception-handling context in
2574 ObjCEHStack.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002575
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002576 In order to implement proper @finally semantics, we support one basic
2577 mechanism for jumping through the finally block to an arbitrary
2578 destination. Constructs which generate exits from a @try or @catch
2579 block use this mechanism to implement the proper semantics by chaining
2580 jumps, as necessary.
Daniel Dunbar898d5082008-09-30 01:06:03 +00002581
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002582 This mechanism works like the one used for indirect goto: we
2583 arbitrarily assign an ID to each destination and store the ID for the
2584 destination in a variable prior to entering the finally block. At the
2585 end of the finally block we simply create a switch to the proper
2586 destination.
2587
2588 Code gen for @synchronized(expr) stmt;
2589 Effectively generating code for:
2590 objc_sync_enter(expr);
2591 @try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002592*/
2593
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002594void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2595 const Stmt &S) {
2596 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002597 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00002598 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002599 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar55e87422008-11-11 02:29:29 +00002600 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
2601 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
2602 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002603
Daniel Dunbar1c566672009-02-24 01:43:46 +00002604 // For @synchronized, call objc_sync_enter(sync.expr). The
2605 // evaluation of the expression must occur before we enter the
2606 // @synchronized. We can safely avoid a temp here because jumps into
2607 // @synchronized are illegal & this will dominate uses.
2608 llvm::Value *SyncArg = 0;
2609 if (!isTry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002610 SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00002611 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2612 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00002613 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002614 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002615
2616 // Push an EH context entry, used for handling rethrows and jumps
2617 // through finally.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002618 CGF.PushCleanupBlock(FinallyBlock);
2619
Fariborz Jahanian8c89ff92009-12-14 21:35:01 +00002620 if (CGF.ObjCEHValueStack.empty())
2621 CGF.ObjCEHValueStack.push_back(0);
2622 // If This is a nested @try, caught exception is that of enclosing @try.
2623 else
2624 CGF.ObjCEHValueStack.push_back(CGF.ObjCEHValueStack.back());
Daniel Dunbar898d5082008-09-30 01:06:03 +00002625 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00002626 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2627 "exceptiondata.ptr");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002628 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002629 "_rethrow");
Owen Anderson0032b272009-08-13 21:57:51 +00002630 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(
2631 llvm::Type::getInt1Ty(VMContext),
Anders Carlsson190d00e2009-02-07 21:26:04 +00002632 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002633 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(VMContext),
2634 CallTryExitPtr);
2635
Anders Carlsson80f25672008-09-09 17:59:25 +00002636 // Enter a new try block and call setjmp.
Chris Lattner34b02a12009-04-22 02:26:14 +00002637 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002638 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
Anders Carlsson80f25672008-09-09 17:59:25 +00002639 "jmpbufarray");
2640 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
Chris Lattner34b02a12009-04-22 02:26:14 +00002641 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlsson80f25672008-09-09 17:59:25 +00002642 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00002643
Daniel Dunbar55e87422008-11-11 02:29:29 +00002644 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2645 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002646 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002647 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002648
2649 // Emit the @try block.
2650 CGF.EmitBlock(TryBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002651 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
2652 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002653 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002654
Anders Carlsson80f25672008-09-09 17:59:25 +00002655 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002656 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002657
2658 // Retrieve the exception object. We may emit multiple blocks but
2659 // nothing can cross this so the value is already in SSA form.
Chris Lattner34b02a12009-04-22 02:26:14 +00002660 llvm::Value *Caught =
2661 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2662 ExceptionData, "caught");
Anders Carlsson273558f2009-02-07 21:37:21 +00002663 CGF.ObjCEHValueStack.back() = Caught;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002664 if (!isTry) {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002665 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002666 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(VMContext),
2667 CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002668 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00002669 } else if (cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
2670 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
2671
Daniel Dunbar55e40722008-09-27 07:03:52 +00002672 // Enter a new exception try block (in case a @catch block throws
2673 // an exception).
Chris Lattner34b02a12009-04-22 02:26:14 +00002674 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002675
Chris Lattner34b02a12009-04-22 02:26:14 +00002676 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlsson80f25672008-09-09 17:59:25 +00002677 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002678 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00002679
Daniel Dunbar55e87422008-11-11 02:29:29 +00002680 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
2681 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002682 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002683
Anders Carlsson80f25672008-09-09 17:59:25 +00002684 CGF.EmitBlock(CatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002685
Daniel Dunbar55e40722008-09-27 07:03:52 +00002686 // Handle catch list. As a special case we check if everything is
2687 // matched and avoid generating code for falling off the end if
2688 // so.
2689 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00002690 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
2691 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Daniel Dunbar55e87422008-11-11 02:29:29 +00002692 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00002693
Douglas Gregorc00d8e12010-04-26 16:46:50 +00002694 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00002695 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00002696
Anders Carlsson80f25672008-09-09 17:59:25 +00002697 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00002698 if (!CatchParam) {
2699 AllMatched = true;
2700 } else {
John McCall183700f2009-09-21 23:43:11 +00002701 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002702
2703 // catch(id e) always matches.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00002704 // FIXME: For the time being we also match id<X>; this should
2705 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00002706 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00002707 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00002708 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002709
2710 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00002711 if (CatchParam) {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002712 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002713 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002714 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002715 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002716
Anders Carlssondde0a942008-09-11 09:15:33 +00002717 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002718 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002719 break;
2720 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002721
Steve Naroff14108da2009-07-10 23:34:53 +00002722 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00002723 const ObjCObjectType *ObjTy = OPT->getObjectType();
2724 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
2725 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00002726
2727 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00002728 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002729
Chris Lattner34b02a12009-04-22 02:26:14 +00002730 llvm::Value *Match =
2731 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
2732 Class, Caught, "match");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002733
Daniel Dunbar55e87422008-11-11 02:29:29 +00002734 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002735
2736 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002737 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002738
Anders Carlsson80f25672008-09-09 17:59:25 +00002739 // Emit the @catch block.
2740 CGF.EmitBlock(MatchedBlock);
Steve Naroff7ba138a2009-03-03 19:52:17 +00002741 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002742 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002743
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002744 llvm::Value *Tmp =
2745 CGF.Builder.CreateBitCast(Caught,
2746 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002747 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002748 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002749
Anders Carlssondde0a942008-09-11 09:15:33 +00002750 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002751 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002752
Anders Carlsson80f25672008-09-09 17:59:25 +00002753 CGF.EmitBlock(NextCatchBlock);
2754 }
2755
Daniel Dunbar55e40722008-09-27 07:03:52 +00002756 if (!AllMatched) {
2757 // None of the handlers caught the exception, so store it to be
2758 // rethrown at the end of the @finally block.
2759 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002760 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002761 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002762
Daniel Dunbar55e40722008-09-27 07:03:52 +00002763 // Emit the exception handler for the @catch blocks.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002764 CGF.EmitBlock(CatchHandler);
Chris Lattner34b02a12009-04-22 02:26:14 +00002765 CGF.Builder.CreateStore(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002766 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2767 ExceptionData),
2768 RethrowPtr);
2769 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(VMContext),
2770 CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002771 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002772 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00002773 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002774 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(VMContext),
2775 CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002776 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson80f25672008-09-09 17:59:25 +00002777 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002778
Daniel Dunbar898d5082008-09-30 01:06:03 +00002779 // Pop the exception-handling stack entry. It is important to do
2780 // this now, because the code in the @finally block is not in this
2781 // context.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002782 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2783
Anders Carlsson273558f2009-02-07 21:37:21 +00002784 CGF.ObjCEHValueStack.pop_back();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002785
Anders Carlsson80f25672008-09-09 17:59:25 +00002786 // Emit the @finally block.
2787 CGF.EmitBlock(FinallyBlock);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002788 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002789
Anders Carlsson190d00e2009-02-07 21:26:04 +00002790 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002791
Anders Carlsson190d00e2009-02-07 21:26:04 +00002792 CGF.EmitBlock(FinallyExit);
Chris Lattner34b02a12009-04-22 02:26:14 +00002793 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002794
2795 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002796 if (isTry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002797 if (const ObjCAtFinallyStmt* FinallyStmt =
2798 cast<ObjCAtTryStmt>(S).getFinallyStmt())
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002799 CGF.EmitStmt(FinallyStmt->getFinallyBody());
Daniel Dunbar1c566672009-02-24 01:43:46 +00002800 } else {
2801 // Emit objc_sync_exit(expr); as finally's sole statement for
2802 // @synchronized.
Chris Lattnerbbccd612009-04-22 02:38:11 +00002803 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002804 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002805
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002806 // Emit the switch block
2807 if (Info.SwitchBlock)
2808 CGF.EmitBlock(Info.SwitchBlock);
2809 if (Info.EndBlock)
2810 CGF.EmitBlock(Info.EndBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002811
Daniel Dunbar898d5082008-09-30 01:06:03 +00002812 CGF.EmitBlock(FinallyRethrow);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002813 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbar898d5082008-09-30 01:06:03 +00002814 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002815 CGF.Builder.CreateUnreachable();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002816
Daniel Dunbar898d5082008-09-30 01:06:03 +00002817 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002818}
2819
2820void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002821 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002822 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002823
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002824 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2825 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002826 ExceptionAsObject =
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002827 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2828 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002829 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002830 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00002831 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002832 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002833
Chris Lattnerbbccd612009-04-22 02:38:11 +00002834 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002835 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002836
2837 // Clear the insertion point to indicate we are in unreachable code.
2838 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002839}
2840
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002841/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002842/// object: objc_read_weak (id *src)
2843///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002844llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00002845 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00002846 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002847 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
2848 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
2849 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00002850 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002851 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00002852 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002853 return read_weak;
2854}
2855
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002856/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2857/// objc_assign_weak (id src, id *dst)
2858///
2859void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00002860 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002861 const llvm::Type * SrcTy = src->getType();
2862 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002863 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002864 assert(Size <= 8 && "does not support size > 8");
2865 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002866 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002867 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2868 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002869 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2870 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00002871 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002872 src, dst, "weakassign");
2873 return;
2874}
2875
Fariborz Jahanian58626502008-11-19 00:59:10 +00002876/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2877/// objc_assign_global (id src, id *dst)
2878///
2879void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00002880 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002881 const llvm::Type * SrcTy = src->getType();
2882 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002883 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002884 assert(Size <= 8 && "does not support size > 8");
2885 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002886 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002887 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2888 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002889 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2890 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002891 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00002892 src, dst, "globalassign");
2893 return;
2894}
2895
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002896/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00002897/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002898///
2899void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00002900 llvm::Value *src, llvm::Value *dst,
2901 llvm::Value *ivarOffset) {
2902 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002903 const llvm::Type * SrcTy = src->getType();
2904 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002905 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002906 assert(Size <= 8 && "does not support size > 8");
2907 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002908 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002909 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2910 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002911 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2912 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00002913 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
2914 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002915 return;
2916}
2917
Fariborz Jahanian58626502008-11-19 00:59:10 +00002918/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2919/// objc_assign_strongCast (id src, id *dst)
2920///
2921void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00002922 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002923 const llvm::Type * SrcTy = src->getType();
2924 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002925 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002926 assert(Size <= 8 && "does not support size > 8");
2927 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002928 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002929 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2930 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002931 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2932 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002933 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00002934 src, dst, "weakassign");
2935 return;
2936}
2937
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002938void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002939 llvm::Value *DestPtr,
2940 llvm::Value *SrcPtr,
Fariborz Jahanian08c32132009-08-31 19:33:16 +00002941 QualType Ty) {
2942 // Get size info for this aggregate.
2943 std::pair<uint64_t, unsigned> TypeInfo = CGM.getContext().getTypeInfo(Ty);
Fariborz Jahanian736d8a62009-09-10 16:20:31 +00002944 unsigned long size = TypeInfo.first/8;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002945 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
2946 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002947 llvm::Value *N = llvm::ConstantInt::get(ObjCTypes.LongTy, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002948 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
2949 DestPtr, SrcPtr, N);
2950 return;
2951}
2952
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002953/// EmitObjCValueForIvar - Code Gen for ivar reference.
2954///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002955LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2956 QualType ObjectTy,
2957 llvm::Value *BaseValue,
2958 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002959 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00002960 const ObjCInterfaceDecl *ID =
2961 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00002962 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
2963 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002964}
2965
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002966llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00002967 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002968 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00002969 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002970 return llvm::ConstantInt::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002971 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2972 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002973}
2974
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002975/* *** Private Interface *** */
2976
2977/// EmitImageInfo - Emit the image info marker used to encode some module
2978/// level information.
2979///
2980/// See: <rdr://4810609&4810587&4810587>
2981/// struct IMAGE_INFO {
2982/// unsigned version;
2983/// unsigned flags;
2984/// };
2985enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00002986 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002987 eImageInfo_GarbageCollected = (1 << 1),
2988 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002989 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
2990
Daniel Dunbarfce176b2010-04-25 20:39:01 +00002991 // A flag indicating that the module has no instances of a @synthesize of a
2992 // superclass variable. <rdar://problem/6803242>
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002993 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002994};
2995
Daniel Dunbarfce176b2010-04-25 20:39:01 +00002996void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002997 unsigned version = 0; // Version is unused?
2998 unsigned flags = 0;
2999
3000 // FIXME: Fix and continue?
3001 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3002 flags |= eImageInfo_GarbageCollected;
3003 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3004 flags |= eImageInfo_GCOnly;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003005
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003006 // We never allow @synthesize of a superclass property.
3007 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003008
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003009 // Emitted as int[2];
3010 llvm::Constant *values[2] = {
Owen Anderson0032b272009-08-13 21:57:51 +00003011 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), version),
3012 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), flags)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003013 };
Owen Anderson0032b272009-08-13 21:57:51 +00003014 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003015
3016 const char *Section;
3017 if (ObjCABI == 1)
3018 Section = "__OBJC, __image_info,regular";
3019 else
3020 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003021 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003022 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson7db6d832009-07-28 18:33:04 +00003023 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003024 Section,
3025 0,
3026 true);
3027 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003028}
3029
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003030
3031// struct objc_module {
3032// unsigned long version;
3033// unsigned long size;
3034// const char *name;
3035// Symtab symtab;
3036// };
3037
3038// FIXME: Get from somewhere
3039static const int ModuleVersion = 7;
3040
3041void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00003042 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003043
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003044 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003045 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3046 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00003047 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003048 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003049 Values[3] = EmitModuleSymbols();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003050 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00003051 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003052 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003053 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003054}
3055
3056llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003057 unsigned NumClasses = DefinedClasses.size();
3058 unsigned NumCategories = DefinedCategories.size();
3059
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003060 // Return null if no symbols were defined.
3061 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003062 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003063
3064 std::vector<llvm::Constant*> Values(5);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003065 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003066 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003067 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3068 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003069
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003070 // The runtime expects exactly the list of defined classes followed
3071 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003072 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003073 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00003074 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003075 ObjCTypes.Int8PtrTy);
3076 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003077 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003078 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003079 ObjCTypes.Int8PtrTy);
3080
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003081 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003082 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003083 NumClasses + NumCategories),
3084 Symbols);
3085
Nick Lewycky0d36dd22009-09-19 20:00:52 +00003086 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003087
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003088 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003089 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3090 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003091 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003092 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003093}
3094
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003095llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003096 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003097 LazySymbols.insert(ID->getIdentifier());
3098
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003099 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003100
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003101 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003102 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003103 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003104 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003105 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003106 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3107 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003108 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003109 }
3110
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003111 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003112}
3113
Daniel Dunbar45d196b2008-11-01 01:53:16 +00003114llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003115 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003116
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003117 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003118 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003119 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003120 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003121 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003122 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3123 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003124 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003125 }
3126
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003127 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003128}
3129
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003130llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003131 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003132
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003133 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003134 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003135 llvm::ConstantArray::get(VMContext,
3136 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003137 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003138 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003139
Owen Andersona1cf15f2009-07-14 23:10:40 +00003140 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003141}
3142
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003143/// GetIvarLayoutName - Returns a unique constant for the given
3144/// ivar layout bitmap.
3145llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003146 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003147 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003148}
3149
John McCall0953e762009-09-24 19:53:00 +00003150static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003151 if (FQT.isObjCGCStrong())
John McCall0953e762009-09-24 19:53:00 +00003152 return Qualifiers::Strong;
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003153
3154 if (FQT.isObjCGCWeak())
John McCall0953e762009-09-24 19:53:00 +00003155 return Qualifiers::Weak;
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003156
Fariborz Jahanian039e6a12009-09-11 17:39:05 +00003157 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00003158 return Qualifiers::Strong;
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003159
Ted Kremenek6217b802009-07-29 21:53:49 +00003160 if (const PointerType *PT = FQT->getAs<PointerType>())
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003161 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003162
John McCall0953e762009-09-24 19:53:00 +00003163 return Qualifiers::GCNone;
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003164}
3165
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003166void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003167 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003168 bool ForStrongLayout,
3169 bool &HasUnion) {
3170 const RecordDecl *RD = RT->getDecl();
3171 // FIXME - Use iterator.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003172 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003173 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003174 const llvm::StructLayout *RecLayout =
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003175 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003176
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003177 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3178 ForStrongLayout, HasUnion);
3179}
3180
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003181void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003182 const llvm::StructLayout *Layout,
3183 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00003184 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003185 unsigned int BytePos, bool ForStrongLayout,
3186 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003187 bool IsUnion = (RD && RD->isUnion());
3188 uint64_t MaxUnionIvarSize = 0;
3189 uint64_t MaxSkippedUnionIvarSize = 0;
3190 FieldDecl *MaxField = 0;
3191 FieldDecl *MaxSkippedField = 0;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003192 FieldDecl *LastFieldBitfield = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003193 uint64_t MaxFieldOffset = 0;
3194 uint64_t MaxSkippedFieldOffset = 0;
3195 uint64_t LastBitfieldOffset = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003196
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003197 if (RecFields.empty())
3198 return;
Chris Lattnerf1690852009-03-31 08:48:01 +00003199 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3200 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3201
Chris Lattnerf1690852009-03-31 08:48:01 +00003202 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003203 FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003204 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003205 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00003206 // Note that 'i' here is actually the field index inside RD of Field,
3207 // although this dependency is hidden.
3208 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
3209 FieldOffset = RL.getFieldOffset(i) / 8;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003210 } else
Daniel Dunbare05cc982009-05-03 23:35:23 +00003211 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003212
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003213 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003214 if (!Field->getIdentifier() || Field->isBitField()) {
3215 LastFieldBitfield = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003216 LastBitfieldOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003217 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003218 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003219
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003220 LastFieldBitfield = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003221 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003222 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003223 if (FQT->isUnionType())
3224 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003225
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003226 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003227 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003228 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003229 continue;
3230 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003231
Chris Lattnerf1690852009-03-31 08:48:01 +00003232 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003233 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003234 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003235 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003236 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003237 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003238 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3239 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003240 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003241 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003242 FQT = CArray->getElementType();
3243 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003244
3245 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003246 "layout for array of unions not supported");
3247 if (FQT->isRecordType()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003248 int OldIndex = IvarsInfo.size() - 1;
3249 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003250
Ted Kremenek6217b802009-07-29 21:53:49 +00003251 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003252 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003253 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003254
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003255 // Replicate layout information for each array element. Note that
3256 // one element is already done.
3257 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003258 for (int FirstIndex = IvarsInfo.size() - 1,
3259 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003260 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003261 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3262 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3263 IvarsInfo[i].ivar_size));
3264 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3265 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3266 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003267 }
3268 continue;
3269 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003270 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003271 // At this point, we are done with Record/Union and array there of.
3272 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00003273 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003274
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003275 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00003276 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3277 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003278 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003279 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003280 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003281 MaxUnionIvarSize = UnionIvarSize;
3282 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003283 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003284 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003285 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003286 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003287 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003288 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003289 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00003290 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3291 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003292 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003293 // FIXME: Why the asymmetry? We divide by word size in bits on other
3294 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003295 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003296 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003297 MaxSkippedUnionIvarSize = UnionIvarSize;
3298 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003299 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003300 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003301 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003302 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003303 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003304 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003305 }
3306 }
3307 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003308
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003309 if (LastFieldBitfield) {
3310 // Last field was a bitfield. Must update skip info.
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003311 Expr *BitWidth = LastFieldBitfield->getBitWidth();
3312 uint64_t BitFieldSize =
Eli Friedman9a901bb2009-04-26 19:19:15 +00003313 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Daniel Dunbar487993b2009-05-03 13:32:01 +00003314 GC_IVAR skivar;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003315 skivar.ivar_bytepos = BytePos + LastBitfieldOffset;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003316 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3317 + ((BitFieldSize % ByteSizeInBits) != 0);
3318 SkipIvars.push_back(skivar);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003319 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003320
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003321 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003322 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003323 MaxUnionIvarSize));
3324 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003325 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003326 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003327}
3328
3329/// BuildIvarLayout - Builds ivar layout bitmap for the class
3330/// implementation for the __strong or __weak case.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003331/// The layout map displays which words in ivar list must be skipped
3332/// and which must be scanned by GC (see below). String is built of bytes.
3333/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003334/// of words to skip and right nibble is count of words to scan. So, each
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003335/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003336/// represented by a 0x00 byte which also ends the string.
3337/// 1. when ForStrongLayout is true, following ivars are scanned:
3338/// - id, Class
3339/// - object *
3340/// - __strong anything
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003341///
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003342/// 2. When ForStrongLayout is false, following ivars are scanned:
3343/// - __weak anything
3344///
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003345llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003346 const ObjCImplementationDecl *OMD,
3347 bool ForStrongLayout) {
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003348 bool hasUnion = false;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003349
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003350 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00003351 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003352 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003353 return llvm::Constant::getNullValue(PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003354
Chris Lattnerf1690852009-03-31 08:48:01 +00003355 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003356 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003357 CGM.getContext().CollectObjCIvars(OI, RecFields);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003358
Daniel Dunbar37153282009-05-04 04:10:48 +00003359 // Add this implementations synthesized ivars.
Fariborz Jahanian98200742009-05-12 18:14:29 +00003360 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
Fariborz Jahanian11062e12010-02-19 00:31:17 +00003361 CGM.getContext().CollectNonClassIvars(OI, Ivars);
Fariborz Jahanian98200742009-05-12 18:14:29 +00003362 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3363 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003364
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003365 if (RecFields.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00003366 return llvm::Constant::getNullValue(PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003367
3368 SkipIvars.clear();
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003369 IvarsInfo.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003370
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003371 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003372 if (IvarsInfo.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00003373 return llvm::Constant::getNullValue(PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003374
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003375 // Sort on byte position in case we encounterred a union nested in
3376 // the ivar list.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003377 if (hasUnion && !IvarsInfo.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003378 std::sort(IvarsInfo.begin(), IvarsInfo.end());
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003379 if (hasUnion && !SkipIvars.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003380 std::sort(SkipIvars.begin(), SkipIvars.end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003381
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003382 // Build the string of skip/scan nibbles
Fariborz Jahanian8c2f2d12009-04-24 17:15:27 +00003383 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003384 unsigned int WordSize =
Duncan Sands9408c452009-05-09 07:08:47 +00003385 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003386 if (IvarsInfo[0].ivar_bytepos == 0) {
3387 WordsToSkip = 0;
3388 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003389 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003390 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3391 WordsToScan = IvarsInfo[0].ivar_size;
3392 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003393 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003394 unsigned int TailPrevGCObjC =
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003395 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003396 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003397 // consecutive 'scanned' object pointers.
3398 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003399 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003400 // Skip over 'gc'able object pointer which lay over each other.
3401 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3402 continue;
3403 // Must skip over 1 or more words. We save current skip/scan values
3404 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003405 SKIP_SCAN SkScan;
3406 SkScan.skip = WordsToSkip;
3407 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003408 SkipScanIvars.push_back(SkScan);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003409
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003410 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003411 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3412 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003413 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003414 WordsToSkip = 0;
3415 WordsToScan = IvarsInfo[i].ivar_size;
3416 }
3417 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003418 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003419 SKIP_SCAN SkScan;
3420 SkScan.skip = WordsToSkip;
3421 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003422 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003423 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003424
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003425 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003426 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003427 int LastByteSkipped =
3428 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003429 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003430 int LastByteScanned =
3431 IvarsInfo[LastIndex].ivar_bytepos +
3432 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003433 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00003434 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003435 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003436 SKIP_SCAN SkScan;
3437 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3438 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003439 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003440 }
3441 }
3442 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3443 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003444 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003445 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003446 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3447 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3448 // 0xM0 followed by 0x0N detected.
3449 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3450 for (int j = i+1; j < SkipScan; j++)
3451 SkipScanIvars[j] = SkipScanIvars[j+1];
3452 --SkipScan;
3453 }
3454 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003455
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003456 // Generate the string.
3457 std::string BitMap;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003458 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003459 unsigned char byte;
3460 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3461 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3462 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3463 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
3464
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003465 // first skip big.
3466 for (unsigned int ix = 0; ix < skip_big; ix++)
3467 BitMap += (unsigned char)(0xf0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003468
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003469 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003470 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003471 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003472 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003473 byte |= 0xf;
3474 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003475 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003476 byte |= scan_small;
3477 scan_small = 0;
3478 }
3479 BitMap += byte;
3480 }
3481 // next scan big
3482 for (unsigned int ix = 0; ix < scan_big; ix++)
3483 BitMap += (unsigned char)(0x0f);
3484 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003485 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003486 byte = scan_small;
3487 BitMap += byte;
3488 }
3489 }
3490 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003491 unsigned char zero = 0;
3492 BitMap += zero;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003493
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003494 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003495 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003496 ForStrongLayout ? "strong" : "weak",
3497 OMD->getClassInterface()->getNameAsCString());
3498 const unsigned char *s = (unsigned char*)BitMap.c_str();
3499 for (unsigned i = 0; i < BitMap.size(); i++)
3500 if (!(s[i] & 0xf0))
3501 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3502 else
3503 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3504 printf("\n");
3505 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003506 llvm::GlobalVariable * Entry =
3507 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Owen Anderson0032b272009-08-13 21:57:51 +00003508 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003509 "__TEXT,__cstring,cstring_literals",
3510 1, true);
3511 return getConstantGEP(VMContext, Entry, 0, 0);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003512}
3513
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003514llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003515 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3516
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003517 // FIXME: Avoid std::string copying.
3518 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003519 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson0032b272009-08-13 21:57:51 +00003520 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003521 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003522 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003523
Owen Andersona1cf15f2009-07-14 23:10:40 +00003524 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003525}
3526
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003527// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003528llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003529 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3530}
3531
3532// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003533llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003534 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
3535}
3536
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003537llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003538 std::string TypeStr;
3539 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3540
3541 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003542
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003543 if (!Entry)
3544 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00003545 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003546 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003547 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003548
Owen Andersona1cf15f2009-07-14 23:10:40 +00003549 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003550}
3551
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003552llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003553 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003554 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3555 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00003556
3557 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3558
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003559 if (!Entry)
3560 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00003561 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003562 "__TEXT,__cstring,cstring_literals",
3563 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00003564
Owen Andersona1cf15f2009-07-14 23:10:40 +00003565 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003566}
3567
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003568// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003569llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003570 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003571
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003572 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003573 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003574 llvm::ConstantArray::get(VMContext,
3575 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003576 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003577 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003578
Owen Andersona1cf15f2009-07-14 23:10:40 +00003579 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003580}
3581
3582// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003583// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003584llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003585CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3586 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003587 std::string TypeStr;
3588 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003589 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3590}
3591
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003592void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003593 const ObjCContainerDecl *CD,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003594 llvm::SmallVectorImpl<char> &Name) {
3595 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003596 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003597 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
3598 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003599 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003600 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramer900fc632010-04-17 09:33:03 +00003601 OS << '(' << CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003602 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003603}
3604
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003605void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003606 EmitModuleInfo();
3607
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003608 // Emit the dummy bodies for any protocols which were referenced but
3609 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003610 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00003611 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
3612 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003613 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003614
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003615 std::vector<llvm::Constant*> Values(5);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003616 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00003617 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003618 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003619 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00003620 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00003621 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00003622 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003623 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00003624 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003625 }
3626
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003627 // Add assembler directives to add lazy undefined symbol references
3628 // for classes which are referenced but not defined. This is
3629 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00003630 //
3631 // FIXME: It would be nice if we had an LLVM construct for this.
3632 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
3633 llvm::SmallString<256> Asm;
3634 Asm += CGM.getModule().getModuleInlineAsm();
3635 if (!Asm.empty() && Asm.back() != '\n')
3636 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003637
Daniel Dunbar33063492009-09-07 00:20:42 +00003638 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00003639 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
3640 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003641 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
3642 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00003643 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
3644 e = LazySymbols.end(); I != e; ++I)
3645 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
3646
Daniel Dunbar33063492009-09-07 00:20:42 +00003647 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003648 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003649}
3650
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003651CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003652 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00003653 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003654 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003655 ObjCABI = 2;
3656}
3657
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003658/* *** */
3659
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003660ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00003661 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003662 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3663 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003664
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003665 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003666 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003667 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003668 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00003669 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003670
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003671 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00003672 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003673 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003674
Mike Stumpf5408fe2009-05-16 07:57:57 +00003675 // FIXME: It would be nice to unify this with the opaque type, so that the IR
3676 // comes out a bit cleaner.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003677 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00003678 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003679
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003680 // I'm not sure I like this. The implicit coordination is a bit
3681 // gross. We should solve this in a reasonable fashion because this
3682 // is a pretty common task (match some runtime data structure with
3683 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003684
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003685 // FIXME: This is leaked.
3686 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003687
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003688 // struct _objc_super {
3689 // id self;
3690 // Class cls;
3691 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003692 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00003693 Ctx.getTranslationUnitDecl(),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003694 SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003695 &Ctx.Idents.get("_objc_super"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003696 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00003697 Ctx.getObjCIdType(), 0, 0, false));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003698 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00003699 Ctx.getObjCClassType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00003700 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003701
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003702 SuperCTy = Ctx.getTagDeclType(RD);
3703 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003704
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003705 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003706 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
3707
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003708 // struct _prop_t {
3709 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003710 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003711 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00003712 PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003713 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003714 PropertyTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003715
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003716 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003717 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003718 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003719 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003720 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00003721 PropertyListTy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003722 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00003723 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003724 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003725 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003726 PropertyListTy);
3727 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00003728 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003729
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003730 // struct _objc_method {
3731 // SEL _cmd;
3732 // char *method_type;
3733 // char *_imp;
3734 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00003735 MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003736 Int8PtrTy,
3737 Int8PtrTy,
3738 NULL);
3739 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003740
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003741 // struct _objc_cache *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00003742 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003743 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00003744 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003745}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003746
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003747ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00003748 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003749 // struct _objc_method_description {
3750 // SEL name;
3751 // char *types;
3752 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003753 MethodDescriptionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00003754 llvm::StructType::get(VMContext, SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003755 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003756 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003757 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003758 MethodDescriptionTy);
3759
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003760 // struct _objc_method_description_list {
3761 // int count;
3762 // struct _objc_method_description[1];
3763 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003764 MethodDescriptionListTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00003765 llvm::StructType::get(VMContext, IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00003766 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003767 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003768 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003769 MethodDescriptionListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003770
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003771 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003772 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003773 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003774
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003775 // Protocol description structures
3776
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003777 // struct _objc_protocol_extension {
3778 // uint32_t size; // sizeof(struct _objc_protocol_extension)
3779 // struct _objc_method_description_list *optional_instance_methods;
3780 // struct _objc_method_description_list *optional_class_methods;
3781 // struct _objc_property_list *instance_properties;
3782 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003783 ProtocolExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00003784 llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003785 MethodDescriptionListPtrTy,
3786 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003787 PropertyListPtrTy,
3788 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003789 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003790 ProtocolExtensionTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003791
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003792 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00003793 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003794
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003795 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003796
Owen Anderson8c8f69e2009-08-13 23:27:53 +00003797 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
3798 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003799
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003800 const llvm::Type *T =
Mike Stump1eb44332009-09-09 15:08:12 +00003801 llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00003802 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003803 LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00003804 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003805 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003806 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
3807
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003808 // struct _objc_protocol {
3809 // struct _objc_protocol_extension *isa;
3810 // char *protocol_name;
3811 // struct _objc_protocol **_objc_protocol_list;
3812 // struct _objc_method_description_list *instance_methods;
3813 // struct _objc_method_description_list *class_methods;
3814 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00003815 T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003816 Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003817 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003818 MethodDescriptionListPtrTy,
3819 MethodDescriptionListPtrTy,
3820 NULL);
3821 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
3822
3823 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003824 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003825 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003826 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00003827 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003828
3829 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003830 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00003831 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003832
3833 // Class description structures
3834
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003835 // struct _objc_ivar {
3836 // char *ivar_name;
3837 // char *ivar_type;
3838 // int ivar_offset;
3839 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00003840 IvarTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003841 Int8PtrTy,
3842 IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003843 NULL);
3844 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
3845
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003846 // struct _objc_ivar_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00003847 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003848 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00003849 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003850
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003851 // struct _objc_method_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00003852 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003853 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00003854 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003855
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003856 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003857 ClassExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00003858 llvm::StructType::get(VMContext, IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003859 Int8PtrTy,
3860 PropertyListPtrTy,
3861 NULL);
3862 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00003863 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003864
Owen Anderson8c8f69e2009-08-13 23:27:53 +00003865 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003866
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003867 // struct _objc_class {
3868 // Class isa;
3869 // Class super_class;
3870 // char *name;
3871 // long version;
3872 // long info;
3873 // long instance_size;
3874 // struct _objc_ivar_list *ivars;
3875 // struct _objc_method_list *methods;
3876 // struct _objc_cache *cache;
3877 // struct _objc_protocol_list *protocols;
3878 // char *ivar_layout;
3879 // struct _objc_class_ext *ext;
3880 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00003881 T = llvm::StructType::get(VMContext,
3882 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson96e0fc72009-07-29 22:16:19 +00003883 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003884 Int8PtrTy,
3885 LongTy,
3886 LongTy,
3887 LongTy,
3888 IvarListPtrTy,
3889 MethodListPtrTy,
3890 CachePtrTy,
3891 ProtocolListPtrTy,
3892 Int8PtrTy,
3893 ClassExtensionPtrTy,
3894 NULL);
3895 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003896
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003897 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
3898 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00003899 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003900
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003901 // struct _objc_category {
3902 // char *category_name;
3903 // char *class_name;
3904 // struct _objc_method_list *instance_method;
3905 // struct _objc_method_list *class_method;
3906 // uint32_t size; // sizeof(struct _objc_category)
3907 // struct _objc_property_list *instance_properties;// category's @property
3908 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00003909 CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003910 Int8PtrTy,
3911 MethodListPtrTy,
3912 MethodListPtrTy,
3913 ProtocolListPtrTy,
3914 IntTy,
3915 PropertyListPtrTy,
3916 NULL);
3917 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
3918
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003919 // Global metadata structures
3920
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003921 // struct _objc_symtab {
3922 // long sel_ref_cnt;
3923 // SEL *refs;
3924 // short cls_def_cnt;
3925 // short cat_def_cnt;
3926 // char *defs[cls_def_cnt + cat_def_cnt];
3927 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00003928 SymtabTy = llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003929 SelectorPtrTy,
3930 ShortTy,
3931 ShortTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00003932 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003933 NULL);
3934 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00003935 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003936
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003937 // struct _objc_module {
3938 // long version;
3939 // long size; // sizeof(struct _objc_module)
3940 // char *name;
3941 // struct _objc_symtab* symtab;
3942 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003943 ModuleTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00003944 llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003945 LongTy,
3946 Int8PtrTy,
3947 SymtabPtrTy,
3948 NULL);
3949 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003950
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003951
Mike Stumpf5408fe2009-05-16 07:57:57 +00003952 // FIXME: This is the size of the setjmp buffer and should be target
3953 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00003954 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003955
Anders Carlsson124526b2008-09-09 10:10:21 +00003956 // Exceptions
Owen Anderson96e0fc72009-07-29 22:16:19 +00003957 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00003958 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003959
3960 ExceptionDataTy =
Owen Anderson0032b272009-08-13 21:57:51 +00003961 llvm::StructType::get(VMContext, llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
Owen Anderson47a434f2009-08-05 23:18:46 +00003962 SetJmpBufferSize),
Anders Carlsson124526b2008-09-09 10:10:21 +00003963 StackPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003964 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson124526b2008-09-09 10:10:21 +00003965 ExceptionDataTy);
3966
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003967}
3968
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003969ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00003970 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003971 // struct _method_list_t {
3972 // uint32_t entsize; // sizeof(struct _objc_method)
3973 // uint32_t method_count;
3974 // struct _objc_method method_list[method_count];
3975 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00003976 MethodListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003977 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00003978 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003979 NULL);
3980 CGM.getModule().addTypeName("struct.__method_list_t",
3981 MethodListnfABITy);
3982 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00003983 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003984
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003985 // struct _protocol_t {
3986 // id isa; // NULL
3987 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003988 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003989 // const struct method_list_t * const instance_methods;
3990 // const struct method_list_t * const class_methods;
3991 // const struct method_list_t *optionalInstanceMethods;
3992 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003993 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003994 // const uint32_t size; // sizeof(struct _protocol_t)
3995 // const uint32_t flags; // = 0
3996 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003997
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003998 // Holder for struct _protocol_list_t *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00003999 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004000
Owen Anderson47a434f2009-08-05 23:18:46 +00004001 ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004002 Int8PtrTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004003 llvm::PointerType::getUnqual(
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004004 ProtocolListTyHolder),
4005 MethodListnfABIPtrTy,
4006 MethodListnfABIPtrTy,
4007 MethodListnfABIPtrTy,
4008 MethodListnfABIPtrTy,
4009 PropertyListPtrTy,
4010 IntTy,
4011 IntTy,
4012 NULL);
4013 CGM.getModule().addTypeName("struct._protocol_t",
4014 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004015
4016 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004017 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004018
Fariborz Jahanianda320092009-01-29 19:24:30 +00004019 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004020 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00004021 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004022 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004023 ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004024 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004025 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004026 NULL);
4027 CGM.getModule().addTypeName("struct._objc_protocol_list",
4028 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004029 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004030 ProtocolListnfABITy);
4031
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004032 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004033 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004034
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004035 // struct _ivar_t {
4036 // unsigned long int *offset; // pointer to ivar offset location
4037 // char *name;
4038 // char *type;
4039 // uint32_t alignment;
4040 // uint32_t size;
4041 // }
Mike Stump1eb44332009-09-09 15:08:12 +00004042 IvarnfABITy = llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00004043 llvm::PointerType::getUnqual(LongTy),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004044 Int8PtrTy,
4045 Int8PtrTy,
4046 IntTy,
4047 IntTy,
4048 NULL);
4049 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004050
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004051 // struct _ivar_list_t {
4052 // uint32 entsize; // sizeof(struct _ivar_t)
4053 // uint32 count;
4054 // struct _iver_t list[count];
4055 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004056 IvarListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004057 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004058 llvm::ArrayType::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004059 IvarnfABITy, 0),
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004060 NULL);
4061 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004062
Owen Anderson96e0fc72009-07-29 22:16:19 +00004063 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004064
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004065 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004066 // uint32_t const flags;
4067 // uint32_t const instanceStart;
4068 // uint32_t const instanceSize;
4069 // uint32_t const reserved; // only when building for 64bit targets
4070 // const uint8_t * const ivarLayout;
4071 // const char *const name;
4072 // const struct _method_list_t * const baseMethods;
4073 // const struct _objc_protocol_list *const baseProtocols;
4074 // const struct _ivar_list_t *const ivars;
4075 // const uint8_t * const weakIvarLayout;
4076 // const struct _prop_list_t * const properties;
4077 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004078
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004079 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson47a434f2009-08-05 23:18:46 +00004080 ClassRonfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004081 IntTy,
4082 IntTy,
4083 Int8PtrTy,
4084 Int8PtrTy,
4085 MethodListnfABIPtrTy,
4086 ProtocolListnfABIPtrTy,
4087 IvarListnfABIPtrTy,
4088 Int8PtrTy,
4089 PropertyListPtrTy,
4090 NULL);
4091 CGM.getModule().addTypeName("struct._class_ro_t",
4092 ClassRonfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004093
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004094 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
4095 std::vector<const llvm::Type*> Params;
4096 Params.push_back(ObjectPtrTy);
4097 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004098 ImpnfABITy = llvm::PointerType::getUnqual(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004099 llvm::FunctionType::get(ObjectPtrTy, Params, false));
4100
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004101 // struct _class_t {
4102 // struct _class_t *isa;
4103 // struct _class_t * const superclass;
4104 // void *cache;
4105 // IMP *vtable;
4106 // struct class_ro_t *ro;
4107 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004108
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004109 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004110 ClassnfABITy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004111 llvm::StructType::get(VMContext,
4112 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004113 llvm::PointerType::getUnqual(ClassTyHolder),
4114 CachePtrTy,
4115 llvm::PointerType::getUnqual(ImpnfABITy),
4116 llvm::PointerType::getUnqual(ClassRonfABITy),
4117 NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004118 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4119
4120 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004121 ClassnfABITy);
4122
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004123 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004124 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004125
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004126 // struct _category_t {
4127 // const char * const name;
4128 // struct _class_t *const cls;
4129 // const struct _method_list_t * const instance_methods;
4130 // const struct _method_list_t * const class_methods;
4131 // const struct _protocol_list_t * const protocols;
4132 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004133 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004134 CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004135 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004136 MethodListnfABIPtrTy,
4137 MethodListnfABIPtrTy,
4138 ProtocolListnfABIPtrTy,
4139 PropertyListPtrTy,
4140 NULL);
4141 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004142
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004143 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004144 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4145 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004146
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004147 // MessageRefTy - LLVM for:
4148 // struct _message_ref_t {
4149 // IMP messenger;
4150 // SEL name;
4151 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004152
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004153 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004154 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004155 Ctx.getTranslationUnitDecl(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004156 SourceLocation(),
4157 &Ctx.Idents.get("_message_ref_t"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004158 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004159 Ctx.VoidPtrTy, 0, 0, false));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004160 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004161 Ctx.getObjCSelType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004162 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004163
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004164 MessageRefCTy = Ctx.getTagDeclType(RD);
4165 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4166 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004167
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004168 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004169 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004170
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004171 // SuperMessageRefTy - LLVM for:
4172 // struct _super_message_ref_t {
4173 // SUPER_IMP messenger;
4174 // SEL name;
4175 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004176 SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004177 SelectorPtrTy,
4178 NULL);
4179 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004180
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004181 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004182 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4183
Daniel Dunbare588b992009-03-01 04:46:24 +00004184
4185 // struct objc_typeinfo {
4186 // const void** vtable; // objc_ehtype_vtable + 2
4187 // const char* name; // c++ typeinfo string
4188 // Class cls;
4189 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004190 EHTypeTy = llvm::StructType::get(VMContext,
4191 llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbare588b992009-03-01 04:46:24 +00004192 Int8PtrTy,
4193 ClassnfABIPtrTy,
4194 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004195 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004196 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004197}
4198
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004199llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004200 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004201
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004202 return NULL;
4203}
4204
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004205void CGObjCNonFragileABIMac::AddModuleClassList(const
4206 std::vector<llvm::GlobalValue*>
4207 &Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004208 const char *SymbolName,
4209 const char *SectionName) {
4210 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004211
Daniel Dunbar463b8762009-05-15 21:48:48 +00004212 if (!NumClasses)
4213 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004214
Daniel Dunbar463b8762009-05-15 21:48:48 +00004215 std::vector<llvm::Constant*> Symbols(NumClasses);
4216 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004217 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004218 ObjCTypes.Int8PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004219 llvm::Constant* Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004220 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004221 NumClasses),
4222 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004223
Daniel Dunbar463b8762009-05-15 21:48:48 +00004224 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004225 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004226 llvm::GlobalValue::InternalLinkage,
4227 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004228 SymbolName);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004229 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00004230 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004231 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004232}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004233
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004234void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4235 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004236
Daniel Dunbar463b8762009-05-15 21:48:48 +00004237 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004238 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004239 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004240 "\01L_OBJC_LABEL_CLASS_$",
4241 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004242
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004243 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4244 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4245 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4246 continue;
4247 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004248 }
4249
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004250 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4251 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4252 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4253 continue;
4254 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4255 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004256
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004257 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004258 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4259 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004260
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004261 // Build list of all implemented category addresses in array
4262 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004263 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004264 "\01L_OBJC_LABEL_CATEGORY_$",
4265 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004266 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004267 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4268 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004269
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004270 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004271}
4272
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004273/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004274/// NonLegacyDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004275/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004276/// message dispatch call for all the rest.
4277///
4278bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004279 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4280 default:
4281 assert(0 && "Invalid dispatch method!");
4282 case CodeGenOptions::Legacy:
Daniel Dunbar2feefe82010-02-01 21:07:33 +00004283 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004284 case CodeGenOptions::NonLegacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00004285 return false;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004286 case CodeGenOptions::Mixed:
4287 break;
4288 }
4289
4290 // If so, see whether this selector is in the white-list of things which must
4291 // use the new dispatch convention. We lazily build a dense set for this.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004292 if (NonLegacyDispatchMethods.empty()) {
4293 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4294 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4295 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4296 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4297 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4298 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4299 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4300 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4301 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4302 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004303
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004304 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4305 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4306 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4307 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4308 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4309 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4310 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4311 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004312 // "countByEnumeratingWithState:objects:count"
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004313 IdentifierInfo *KeyIdents[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004314 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4315 &CGM.getContext().Idents.get("objects"),
4316 &CGM.getContext().Idents.get("count")
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004317 };
4318 NonLegacyDispatchMethods.insert(
4319 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004320 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004321
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004322 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004323}
4324
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004325// Metadata flags
4326enum MetaDataDlags {
4327 CLS = 0x0,
4328 CLS_META = 0x1,
4329 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004330 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004331 CLS_EXCEPTION = 0x20
4332};
4333/// BuildClassRoTInitializer - generate meta-data for:
4334/// struct _class_ro_t {
4335/// uint32_t const flags;
4336/// uint32_t const instanceStart;
4337/// uint32_t const instanceSize;
4338/// uint32_t const reserved; // only when building for 64bit targets
4339/// const uint8_t * const ivarLayout;
4340/// const char *const name;
4341/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004342/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004343/// const struct _ivar_list_t *const ivars;
4344/// const uint8_t * const weakIvarLayout;
4345/// const struct _prop_list_t * const properties;
4346/// }
4347///
4348llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004349 unsigned flags,
4350 unsigned InstanceStart,
4351 unsigned InstanceSize,
4352 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004353 std::string ClassName = ID->getNameAsString();
4354 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004355 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4356 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4357 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004358 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004359 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4360 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004361 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004362 // const struct _method_list_t * const baseMethods;
4363 std::vector<llvm::Constant*> Methods;
4364 std::string MethodListName("\01l_OBJC_$_");
4365 if (flags & CLS_META) {
4366 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004367 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004368 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004369 // Class methods should always be defined.
4370 Methods.push_back(GetMethodConstant(*i));
4371 }
4372 } else {
4373 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004374 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004375 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004376 // Instance methods should always be defined.
4377 Methods.push_back(GetMethodConstant(*i));
4378 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004379 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004380 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004381 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004382
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004383 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4384 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004385
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004386 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4387 if (llvm::Constant *C = GetMethodConstant(MD))
4388 Methods.push_back(C);
4389 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4390 if (llvm::Constant *C = GetMethodConstant(MD))
4391 Methods.push_back(C);
4392 }
4393 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004394 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004395 Values[ 5] = EmitMethodList(MethodListName,
4396 "__DATA, __objc_const", Methods);
4397
Fariborz Jahanianda320092009-01-29 19:24:30 +00004398 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4399 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004400 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004401 + OID->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00004402 OID->protocol_begin(),
4403 OID->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004404
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004405 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004406 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004407 else
4408 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004409 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4410 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004411 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004412 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004413 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004414 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4415 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00004416 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004417 Values);
4418 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004419 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4420 llvm::GlobalValue::InternalLinkage,
4421 Init,
4422 (flags & CLS_META) ?
4423 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4424 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004425 CLASS_RO_GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004426 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004427 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004428 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004429
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004430}
4431
4432/// BuildClassMetaData - This routine defines that to-level meta-data
4433/// for the given ClassName for:
4434/// struct _class_t {
4435/// struct _class_t *isa;
4436/// struct _class_t * const superclass;
4437/// void *cache;
4438/// IMP *vtable;
4439/// struct class_ro_t *ro;
4440/// }
4441///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004442llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004443 std::string &ClassName,
4444 llvm::Constant *IsAGV,
4445 llvm::Constant *SuperClassGV,
4446 llvm::Constant *ClassRoGV,
4447 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004448 std::vector<llvm::Constant*> Values(5);
4449 Values[0] = IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004450 Values[1] = SuperClassGV;
4451 if (!Values[1])
4452 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004453 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4454 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4455 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004456 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004457 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004458 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4459 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004460 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004461 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004462 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004463 if (HiddenVisibility)
4464 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004465 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004466}
4467
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004468bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00004469CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004470 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004471}
4472
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004473void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004474 uint32_t &InstanceStart,
4475 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004476 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004477 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004478
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004479 // InstanceSize is really instance end.
Anders Carlsson243a6852009-07-18 21:26:44 +00004480 InstanceSize = llvm::RoundUpToAlignment(RL.getDataSize(), 8) / 8;
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004481
4482 // If there are no fields, the start is the same as the end.
4483 if (!RL.getFieldCount())
4484 InstanceStart = InstanceSize;
4485 else
4486 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004487}
4488
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004489void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4490 std::string ClassName = ID->getNameAsString();
4491 if (!ObjCEmptyCacheVar) {
4492 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004493 CGM.getModule(),
4494 ObjCTypes.CacheTy,
4495 false,
4496 llvm::GlobalValue::ExternalLinkage,
4497 0,
4498 "_objc_empty_cache");
4499
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004500 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004501 CGM.getModule(),
4502 ObjCTypes.ImpnfABITy,
4503 false,
4504 llvm::GlobalValue::ExternalLinkage,
4505 0,
4506 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004507 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004508 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004509 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004510 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004511 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00004512 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004513 uint32_t InstanceSize = InstanceStart;
4514 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004515 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4516 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004517
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004518 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004519
4520 bool classIsHidden =
Daniel Dunbar04d40782009-04-14 06:00:08 +00004521 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004522 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004523 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004524 if (ID->getNumIvarInitializers())
4525 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004526 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004527 // class is root
4528 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004529 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004530 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004531 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004532 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004533 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4534 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4535 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004536 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004537 if (Root->hasAttr<WeakImportAttr>())
4538 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004539 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004540 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004541 ObjCMetaClassName +
4542 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004543 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004544 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
4545 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004546 }
4547 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4548 InstanceStart,
4549 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004550 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004551 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004552 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4553 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004554 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004555
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004556 // Metadata for the class
4557 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004558 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004559 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004560 if (ID->getNumIvarInitializers())
4561 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004562
Douglas Gregor68584ed2009-06-18 16:11:24 +00004563 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004564 flags |= CLS_EXCEPTION;
4565
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004566 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004567 flags |= CLS_ROOT;
4568 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00004569 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004570 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004571 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004572 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004573 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004574 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
4575 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004576 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004577 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004578 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004579 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004580 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004581 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004582
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004583 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004584 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004585 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4586 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004587 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004588
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004589 // Determine if this class is also "non-lazy".
4590 if (ImplementationIsNonLazy(ID))
4591 DefinedNonLazyClasses.push_back(ClassMD);
4592
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004593 // Force the definition of the EHType if necessary.
4594 if (flags & CLS_EXCEPTION)
4595 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004596}
4597
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004598/// GenerateProtocolRef - This routine is called to generate code for
4599/// a protocol reference expression; as in:
4600/// @code
4601/// @protocol(Proto1);
4602/// @endcode
4603/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4604/// which will hold address of the protocol meta-data.
4605///
4606llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004607 const ObjCProtocolDecl *PD) {
4608
Fariborz Jahanian960cd062009-04-10 18:47:34 +00004609 // This routine is called for @protocol only. So, we must build definition
4610 // of protocol's meta-data (not a reference to it!)
4611 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004612 llvm::Constant *Init =
4613 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
4614 ObjCTypes.ExternalProtocolPtrTy);
4615
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004616 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
4617 ProtocolName += PD->getNameAsCString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004618
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004619 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
4620 if (PTGV)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00004621 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004622 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004623 CGM.getModule(),
4624 Init->getType(), false,
4625 llvm::GlobalValue::WeakAnyLinkage,
4626 Init,
4627 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004628 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
4629 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00004630 CGM.AddUsedGlobal(PTGV);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00004631 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004632}
4633
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004634/// GenerateCategory - Build metadata for a category implementation.
4635/// struct _category_t {
4636/// const char * const name;
4637/// struct _class_t *const cls;
4638/// const struct _method_list_t * const instance_methods;
4639/// const struct _method_list_t * const class_methods;
4640/// const struct _protocol_list_t * const protocols;
4641/// const struct _prop_list_t * const properties;
4642/// }
4643///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004644void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004645 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004646 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004647 std::string ExtCatName(Prefix + Interface->getNameAsString()+
4648 "_$_" + OCD->getNameAsString());
4649 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004650 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004651
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004652 std::vector<llvm::Constant*> Values(6);
4653 Values[0] = GetClassName(OCD->getIdentifier());
4654 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004655 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00004656 if (Interface->hasAttr<WeakImportAttr>())
4657 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
4658
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004659 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004660 std::vector<llvm::Constant*> Methods;
4661 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004662 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004663 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004664
4665 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004666 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004667 // Instance methods should always be defined.
4668 Methods.push_back(GetMethodConstant(*i));
4669 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004670
4671 Values[2] = EmitMethodList(MethodListName,
4672 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004673 Methods);
4674
4675 MethodListName = Prefix;
4676 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
4677 OCD->getNameAsString();
4678 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004679 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004680 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004681 // Class methods should always be defined.
4682 Methods.push_back(GetMethodConstant(*i));
4683 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004684
4685 Values[3] = EmitMethodList(MethodListName,
4686 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004687 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004688 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004689 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004690 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004691 llvm::SmallString<256> ExtName;
4692 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
4693 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004694 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004695 + Interface->getName() + "_$_"
4696 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004697 Category->protocol_begin(),
4698 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004699 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
4700 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00004701 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00004702 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4703 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004704 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004705
4706 llvm::Constant *Init =
4707 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004708 Values);
4709 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004710 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004711 false,
4712 llvm::GlobalValue::InternalLinkage,
4713 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004714 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004715 GCATV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004716 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004717 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00004718 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004719 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004720
4721 // Determine if this category is also "non-lazy".
4722 if (ImplementationIsNonLazy(OCD))
4723 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004724}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004725
4726/// GetMethodConstant - Return a struct objc_method constant for the
4727/// given method if it has been defined. The result is null if the
4728/// method has not been defined. The return value has type MethodPtrTy.
4729llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004730 const ObjCMethodDecl *MD) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004731 // FIXME: Use DenseMap::lookup
4732 llvm::Function *Fn = MethodDefinitions[MD];
4733 if (!Fn)
4734 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004735
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004736 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004737 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00004738 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004739 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004740 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00004741 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00004742 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004743}
4744
4745/// EmitMethodList - Build meta-data for method declarations
4746/// struct _method_list_t {
4747/// uint32_t entsize; // sizeof(struct _objc_method)
4748/// uint32_t method_count;
4749/// struct _objc_method method_list[method_count];
4750/// }
4751///
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004752llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
4753 const char *Section,
4754 const ConstantVector &Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004755 // Return null for empty list.
4756 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00004757 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004758
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004759 std::vector<llvm::Constant*> Values(3);
4760 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00004761 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004762 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004763 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004764 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004765 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004766 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00004767 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00004768 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004769
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004770 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004771 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004772 llvm::GlobalValue::InternalLinkage,
4773 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004774 Name);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004775 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004776 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004777 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00004778 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00004779 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004780 ObjCTypes.MethodListnfABIPtrTy);
4781}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004782
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004783/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
4784/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00004785llvm::GlobalVariable *
4786CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
4787 const ObjCIvarDecl *Ivar) {
4788 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00004789 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00004790 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004791 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004792 CGM.getModule().getGlobalVariable(Name);
4793 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004794 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004795 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004796 false,
4797 llvm::GlobalValue::ExternalLinkage,
4798 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00004799 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004800 return IvarOffsetGV;
4801}
4802
Daniel Dunbare83be122010-04-02 21:14:02 +00004803llvm::Constant *
4804CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
4805 const ObjCIvarDecl *Ivar,
4806 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00004807 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004808 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00004809 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004810 IvarOffsetGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004811 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00004812
Mike Stumpf5408fe2009-05-16 07:57:57 +00004813 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
4814 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00004815 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
4816 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
4817 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004818 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00004819 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00004820 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004821 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004822 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004823}
4824
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004825/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00004826/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004827/// IvarListnfABIPtrTy.
4828/// struct _ivar_t {
4829/// unsigned long int *offset; // pointer to ivar offset location
4830/// char *name;
4831/// char *type;
4832/// uint32_t alignment;
4833/// uint32_t size;
4834/// }
4835/// struct _ivar_list_t {
4836/// uint32 entsize; // sizeof(struct _ivar_t)
4837/// uint32 count;
4838/// struct _iver_t list[count];
4839/// }
4840///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004841
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004842llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004843 const ObjCImplementationDecl *ID) {
4844
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004845 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004846
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004847 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4848 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004849
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004850 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004851
Daniel Dunbar91636d62009-04-20 00:33:43 +00004852 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00004853 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00004854 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004855
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004856 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
4857 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00004858 // Ignore unnamed bit-fields.
4859 if (!IVD->getDeclName())
4860 continue;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004861 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004862 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004863 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
4864 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004865 const llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004866 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00004867 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004868 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004869 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004870 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004871 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00004872 // NOTE. Size of a bitfield does not match gcc's, because of the
4873 // way bitfields are treated special in each. But I am told that
4874 // 'size' for bitfield ivars is ignored by the runtime so it does
4875 // not matter. If it matters, there is enough info to get the
4876 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004877 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00004878 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004879 }
4880 // Return null for empty list.
4881 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00004882 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004883 std::vector<llvm::Constant*> Values(3);
Duncan Sands9408c452009-05-09 07:08:47 +00004884 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004885 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4886 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004887 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004888 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00004889 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00004890 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004891 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4892 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004893 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004894 llvm::GlobalValue::InternalLinkage,
4895 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004896 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004897 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004898 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004899 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004900
Chris Lattnerad64e022009-07-17 23:57:13 +00004901 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00004902 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004903}
4904
4905llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004906 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004907 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004908
Fariborz Jahanianda320092009-01-29 19:24:30 +00004909 if (!Entry) {
4910 // We use the initializer as a marker of whether this is a forward
4911 // reference or not. At module finalization we add the empty
4912 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004913 Entry =
4914 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
4915 llvm::GlobalValue::ExternalLinkage,
4916 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004917 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00004918 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004919 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004920
Fariborz Jahanianda320092009-01-29 19:24:30 +00004921 return Entry;
4922}
4923
4924/// GetOrEmitProtocol - Generate the protocol meta-data:
4925/// @code
4926/// struct _protocol_t {
4927/// id isa; // NULL
4928/// const char * const protocol_name;
4929/// const struct _protocol_list_t * protocol_list; // super protocols
4930/// const struct method_list_t * const instance_methods;
4931/// const struct method_list_t * const class_methods;
4932/// const struct method_list_t *optionalInstanceMethods;
4933/// const struct method_list_t *optionalClassMethods;
4934/// const struct _prop_list_t * properties;
4935/// const uint32_t size; // sizeof(struct _protocol_t)
4936/// const uint32_t flags; // = 0
4937/// }
4938/// @endcode
4939///
4940
4941llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004942 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004943 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004944
Fariborz Jahanianda320092009-01-29 19:24:30 +00004945 // Early exit if a defining object has already been generated.
4946 if (Entry && Entry->hasInitializer())
4947 return Entry;
4948
Fariborz Jahanianda320092009-01-29 19:24:30 +00004949 // Construct method lists.
4950 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4951 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004952 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004953 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004954 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004955 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004956 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4957 OptInstanceMethods.push_back(C);
4958 } else {
4959 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004960 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00004961 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004962
4963 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004964 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004965 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004966 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004967 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4968 OptClassMethods.push_back(C);
4969 } else {
4970 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004971 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00004972 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004973
Fariborz Jahanianda320092009-01-29 19:24:30 +00004974 std::vector<llvm::Constant*> Values(10);
4975 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00004976 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004977 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004978 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
4979 PD->protocol_begin(),
4980 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004981
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004982 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004983 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00004984 "__DATA, __objc_const",
4985 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004986 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004987 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00004988 "__DATA, __objc_const",
4989 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004990 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004991 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00004992 "__DATA, __objc_const",
4993 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004994 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004995 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00004996 "__DATA, __objc_const",
4997 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004998 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00004999 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005000 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00005001 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005002 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005003 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005004 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005005 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005006
Fariborz Jahanianda320092009-01-29 19:24:30 +00005007 if (Entry) {
5008 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00005009 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005010 Entry->setInitializer(Init);
5011 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005012 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005013 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5014 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5015 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005016 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005017 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005018 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005019 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005020 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005021 CGM.AddUsedGlobal(Entry);
5022
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005023 // Use this protocol meta-data to build protocol list table in section
5024 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005025 llvm::GlobalVariable *PTGV =
5026 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5027 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5028 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005029 PTGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005030 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00005031 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005032 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005033 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005034 return Entry;
5035}
5036
5037/// EmitProtocolList - Generate protocol list meta-data:
5038/// @code
5039/// struct _protocol_list_t {
5040/// long protocol_count; // Note, this is 32/64 bit
5041/// struct _protocol_t[protocol_count];
5042/// }
5043/// @endcode
5044///
5045llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005046CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5047 ObjCProtocolDecl::protocol_iterator begin,
5048 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005049 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005050
Fariborz Jahanianda320092009-01-29 19:24:30 +00005051 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005052 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005053 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005054
Daniel Dunbar948e2582009-02-15 07:36:20 +00005055 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005056 llvm::SmallString<256> TmpName;
5057 Name.toVector(TmpName);
5058 llvm::GlobalVariable *GV =
5059 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005060 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005061 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005062
Daniel Dunbar948e2582009-02-15 07:36:20 +00005063 for (; begin != end; ++begin)
5064 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5065
Fariborz Jahanianda320092009-01-29 19:24:30 +00005066 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005067 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005068 ObjCTypes.ProtocolnfABIPtrTy));
5069
Fariborz Jahanianda320092009-01-29 19:24:30 +00005070 std::vector<llvm::Constant*> Values(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005071 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005072 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005073 Values[1] =
Owen Anderson7db6d832009-07-28 18:33:04 +00005074 llvm::ConstantArray::get(
Owen Anderson96e0fc72009-07-29 22:16:19 +00005075 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005076 ProtocolRefs.size()),
5077 ProtocolRefs);
5078
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005079 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005080 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005081 llvm::GlobalValue::InternalLinkage,
5082 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005083 Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005084 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005085 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005086 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00005087 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005088 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00005089 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005090}
5091
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005092/// GetMethodDescriptionConstant - This routine build following meta-data:
5093/// struct _objc_method {
5094/// SEL _cmd;
5095/// char *method_type;
5096/// char *_imp;
5097/// }
5098
5099llvm::Constant *
5100CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5101 std::vector<llvm::Constant*> Desc(3);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005102 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005103 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5104 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005105 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005106 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005107 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005108 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005109}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005110
5111/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5112/// This code gen. amounts to generating code for:
5113/// @code
5114/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5115/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005116///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00005117LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00005118 CodeGen::CodeGenFunction &CGF,
5119 QualType ObjectTy,
5120 llvm::Value *BaseValue,
5121 const ObjCIvarDecl *Ivar,
5122 unsigned CVRQualifiers) {
5123 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00005124 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5125 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005126}
5127
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005128llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005129 CodeGen::CodeGenFunction &CGF,
5130 const ObjCInterfaceDecl *Interface,
5131 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005132 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005133}
5134
Fariborz Jahanian46551122009-02-04 00:22:57 +00005135CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005136 CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005137 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005138 QualType ResultType,
5139 Selector Sel,
5140 llvm::Value *Receiver,
5141 QualType Arg0Ty,
5142 bool IsSuper,
5143 const CallArgList &CallArgs) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00005144 // FIXME. Even though IsSuper is passes. This function doese not handle calls
5145 // to 'super' receivers.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005146 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005147 llvm::Value *Arg0 = Receiver;
5148 if (!IsSuper)
5149 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005150
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005151 // Find the message function name.
Mike Stumpf5408fe2009-05-16 07:57:57 +00005152 // FIXME. This is too much work to get the ABI-specific result type needed to
5153 // find the message name.
John McCallead608a2010-02-26 00:48:12 +00005154 const CGFunctionInfo &FnInfo
Rafael Espindola264ba482010-03-30 20:24:48 +00005155 = Types.getFunctionInfo(ResultType, CallArgList(),
5156 FunctionType::ExtInfo());
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005157 llvm::Constant *Fn = 0;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005158 std::string Name("\01l_");
5159 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005160#if 0
5161 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005162 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005163 Fn = ObjCTypes.getMessageSendIdStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005164 // FIXME. Is there a better way of getting these names.
5165 // They are available in RuntimeFunctions vector pair.
5166 Name += "objc_msgSendId_stret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005167 } else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005168#endif
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005169 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005170 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005171 Name += "objc_msgSendSuper2_stret_fixup";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005172 } else {
5173 Fn = ObjCTypes.getMessageSendStretFixupFn();
5174 Name += "objc_msgSend_stret_fixup";
5175 }
Mike Stumpb3589f42009-07-30 22:28:39 +00005176 } else if (!IsSuper && ResultType->isFloatingType()) {
Daniel Dunbarc0183e82009-06-26 18:32:06 +00005177 if (ResultType->isSpecificBuiltinType(BuiltinType::LongDouble)) {
5178 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5179 Name += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005180 } else {
Daniel Dunbarc0183e82009-06-26 18:32:06 +00005181 Fn = ObjCTypes.getMessageSendFixupFn();
5182 Name += "objc_msgSend_fixup";
Fariborz Jahanian5b2bad02009-04-30 16:31:11 +00005183 }
Mike Stumpb3589f42009-07-30 22:28:39 +00005184 } else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005185#if 0
5186// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005187 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005188 Fn = ObjCTypes.getMessageSendIdFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005189 Name += "objc_msgSendId_fixup";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005190 } else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005191#endif
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005192 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005193 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005194 Name += "objc_msgSendSuper2_fixup";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005195 } else {
5196 Fn = ObjCTypes.getMessageSendFixupFn();
5197 Name += "objc_msgSend_fixup";
5198 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005199 }
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005200 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005201 Name += '_';
5202 std::string SelName(Sel.getAsString());
5203 // Replace all ':' in selector name with '_' ouch!
Mike Stump1eb44332009-09-09 15:08:12 +00005204 for (unsigned i = 0; i < SelName.size(); i++)
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005205 if (SelName[i] == ':')
5206 SelName[i] = '_';
5207 Name += SelName;
5208 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5209 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005210 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005211 std::vector<llvm::Constant*> Values(2);
5212 Values[0] = Fn;
5213 Values[1] = GetMethodVarName(Sel);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005214 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005215 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005216 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005217 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005218 Name);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005219 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005220 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005221 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005222 }
5223 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005224
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005225 CallArgList ActualArgs;
5226 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005227 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005228 ObjCTypes.MessageRefCPtrTy));
5229 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
John McCall04a67a62010-02-05 21:31:56 +00005230 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00005231 FunctionType::ExtInfo());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005232 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5233 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00005234 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005235 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Anderson96e0fc72009-07-29 22:16:19 +00005236 llvm::PointerType::getUnqual(FTy));
John McCallef072fd2010-05-22 01:48:05 +00005237 return CGF.EmitCall(FnInfo1, Callee, Return, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005238}
5239
5240/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005241CodeGen::RValue
5242CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005243 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005244 QualType ResultType,
5245 Selector Sel,
5246 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005247 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00005248 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005249 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005250 return LegacyDispatchedSelector(Sel)
John McCallef072fd2010-05-22 01:48:05 +00005251 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5252 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005253 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005254 false, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005255 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005256 Receiver, CGF.getContext().getObjCIdType(),
5257 false, CallArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005258}
5259
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005260llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005261CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005262 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5263
Daniel Dunbardfff2302009-03-02 05:18:14 +00005264 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005265 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005266 false, llvm::GlobalValue::ExternalLinkage,
5267 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005268 }
5269
5270 return GV;
5271}
5272
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005273llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5274 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005275 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005276
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005277 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005278 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005279 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005280 Entry =
5281 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005282 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005283 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005284 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005285 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005286 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005287 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005288 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005289 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005290 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005291
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005292 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar11394522009-04-18 08:51:00 +00005293}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005294
Daniel Dunbar11394522009-04-18 08:51:00 +00005295llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005296CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005297 const ObjCInterfaceDecl *ID) {
5298 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005299
Daniel Dunbar11394522009-04-18 08:51:00 +00005300 if (!Entry) {
5301 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5302 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005303 Entry =
5304 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005305 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005306 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005307 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005308 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005309 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005310 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005311 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005312 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005313 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005314
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005315 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005316}
5317
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005318/// EmitMetaClassRef - Return a Value * of the address of _class_t
5319/// meta-data
5320///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005321llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5322 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005323 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5324 if (Entry)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005325 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005326
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005327 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005328 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005329 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005330 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005331 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005332 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005333 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005334 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005335 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005336 ObjCTypes.ClassnfABIPtrTy));
5337
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005338 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005339 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005340
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005341 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005342}
5343
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005344/// GetClass - Return a reference to the class for the given interface
5345/// decl.
5346llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5347 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00005348 if (ID->hasAttr<WeakImportAttr>()) {
5349 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5350 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5351 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5352 }
5353
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005354 return EmitClassRef(Builder, ID);
5355}
5356
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005357/// Generates a message send where the super is the receiver. This is
5358/// a message send to self with special delivery semantics indicating
5359/// which class's method should be called.
5360CodeGen::RValue
5361CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005362 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005363 QualType ResultType,
5364 Selector Sel,
5365 const ObjCInterfaceDecl *Class,
5366 bool isCategoryImpl,
5367 llvm::Value *Receiver,
5368 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005369 const CodeGen::CallArgList &CallArgs,
5370 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005371 // ...
5372 // Create and init a super structure; this is a (receiver, class)
5373 // pair we will pass to objc_msgSendSuper.
5374 llvm::Value *ObjCSuper =
5375 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005376
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005377 llvm::Value *ReceiverAsObject =
5378 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5379 CGF.Builder.CreateStore(ReceiverAsObject,
5380 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005381
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005382 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005383 llvm::Value *Target;
5384 if (IsClassMessage) {
5385 if (isCategoryImpl) {
5386 // Message sent to "super' in a class method defined in
5387 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005388 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005389 Target = CGF.Builder.CreateStructGEP(Target, 0);
5390 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00005391 } else
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005392 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stumpb3589f42009-07-30 22:28:39 +00005393 } else
Daniel Dunbar11394522009-04-18 08:51:00 +00005394 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005395
Mike Stumpf5408fe2009-05-16 07:57:57 +00005396 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5397 // ObjCTypes types.
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005398 const llvm::Type *ClassTy =
5399 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5400 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5401 CGF.Builder.CreateStore(Target,
5402 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005403
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005404 return (LegacyDispatchedSelector(Sel))
John McCallef072fd2010-05-22 01:48:05 +00005405 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5406 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005407 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005408 true, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005409 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005410 ObjCSuper, ObjCTypes.SuperPtrCTy,
5411 true, CallArgs);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005412}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005413
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005414llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005415 Selector Sel) {
5416 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005417
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005418 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005419 llvm::Constant *Casted =
5420 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5421 ObjCTypes.SelectorPtrTy);
5422 Entry =
5423 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5424 llvm::GlobalValue::InternalLinkage,
5425 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005426 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005427 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005428 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005429
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005430 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005431}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005432/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005433/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005434///
5435void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005436 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005437 llvm::Value *dst,
5438 llvm::Value *ivarOffset) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005439 const llvm::Type * SrcTy = src->getType();
5440 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005441 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005442 assert(Size <= 8 && "does not support size > 8");
5443 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5444 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005445 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5446 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005447 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5448 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005449 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5450 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005451 return;
5452}
5453
5454/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5455/// objc_assign_strongCast (id src, id *dst)
5456///
5457void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005458 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005459 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005460 const llvm::Type * SrcTy = src->getType();
5461 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005462 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005463 assert(Size <= 8 && "does not support size > 8");
5464 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005465 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005466 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5467 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005468 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5469 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005470 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005471 src, dst, "weakassign");
5472 return;
5473}
5474
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005475void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005476 CodeGen::CodeGenFunction &CGF,
5477 llvm::Value *DestPtr,
5478 llvm::Value *SrcPtr,
Fariborz Jahanian08c32132009-08-31 19:33:16 +00005479 QualType Ty) {
5480 // Get size info for this aggregate.
5481 std::pair<uint64_t, unsigned> TypeInfo = CGM.getContext().getTypeInfo(Ty);
Fariborz Jahanian736d8a62009-09-10 16:20:31 +00005482 unsigned long size = TypeInfo.first/8;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005483 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5484 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005485 llvm::Value *N = llvm::ConstantInt::get(ObjCTypes.LongTy, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005486 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
5487 DestPtr, SrcPtr, N);
5488 return;
5489}
5490
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005491/// EmitObjCWeakRead - Code gen for loading value of a __weak
5492/// object: objc_read_weak (id *src)
5493///
5494llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005495 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005496 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00005497 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005498 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5499 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005500 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005501 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005502 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005503 return read_weak;
5504}
5505
5506/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5507/// objc_assign_weak (id src, id *dst)
5508///
5509void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005510 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005511 const llvm::Type * SrcTy = src->getType();
5512 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005513 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005514 assert(Size <= 8 && "does not support size > 8");
5515 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5516 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005517 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5518 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005519 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5520 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005521 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005522 src, dst, "weakassign");
5523 return;
5524}
5525
5526/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5527/// objc_assign_global (id src, id *dst)
5528///
5529void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005530 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005531 const llvm::Type * SrcTy = src->getType();
5532 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005533 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005534 assert(Size <= 8 && "does not support size > 8");
5535 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5536 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005537 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5538 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005539 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5540 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005541 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005542 src, dst, "globalassign");
5543 return;
5544}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005545
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005546void
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005547CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5548 const Stmt &S) {
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005549 bool isTry = isa<ObjCAtTryStmt>(S);
5550 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
5551 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005552 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005553 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005554 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005555 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
5556
5557 // For @synchronized, call objc_sync_enter(sync.expr). The
5558 // evaluation of the expression must occur before we enter the
5559 // @synchronized. We can safely avoid a temp here because jumps into
5560 // @synchronized are illegal & this will dominate uses.
5561 llvm::Value *SyncArg = 0;
5562 if (!isTry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005563 SyncArg =
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005564 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
5565 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005566 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005567 }
5568
5569 // Push an EH context entry, used for handling rethrows and jumps
5570 // through finally.
5571 CGF.PushCleanupBlock(FinallyBlock);
5572
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005573 CGF.setInvokeDest(TryHandler);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005574
5575 CGF.EmitBlock(TryBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005576 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
5577 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005578 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005579
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005580 // Emit the exception handler.
5581
5582 CGF.EmitBlock(TryHandler);
5583
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005584 llvm::Value *llvm_eh_exception =
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005585 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
Duncan Sands790b20e2009-10-14 16:13:30 +00005586 llvm::Value *llvm_eh_selector =
5587 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
5588 llvm::Value *llvm_eh_typeid_for =
5589 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005590 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5591 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow");
5592
5593 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
5594 SelectorArgs.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005595 SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005596
5597 // Construct the lists of (type, catch body) to handle.
Douglas Gregorc00d8e12010-04-26 16:46:50 +00005598 llvm::SmallVector<std::pair<const VarDecl*, const Stmt*>, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005599 bool HasCatchAll = false;
5600 if (isTry) {
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00005601 const ObjCAtTryStmt &AtTry = cast<ObjCAtTryStmt>(S);
5602 for (unsigned I = 0, N = AtTry.getNumCatchStmts(); I != N; ++I) {
5603 const ObjCAtCatchStmt *CatchStmt = AtTry.getCatchStmt(I);
Douglas Gregorc00d8e12010-04-26 16:46:50 +00005604 const VarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00005605 Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody()));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005606
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00005607 // catch(...) always matches.
5608 if (!CatchDecl) {
5609 // Use i8* null here to signal this is a catch all, not a cleanup.
5610 llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
5611 SelectorArgs.push_back(Null);
5612 HasCatchAll = true;
5613 break;
5614 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005615
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00005616 if (CatchDecl->getType()->isObjCIdType() ||
5617 CatchDecl->getType()->isObjCQualifiedIdType()) {
5618 llvm::Value *IDEHType =
5619 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5620 if (!IDEHType)
5621 IDEHType =
5622 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
5623 false,
5624 llvm::GlobalValue::ExternalLinkage,
5625 0, "OBJC_EHTYPE_id");
5626 SelectorArgs.push_back(IDEHType);
5627 } else {
5628 // All other types should be Objective-C interface pointer types.
5629 const ObjCObjectPointerType *PT =
5630 CatchDecl->getType()->getAs<ObjCObjectPointerType>();
5631 assert(PT && "Invalid @catch type.");
5632 const ObjCInterfaceType *IT = PT->getInterfaceType();
5633 assert(IT && "Invalid @catch type.");
5634 llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false);
5635 SelectorArgs.push_back(EHType);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005636 }
5637 }
5638 }
5639
5640 // We use a cleanup unless there was already a catch all.
5641 if (!HasCatchAll) {
Daniel Dunbare3d25ab2009-08-03 21:20:07 +00005642 // Even though this is a cleanup, treat it as a catch all to avoid the C++
5643 // personality behavior of terminating the process if only cleanups are
5644 // found in the exception handling stack.
5645 SelectorArgs.push_back(llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy));
Daniel Dunbarede8de92009-03-06 00:01:21 +00005646 Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005647 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005648
5649 llvm::Value *Selector =
Duncan Sands790b20e2009-10-14 16:13:30 +00005650 CGF.Builder.CreateCall(llvm_eh_selector,
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005651 SelectorArgs.begin(), SelectorArgs.end(),
5652 "selector");
5653 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
Douglas Gregorc00d8e12010-04-26 16:46:50 +00005654 const VarDecl *CatchParam = Handlers[i].first;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005655 const Stmt *CatchBody = Handlers[i].second;
5656
5657 llvm::BasicBlock *Next = 0;
5658
5659 // The last handler always matches.
5660 if (i + 1 != e) {
5661 assert(CatchParam && "Only last handler can be a catch all.");
5662
5663 llvm::BasicBlock *Match = CGF.createBasicBlock("match");
5664 Next = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005665 llvm::Value *Id =
Duncan Sands790b20e2009-10-14 16:13:30 +00005666 CGF.Builder.CreateCall(llvm_eh_typeid_for,
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005667 CGF.Builder.CreateBitCast(SelectorArgs[i+2],
5668 ObjCTypes.Int8PtrTy));
5669 CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id),
5670 Match, Next);
5671
5672 CGF.EmitBlock(Match);
5673 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005674
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005675 if (CatchBody) {
5676 llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005677
5678 // Cleanups must call objc_end_catch.
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005679 CGF.PushCleanupBlock(MatchEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005680
5681 llvm::Value *ExcObject =
Chris Lattner8a569112009-04-22 02:15:23 +00005682 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005683
5684 // Bind the catch parameter if it exists.
5685 if (CatchParam) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005686 ExcObject =
5687 CGF.Builder.CreateBitCast(ExcObject,
Daniel Dunbarede8de92009-03-06 00:01:21 +00005688 CGF.ConvertType(CatchParam->getType()));
5689 // CatchParam is a ParmVarDecl because of the grammar
5690 // construction used to handle this, but for codegen purposes
5691 // we treat this as a local decl.
5692 CGF.EmitLocalBlockVarDecl(*CatchParam);
5693 CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005694 }
5695
Daniel Dunbarba6940a2010-04-23 19:12:24 +00005696 // Exceptions inside the catch block must be rethrown. We set a special
5697 // purpose invoke destination for this which just collects the thrown
5698 // exception and overwrites the object in RethrowPtr, branches through the
5699 // match.end to make sure we call objc_end_catch, before branching to the
5700 // rethrow handler.
5701 llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler");
5702 CGF.setInvokeDest(MatchHandler);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005703 CGF.ObjCEHValueStack.push_back(ExcObject);
5704 CGF.EmitStmt(CatchBody);
5705 CGF.ObjCEHValueStack.pop_back();
Daniel Dunbarba6940a2010-04-23 19:12:24 +00005706 CGF.setInvokeDest(0);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005707
5708 CGF.EmitBranchThroughCleanup(FinallyEnd);
5709
Daniel Dunbarba6940a2010-04-23 19:12:24 +00005710 // Don't emit the extra match handler if there we no unprotected calls in
5711 // the catch block.
5712 if (MatchHandler->use_empty()) {
5713 delete MatchHandler;
5714 } else {
5715 CGF.EmitBlock(MatchHandler);
5716 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5717 // We are required to emit this call to satisfy LLVM, even
5718 // though we don't use the result.
5719 CGF.Builder.CreateCall3(llvm_eh_selector,
5720 Exc, ObjCTypes.getEHPersonalityPtr(),
5721 llvm::ConstantInt::get(
5722 llvm::Type::getInt32Ty(VMContext), 0),
5723 "unused_eh_selector");
5724 CGF.Builder.CreateStore(Exc, RethrowPtr);
5725 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5726 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005727
5728 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005729
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005730 CGF.EmitBlock(MatchEnd);
5731
5732 // Unfortunately, we also have to generate another EH frame here
5733 // in case this throws.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005734 llvm::BasicBlock *MatchEndHandler =
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005735 CGF.createBasicBlock("match.end.handler");
5736 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005737 CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(),
Daniel Dunbarba6940a2010-04-23 19:12:24 +00005738 Cont, MatchEndHandler);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005739
5740 CGF.EmitBlock(Cont);
5741 if (Info.SwitchBlock)
5742 CGF.EmitBlock(Info.SwitchBlock);
5743 if (Info.EndBlock)
5744 CGF.EmitBlock(Info.EndBlock);
5745
5746 CGF.EmitBlock(MatchEndHandler);
Daniel Dunbarba6940a2010-04-23 19:12:24 +00005747 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005748 // We are required to emit this call to satisfy LLVM, even
5749 // though we don't use the result.
Daniel Dunbarba6940a2010-04-23 19:12:24 +00005750 CGF.Builder.CreateCall3(llvm_eh_selector,
5751 Exc, ObjCTypes.getEHPersonalityPtr(),
5752 llvm::ConstantInt::get(
5753 llvm::Type::getInt32Ty(VMContext), 0),
5754 "unused_eh_selector");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005755 CGF.Builder.CreateStore(Exc, RethrowPtr);
5756 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5757
5758 if (Next)
5759 CGF.EmitBlock(Next);
5760 } else {
5761 assert(!Next && "catchup should be last handler.");
5762
5763 CGF.Builder.CreateStore(Exc, RethrowPtr);
5764 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5765 }
5766 }
5767
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005768 // Pop the cleanup entry, the @finally is outside this cleanup
5769 // scope.
5770 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5771 CGF.setInvokeDest(PrevLandingPad);
5772
5773 CGF.EmitBlock(FinallyBlock);
5774
5775 if (isTry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005776 if (const ObjCAtFinallyStmt* FinallyStmt =
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005777 cast<ObjCAtTryStmt>(S).getFinallyStmt())
5778 CGF.EmitStmt(FinallyStmt->getFinallyBody());
5779 } else {
5780 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
5781 // @synchronized.
Chris Lattnerbbccd612009-04-22 02:38:11 +00005782 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005783 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005784
5785 if (Info.SwitchBlock)
5786 CGF.EmitBlock(Info.SwitchBlock);
5787 if (Info.EndBlock)
5788 CGF.EmitBlock(Info.EndBlock);
5789
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005790 // Branch around the rethrow code.
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005791 CGF.EmitBranch(FinallyEnd);
5792
Daniel Dunbarb76db232010-04-23 19:12:32 +00005793 // Generate the rethrow code, taking care to use an invoke if we are in a
5794 // nested exception scope.
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005795 CGF.EmitBlock(FinallyRethrow);
Daniel Dunbarb76db232010-04-23 19:12:32 +00005796 if (PrevLandingPad) {
5797 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
5798 CGF.Builder.CreateInvoke(ObjCTypes.getUnwindResumeOrRethrowFn(),
5799 Cont, PrevLandingPad,
5800 CGF.Builder.CreateLoad(RethrowPtr));
5801 CGF.EmitBlock(Cont);
5802 } else {
5803 CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(),
5804 CGF.Builder.CreateLoad(RethrowPtr));
5805 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005806 CGF.Builder.CreateUnreachable();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005807
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005808 CGF.EmitBlock(FinallyEnd);
5809}
5810
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005811/// EmitThrowStmt - Generate code for a throw statement.
5812void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
5813 const ObjCAtThrowStmt &S) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005814 llvm::Value *Exception;
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00005815 llvm::Constant *FunctionThrowOrRethrow;
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005816 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005817 Exception = CGF.EmitScalarExpr(ThrowExpr);
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00005818 FunctionThrowOrRethrow = ObjCTypes.getExceptionThrowFn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005819 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005820 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005821 "Unexpected rethrow outside @catch block.");
5822 Exception = CGF.ObjCEHValueStack.back();
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00005823 FunctionThrowOrRethrow = ObjCTypes.getExceptionRethrowFn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005824 }
5825
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005826 llvm::Value *ExceptionAsObject =
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005827 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
5828 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
5829 if (InvokeDest) {
5830 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00005831 CGF.Builder.CreateInvoke(FunctionThrowOrRethrow,
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005832 Cont, InvokeDest,
5833 &ExceptionAsObject, &ExceptionAsObject + 1);
5834 CGF.EmitBlock(Cont);
5835 } else
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00005836 CGF.Builder.CreateCall(FunctionThrowOrRethrow, ExceptionAsObject);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005837 CGF.Builder.CreateUnreachable();
5838
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005839 // Clear the insertion point to indicate we are in unreachable code.
5840 CGF.Builder.ClearInsertionPoint();
5841}
Daniel Dunbare588b992009-03-01 04:46:24 +00005842
5843llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005844CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005845 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00005846 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00005847
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005848 // If we don't need a definition, return the entry if found or check
5849 // if we use an external reference.
5850 if (!ForDefinition) {
5851 if (Entry)
5852 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00005853
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005854 // If this type (or a super class) has the __objc_exception__
5855 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00005856 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005857 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005858 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005859 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005860 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005861 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00005862 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005863 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005864
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005865 // Otherwise we need to either make a new entry or fill in the
5866 // initializer.
5867 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005868 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00005869 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005870 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00005871 CGM.getModule().getGlobalVariable(VTableName);
5872 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005873 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
5874 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00005875 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00005876 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00005877
Owen Anderson0032b272009-08-13 21:57:51 +00005878 llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00005879
5880 std::vector<llvm::Constant*> Values(3);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005881 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbare588b992009-03-01 04:46:24 +00005882 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005883 Values[2] = GetClassGlobal(ClassName);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005884 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00005885 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00005886
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005887 if (Entry) {
5888 Entry->setInitializer(Init);
5889 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00005890 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005891 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005892 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005893 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00005894 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005895 }
5896
Daniel Dunbar04d40782009-04-14 06:00:08 +00005897 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005898 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005899 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
5900 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005901
5902 if (ForDefinition) {
5903 Entry->setSection("__DATA,__objc_const");
5904 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
5905 } else {
5906 Entry->setSection("__DATA,__datacoal_nt,coalesced");
5907 }
Daniel Dunbare588b992009-03-01 04:46:24 +00005908
5909 return Entry;
5910}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005911
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005912/* *** */
5913
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005914CodeGen::CGObjCRuntime *
5915CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00005916 return new CGObjCMac(CGM);
5917}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005918
5919CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005920CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005921 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005922}