blob: a33e0941cb50ef5e71ee67f9d70c90704b0975c5 [file] [log] [blame]
Daniel Dunbar303e2c22008-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 Dunbar3ad53482008-08-11 21:35:06 +000015
Daniel Dunbar034299e2010-03-31 01:09:11 +000016#include "CGRecordLayout.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000017#include "CodeGenModule.h"
Daniel Dunbara94ecd22008-08-16 03:19:19 +000018#include "CodeGenFunction.h"
John McCallad7c5c12011-02-08 08:22:06 +000019#include "CGBlocks.h"
John McCalled1ae862011-01-28 11:13:47 +000020#include "CGCleanup.h"
Daniel Dunbar8b8683f2008-08-12 00:12:39 +000021#include "clang/AST/ASTContext.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000022#include "clang/AST/Decl.h"
Daniel Dunbarb036db82008-08-13 03:21:16 +000023#include "clang/AST/DeclObjC.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000025#include "clang/AST/StmtObjC.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000026#include "clang/Basic/LangOptions.h"
Chandler Carruth85098242010-06-15 23:19:56 +000027#include "clang/Frontend/CodeGenOptions.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000028
John McCall42227ed2010-07-31 23:20:56 +000029#include "llvm/InlineAsm.h"
30#include "llvm/IntrinsicInst.h"
Owen Andersonae86c192009-07-13 04:10:07 +000031#include "llvm/LLVMContext.h"
Daniel Dunbar8b8683f2008-08-12 00:12:39 +000032#include "llvm/Module.h"
Daniel Dunbarc475d422008-10-29 22:36:39 +000033#include "llvm/ADT/DenseSet.h"
Daniel Dunbard027a922009-09-07 00:20:42 +000034#include "llvm/ADT/SetVector.h"
35#include "llvm/ADT/SmallString.h"
Fariborz Jahanian751c1e72009-12-12 21:26:21 +000036#include "llvm/ADT/SmallPtrSet.h"
John McCall5c08ab92010-07-13 22:12:14 +000037#include "llvm/Support/CallSite.h"
Daniel Dunbard027a922009-09-07 00:20:42 +000038#include "llvm/Support/raw_ostream.h"
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +000039#include "llvm/Target/TargetData.h"
Torok Edwindb714922009-08-24 13:25:12 +000040#include <cstdio>
Daniel Dunbar303e2c22008-08-11 02:45:11 +000041
42using namespace clang;
Daniel Dunbar41cf9de2008-09-09 01:06:48 +000043using namespace CodeGen;
Daniel Dunbar303e2c22008-08-11 02:45:11 +000044
Daniel Dunbar9fd114d2009-04-22 07:32:20 +000045// Common CGObjCRuntime functions, these don't belong here, but they
46// don't belong in CGObjCRuntime either so we will live with it for
47// now.
48
John McCallc5799442011-02-26 09:48:59 +000049static void EmitNullReturnInitialization(CodeGenFunction &CGF,
50 ReturnValueSlot &returnSlot,
51 QualType resultType) {
52 // Force the return slot to exist.
53 if (!returnSlot.getValue())
54 returnSlot = ReturnValueSlot(CGF.CreateMemTemp(resultType), false);
55 CGF.EmitNullInitialization(returnSlot.getValue(), resultType);
56}
57
Daniel Dunbar0cec95f2009-05-03 08:55:17 +000058static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM,
59 const ObjCInterfaceDecl *OID,
Daniel Dunbar961202372009-05-03 12:57:56 +000060 const ObjCImplementationDecl *ID,
Daniel Dunbar0cec95f2009-05-03 08:55:17 +000061 const ObjCIvarDecl *Ivar) {
Daniel Dunbar8c7f9812010-04-02 21:14:02 +000062 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar80b4eef2009-05-03 13:15:50 +000063
Daniel Dunbar7e5aba42010-04-02 22:29:40 +000064 // FIXME: We should eliminate the need to have ObjCImplementationDecl passed
65 // in here; it should never be necessary because that should be the lexical
66 // decl context for the ivar.
Daniel Dunbar031d4d42010-04-02 15:43:29 +000067
Daniel Dunbar80b4eef2009-05-03 13:15:50 +000068 // If we know have an implementation (and the ivar is in it) then
69 // look up in the implementation layout.
Daniel Dunbar59e476b2009-08-03 17:06:42 +000070 const ASTRecordLayout *RL;
Daniel Dunbar80b4eef2009-05-03 13:15:50 +000071 if (ID && ID->getClassInterface() == Container)
72 RL = &CGM.getContext().getASTObjCImplementationLayout(ID);
73 else
74 RL = &CGM.getContext().getASTObjCInterfaceLayout(Container);
Daniel Dunbar8c7f9812010-04-02 21:14:02 +000075
76 // Compute field index.
77 //
78 // FIXME: The index here is closely tied to how ASTContext::getObjCLayout is
79 // implemented. This should be fixed to get the information from the layout
80 // directly.
81 unsigned Index = 0;
82 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
83 CGM.getContext().ShallowCollectObjCIvars(Container, Ivars);
84 for (unsigned k = 0, e = Ivars.size(); k != e; ++k) {
85 if (Ivar == Ivars[k])
86 break;
87 ++Index;
88 }
89 assert(Index != Ivars.size() && "Ivar is not inside container!");
David Chisnalle8431a72010-11-03 16:12:44 +000090 assert(Index < RL->getFieldCount() && "Ivar is not inside record layout!");
Daniel Dunbar8c7f9812010-04-02 21:14:02 +000091
Daniel Dunbar80b4eef2009-05-03 13:15:50 +000092 return RL->getFieldOffset(Index);
Daniel Dunbar0cec95f2009-05-03 08:55:17 +000093}
94
95uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
96 const ObjCInterfaceDecl *OID,
97 const ObjCIvarDecl *Ivar) {
Daniel Dunbar961202372009-05-03 12:57:56 +000098 return LookupFieldBitOffset(CGM, OID, 0, Ivar) / 8;
99}
100
101uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
102 const ObjCImplementationDecl *OID,
103 const ObjCIvarDecl *Ivar) {
104 return LookupFieldBitOffset(CGM, OID->getClassInterface(), OID, Ivar) / 8;
Daniel Dunbar9fd114d2009-04-22 07:32:20 +0000105}
106
107LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
108 const ObjCInterfaceDecl *OID,
109 llvm::Value *BaseValue,
110 const ObjCIvarDecl *Ivar,
111 unsigned CVRQualifiers,
112 llvm::Value *Offset) {
Daniel Dunbar0cec95f2009-05-03 08:55:17 +0000113 // Compute (type*) ( (char *) BaseValue + Offset)
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000114 const llvm::Type *I8Ptr = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Daniel Dunbar0cec95f2009-05-03 08:55:17 +0000115 QualType IvarTy = Ivar->getType();
116 const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy);
Daniel Dunbar9fd114d2009-04-22 07:32:20 +0000117 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr);
John McCall74186ab2011-03-04 08:25:59 +0000118 V = CGF.Builder.CreateInBoundsGEP(V, Offset, "add.ptr");
Owen Anderson9793f0e2009-07-29 22:16:19 +0000119 V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000120
Daniel Dunbarf166a522010-08-21 03:44:13 +0000121 if (!Ivar->isBitField()) {
122 LValue LV = CGF.MakeAddrLValue(V, IvarTy);
123 LV.getQuals().addCVRQualifiers(CVRQualifiers);
124 return LV;
125 }
Daniel Dunbar961202372009-05-03 12:57:56 +0000126
Daniel Dunbara70fab82010-09-02 23:53:31 +0000127 // We need to compute an access strategy for this bit-field. We are given the
128 // offset to the first byte in the bit-field, the sub-byte offset is taken
129 // from the original layout. We reuse the normal bit-field access strategy by
130 // treating this as an access to a struct where the bit-field is in byte 0,
131 // and adjust the containing type size as appropriate.
132 //
133 // FIXME: Note that currently we make a very conservative estimate of the
134 // alignment of the bit-field, because (a) it is not clear what guarantees the
135 // runtime makes us, and (b) we don't have a way to specify that the struct is
136 // at an alignment plus offset.
137 //
138 // Note, there is a subtle invariant here: we can only call this routine on
139 // non-synthesized ivars but we may be called for synthesized ivars. However,
140 // a synthesized ivar can never be a bit-field, so this is safe.
141 const ASTRecordLayout &RL =
142 CGF.CGM.getContext().getASTObjCInterfaceLayout(OID);
Ken Dyckb0fcc592011-02-11 01:54:29 +0000143 uint64_t TypeSizeInBits = CGF.CGM.getContext().toBits(RL.getSize());
Daniel Dunbara70fab82010-09-02 23:53:31 +0000144 uint64_t FieldBitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar);
145 uint64_t BitOffset = FieldBitOffset % 8;
146 uint64_t ContainingTypeAlign = 8;
147 uint64_t ContainingTypeSize = TypeSizeInBits - (FieldBitOffset - BitOffset);
Daniel Dunbarb76c4cd2010-04-05 16:20:33 +0000148 uint64_t BitFieldSize =
149 Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +0000150
Daniel Dunbardc406b82010-04-05 21:36:35 +0000151 // Allocate a new CGBitFieldInfo object to describe this access.
152 //
153 // FIXME: This is incredibly wasteful, these should be uniqued or part of some
154 // layout object. However, this is blocked on other cleanups to the
155 // Objective-C code, so for now we just live with allocating a bunch of these
156 // objects.
Daniel Dunbara70fab82010-09-02 23:53:31 +0000157 CGBitFieldInfo *Info = new (CGF.CGM.getContext()) CGBitFieldInfo(
158 CGBitFieldInfo::MakeInfo(CGF.CGM.getTypes(), Ivar, BitOffset, BitFieldSize,
159 ContainingTypeSize, ContainingTypeAlign));
Daniel Dunbardc406b82010-04-05 21:36:35 +0000160
Daniel Dunbar26d2c392010-08-21 04:05:54 +0000161 return LValue::MakeBitfield(V, *Info,
162 IvarTy.getCVRQualifiers() | CVRQualifiers);
Daniel Dunbar9fd114d2009-04-22 07:32:20 +0000163}
164
165///
166
Daniel Dunbar303e2c22008-08-11 02:45:11 +0000167namespace {
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000168
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000169typedef std::vector<llvm::Constant*> ConstantVector;
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000170
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000171// FIXME: We should find a nicer way to make the labels for metadata, string
172// concatenation is lame.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000173
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000174class ObjCCommonTypesHelper {
Owen Anderson170229f2009-07-14 23:10:40 +0000175protected:
176 llvm::LLVMContext &VMContext;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000177
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000178private:
179 llvm::Constant *getMessageSendFn() const {
180 // id objc_msgSend (id, SEL, ...)
181 std::vector<const llvm::Type*> Params;
182 Params.push_back(ObjectPtrTy);
183 Params.push_back(SelectorPtrTy);
184 return
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000185 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000186 Params, true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000187 "objc_msgSend");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000188 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000189
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000190 llvm::Constant *getMessageSendStretFn() const {
191 // id objc_msgSend_stret (id, SEL, ...)
192 std::vector<const llvm::Type*> Params;
193 Params.push_back(ObjectPtrTy);
194 Params.push_back(SelectorPtrTy);
195 return
Owen Anderson41a75022009-08-13 21:57:51 +0000196 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000197 Params, true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000198 "objc_msgSend_stret");
199
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000200 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000201
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000202 llvm::Constant *getMessageSendFpretFn() const {
203 // FIXME: This should be long double on x86_64?
204 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
205 std::vector<const llvm::Type*> Params;
206 Params.push_back(ObjectPtrTy);
207 Params.push_back(SelectorPtrTy);
208 return
Owen Anderson41a75022009-08-13 21:57:51 +0000209 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
210 llvm::Type::getDoubleTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000211 Params,
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000212 true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000213 "objc_msgSend_fpret");
214
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000215 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000216
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000217 llvm::Constant *getMessageSendSuperFn() const {
218 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
219 const char *SuperName = "objc_msgSendSuper";
220 std::vector<const llvm::Type*> Params;
221 Params.push_back(SuperPtrTy);
222 Params.push_back(SelectorPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000223 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000224 Params, true),
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000225 SuperName);
226 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000227
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000228 llvm::Constant *getMessageSendSuperFn2() const {
229 // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
230 const char *SuperName = "objc_msgSendSuper2";
231 std::vector<const llvm::Type*> Params;
232 Params.push_back(SuperPtrTy);
233 Params.push_back(SelectorPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000234 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000235 Params, true),
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000236 SuperName);
237 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000238
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000239 llvm::Constant *getMessageSendSuperStretFn() const {
240 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
241 // SEL op, ...)
242 std::vector<const llvm::Type*> Params;
243 Params.push_back(Int8PtrTy);
244 Params.push_back(SuperPtrTy);
245 Params.push_back(SelectorPtrTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000246 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000247 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000248 Params, true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000249 "objc_msgSendSuper_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000250 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000251
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000252 llvm::Constant *getMessageSendSuperStretFn2() const {
253 // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
254 // SEL op, ...)
255 std::vector<const llvm::Type*> Params;
256 Params.push_back(Int8PtrTy);
257 Params.push_back(SuperPtrTy);
258 Params.push_back(SelectorPtrTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000259 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000260 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000261 Params, true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000262 "objc_msgSendSuper2_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000263 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000264
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000265 llvm::Constant *getMessageSendSuperFpretFn() const {
266 // There is no objc_msgSendSuper_fpret? How can that work?
267 return getMessageSendSuperFn();
268 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000269
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000270 llvm::Constant *getMessageSendSuperFpretFn2() const {
271 // There is no objc_msgSendSuper_fpret? How can that work?
272 return getMessageSendSuperFn2();
273 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000274
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000275protected:
276 CodeGen::CodeGenModule &CGM;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000277
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000278public:
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +0000279 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000280 const llvm::Type *Int8PtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000281
Daniel Dunbar5d715592008-08-12 05:28:47 +0000282 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
283 const llvm::Type *ObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000284
Fariborz Jahanian406b1172008-11-18 20:18:11 +0000285 /// PtrObjectPtrTy - LLVM type for id *
286 const llvm::Type *PtrObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000287
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000288 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar5d715592008-08-12 05:28:47 +0000289 const llvm::Type *SelectorPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000290 /// ProtocolPtrTy - LLVM type for external protocol handles
291 /// (typeof(Protocol))
292 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000293
Daniel Dunbarc722b852008-08-30 03:02:31 +0000294 // SuperCTy - clang type for struct objc_super.
295 QualType SuperCTy;
296 // SuperPtrCTy - clang type for struct objc_super *.
297 QualType SuperPtrCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000298
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000299 /// SuperTy - LLVM type for struct objc_super.
300 const llvm::StructType *SuperTy;
Daniel Dunbar97ff50d2008-08-23 09:25:55 +0000301 /// SuperPtrTy - LLVM type for struct objc_super *.
302 const llvm::Type *SuperPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000303
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000304 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
305 /// in GCC parlance).
306 const llvm::StructType *PropertyTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000307
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000308 /// PropertyListTy - LLVM type for struct objc_property_list
309 /// (_prop_list_t in GCC parlance).
310 const llvm::StructType *PropertyListTy;
311 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
312 const llvm::Type *PropertyListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000313
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000314 // MethodTy - LLVM type for struct objc_method.
315 const llvm::StructType *MethodTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000316
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000317 /// CacheTy - LLVM type for struct objc_cache.
318 const llvm::Type *CacheTy;
319 /// CachePtrTy - LLVM type for struct objc_cache *.
320 const llvm::Type *CachePtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000321
Chris Lattnerce8754e2009-04-22 02:44:54 +0000322 llvm::Constant *getGetPropertyFn() {
323 CodeGen::CodeGenTypes &Types = CGM.getTypes();
324 ASTContext &Ctx = CGM.getContext();
325 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
John McCall2da83a32010-02-26 00:48:12 +0000326 llvm::SmallVector<CanQualType,4> Params;
327 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
328 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-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(Ctx.BoolTy);
333 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000334 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000335 FunctionType::ExtInfo()),
336 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000337 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
338 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000339
Chris Lattnerce8754e2009-04-22 02:44:54 +0000340 llvm::Constant *getSetPropertyFn() {
341 CodeGen::CodeGenTypes &Types = CGM.getTypes();
342 ASTContext &Ctx = CGM.getContext();
343 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
John McCall2da83a32010-02-26 00:48:12 +0000344 llvm::SmallVector<CanQualType,6> Params;
345 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
346 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000347 Params.push_back(IdType);
348 Params.push_back(SelType);
349 Params.push_back(Ctx.LongTy);
350 Params.push_back(IdType);
351 Params.push_back(Ctx.BoolTy);
352 Params.push_back(Ctx.BoolTy);
353 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000354 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000355 FunctionType::ExtInfo()),
356 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000357 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
358 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000359
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000360
361 llvm::Constant *getCopyStructFn() {
362 CodeGen::CodeGenTypes &Types = CGM.getTypes();
363 ASTContext &Ctx = CGM.getContext();
364 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
365 llvm::SmallVector<CanQualType,5> Params;
366 Params.push_back(Ctx.VoidPtrTy);
367 Params.push_back(Ctx.VoidPtrTy);
368 Params.push_back(Ctx.LongTy);
369 Params.push_back(Ctx.BoolTy);
370 Params.push_back(Ctx.BoolTy);
371 const llvm::FunctionType *FTy =
372 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
373 FunctionType::ExtInfo()),
374 false);
375 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
376 }
377
Chris Lattnerce8754e2009-04-22 02:44:54 +0000378 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000379 CodeGen::CodeGenTypes &Types = CGM.getTypes();
380 ASTContext &Ctx = CGM.getContext();
Chris Lattnerce8754e2009-04-22 02:44:54 +0000381 // void objc_enumerationMutation (id)
John McCall2da83a32010-02-26 00:48:12 +0000382 llvm::SmallVector<CanQualType,1> Params;
383 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000384 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000385 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000386 FunctionType::ExtInfo()),
387 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000388 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
389 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000390
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000391 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattnerce8754e2009-04-22 02:44:54 +0000392 llvm::Constant *getGcReadWeakFn() {
393 // id objc_read_weak (id *)
394 std::vector<const llvm::Type*> Args;
395 Args.push_back(ObjectPtrTy->getPointerTo());
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000396 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000397 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000398 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000399 }
400
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000401 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000402 llvm::Constant *getGcAssignWeakFn() {
403 // id objc_assign_weak (id, id *)
404 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
405 Args.push_back(ObjectPtrTy->getPointerTo());
406 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000407 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000408 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
409 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000410
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000411 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000412 llvm::Constant *getGcAssignGlobalFn() {
413 // id objc_assign_global(id, id *)
414 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
415 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Anderson170229f2009-07-14 23:10:40 +0000416 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000417 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000418 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
419 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000420
Fariborz Jahanian217af242010-07-20 20:30:03 +0000421 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
422 llvm::Constant *getGcAssignThreadLocalFn() {
423 // id objc_assign_threadlocal(id src, id * dest)
424 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
425 Args.push_back(ObjectPtrTy->getPointerTo());
426 llvm::FunctionType *FTy =
427 llvm::FunctionType::get(ObjectPtrTy, Args, false);
428 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
429 }
430
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000431 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000432 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000433 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattner0a696a422009-04-22 02:38:11 +0000434 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
435 Args.push_back(ObjectPtrTy->getPointerTo());
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000436 Args.push_back(LongTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000437 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000438 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000439 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
440 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000441
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000442 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
443 llvm::Constant *GcMemmoveCollectableFn() {
444 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
445 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
446 Args.push_back(Int8PtrTy);
447 Args.push_back(LongTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000448 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000449 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
450 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000451
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000452 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000453 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian217af242010-07-20 20:30:03 +0000454 // id objc_assign_strongCast(id, id *)
Chris Lattner0a696a422009-04-22 02:38:11 +0000455 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
456 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Anderson170229f2009-07-14 23:10:40 +0000457 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000458 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000459 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
460 }
Anders Carlsson9ab53d12009-02-16 22:59:18 +0000461
462 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000463 llvm::Constant *getExceptionThrowFn() {
464 // void objc_exception_throw(id)
465 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
466 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +0000467 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000468 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
469 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000470
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000471 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
472 llvm::Constant *getExceptionRethrowFn() {
473 // void objc_exception_rethrow(void)
474 std::vector<const llvm::Type*> Args;
475 llvm::FunctionType *FTy =
John McCall17afe452010-10-16 08:21:07 +0000476 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000477 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
478 }
479
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000480 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerdcceee72009-04-06 16:53:45 +0000481 llvm::Constant *getSyncEnterFn() {
482 // void objc_sync_enter (id)
483 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
484 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +0000485 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerdcceee72009-04-06 16:53:45 +0000486 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
487 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000488
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000489 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000490 llvm::Constant *getSyncExitFn() {
491 // void objc_sync_exit (id)
492 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
493 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +0000494 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000495 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
496 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000497
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000498 llvm::Constant *getSendFn(bool IsSuper) const {
499 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
500 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000501
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000502 llvm::Constant *getSendFn2(bool IsSuper) const {
503 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
504 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000505
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000506 llvm::Constant *getSendStretFn(bool IsSuper) const {
507 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
508 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000509
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000510 llvm::Constant *getSendStretFn2(bool IsSuper) const {
511 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
512 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000513
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000514 llvm::Constant *getSendFpretFn(bool IsSuper) const {
515 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
516 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000517
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000518 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
519 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
520 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000521
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000522 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
523 ~ObjCCommonTypesHelper(){}
524};
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000525
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000526/// ObjCTypesHelper - Helper class that encapsulates lazy
527/// construction of varies types used during ObjC generation.
528class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000529public:
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000530 /// SymtabTy - LLVM type for struct objc_symtab.
531 const llvm::StructType *SymtabTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000532 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
533 const llvm::Type *SymtabPtrTy;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000534 /// ModuleTy - LLVM type for struct objc_module.
535 const llvm::StructType *ModuleTy;
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000536
Daniel Dunbarb036db82008-08-13 03:21:16 +0000537 /// ProtocolTy - LLVM type for struct objc_protocol.
538 const llvm::StructType *ProtocolTy;
539 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
540 const llvm::Type *ProtocolPtrTy;
541 /// ProtocolExtensionTy - LLVM type for struct
542 /// objc_protocol_extension.
543 const llvm::StructType *ProtocolExtensionTy;
544 /// ProtocolExtensionTy - LLVM type for struct
545 /// objc_protocol_extension *.
546 const llvm::Type *ProtocolExtensionPtrTy;
547 /// MethodDescriptionTy - LLVM type for struct
548 /// objc_method_description.
549 const llvm::StructType *MethodDescriptionTy;
550 /// MethodDescriptionListTy - LLVM type for struct
551 /// objc_method_description_list.
552 const llvm::StructType *MethodDescriptionListTy;
553 /// MethodDescriptionListPtrTy - LLVM type for struct
554 /// objc_method_description_list *.
555 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000556 /// ProtocolListTy - LLVM type for struct objc_property_list.
557 const llvm::Type *ProtocolListTy;
558 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
559 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar938a77f2008-08-22 20:34:54 +0000560 /// CategoryTy - LLVM type for struct objc_category.
561 const llvm::StructType *CategoryTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000562 /// ClassTy - LLVM type for struct objc_class.
563 const llvm::StructType *ClassTy;
564 /// ClassPtrTy - LLVM type for struct objc_class *.
565 const llvm::Type *ClassPtrTy;
566 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
567 const llvm::StructType *ClassExtensionTy;
568 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
569 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000570 // IvarTy - LLVM type for struct objc_ivar.
571 const llvm::StructType *IvarTy;
572 /// IvarListTy - LLVM type for struct objc_ivar_list.
573 const llvm::Type *IvarListTy;
574 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
575 const llvm::Type *IvarListPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000576 /// MethodListTy - LLVM type for struct objc_method_list.
577 const llvm::Type *MethodListTy;
578 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
579 const llvm::Type *MethodListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000580
Anders Carlsson9ff22482008-09-09 10:10:21 +0000581 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
582 const llvm::Type *ExceptionDataTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000583
Anders Carlsson9ff22482008-09-09 10:10:21 +0000584 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000585 llvm::Constant *getExceptionTryEnterFn() {
586 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000587 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Anderson170229f2009-07-14 23:10:40 +0000588 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000589 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000590 Params, false),
591 "objc_exception_try_enter");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000592 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000593
594 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000595 llvm::Constant *getExceptionTryExitFn() {
596 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000597 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Anderson170229f2009-07-14 23:10:40 +0000598 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000599 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000600 Params, false),
601 "objc_exception_try_exit");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000602 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000603
604 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000605 llvm::Constant *getExceptionExtractFn() {
606 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000607 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
608 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattnerc6406db2009-04-22 02:26:14 +0000609 Params, false),
610 "objc_exception_extract");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000611
Chris Lattnerc6406db2009-04-22 02:26:14 +0000612 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000613
Anders Carlsson9ff22482008-09-09 10:10:21 +0000614 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000615 llvm::Constant *getExceptionMatchFn() {
616 std::vector<const llvm::Type*> Params;
617 Params.push_back(ClassPtrTy);
618 Params.push_back(ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000619 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000620 llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000621 Params, false),
622 "objc_exception_match");
623
Chris Lattnerc6406db2009-04-22 02:26:14 +0000624 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000625
Anders Carlsson9ff22482008-09-09 10:10:21 +0000626 /// SetJmpFn - LLVM _setjmp function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000627 llvm::Constant *getSetJmpFn() {
628 std::vector<const llvm::Type*> Params;
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000629 Params.push_back(llvm::Type::getInt32PtrTy(VMContext));
Chris Lattnerc6406db2009-04-22 02:26:14 +0000630 return
Owen Anderson41a75022009-08-13 21:57:51 +0000631 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattnerc6406db2009-04-22 02:26:14 +0000632 Params, false),
633 "_setjmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000634
Chris Lattnerc6406db2009-04-22 02:26:14 +0000635 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000636
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000637public:
638 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000639 ~ObjCTypesHelper() {}
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000640};
641
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000642/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000643/// modern abi
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000644class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000645public:
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000646
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000647 // MethodListnfABITy - LLVM for struct _method_list_t
648 const llvm::StructType *MethodListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000649
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000650 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
651 const llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000652
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000653 // ProtocolnfABITy = LLVM for struct _protocol_t
654 const llvm::StructType *ProtocolnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000655
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000656 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
657 const llvm::Type *ProtocolnfABIPtrTy;
658
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000659 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
660 const llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000661
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000662 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
663 const llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000664
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000665 // ClassnfABITy - LLVM for struct _class_t
666 const llvm::StructType *ClassnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000667
Fariborz Jahanian71394042009-01-23 23:53:38 +0000668 // ClassnfABIPtrTy - LLVM for struct _class_t*
669 const llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000670
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000671 // IvarnfABITy - LLVM for struct _ivar_t
672 const llvm::StructType *IvarnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000673
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000674 // IvarListnfABITy - LLVM for struct _ivar_list_t
675 const llvm::StructType *IvarListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000676
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000677 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
678 const llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000679
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000680 // ClassRonfABITy - LLVM for struct _class_ro_t
681 const llvm::StructType *ClassRonfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000682
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000683 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
684 const llvm::Type *ImpnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000685
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000686 // CategorynfABITy - LLVM for struct _category_t
687 const llvm::StructType *CategorynfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000688
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000689 // New types for nonfragile abi messaging.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000690
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000691 // MessageRefTy - LLVM for:
692 // struct _message_ref_t {
693 // IMP messenger;
694 // SEL name;
695 // };
696 const llvm::StructType *MessageRefTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000697 // MessageRefCTy - clang type for struct _message_ref_t
698 QualType MessageRefCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000699
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000700 // MessageRefPtrTy - LLVM for struct _message_ref_t*
701 const llvm::Type *MessageRefPtrTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000702 // MessageRefCPtrTy - clang type for struct _message_ref_t*
703 QualType MessageRefCPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000704
Fariborz Jahanian4e87c832009-02-05 01:13:09 +0000705 // MessengerTy - Type of the messenger (shown as IMP above)
706 const llvm::FunctionType *MessengerTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000707
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000708 // SuperMessageRefTy - LLVM for:
709 // struct _super_message_ref_t {
710 // SUPER_IMP messenger;
711 // SEL name;
712 // };
713 const llvm::StructType *SuperMessageRefTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000714
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000715 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
716 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000717
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000718 llvm::Constant *getMessageSendFixupFn() {
719 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
720 std::vector<const llvm::Type*> Params;
721 Params.push_back(ObjectPtrTy);
722 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000723 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000724 Params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000725 "objc_msgSend_fixup");
726 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000727
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000728 llvm::Constant *getMessageSendFpretFixupFn() {
729 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
730 std::vector<const llvm::Type*> Params;
731 Params.push_back(ObjectPtrTy);
732 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000733 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000734 Params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000735 "objc_msgSend_fpret_fixup");
736 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000737
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000738 llvm::Constant *getMessageSendStretFixupFn() {
739 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
740 std::vector<const llvm::Type*> Params;
741 Params.push_back(ObjectPtrTy);
742 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000743 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000744 Params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000745 "objc_msgSend_stret_fixup");
746 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000747
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000748 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000749 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000750 // struct _super_message_ref_t*, ...)
751 std::vector<const llvm::Type*> Params;
752 Params.push_back(SuperPtrTy);
753 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000754 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000755 Params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000756 "objc_msgSendSuper2_fixup");
757 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000758
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000759 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000760 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000761 // struct _super_message_ref_t*, ...)
762 std::vector<const llvm::Type*> Params;
763 Params.push_back(SuperPtrTy);
764 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000765 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000766 Params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000767 "objc_msgSendSuper2_stret_fixup");
768 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000769
Chris Lattnera7c00b42009-04-22 02:15:23 +0000770 llvm::Constant *getObjCEndCatchFn() {
Owen Anderson41a75022009-08-13 21:57:51 +0000771 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Chris Lattner3dd1b4b2009-07-01 04:13:52 +0000772 false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000773 "objc_end_catch");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000774
Chris Lattnera7c00b42009-04-22 02:15:23 +0000775 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000776
Chris Lattnera7c00b42009-04-22 02:15:23 +0000777 llvm::Constant *getObjCBeginCatchFn() {
778 std::vector<const llvm::Type*> Params;
779 Params.push_back(Int8PtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000780 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
Chris Lattnera7c00b42009-04-22 02:15:23 +0000781 Params, false),
782 "objc_begin_catch");
783 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +0000784
785 const llvm::StructType *EHTypeTy;
786 const llvm::Type *EHTypePtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000787
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000788 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
789 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000790};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000791
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000792class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000793public:
794 // FIXME - accessibility
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000795 class GC_IVAR {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +0000796 public:
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000797 unsigned ivar_bytepos;
798 unsigned ivar_size;
799 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000800 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar22b0ada2009-04-23 01:29:05 +0000801
802 // Allow sorting based on byte pos.
803 bool operator<(const GC_IVAR &b) const {
804 return ivar_bytepos < b.ivar_bytepos;
805 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000806 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000807
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000808 class SKIP_SCAN {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000809 public:
810 unsigned skip;
811 unsigned scan;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000812 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000813 : skip(_skip), scan(_scan) {}
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000814 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000815
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000816protected:
817 CodeGen::CodeGenModule &CGM;
Owen Andersonae86c192009-07-13 04:10:07 +0000818 llvm::LLVMContext &VMContext;
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000819 // FIXME! May not be needing this after all.
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000820 unsigned ObjCABI;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000821
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000822 // gc ivar layout bitmap calculation helper caches.
823 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
824 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000825
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000826 /// LazySymbols - Symbols to generate a lazy reference for. See
827 /// DefinedSymbols and FinishModule().
Daniel Dunbard027a922009-09-07 00:20:42 +0000828 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000829
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000830 /// DefinedSymbols - External symbols which are defined by this
831 /// module. The symbols in this list and LazySymbols are used to add
832 /// special linker symbols which ensure that Objective-C modules are
833 /// linked properly.
Daniel Dunbard027a922009-09-07 00:20:42 +0000834 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000835
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000836 /// ClassNames - uniqued class names.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000837 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000838
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000839 /// MethodVarNames - uniqued method variable names.
840 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000841
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +0000842 /// DefinedCategoryNames - list of category names in form Class_Category.
843 llvm::SetVector<std::string> DefinedCategoryNames;
844
Daniel Dunbarb036db82008-08-13 03:21:16 +0000845 /// MethodVarTypes - uniqued method type signatures. We have to use
846 /// a StringMap here because have no other unique reference.
847 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000848
Daniel Dunbar3c76cb52008-08-26 21:51:14 +0000849 /// MethodDefinitions - map of methods which have been defined in
850 /// this translation unit.
851 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000852
Daniel Dunbar80a840b2008-08-23 00:19:03 +0000853 /// PropertyNames - uniqued method variable names.
854 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000855
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000856 /// ClassReferences - uniqued class references.
857 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000858
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000859 /// SelectorReferences - uniqued selector references.
860 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000861
Daniel Dunbarb036db82008-08-13 03:21:16 +0000862 /// Protocols - Protocols for which an objc_protocol structure has
863 /// been emitted. Forward declarations are handled by creating an
864 /// empty structure whose initializer is filled in when/if defined.
865 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000866
Daniel Dunbarc475d422008-10-29 22:36:39 +0000867 /// DefinedProtocols - Protocols which have actually been
868 /// defined. We should not need this, see FIXME in GenerateProtocol.
869 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000870
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000871 /// DefinedClasses - List of defined classes.
872 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000873
874 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
875 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000876
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000877 /// DefinedCategories - List of defined categories.
878 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000879
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000880 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
881 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000882
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000883 /// GetNameForMethod - Return a name for the given method.
884 /// \param[out] NameOut - The return value.
885 void GetNameForMethod(const ObjCMethodDecl *OMD,
886 const ObjCContainerDecl *CD,
Daniel Dunbard2386812009-10-19 01:21:19 +0000887 llvm::SmallVectorImpl<char> &NameOut);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000888
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000889 /// GetMethodVarName - Return a unique constant for the given
890 /// selector's name. The return value has type char *.
891 llvm::Constant *GetMethodVarName(Selector Sel);
892 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000893
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000894 /// GetMethodVarType - Return a unique constant for the given
895 /// selector's name. The return value has type char *.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000896
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000897 // FIXME: This is a horrible name.
898 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000899 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000900
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000901 /// GetPropertyName - Return a unique constant for the given
902 /// name. The return value has type char *.
903 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000904
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000905 // FIXME: This can be dropped once string functions are unified.
906 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
907 const Decl *Container);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000908
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +0000909 /// GetClassName - Return a unique constant for the given selector's
910 /// name. The return value has type char *.
911 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000912
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +0000913 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
914
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000915 /// BuildIvarLayout - Builds ivar layout bitmap for the class
916 /// implementation for the __strong or __weak case.
917 ///
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000918 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
919 bool ForStrongLayout);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +0000920
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +0000921 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000922
Daniel Dunbar15bd8882009-05-03 14:10:34 +0000923 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000924 unsigned int BytePos, bool ForStrongLayout,
925 bool &HasUnion);
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +0000926 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000927 const llvm::StructLayout *Layout,
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000928 const RecordDecl *RD,
Chris Lattner5b36ddb2009-03-31 08:48:01 +0000929 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000930 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahaniana123b642009-04-24 16:17:09 +0000931 bool &HasUnion);
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000932
Fariborz Jahanian01dff422009-03-05 19:17:31 +0000933 /// GetIvarLayoutName - Returns a unique constant for the given
934 /// ivar layout bitmap.
935 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
936 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000937
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000938 /// EmitPropertyList - Emit the given property list. The return
939 /// value has type PropertyListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000940 llvm::Constant *EmitPropertyList(llvm::Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000941 const Decl *Container,
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000942 const ObjCContainerDecl *OCD,
943 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000944
Fariborz Jahanian751c1e72009-12-12 21:26:21 +0000945 /// PushProtocolProperties - Push protocol's property on the input stack.
946 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
947 std::vector<llvm::Constant*> &Properties,
948 const Decl *Container,
949 const ObjCProtocolDecl *PROTO,
950 const ObjCCommonTypesHelper &ObjCTypes);
951
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000952 /// GetProtocolRef - Return a reference to the internal protocol
953 /// description, creating an empty one if it has not been
954 /// defined. The return value has type ProtocolPtrTy.
955 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanian67726212009-03-08 20:18:37 +0000956
Daniel Dunbar30c65362009-03-09 20:09:19 +0000957 /// CreateMetadataVar - Create a global variable with internal
958 /// linkage for use by the Objective-C runtime.
959 ///
960 /// This is a convenience wrapper which not only creates the
961 /// variable, but also sets the section and alignment and adds the
Chris Lattnerf56501c2009-07-17 23:57:13 +0000962 /// global to the "llvm.used" list.
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000963 ///
964 /// \param Name - The variable name.
965 /// \param Init - The variable initializer; this is also used to
966 /// define the type of the variable.
967 /// \param Section - The section the variable should go into, or 0.
968 /// \param Align - The alignment for the variable, or 0.
969 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbar4527d302009-04-14 17:42:51 +0000970 /// "llvm.used".
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000971 llvm::GlobalVariable *CreateMetadataVar(llvm::Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +0000972 llvm::Constant *Init,
973 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000974 unsigned Align,
975 bool AddToUsed);
Daniel Dunbar30c65362009-03-09 20:09:19 +0000976
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000977 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +0000978 ReturnValueSlot Return,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000979 QualType ResultType,
980 llvm::Value *Sel,
981 llvm::Value *Arg0,
982 QualType Arg0Ty,
983 bool IsSuper,
984 const CallArgList &CallArgs,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +0000985 const ObjCMethodDecl *OMD,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000986 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000987
Daniel Dunbar5e639272010-04-25 20:39:01 +0000988 /// EmitImageInfo - Emit the image info marker used to encode some module
989 /// level information.
990 void EmitImageInfo();
991
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000992public:
Owen Andersonae86c192009-07-13 04:10:07 +0000993 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump11289f42009-09-09 15:08:12 +0000994 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000995
David Chisnall481e3a82010-01-23 02:40:42 +0000996 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000997
Fariborz Jahanian99113fd2009-01-26 21:38:32 +0000998 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
999 const ObjCContainerDecl *CD=0);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001000
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001001 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001002
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001003 /// GetOrEmitProtocol - Get the protocol object for the given
1004 /// declaration, emitting it if necessary. The return value has type
1005 /// ProtocolPtrTy.
1006 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001007
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001008 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1009 /// object for the given declaration, emitting it if needed. These
1010 /// forward references will be filled in with empty bodies if no
1011 /// definition is seen. The return value has type ProtocolPtrTy.
1012 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall351762c2011-02-07 10:33:21 +00001013 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
1014 const CGBlockInfo &blockInfo);
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001015
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001016};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001017
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001018class CGObjCMac : public CGObjCCommonMac {
1019private:
1020 ObjCTypesHelper ObjCTypes;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00001021
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001022 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001023 /// information.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001024 void EmitModuleInfo();
1025
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001026 /// EmitModuleSymols - Emit module symbols, the list of defined
1027 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001028 llvm::Constant *EmitModuleSymbols();
1029
Daniel Dunbar3ad53482008-08-11 21:35:06 +00001030 /// FinishModule - Write out global data structures at the end of
1031 /// processing a translation unit.
1032 void FinishModule();
Daniel Dunbarb036db82008-08-13 03:21:16 +00001033
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001034 /// EmitClassExtension - Generate the class extension structure used
1035 /// to store the weak ivar layout and properties. The return value
1036 /// has type ClassExtensionPtrTy.
1037 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
1038
1039 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1040 /// for the given class.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001041 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001042 const ObjCInterfaceDecl *ID);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001043
1044 /// EmitSuperClassRef - Emits reference to class's main metadata class.
1045 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001046
1047 /// EmitIvarList - Emit the ivar list for the given
1048 /// implementation. If ForClass is true the list of class ivars
1049 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1050 /// interface ivars will be emitted. The return value has type
1051 /// IvarListPtrTy.
1052 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00001053 bool ForClass);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001054
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001055 /// EmitMetaClass - Emit a forward reference to the class structure
1056 /// for the metaclass of the given interface. The return value has
1057 /// type ClassPtrTy.
1058 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1059
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001060 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001061 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001062 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1063 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001064 const ConstantVector &Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001065
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001066 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001067
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001068 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001069
1070 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar89654ee2008-08-26 08:29:31 +00001071 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001072 llvm::Constant *EmitMethodList(llvm::Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00001073 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001074 const ConstantVector &Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001075
1076 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001077 /// method declarations.
Daniel Dunbarb036db82008-08-13 03:21:16 +00001078 /// - TypeName: The name for the type containing the methods.
1079 /// - IsProtocol: True iff these methods are for a protocol.
1080 /// - ClassMethds: True iff these are class methods.
1081 /// - Required: When true, only "required" methods are
1082 /// listed. Similarly, when false only "optional" methods are
1083 /// listed. For classes this should always be true.
1084 /// - begin, end: The method list to output.
1085 ///
1086 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001087 llvm::Constant *EmitMethodDescList(llvm::Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001088 const char *Section,
1089 const ConstantVector &Methods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001090
Daniel Dunbarc475d422008-10-29 22:36:39 +00001091 /// GetOrEmitProtocol - Get the protocol object for the given
1092 /// declaration, emitting it if necessary. The return value has type
1093 /// ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001094 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001095
1096 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1097 /// object for the given declaration, emitting it if needed. These
1098 /// forward references will be filled in with empty bodies if no
1099 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001100 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001101
Daniel Dunbarb036db82008-08-13 03:21:16 +00001102 /// EmitProtocolExtension - Generate the protocol extension
1103 /// structure used to store optional instance and class methods, and
1104 /// protocol properties. The return value has type
1105 /// ProtocolExtensionPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001106 llvm::Constant *
1107 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1108 const ConstantVector &OptInstanceMethods,
1109 const ConstantVector &OptClassMethods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001110
1111 /// EmitProtocolList - Generate the list of referenced
1112 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001113 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001114 ObjCProtocolDecl::protocol_iterator begin,
1115 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001116
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001117 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1118 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001119 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1120 bool lval=false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001121
1122public:
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001123 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001124
Fariborz Jahanian71394042009-01-23 23:53:38 +00001125 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001126
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001127 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001128 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001129 QualType ResultType,
1130 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001131 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001132 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001133 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001134 const ObjCMethodDecl *Method);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001135
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001136 virtual CodeGen::RValue
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001137 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001138 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001139 QualType ResultType,
1140 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001141 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001142 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001143 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001144 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001145 const CallArgList &CallArgs,
1146 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001147
Daniel Dunbarcb463852008-11-01 01:53:16 +00001148 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001149 const ObjCInterfaceDecl *ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001150
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001151 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1152 bool lval = false);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001153
1154 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1155 /// untyped one.
1156 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1157 const ObjCMethodDecl *Method);
1158
John McCall2ca705e2010-07-24 00:37:23 +00001159 virtual llvm::Constant *GetEHType(QualType T);
1160
Daniel Dunbar92992502008-08-15 22:20:32 +00001161 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001162
Daniel Dunbar92992502008-08-15 22:20:32 +00001163 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001164
Daniel Dunbarcb463852008-11-01 01:53:16 +00001165 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001166 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001167
Chris Lattnerd4808922009-03-22 21:03:39 +00001168 virtual llvm::Constant *GetPropertyGetFunction();
1169 virtual llvm::Constant *GetPropertySetFunction();
David Chisnall168b80f2010-12-26 22:13:16 +00001170 virtual llvm::Constant *GetGetStructFunction();
1171 virtual llvm::Constant *GetSetStructFunction();
Chris Lattnerd4808922009-03-22 21:03:39 +00001172 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001173
John McCallbd309292010-07-06 01:34:17 +00001174 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1175 const ObjCAtTryStmt &S);
1176 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1177 const ObjCAtSynchronizedStmt &S);
1178 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00001179 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1180 const ObjCAtThrowStmt &S);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001181 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001182 llvm::Value *AddrWeakObj);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001183 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001184 llvm::Value *src, llvm::Value *dst);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001185 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001186 llvm::Value *src, llvm::Value *dest,
1187 bool threadlocal = false);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00001188 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001189 llvm::Value *src, llvm::Value *dest,
1190 llvm::Value *ivarOffset);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001191 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1192 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001193 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1194 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001195 llvm::Value *size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001196
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001197 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1198 QualType ObjectTy,
1199 llvm::Value *BaseValue,
1200 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001201 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001202 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001203 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001204 const ObjCIvarDecl *Ivar);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001205};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001206
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001207class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001208private:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001209 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanian71394042009-01-23 23:53:38 +00001210 llvm::GlobalVariable* ObjCEmptyCacheVar;
1211 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001212
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001213 /// SuperClassReferences - uniqued super class references.
1214 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001215
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001216 /// MetaClassReferences - uniqued meta class references.
1217 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001218
1219 /// EHTypeReferences - uniqued class ehtype references.
1220 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001221
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001222 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001223 /// legacy messaging dispatch.
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001224 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001225
Fariborz Jahanian67260552009-11-17 21:37:35 +00001226 /// DefinedMetaClasses - List of defined meta-classes.
1227 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1228
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001229 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001230 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001231 bool LegacyDispatchedSelector(Selector Sel);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001232
Fariborz Jahanian71394042009-01-23 23:53:38 +00001233 /// FinishNonFragileABIModule - Write out global data structures at the end of
1234 /// processing a translation unit.
1235 void FinishNonFragileABIModule();
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001236
Daniel Dunbar19573e72009-05-15 21:48:48 +00001237 /// AddModuleClassList - Add the given list of class pointers to the
1238 /// module with the provided symbol and section names.
1239 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1240 const char *SymbolName,
1241 const char *SectionName);
1242
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001243 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1244 unsigned InstanceStart,
1245 unsigned InstanceSize,
1246 const ObjCImplementationDecl *ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001247 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001248 llvm::Constant *IsAGV,
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001249 llvm::Constant *SuperClassGV,
Fariborz Jahanian82208252009-01-31 00:59:10 +00001250 llvm::Constant *ClassRoGV,
1251 bool HiddenVisibility);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001252
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001253 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001254
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00001255 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001256
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001257 /// EmitMethodList - Emit the method list for the given
1258 /// implementation. The return value has type MethodListnfABITy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001259 llvm::Constant *EmitMethodList(llvm::Twine Name,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001260 const char *Section,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00001261 const ConstantVector &Methods);
1262 /// EmitIvarList - Emit the ivar list for the given
1263 /// implementation. If ForClass is true the list of class ivars
1264 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1265 /// interface ivars will be emitted. The return value has type
1266 /// IvarListnfABIPtrTy.
1267 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001268
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001269 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00001270 const ObjCIvarDecl *Ivar,
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00001271 unsigned long int offset);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001272
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001273 /// GetOrEmitProtocol - Get the protocol object for the given
1274 /// declaration, emitting it if necessary. The return value has type
1275 /// ProtocolPtrTy.
1276 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001277
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001278 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1279 /// object for the given declaration, emitting it if needed. These
1280 /// forward references will be filled in with empty bodies if no
1281 /// definition is seen. The return value has type ProtocolPtrTy.
1282 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001283
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001284 /// EmitProtocolList - Generate the list of referenced
1285 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001286 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001287 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001288 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001289
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001290 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001291 ReturnValueSlot Return,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001292 QualType ResultType,
1293 Selector Sel,
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00001294 llvm::Value *Receiver,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001295 QualType Arg0Ty,
1296 bool IsSuper,
Fariborz Jahaniancf7f66f2011-03-01 17:28:13 +00001297 const CallArgList &CallArgs,
1298 const ObjCMethodDecl *Method);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00001299
1300 /// GetClassGlobal - Return the global variable for the Objective-C
1301 /// class of the given name.
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00001302 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001303
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001304 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001305 /// for the given class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001306 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001307 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001308
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001309 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1310 /// for the given super class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001311 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1312 const ObjCInterfaceDecl *ID);
1313
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001314 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1315 /// meta-data
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001316 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001317 const ObjCInterfaceDecl *ID);
1318
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001319 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1320 /// the given ivar.
1321 ///
Daniel Dunbara1060522009-04-19 00:31:15 +00001322 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001323 const ObjCInterfaceDecl *ID,
1324 const ObjCIvarDecl *Ivar);
1325
Fariborz Jahanian74b77222009-02-11 20:51:17 +00001326 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1327 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001328 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1329 bool lval=false);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001330
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001331 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001332 /// interface. The return value has type EHTypePtrTy.
John McCall2ca705e2010-07-24 00:37:23 +00001333 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001334 bool ForDefinition);
Daniel Dunbar15894b72009-04-07 05:48:37 +00001335
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001336 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar15894b72009-04-07 05:48:37 +00001337 return "OBJC_METACLASS_$_";
1338 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001339
Daniel Dunbar15894b72009-04-07 05:48:37 +00001340 const char *getClassSymbolPrefix() const {
1341 return "OBJC_CLASS_$_";
1342 }
1343
Daniel Dunbar961202372009-05-03 12:57:56 +00001344 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00001345 uint32_t &InstanceStart,
1346 uint32_t &InstanceSize);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001347
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001348 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001349 Selector GetNullarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001350 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1351 return CGM.getContext().Selectors.getSelector(0, &II);
1352 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001353
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001354 Selector GetUnarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001355 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1356 return CGM.getContext().Selectors.getSelector(1, &II);
1357 }
Daniel Dunbar554fd792009-04-19 23:41:48 +00001358
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001359 /// ImplementationIsNonLazy - Check whether the given category or
1360 /// class implementation is "non-lazy".
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00001361 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001362
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001363public:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001364 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001365 // FIXME. All stubs for now!
1366 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001367
Fariborz Jahanian71394042009-01-23 23:53:38 +00001368 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001369 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001370 QualType ResultType,
1371 Selector Sel,
1372 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001373 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001374 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001375 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001376
1377 virtual CodeGen::RValue
Fariborz Jahanian71394042009-01-23 23:53:38 +00001378 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001379 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001380 QualType ResultType,
1381 Selector Sel,
1382 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001383 bool isCategoryImpl,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001384 llvm::Value *Receiver,
1385 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001386 const CallArgList &CallArgs,
1387 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001388
Fariborz Jahanian71394042009-01-23 23:53:38 +00001389 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001390 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001391
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001392 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1393 bool lvalue = false)
1394 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001395
1396 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1397 /// untyped one.
1398 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1399 const ObjCMethodDecl *Method)
1400 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001401
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00001402 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001403
Fariborz Jahanian71394042009-01-23 23:53:38 +00001404 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001405 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian097feda2009-01-30 18:58:59 +00001406 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001407
John McCall2ca705e2010-07-24 00:37:23 +00001408 virtual llvm::Constant *GetEHType(QualType T);
1409
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001410 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001411 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001412 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001413 virtual llvm::Constant *GetPropertySetFunction() {
1414 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001415 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001416
David Chisnall168b80f2010-12-26 22:13:16 +00001417 virtual llvm::Constant *GetSetStructFunction() {
1418 return ObjCTypes.getCopyStructFn();
1419 }
1420 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001421 return ObjCTypes.getCopyStructFn();
1422 }
1423
Chris Lattnerd4808922009-03-22 21:03:39 +00001424 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001425 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbard73ea8162009-02-16 18:48:45 +00001426 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001427
John McCallbd309292010-07-06 01:34:17 +00001428 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1429 const ObjCAtTryStmt &S);
1430 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1431 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001432 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlsson9ab53d12009-02-16 22:59:18 +00001433 const ObjCAtThrowStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001434 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001435 llvm::Value *AddrWeakObj);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001436 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001437 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001438 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001439 llvm::Value *src, llvm::Value *dest,
1440 bool threadlocal = false);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001441 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001442 llvm::Value *src, llvm::Value *dest,
1443 llvm::Value *ivarOffset);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001444 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001445 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001446 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1447 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001448 llvm::Value *size);
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001449 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1450 QualType ObjectTy,
1451 llvm::Value *BaseValue,
1452 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001453 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001454 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001455 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001456 const ObjCIvarDecl *Ivar);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001457};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001458
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001459} // end anonymous namespace
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001460
1461/* *** Helper Functions *** */
1462
1463/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Anderson170229f2009-07-14 23:10:40 +00001464static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001465 llvm::Constant *C,
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001466 unsigned idx0,
1467 unsigned idx1) {
1468 llvm::Value *Idxs[] = {
Owen Anderson41a75022009-08-13 21:57:51 +00001469 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1470 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001471 };
Owen Andersonade90fd2009-07-29 18:54:39 +00001472 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001473}
1474
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001475/// hasObjCExceptionAttribute - Return true if this class or any super
1476/// class has the __objc_exception__ attribute.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001477static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001478 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001479 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001480 return true;
1481 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001482 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001483 return false;
1484}
1485
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001486/* *** CGObjCMac Public Interface *** */
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001487
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001488CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00001489 ObjCTypes(cgm) {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001490 ObjCABI = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001491 EmitImageInfo();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001492}
1493
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +00001494/// GetClass - Return a reference to the class for the given interface
1495/// decl.
Daniel Dunbarcb463852008-11-01 01:53:16 +00001496llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001497 const ObjCInterfaceDecl *ID) {
1498 return EmitClassRef(Builder, ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001499}
1500
1501/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001502llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1503 bool lval) {
1504 return EmitSelector(Builder, Sel, lval);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001505}
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001506llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001507 *Method) {
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001508 return EmitSelector(Builder, Method->getSelector());
1509}
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001510
John McCall2ca705e2010-07-24 00:37:23 +00001511llvm::Constant *CGObjCMac::GetEHType(QualType T) {
1512 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1513 return 0;
1514}
1515
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001516/// Generate a constant CFString object.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001517/*
1518 struct __builtin_CFString {
1519 const int *isa; // point to __CFConstantStringClassReference
1520 int flags;
1521 const char *str;
1522 long length;
1523 };
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001524*/
1525
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001526/// or Generate a constant NSString object.
1527/*
1528 struct __builtin_NSString {
1529 const int *isa; // point to __NSConstantStringClassReference
1530 const char *str;
1531 unsigned int length;
1532 };
1533*/
1534
Fariborz Jahanian71394042009-01-23 23:53:38 +00001535llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall481e3a82010-01-23 02:40:42 +00001536 const StringLiteral *SL) {
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001537 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1538 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian50c925f2010-10-19 17:19:29 +00001539 CGM.GetAddrOfConstantString(SL));
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001540}
1541
1542/// Generates a message send where the super is the receiver. This is
1543/// a message send to self with special delivery semantics indicating
1544/// which class's method should be called.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001545CodeGen::RValue
1546CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001547 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001548 QualType ResultType,
1549 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001550 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001551 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001552 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001553 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001554 const CodeGen::CallArgList &CallArgs,
1555 const ObjCMethodDecl *Method) {
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001556 // Create and init a super structure; this is a (receiver, class)
1557 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001558 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00001559 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001560 llvm::Value *ReceiverAsObject =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001561 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001562 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001563 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001564
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001565 // If this is a class message the metaclass is passed as the target.
1566 llvm::Value *Target;
1567 if (IsClassMessage) {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001568 if (isCategoryImpl) {
1569 // Message sent to 'super' in a class method defined in a category
1570 // implementation requires an odd treatment.
1571 // If we are in a class method, we must retrieve the
1572 // _metaclass_ for the current class, pointed at by
1573 // the class's "isa" pointer. The following assumes that
1574 // isa" is the first ivar in a class (which it must be).
1575 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1576 Target = CGF.Builder.CreateStructGEP(Target, 0);
1577 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00001578 } else {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001579 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1580 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1581 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1582 Target = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001583 }
Fariborz Jahanianda2efb02009-11-14 02:18:31 +00001584 }
1585 else if (isCategoryImpl)
1586 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1587 else {
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001588 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1589 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1590 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001591 }
Mike Stump18bb9282009-05-16 07:57:57 +00001592 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1593 // ObjCTypes types.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001594 const llvm::Type *ClassTy =
Daniel Dunbarc722b852008-08-30 03:02:31 +00001595 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbarc475d422008-10-29 22:36:39 +00001596 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001597 CGF.Builder.CreateStore(Target,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001598 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall78a15112010-05-22 01:48:05 +00001599 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001600 EmitSelector(CGF.Builder, Sel),
1601 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001602 true, CallArgs, Method, ObjCTypes);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001603}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001604
1605/// Generate code for a message send expression.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001606CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001607 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001608 QualType ResultType,
1609 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001610 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001611 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001612 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001613 const ObjCMethodDecl *Method) {
John McCall78a15112010-05-22 01:48:05 +00001614 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001615 EmitSelector(CGF.Builder, Sel),
1616 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001617 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001618}
1619
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001620CodeGen::RValue
1621CGObjCCommonMac::EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001622 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001623 QualType ResultType,
1624 llvm::Value *Sel,
1625 llvm::Value *Arg0,
1626 QualType Arg0Ty,
1627 bool IsSuper,
1628 const CallArgList &CallArgs,
1629 const ObjCMethodDecl *Method,
1630 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc722b852008-08-30 03:02:31 +00001631 CallArgList ActualArgs;
Fariborz Jahanian969bc682009-04-24 21:07:43 +00001632 if (!IsSuper)
1633 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar41cf9de2008-09-09 01:06:48 +00001634 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001635 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbarc722b852008-08-30 03:02:31 +00001636 CGF.getContext().getObjCSelType()));
1637 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001638
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00001639 CodeGenTypes &Types = CGM.getTypes();
John McCallab26cfa2010-02-05 21:31:56 +00001640 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001641 FunctionType::ExtInfo());
Daniel Dunbardf0e62d2009-09-17 04:01:40 +00001642 const llvm::FunctionType *FTy =
1643 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001644
Anders Carlsson280e61f12010-06-21 20:59:55 +00001645 if (Method)
1646 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1647 CGM.getContext().getCanonicalType(ResultType) &&
1648 "Result type mismatch!");
1649
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001650 llvm::Constant *Fn = NULL;
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001651 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
John McCallc5799442011-02-26 09:48:59 +00001652 EmitNullReturnInitialization(CGF, Return, ResultType);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001653 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001654 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001655 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1656 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1657 : ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001658 } else {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001659 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001660 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001661 }
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001662 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCall78a15112010-05-22 01:48:05 +00001663 return CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001664}
1665
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001666static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1667 if (FQT.isObjCGCStrong())
1668 return Qualifiers::Strong;
1669
1670 if (FQT.isObjCGCWeak())
1671 return Qualifiers::Weak;
1672
1673 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1674 return Qualifiers::Strong;
1675
1676 if (const PointerType *PT = FQT->getAs<PointerType>())
1677 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1678
1679 return Qualifiers::GCNone;
1680}
1681
John McCall351762c2011-02-07 10:33:21 +00001682llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1683 const CGBlockInfo &blockInfo) {
1684 llvm::Constant *nullPtr =
Fariborz Jahanianafa3c0a2010-08-04 18:44:59 +00001685 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
John McCall351762c2011-02-07 10:33:21 +00001686
Fariborz Jahanian0aa35b92010-09-13 16:09:44 +00001687 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
John McCall351762c2011-02-07 10:33:21 +00001688 return nullPtr;
1689
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001690 bool hasUnion = false;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001691 SkipIvars.clear();
1692 IvarsInfo.clear();
1693 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
1694 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
1695
Fariborz Jahaniancfddabf2010-09-09 00:21:45 +00001696 // __isa is the first field in block descriptor and must assume by runtime's
1697 // convention that it is GC'able.
1698 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall351762c2011-02-07 10:33:21 +00001699
1700 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1701
1702 // Calculate the basic layout of the block structure.
1703 const llvm::StructLayout *layout =
1704 CGM.getTargetData().getStructLayout(blockInfo.StructureType);
1705
1706 // Ignore the optional 'this' capture: C++ objects are not assumed
1707 // to be GC'ed.
1708
1709 // Walk the captured variables.
1710 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1711 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1712 const VarDecl *variable = ci->getVariable();
1713 QualType type = variable->getType();
1714
1715 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1716
1717 // Ignore constant captures.
1718 if (capture.isConstant()) continue;
1719
1720 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1721
1722 // __block variables are passed by their descriptor address.
1723 if (ci->isByRef()) {
1724 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanian933c6722010-09-11 01:27:29 +00001725 continue;
John McCall351762c2011-02-07 10:33:21 +00001726 }
1727
1728 assert(!type->isArrayType() && "array variable should not be caught");
1729 if (const RecordType *record = type->getAs<RecordType>()) {
1730 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahanian903aba32010-08-05 21:00:25 +00001731 continue;
1732 }
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001733
John McCall351762c2011-02-07 10:33:21 +00001734 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1735 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1736
1737 if (GCAttr == Qualifiers::Strong)
1738 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1739 fieldSize / WordSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001740 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall351762c2011-02-07 10:33:21 +00001741 SkipIvars.push_back(GC_IVAR(fieldOffset,
1742 fieldSize / ByteSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001743 }
1744
1745 if (IvarsInfo.empty())
John McCall351762c2011-02-07 10:33:21 +00001746 return nullPtr;
1747
1748 // Sort on byte position; captures might not be allocated in order,
1749 // and unions can do funny things.
1750 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1751 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001752
1753 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00001754 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001755 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1756 printf("\n block variable layout for block: ");
1757 const unsigned char *s = (unsigned char*)BitMap.c_str();
1758 for (unsigned i = 0; i < BitMap.size(); i++)
1759 if (!(s[i] & 0xf0))
1760 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1761 else
1762 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1763 printf("\n");
1764 }
1765
1766 return C;
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001767}
1768
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001769llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001770 const ObjCProtocolDecl *PD) {
Daniel Dunbar7050c552008-09-04 04:33:15 +00001771 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001772 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar7050c552008-09-04 04:33:15 +00001773 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1774
Owen Andersonade90fd2009-07-29 18:54:39 +00001775 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001776 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001777}
1778
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001779void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stump18bb9282009-05-16 07:57:57 +00001780 // FIXME: We shouldn't need this, the protocol decl should contain enough
1781 // information to tell us whether this was a declaration or a definition.
Daniel Dunbarc475d422008-10-29 22:36:39 +00001782 DefinedProtocols.insert(PD->getIdentifier());
1783
1784 // If we have generated a forward reference to this protocol, emit
1785 // it now. Otherwise do nothing, the protocol objects are lazily
1786 // emitted.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001787 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbarc475d422008-10-29 22:36:39 +00001788 GetOrEmitProtocol(PD);
1789}
1790
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001791llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001792 if (DefinedProtocols.count(PD->getIdentifier()))
1793 return GetOrEmitProtocol(PD);
1794 return GetOrEmitProtocolRef(PD);
1795}
1796
Daniel Dunbarb036db82008-08-13 03:21:16 +00001797/*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001798// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1799struct _objc_protocol {
1800struct _objc_protocol_extension *isa;
1801char *protocol_name;
1802struct _objc_protocol_list *protocol_list;
1803struct _objc__method_prototype_list *instance_methods;
1804struct _objc__method_prototype_list *class_methods
1805};
Daniel Dunbarb036db82008-08-13 03:21:16 +00001806
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001807See EmitProtocolExtension().
Daniel Dunbarb036db82008-08-13 03:21:16 +00001808*/
Daniel Dunbarc475d422008-10-29 22:36:39 +00001809llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1810 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1811
1812 // Early exit if a defining object has already been generated.
1813 if (Entry && Entry->hasInitializer())
1814 return Entry;
1815
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001816 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001817 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001818 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1819
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001820 // Construct method lists.
1821 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1822 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001823 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001824 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001825 ObjCMethodDecl *MD = *i;
1826 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1827 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1828 OptInstanceMethods.push_back(C);
1829 } else {
1830 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001831 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001832 }
1833
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001834 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001835 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001836 ObjCMethodDecl *MD = *i;
1837 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1838 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1839 OptClassMethods.push_back(C);
1840 } else {
1841 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001842 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001843 }
1844
Daniel Dunbarb036db82008-08-13 03:21:16 +00001845 std::vector<llvm::Constant*> Values(5);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001846 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001847 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001848 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001849 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00001850 PD->protocol_begin(),
1851 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001852 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001853 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001854 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1855 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001856 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001857 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001858 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1859 ClassMethods);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001860 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001861 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001862
Daniel Dunbarb036db82008-08-13 03:21:16 +00001863 if (Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001864 // Already created, fix the linkage and update the initializer.
1865 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001866 Entry->setInitializer(Init);
1867 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001868 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001869 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001870 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001871 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001872 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001873 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001874 // FIXME: Is this necessary? Why only for protocol?
1875 Entry->setAlignment(4);
1876 }
Chris Lattnerf56501c2009-07-17 23:57:13 +00001877 CGM.AddUsedGlobal(Entry);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001878
1879 return Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001880}
1881
Daniel Dunbarc475d422008-10-29 22:36:39 +00001882llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001883 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1884
1885 if (!Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001886 // We use the initializer as a marker of whether this is a forward
1887 // reference or not. At module finalization we add the empty
1888 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001889 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001890 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarc475d422008-10-29 22:36:39 +00001891 llvm::GlobalValue::ExternalLinkage,
1892 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001893 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001894 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001895 // FIXME: Is this necessary? Why only for protocol?
1896 Entry->setAlignment(4);
1897 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001898
Daniel Dunbarb036db82008-08-13 03:21:16 +00001899 return Entry;
1900}
1901
1902/*
1903 struct _objc_protocol_extension {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001904 uint32_t size;
1905 struct objc_method_description_list *optional_instance_methods;
1906 struct objc_method_description_list *optional_class_methods;
1907 struct objc_property_list *instance_properties;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001908 };
1909*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001910llvm::Constant *
1911CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1912 const ConstantVector &OptInstanceMethods,
1913 const ConstantVector &OptClassMethods) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001914 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00001915 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001916 std::vector<llvm::Constant*> Values(4);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001917 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001918 Values[1] =
1919 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001920 + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001921 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1922 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001923 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001924 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001925 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1926 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001927 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00001928 0, PD, ObjCTypes);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001929
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001930 // Return null if no extension bits are used.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001931 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbarb036db82008-08-13 03:21:16 +00001932 Values[3]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00001933 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001934
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001935 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00001936 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001937
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001938 // No special section, but goes in llvm.used
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001939 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001940 Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001941 0, 0, true);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001942}
1943
1944/*
1945 struct objc_protocol_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001946 struct objc_protocol_list *next;
1947 long count;
1948 Protocol *list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00001949 };
1950*/
Daniel Dunbardec75f82008-08-21 21:57:41 +00001951llvm::Constant *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001952CGObjCMac::EmitProtocolList(llvm::Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001953 ObjCProtocolDecl::protocol_iterator begin,
1954 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001955 std::vector<llvm::Constant*> ProtocolRefs;
1956
Daniel Dunbardec75f82008-08-21 21:57:41 +00001957 for (; begin != end; ++begin)
1958 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001959
1960 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001961 if (ProtocolRefs.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001962 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001963
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001964 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00001965 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001966
1967 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001968 // This field is only used by the runtime.
Owen Anderson0b75f232009-07-31 20:28:54 +00001969 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001970 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001971 ProtocolRefs.size() - 1);
1972 Values[2] =
1973 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1974 ProtocolRefs.size()),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001975 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001976
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00001977 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001978 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001979 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00001980 4, false);
Owen Andersonade90fd2009-07-29 18:54:39 +00001981 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001982}
1983
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001984void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1985 std::vector<llvm::Constant*> &Properties,
1986 const Decl *Container,
1987 const ObjCProtocolDecl *PROTO,
1988 const ObjCCommonTypesHelper &ObjCTypes) {
1989 std::vector<llvm::Constant*> Prop(2);
1990 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1991 E = PROTO->protocol_end(); P != E; ++P)
1992 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1993 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1994 E = PROTO->prop_end(); I != E; ++I) {
1995 const ObjCPropertyDecl *PD = *I;
1996 if (!PropertySet.insert(PD->getIdentifier()))
1997 continue;
1998 Prop[0] = GetPropertyName(PD->getIdentifier());
1999 Prop[1] = GetPropertyTypeString(PD, Container);
2000 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
2001 }
2002}
2003
Daniel Dunbarb036db82008-08-13 03:21:16 +00002004/*
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002005 struct _objc_property {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002006 const char * const name;
2007 const char * const attributes;
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002008 };
2009
2010 struct _objc_property_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002011 uint32_t entsize; // sizeof (struct _objc_property)
2012 uint32_t prop_count;
2013 struct _objc_property[prop_count];
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002014 };
2015*/
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002016llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002017 const Decl *Container,
2018 const ObjCContainerDecl *OCD,
2019 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002020 std::vector<llvm::Constant*> Properties, Prop(2);
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002021 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002022 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2023 E = OCD->prop_end(); I != E; ++I) {
Steve Naroffba3dc382009-01-11 12:47:58 +00002024 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002025 PropertySet.insert(PD->getIdentifier());
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002026 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbar4932b362008-08-28 04:38:10 +00002027 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002028 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002029 Prop));
2030 }
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002031 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002032 for (ObjCInterfaceDecl::all_protocol_iterator
2033 P = OID->all_referenced_protocol_begin(),
2034 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002035 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2036 ObjCTypes);
2037 }
2038 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2039 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2040 E = CD->protocol_end(); P != E; ++P)
2041 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2042 ObjCTypes);
2043 }
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002044
2045 // Return null for empty list.
2046 if (Properties.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002047 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002048
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002049 unsigned PropertySize =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002050 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002051 std::vector<llvm::Constant*> Values(3);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002052 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2053 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002054 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002055 Properties.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002056 Values[2] = llvm::ConstantArray::get(AT, Properties);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002057 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002058
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002059 llvm::GlobalVariable *GV =
2060 CreateMetadataVar(Name, Init,
2061 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002062 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002063 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002064 true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002065 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002066}
2067
2068/*
Daniel Dunbarb036db82008-08-13 03:21:16 +00002069 struct objc_method_description_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002070 int count;
2071 struct objc_method_description list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00002072 };
2073*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002074llvm::Constant *
2075CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
2076 std::vector<llvm::Constant*> Desc(2);
Owen Anderson170229f2009-07-14 23:10:40 +00002077 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002078 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2079 ObjCTypes.SelectorPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002080 Desc[1] = GetMethodVarType(MD);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002081 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002082 Desc);
2083}
Daniel Dunbarb036db82008-08-13 03:21:16 +00002084
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002085llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002086 const char *Section,
2087 const ConstantVector &Methods) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00002088 // Return null for empty list.
2089 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002090 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002091
2092 std::vector<llvm::Constant*> Values(2);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002093 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002094 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002095 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002096 Values[1] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002097 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002098
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002099 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002100 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002101 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002102}
2103
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002104/*
2105 struct _objc_category {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002106 char *category_name;
2107 char *class_name;
2108 struct _objc_method_list *instance_methods;
2109 struct _objc_method_list *class_methods;
2110 struct _objc_protocol_list *protocols;
2111 uint32_t size; // <rdar://4585769>
2112 struct _objc_property_list *instance_properties;
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002113 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002114*/
Daniel Dunbar92992502008-08-15 22:20:32 +00002115void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002116 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002117
Mike Stump18bb9282009-05-16 07:57:57 +00002118 // FIXME: This is poor design, the OCD should have a pointer to the category
2119 // decl. Additionally, note that Category can be null for the @implementation
2120 // w/o an @interface case. Sema should just create one for us as it does for
2121 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002122 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002123 const ObjCCategoryDecl *Category =
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002124 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002125
2126 llvm::SmallString<256> ExtName;
2127 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2128 << OCD->getName();
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002129
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002130 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002131 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002132 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002133 // Instance methods should always be defined.
2134 InstanceMethods.push_back(GetMethodConstant(*i));
2135 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002136 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002137 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002138 // Class methods should always be defined.
2139 ClassMethods.push_back(GetMethodConstant(*i));
2140 }
2141
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002142 std::vector<llvm::Constant*> Values(7);
2143 Values[0] = GetClassName(OCD->getIdentifier());
2144 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahaniane55f8662009-04-29 20:40:05 +00002145 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002146 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002147 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002148 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002149 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002150 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002151 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002152 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002153 ClassMethods);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002154 if (Category) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002155 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002156 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002157 Category->protocol_begin(),
2158 Category->protocol_end());
2159 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002160 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002161 }
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002162 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002163
2164 // If there is no category @interface then there can be no properties.
2165 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002166 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002167 OCD, Category, ObjCTypes);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002168 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002169 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002170 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002171
Owen Anderson0e0189d2009-07-27 22:29:56 +00002172 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002173 Values);
2174
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002175 llvm::GlobalVariable *GV =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002176 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002177 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002178 4, true);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002179 DefinedCategories.push_back(GV);
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00002180 DefinedCategoryNames.insert(ExtName.str());
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002181}
2182
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002183// FIXME: Get from somewhere?
2184enum ClassFlags {
2185 eClassFlags_Factory = 0x00001,
2186 eClassFlags_Meta = 0x00002,
2187 // <rdr://5142207>
2188 eClassFlags_HasCXXStructors = 0x02000,
2189 eClassFlags_Hidden = 0x20000,
2190 eClassFlags_ABI2_Hidden = 0x00010,
2191 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2192};
2193
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002194/*
2195 struct _objc_class {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002196 Class isa;
2197 Class super_class;
2198 const char *name;
2199 long version;
2200 long info;
2201 long instance_size;
2202 struct _objc_ivar_list *ivars;
2203 struct _objc_method_list *methods;
2204 struct _objc_cache *cache;
2205 struct _objc_protocol_list *protocols;
2206 // Objective-C 1.0 extensions (<rdr://4585769>)
2207 const char *ivar_layout;
2208 struct _objc_class_ext *ext;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002209 };
2210
2211 See EmitClassExtension();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002212*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002213void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002214 DefinedSymbols.insert(ID->getIdentifier());
2215
Chris Lattner86d7d912008-11-24 03:54:41 +00002216 std::string ClassName = ID->getNameAsString();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002217 // FIXME: Gross
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002218 ObjCInterfaceDecl *Interface =
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002219 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002220 llvm::Constant *Protocols =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002221 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002222 Interface->all_referenced_protocol_begin(),
2223 Interface->all_referenced_protocol_end());
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002224 unsigned Flags = eClassFlags_Factory;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00002225 if (ID->getNumIvarInitializers())
2226 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002227 unsigned Size =
Ken Dyckc8ae5502011-02-09 01:59:34 +00002228 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002229
2230 // FIXME: Set CXX-structors flag.
John McCall457a04e2010-10-22 21:05:15 +00002231 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002232 Flags |= eClassFlags_Hidden;
2233
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002234 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002235 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002236 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002237 // Instance methods should always be defined.
2238 InstanceMethods.push_back(GetMethodConstant(*i));
2239 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002240 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002241 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002242 // Class methods should always be defined.
2243 ClassMethods.push_back(GetMethodConstant(*i));
2244 }
2245
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002246 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002247 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002248 ObjCPropertyImplDecl *PID = *i;
2249
2250 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2251 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2252
2253 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2254 if (llvm::Constant *C = GetMethodConstant(MD))
2255 InstanceMethods.push_back(C);
2256 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2257 if (llvm::Constant *C = GetMethodConstant(MD))
2258 InstanceMethods.push_back(C);
2259 }
2260 }
2261
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002262 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarccf61832009-05-03 08:56:52 +00002263 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002264 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002265 // Record a reference to the super class.
2266 LazySymbols.insert(Super->getIdentifier());
2267
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002268 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002269 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002270 ObjCTypes.ClassPtrTy);
2271 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002272 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002273 }
2274 Values[ 2] = GetClassName(ID->getIdentifier());
2275 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002276 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2277 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2278 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002279 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002280 Values[ 7] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002281 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002282 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002283 InstanceMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002284 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002285 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002286 Values[ 9] = Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002287 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002288 Values[11] = EmitClassExtension(ID);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002289 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002290 Values);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002291 std::string Name("\01L_OBJC_CLASS_");
2292 Name += ClassName;
2293 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2294 // Check for a forward reference.
2295 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2296 if (GV) {
2297 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2298 "Forward metaclass reference has incorrect type.");
2299 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2300 GV->setInitializer(Init);
2301 GV->setSection(Section);
2302 GV->setAlignment(4);
2303 CGM.AddUsedGlobal(GV);
2304 }
2305 else
2306 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002307 DefinedClasses.push_back(GV);
2308}
2309
2310llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2311 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002312 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002313 unsigned Flags = eClassFlags_Meta;
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002314 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002315
John McCall457a04e2010-10-22 21:05:15 +00002316 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002317 Flags |= eClassFlags_Hidden;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002318
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002319 std::vector<llvm::Constant*> Values(12);
2320 // The isa for the metaclass is the root of the hierarchy.
2321 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2322 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2323 Root = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002324 Values[ 0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002325 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002326 ObjCTypes.ClassPtrTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002327 // The super class for the metaclass is emitted as the name of the
2328 // super class. The runtime fixes this up to point to the
2329 // *metaclass* for the super class.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002330 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002331 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002332 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002333 ObjCTypes.ClassPtrTy);
2334 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002335 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002336 }
2337 Values[ 2] = GetClassName(ID->getIdentifier());
2338 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002339 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2340 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2341 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002342 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002343 Values[ 7] =
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002344 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002345 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002346 Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002347 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002348 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002349 Values[ 9] = Protocols;
2350 // ivar_layout for metaclass is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002351 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002352 // The class extension is always unused for metaclasses.
Owen Anderson0b75f232009-07-31 20:28:54 +00002353 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002354 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002355 Values);
2356
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002357 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner86d7d912008-11-24 03:54:41 +00002358 Name += ID->getNameAsCString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002359
2360 // Check for a forward reference.
2361 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2362 if (GV) {
2363 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2364 "Forward metaclass reference has incorrect type.");
2365 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2366 GV->setInitializer(Init);
2367 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00002368 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002369 llvm::GlobalValue::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002370 Init, Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002371 }
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002372 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbarae333842009-03-09 22:18:41 +00002373 GV->setAlignment(4);
Chris Lattnerf56501c2009-07-17 23:57:13 +00002374 CGM.AddUsedGlobal(GV);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002375
2376 return GV;
2377}
2378
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002379llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002380 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002381
Mike Stump18bb9282009-05-16 07:57:57 +00002382 // FIXME: Should we look these up somewhere other than the module. Its a bit
2383 // silly since we only generate these while processing an implementation, so
2384 // exactly one pointer would work if know when we entered/exitted an
2385 // implementation block.
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002386
2387 // Check for an existing forward reference.
Fariborz Jahanian475831b2009-01-07 20:11:22 +00002388 // Previously, metaclass with internal linkage may have been defined.
2389 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002390 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2391 true)) {
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002392 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2393 "Forward metaclass reference has incorrect type.");
2394 return GV;
2395 } else {
2396 // Generate as an external reference to keep a consistent
2397 // module. This will be patched up when we emit the metaclass.
Owen Andersonc10c8d32009-07-08 19:05:04 +00002398 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002399 llvm::GlobalValue::ExternalLinkage,
2400 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002401 Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002402 }
2403}
2404
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002405llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2406 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2407
2408 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2409 true)) {
2410 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2411 "Forward class metadata reference has incorrect type.");
2412 return GV;
2413 } else {
2414 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2415 llvm::GlobalValue::ExternalLinkage,
2416 0,
2417 Name);
2418 }
2419}
2420
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002421/*
2422 struct objc_class_ext {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002423 uint32_t size;
2424 const char *weak_ivar_layout;
2425 struct _objc_property_list *properties;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002426 };
2427*/
2428llvm::Constant *
2429CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002430 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002431 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002432
2433 std::vector<llvm::Constant*> Values(3);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002434 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian6df69862009-04-22 23:00:43 +00002435 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002436 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002437 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002438
2439 // Return null if no extension bits are used.
2440 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002441 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002442
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002443 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002444 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002445 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002446 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002447 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002448}
2449
2450/*
2451 struct objc_ivar {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002452 char *ivar_name;
2453 char *ivar_type;
2454 int ivar_offset;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002455 };
2456
2457 struct objc_ivar_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002458 int ivar_count;
2459 struct objc_ivar list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002460 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002461*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002462llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002463 bool ForClass) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002464 std::vector<llvm::Constant*> Ivars, Ivar(3);
2465
2466 // When emitting the root class GCC emits ivar entries for the
2467 // actual class structure. It is not clear if we need to follow this
2468 // behavior; for now lets try and get away with not doing it. If so,
2469 // the cleanest solution would be to make up an ObjCInterfaceDecl
2470 // for the class.
2471 if (ForClass)
Owen Anderson0b75f232009-07-31 20:28:54 +00002472 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002473
2474 ObjCInterfaceDecl *OID =
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002475 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002476
Daniel Dunbarf5c18462009-04-20 06:54:31 +00002477 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002478 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002479
Daniel Dunbarf5c18462009-04-20 06:54:31 +00002480 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2481 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002482 // Ignore unnamed bit-fields.
2483 if (!IVD->getDeclName())
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002484 continue;
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00002485 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2486 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002487 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00002488 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson0e0189d2009-07-27 22:29:56 +00002489 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002490 }
2491
2492 // Return null for empty list.
2493 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002494 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002495
2496 std::vector<llvm::Constant*> Values(2);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002497 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002498 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002499 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002500 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002501 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002502
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002503 llvm::GlobalVariable *GV;
2504 if (ForClass)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002505 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002506 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002507 4, true);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002508 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002509 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002510 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002511 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002512 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002513}
2514
2515/*
2516 struct objc_method {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002517 SEL method_name;
2518 char *method_types;
2519 void *method;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002520 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002521
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002522 struct objc_method_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002523 struct objc_method_list *obsolete;
2524 int count;
2525 struct objc_method methods_list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002526 };
2527*/
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002528
2529/// GetMethodConstant - Return a struct objc_method constant for the
2530/// given method if it has been defined. The result is null if the
2531/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002532llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00002533 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002534 if (!Fn)
2535 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002536
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002537 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002538 Method[0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002539 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002540 ObjCTypes.SelectorPtrTy);
2541 Method[1] = GetMethodVarType(MD);
Owen Andersonade90fd2009-07-29 18:54:39 +00002542 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002543 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002544}
2545
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002546llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002547 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002548 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002549 // Return null for empty list.
2550 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002551 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002552
2553 std::vector<llvm::Constant*> Values(3);
Owen Anderson0b75f232009-07-31 20:28:54 +00002554 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002555 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002556 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002557 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002558 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002559 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002560
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002561 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002562 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002563 ObjCTypes.MethodListPtrTy);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002564}
2565
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00002566llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002567 const ObjCContainerDecl *CD) {
Daniel Dunbard2386812009-10-19 01:21:19 +00002568 llvm::SmallString<256> Name;
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00002569 GetNameForMethod(OMD, CD, Name);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002570
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002571 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002572 const llvm::FunctionType *MethodTy =
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002573 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002574 llvm::Function *Method =
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002575 llvm::Function::Create(MethodTy,
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002576 llvm::GlobalValue::InternalLinkage,
Daniel Dunbard2386812009-10-19 01:21:19 +00002577 Name.str(),
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002578 &CGM.getModule());
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002579 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002580
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002581 return Method;
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002582}
2583
Daniel Dunbar30c65362009-03-09 20:09:19 +00002584llvm::GlobalVariable *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002585CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +00002586 llvm::Constant *Init,
2587 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002588 unsigned Align,
2589 bool AddToUsed) {
Daniel Dunbar30c65362009-03-09 20:09:19 +00002590 const llvm::Type *Ty = Init->getType();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002591 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002592 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerf56501c2009-07-17 23:57:13 +00002593 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002594 if (Section)
2595 GV->setSection(Section);
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002596 if (Align)
2597 GV->setAlignment(Align);
2598 if (AddToUsed)
Chris Lattnerf56501c2009-07-17 23:57:13 +00002599 CGM.AddUsedGlobal(GV);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002600 return GV;
2601}
2602
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002603llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00002604 // Abuse this interface function as a place to finalize.
2605 FinishModule();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002606 return NULL;
2607}
2608
Chris Lattnerd4808922009-03-22 21:03:39 +00002609llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002610 return ObjCTypes.getGetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002611}
2612
Chris Lattnerd4808922009-03-22 21:03:39 +00002613llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002614 return ObjCTypes.getSetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002615}
2616
David Chisnall168b80f2010-12-26 22:13:16 +00002617llvm::Constant *CGObjCMac::GetGetStructFunction() {
2618 return ObjCTypes.getCopyStructFn();
2619}
2620llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00002621 return ObjCTypes.getCopyStructFn();
2622}
2623
Chris Lattnerd4808922009-03-22 21:03:39 +00002624llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002625 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson3f35a262008-08-31 04:05:03 +00002626}
2627
John McCallbd309292010-07-06 01:34:17 +00002628void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2629 return EmitTryOrSynchronizedStmt(CGF, S);
2630}
2631
2632void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2633 const ObjCAtSynchronizedStmt &S) {
2634 return EmitTryOrSynchronizedStmt(CGF, S);
2635}
2636
John McCall65bea082010-07-21 06:59:36 +00002637namespace {
John McCallcda666c2010-07-21 07:22:38 +00002638 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCall65bea082010-07-21 06:59:36 +00002639 const Stmt &S;
John McCall2dd7d442010-08-04 05:59:32 +00002640 llvm::Value *SyncArgSlot;
John McCall65bea082010-07-21 06:59:36 +00002641 llvm::Value *CallTryExitVar;
2642 llvm::Value *ExceptionData;
2643 ObjCTypesHelper &ObjCTypes;
2644 PerformFragileFinally(const Stmt *S,
John McCall2dd7d442010-08-04 05:59:32 +00002645 llvm::Value *SyncArgSlot,
John McCall65bea082010-07-21 06:59:36 +00002646 llvm::Value *CallTryExitVar,
2647 llvm::Value *ExceptionData,
2648 ObjCTypesHelper *ObjCTypes)
John McCall2dd7d442010-08-04 05:59:32 +00002649 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCall65bea082010-07-21 06:59:36 +00002650 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2651
2652 void Emit(CodeGenFunction &CGF, bool IsForEH) {
2653 // Check whether we need to call objc_exception_try_exit.
2654 // In optimized code, this branch will always be folded.
2655 llvm::BasicBlock *FinallyCallExit =
2656 CGF.createBasicBlock("finally.call_exit");
2657 llvm::BasicBlock *FinallyNoCallExit =
2658 CGF.createBasicBlock("finally.no_call_exit");
2659 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2660 FinallyCallExit, FinallyNoCallExit);
2661
2662 CGF.EmitBlock(FinallyCallExit);
2663 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2664 ->setDoesNotThrow();
2665
2666 CGF.EmitBlock(FinallyNoCallExit);
2667
2668 if (isa<ObjCAtTryStmt>(S)) {
2669 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCallcebe0ca2010-08-11 00:16:14 +00002670 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2671 // Save the current cleanup destination in case there's
2672 // control flow inside the finally statement.
2673 llvm::Value *CurCleanupDest =
2674 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2675
John McCall65bea082010-07-21 06:59:36 +00002676 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2677
John McCallcebe0ca2010-08-11 00:16:14 +00002678 if (CGF.HaveInsertPoint()) {
2679 CGF.Builder.CreateStore(CurCleanupDest,
2680 CGF.getNormalCleanupDestSlot());
2681 } else {
2682 // Currently, the end of the cleanup must always exist.
2683 CGF.EnsureInsertPoint();
2684 }
2685 }
John McCall65bea082010-07-21 06:59:36 +00002686 } else {
2687 // Emit objc_sync_exit(expr); as finally's sole statement for
2688 // @synchronized.
John McCall2dd7d442010-08-04 05:59:32 +00002689 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCall65bea082010-07-21 06:59:36 +00002690 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2691 ->setDoesNotThrow();
2692 }
2693 }
2694 };
John McCall42227ed2010-07-31 23:20:56 +00002695
2696 class FragileHazards {
2697 CodeGenFunction &CGF;
2698 llvm::SmallVector<llvm::Value*, 20> Locals;
2699 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2700
2701 llvm::InlineAsm *ReadHazard;
2702 llvm::InlineAsm *WriteHazard;
2703
2704 llvm::FunctionType *GetAsmFnType();
2705
2706 void collectLocals();
2707 void emitReadHazard(CGBuilderTy &Builder);
2708
2709 public:
2710 FragileHazards(CodeGenFunction &CGF);
John McCall2dd7d442010-08-04 05:59:32 +00002711
John McCall42227ed2010-07-31 23:20:56 +00002712 void emitWriteHazard();
John McCall2dd7d442010-08-04 05:59:32 +00002713 void emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00002714 };
2715}
2716
2717/// Create the fragile-ABI read and write hazards based on the current
2718/// state of the function, which is presumed to be immediately prior
2719/// to a @try block. These hazards are used to maintain correct
2720/// semantics in the face of optimization and the fragile ABI's
2721/// cavalier use of setjmp/longjmp.
2722FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2723 collectLocals();
2724
2725 if (Locals.empty()) return;
2726
2727 // Collect all the blocks in the function.
2728 for (llvm::Function::iterator
2729 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2730 BlocksBeforeTry.insert(&*I);
2731
2732 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2733
2734 // Create a read hazard for the allocas. This inhibits dead-store
2735 // optimizations and forces the values to memory. This hazard is
2736 // inserted before any 'throwing' calls in the protected scope to
2737 // reflect the possibility that the variables might be read from the
2738 // catch block if the call throws.
2739 {
2740 std::string Constraint;
2741 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2742 if (I) Constraint += ',';
2743 Constraint += "*m";
2744 }
2745
2746 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2747 }
2748
2749 // Create a write hazard for the allocas. This inhibits folding
2750 // loads across the hazard. This hazard is inserted at the
2751 // beginning of the catch path to reflect the possibility that the
2752 // variables might have been written within the protected scope.
2753 {
2754 std::string Constraint;
2755 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2756 if (I) Constraint += ',';
2757 Constraint += "=*m";
2758 }
2759
2760 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2761 }
2762}
2763
2764/// Emit a write hazard at the current location.
2765void FragileHazards::emitWriteHazard() {
2766 if (Locals.empty()) return;
2767
2768 CGF.Builder.CreateCall(WriteHazard, Locals.begin(), Locals.end())
2769 ->setDoesNotThrow();
2770}
2771
John McCall42227ed2010-07-31 23:20:56 +00002772void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2773 assert(!Locals.empty());
2774 Builder.CreateCall(ReadHazard, Locals.begin(), Locals.end())
2775 ->setDoesNotThrow();
2776}
2777
2778/// Emit read hazards in all the protected blocks, i.e. all the blocks
2779/// which have been inserted since the beginning of the try.
John McCall2dd7d442010-08-04 05:59:32 +00002780void FragileHazards::emitHazardsInNewBlocks() {
John McCall42227ed2010-07-31 23:20:56 +00002781 if (Locals.empty()) return;
2782
2783 CGBuilderTy Builder(CGF.getLLVMContext());
2784
2785 // Iterate through all blocks, skipping those prior to the try.
2786 for (llvm::Function::iterator
2787 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2788 llvm::BasicBlock &BB = *FI;
2789 if (BlocksBeforeTry.count(&BB)) continue;
2790
2791 // Walk through all the calls in the block.
2792 for (llvm::BasicBlock::iterator
2793 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2794 llvm::Instruction &I = *BI;
2795
2796 // Ignore instructions that aren't non-intrinsic calls.
2797 // These are the only calls that can possibly call longjmp.
2798 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2799 if (isa<llvm::IntrinsicInst>(I))
2800 continue;
2801
2802 // Ignore call sites marked nounwind. This may be questionable,
2803 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2804 llvm::CallSite CS(&I);
2805 if (CS.doesNotThrow()) continue;
2806
John McCall2dd7d442010-08-04 05:59:32 +00002807 // Insert a read hazard before the call. This will ensure that
2808 // any writes to the locals are performed before making the
2809 // call. If the call throws, then this is sufficient to
2810 // guarantee correctness as long as it doesn't also write to any
2811 // locals.
John McCall42227ed2010-07-31 23:20:56 +00002812 Builder.SetInsertPoint(&BB, BI);
2813 emitReadHazard(Builder);
2814 }
2815 }
2816}
2817
2818static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2819 if (V) S.insert(V);
2820}
2821
2822void FragileHazards::collectLocals() {
2823 // Compute a set of allocas to ignore.
2824 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2825 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2826 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
2827 addIfPresent(AllocasToIgnore, CGF.EHCleanupDest);
2828
2829 // Collect all the allocas currently in the function. This is
2830 // probably way too aggressive.
2831 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2832 for (llvm::BasicBlock::iterator
2833 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2834 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2835 Locals.push_back(&*I);
2836}
2837
2838llvm::FunctionType *FragileHazards::GetAsmFnType() {
2839 std::vector<const llvm::Type *> Tys(Locals.size());
2840 for (unsigned I = 0, E = Locals.size(); I != E; ++I)
2841 Tys[I] = Locals[I]->getType();
2842 return llvm::FunctionType::get(CGF.Builder.getVoidTy(), Tys, false);
John McCall65bea082010-07-21 06:59:36 +00002843}
2844
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002845/*
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002846
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002847 Objective-C setjmp-longjmp (sjlj) Exception Handling
2848 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002849
John McCallbd309292010-07-06 01:34:17 +00002850 A catch buffer is a setjmp buffer plus:
2851 - a pointer to the exception that was caught
2852 - a pointer to the previous exception data buffer
2853 - two pointers of reserved storage
2854 Therefore catch buffers form a stack, with a pointer to the top
2855 of the stack kept in thread-local storage.
2856
2857 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2858 objc_exception_try_exit pops the given catch buffer, which is
2859 required to be the top of the EH stack.
2860 objc_exception_throw pops the top of the EH stack, writes the
2861 thrown exception into the appropriate field, and longjmps
2862 to the setjmp buffer. It crashes the process (with a printf
2863 and an abort()) if there are no catch buffers on the stack.
2864 objc_exception_extract just reads the exception pointer out of the
2865 catch buffer.
2866
2867 There's no reason an implementation couldn't use a light-weight
2868 setjmp here --- something like __builtin_setjmp, but API-compatible
2869 with the heavyweight setjmp. This will be more important if we ever
2870 want to implement correct ObjC/C++ exception interactions for the
2871 fragile ABI.
2872
2873 Note that for this use of setjmp/longjmp to be correct, we may need
2874 to mark some local variables volatile: if a non-volatile local
2875 variable is modified between the setjmp and the longjmp, it has
2876 indeterminate value. For the purposes of LLVM IR, it may be
2877 sufficient to make loads and stores within the @try (to variables
2878 declared outside the @try) volatile. This is necessary for
2879 optimized correctness, but is not currently being done; this is
2880 being tracked as rdar://problem/8160285
2881
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002882 The basic framework for a @try-catch-finally is as follows:
2883 {
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002884 objc_exception_data d;
2885 id _rethrow = null;
Anders Carlssonda0e4562009-02-07 21:26:04 +00002886 bool _call_try_exit = true;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002887
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002888 objc_exception_try_enter(&d);
2889 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002890 ... try body ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002891 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002892 // exception path
2893 id _caught = objc_exception_extract(&d);
2894
2895 // enter new try scope for handlers
2896 if (!setjmp(d.jmp_buf)) {
2897 ... match exception and execute catch blocks ...
2898
2899 // fell off end, rethrow.
2900 _rethrow = _caught;
2901 ... jump-through-finally to finally_rethrow ...
2902 } else {
2903 // exception in catch block
2904 _rethrow = objc_exception_extract(&d);
2905 _call_try_exit = false;
2906 ... jump-through-finally to finally_rethrow ...
2907 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002908 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002909 ... jump-through-finally to finally_end ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002910
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002911 finally:
Anders Carlssonda0e4562009-02-07 21:26:04 +00002912 if (_call_try_exit)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002913 objc_exception_try_exit(&d);
Anders Carlssonda0e4562009-02-07 21:26:04 +00002914
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002915 ... finally block ....
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002916 ... dispatch to finally destination ...
2917
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002918 finally_rethrow:
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002919 objc_exception_throw(_rethrow);
2920
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002921 finally_end:
2922 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002923
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002924 This framework differs slightly from the one gcc uses, in that gcc
2925 uses _rethrow to determine if objc_exception_try_exit should be called
2926 and if the object should be rethrown. This breaks in the face of
2927 throwing nil and introduces unnecessary branches.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002928
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002929 We specialize this framework for a few particular circumstances:
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002930
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002931 - If there are no catch blocks, then we avoid emitting the second
2932 exception handling context.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002933
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002934 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2935 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002936
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002937 - FIXME: If there is no @finally block we can do a few more
2938 simplifications.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002939
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002940 Rethrows and Jumps-Through-Finally
2941 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002942
John McCallbd309292010-07-06 01:34:17 +00002943 '@throw;' is supported by pushing the currently-caught exception
2944 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002945
John McCallbd309292010-07-06 01:34:17 +00002946 Branches through the @finally block are handled with an ordinary
2947 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2948 exceptions are not compatible with C++ exceptions, and this is
2949 hardly the only place where this will go wrong.
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002950
John McCallbd309292010-07-06 01:34:17 +00002951 @synchronized(expr) { stmt; } is emitted as if it were:
2952 id synch_value = expr;
2953 objc_sync_enter(synch_value);
2954 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002955*/
2956
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +00002957void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2958 const Stmt &S) {
2959 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallbd309292010-07-06 01:34:17 +00002960
2961 // A destination for the fall-through edges of the catch handlers to
2962 // jump to.
2963 CodeGenFunction::JumpDest FinallyEnd =
2964 CGF.getJumpDestInCurrentScope("finally.end");
2965
2966 // A destination for the rethrow edge of the catch handlers to jump
2967 // to.
2968 CodeGenFunction::JumpDest FinallyRethrow =
2969 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002970
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002971 // For @synchronized, call objc_sync_enter(sync.expr). The
2972 // evaluation of the expression must occur before we enter the
John McCall2dd7d442010-08-04 05:59:32 +00002973 // @synchronized. We can't avoid a temp here because we need the
2974 // value to be preserved. If the backend ever does liveness
2975 // correctly after setjmp, this will be unnecessary.
2976 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002977 if (!isTry) {
John McCall2dd7d442010-08-04 05:59:32 +00002978 llvm::Value *SyncArg =
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002979 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2980 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallbd309292010-07-06 01:34:17 +00002981 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2982 ->setDoesNotThrow();
John McCall2dd7d442010-08-04 05:59:32 +00002983
2984 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2985 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002986 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002987
John McCall2dd7d442010-08-04 05:59:32 +00002988 // Allocate memory for the setjmp buffer. This needs to be kept
2989 // live throughout the try and catch blocks.
2990 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2991 "exceptiondata.ptr");
2992
John McCall42227ed2010-07-31 23:20:56 +00002993 // Create the fragile hazards. Note that this will not capture any
2994 // of the allocas required for exception processing, but will
2995 // capture the current basic block (which extends all the way to the
2996 // setjmp call) as "before the @try".
2997 FragileHazards Hazards(CGF);
2998
John McCallbd309292010-07-06 01:34:17 +00002999 // Create a flag indicating whether the cleanup needs to call
3000 // objc_exception_try_exit. This is true except when
3001 // - no catches match and we're branching through the cleanup
3002 // just to rethrow the exception, or
3003 // - a catch matched and we're falling out of the catch handler.
John McCall2dd7d442010-08-04 05:59:32 +00003004 // The setjmp-safety rule here is that we should always store to this
3005 // variable in a place that dominates the branch through the cleanup
3006 // without passing through any setjmps.
John McCallbd309292010-07-06 01:34:17 +00003007 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlssonda0e4562009-02-07 21:26:04 +00003008 "_call_try_exit");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003009
John McCall9916e3f2010-10-04 23:42:51 +00003010 // A slot containing the exception to rethrow. Only needed when we
3011 // have both a @catch and a @finally.
3012 llvm::Value *PropagatingExnVar = 0;
3013
John McCallbd309292010-07-06 01:34:17 +00003014 // Push a normal cleanup to leave the try scope.
John McCallcda666c2010-07-21 07:22:38 +00003015 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall2dd7d442010-08-04 05:59:32 +00003016 SyncArgSlot,
John McCallcda666c2010-07-21 07:22:38 +00003017 CallTryExitVar,
3018 ExceptionData,
3019 &ObjCTypes);
John McCallbd309292010-07-06 01:34:17 +00003020
3021 // Enter a try block:
3022 // - Call objc_exception_try_enter to push ExceptionData on top of
3023 // the EH stack.
3024 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3025 ->setDoesNotThrow();
3026
3027 // - Call setjmp on the exception data buffer.
3028 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3029 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3030 llvm::Value *SetJmpBuffer =
3031 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer");
3032 llvm::CallInst *SetJmpResult =
3033 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3034 SetJmpResult->setDoesNotThrow();
3035
3036 // If setjmp returned 0, enter the protected block; otherwise,
3037 // branch to the handler.
Daniel Dunbar75283ff2008-11-11 02:29:29 +00003038 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3039 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallbd309292010-07-06 01:34:17 +00003040 llvm::Value *DidCatch =
John McCallcebe0ca2010-08-11 00:16:14 +00003041 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3042 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003043
John McCallbd309292010-07-06 01:34:17 +00003044 // Emit the protected block.
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003045 CGF.EmitBlock(TryBlock);
John McCall2dd7d442010-08-04 05:59:32 +00003046 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003047 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallbd309292010-07-06 01:34:17 +00003048 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall2dd7d442010-08-04 05:59:32 +00003049
3050 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003051
John McCallbd309292010-07-06 01:34:17 +00003052 // Emit the exception handler block.
Daniel Dunbar7f086782008-09-27 23:30:04 +00003053 CGF.EmitBlock(TryHandler);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003054
John McCall42227ed2010-07-31 23:20:56 +00003055 // Don't optimize loads of the in-scope locals across this point.
3056 Hazards.emitWriteHazard();
3057
John McCallbd309292010-07-06 01:34:17 +00003058 // For a @synchronized (or a @try with no catches), just branch
3059 // through the cleanup to the rethrow block.
3060 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3061 // Tell the cleanup not to re-pop the exit.
John McCall2dd7d442010-08-04 05:59:32 +00003062 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003063 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallbd309292010-07-06 01:34:17 +00003064
3065 // Otherwise, we have to match against the caught exceptions.
3066 } else {
John McCall2dd7d442010-08-04 05:59:32 +00003067 // Retrieve the exception object. We may emit multiple blocks but
3068 // nothing can cross this so the value is already in SSA form.
3069 llvm::CallInst *Caught =
3070 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3071 ExceptionData, "caught");
3072 Caught->setDoesNotThrow();
3073
John McCallbd309292010-07-06 01:34:17 +00003074 // Push the exception to rethrow onto the EH value stack for the
3075 // benefit of any @throws in the handlers.
3076 CGF.ObjCEHValueStack.push_back(Caught);
3077
Douglas Gregor96c79492010-04-23 22:50:49 +00003078 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003079
John McCall2dd7d442010-08-04 05:59:32 +00003080 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallbd309292010-07-06 01:34:17 +00003081
John McCall2dd7d442010-08-04 05:59:32 +00003082 llvm::BasicBlock *CatchBlock = 0;
3083 llvm::BasicBlock *CatchHandler = 0;
3084 if (HasFinally) {
John McCall9916e3f2010-10-04 23:42:51 +00003085 // Save the currently-propagating exception before
3086 // objc_exception_try_enter clears the exception slot.
3087 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3088 "propagating_exception");
3089 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3090
John McCall2dd7d442010-08-04 05:59:32 +00003091 // Enter a new exception try block (in case a @catch block
3092 // throws an exception).
3093 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3094 ->setDoesNotThrow();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003095
John McCall2dd7d442010-08-04 05:59:32 +00003096 llvm::CallInst *SetJmpResult =
3097 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3098 "setjmp.result");
3099 SetJmpResult->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003100
John McCall2dd7d442010-08-04 05:59:32 +00003101 llvm::Value *Threw =
3102 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3103
3104 CatchBlock = CGF.createBasicBlock("catch");
3105 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3106 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3107
3108 CGF.EmitBlock(CatchBlock);
3109 }
3110
3111 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003112
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003113 // Handle catch list. As a special case we check if everything is
3114 // matched and avoid generating code for falling off the end if
3115 // so.
3116 bool AllMatched = false;
Douglas Gregor96c79492010-04-23 22:50:49 +00003117 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3118 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003119
Douglas Gregor46a572b2010-04-26 16:46:50 +00003120 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +00003121 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar523208f2008-09-27 07:36:24 +00003122
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003123 // catch(...) always matches.
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003124 if (!CatchParam) {
3125 AllMatched = true;
3126 } else {
John McCall9dd450b2009-09-21 23:43:11 +00003127 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003128
John McCallbd309292010-07-06 01:34:17 +00003129 // catch(id e) always matches under this ABI, since only
3130 // ObjC exceptions end up here in the first place.
Daniel Dunbar86919f42008-09-27 22:21:14 +00003131 // FIXME: For the time being we also match id<X>; this should
3132 // be rejected by Sema instead.
Eli Friedman55179ca2009-07-11 00:57:02 +00003133 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003134 AllMatched = true;
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003135 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003136
John McCallbd309292010-07-06 01:34:17 +00003137 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003138 if (AllMatched) {
John McCallbd309292010-07-06 01:34:17 +00003139 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3140
Anders Carlsson9396a892008-09-11 09:15:33 +00003141 if (CatchParam) {
John McCall1c9c3fd2010-10-15 04:57:14 +00003142 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003143 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallbd309292010-07-06 01:34:17 +00003144
3145 // These types work out because ConvertType(id) == i8*.
Steve Naroff371b8fb2009-03-03 19:52:17 +00003146 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson9396a892008-09-11 09:15:33 +00003147 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003148
Anders Carlsson9396a892008-09-11 09:15:33 +00003149 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003150
3151 // The scope of the catch variable ends right here.
3152 CatchVarCleanups.ForceCleanup();
3153
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003154 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003155 break;
3156 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003157
Steve Naroff7cae42b2009-07-10 23:34:53 +00003158 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall96fa4842010-05-17 21:00:27 +00003159 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallbd309292010-07-06 01:34:17 +00003160
3161 // FIXME: @catch (Class c) ?
John McCall96fa4842010-05-17 21:00:27 +00003162 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3163 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003164
3165 // Check if the @catch block matches the exception object.
John McCall96fa4842010-05-17 21:00:27 +00003166 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003167
John McCallbd309292010-07-06 01:34:17 +00003168 llvm::CallInst *Match =
Chris Lattnerc6406db2009-04-22 02:26:14 +00003169 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3170 Class, Caught, "match");
John McCallbd309292010-07-06 01:34:17 +00003171 Match->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003172
John McCallbd309292010-07-06 01:34:17 +00003173 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3174 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003175
3176 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar7f086782008-09-27 23:30:04 +00003177 MatchedBlock, NextCatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003178
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003179 // Emit the @catch block.
3180 CGF.EmitBlock(MatchedBlock);
John McCallbd309292010-07-06 01:34:17 +00003181
3182 // Collect any cleanups for the catch variable. The scope lasts until
3183 // the end of the catch body.
John McCall2dd7d442010-08-04 05:59:32 +00003184 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallbd309292010-07-06 01:34:17 +00003185
John McCall1c9c3fd2010-10-15 04:57:14 +00003186 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003187 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003188
John McCallbd309292010-07-06 01:34:17 +00003189 // Initialize the catch variable.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003190 llvm::Value *Tmp =
3191 CGF.Builder.CreateBitCast(Caught,
3192 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003193 "tmp");
Steve Naroff371b8fb2009-03-03 19:52:17 +00003194 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003195
Anders Carlsson9396a892008-09-11 09:15:33 +00003196 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003197
3198 // We're done with the catch variable.
3199 CatchVarCleanups.ForceCleanup();
3200
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003201 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003202
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003203 CGF.EmitBlock(NextCatchBlock);
3204 }
3205
John McCallbd309292010-07-06 01:34:17 +00003206 CGF.ObjCEHValueStack.pop_back();
3207
John McCall2dd7d442010-08-04 05:59:32 +00003208 // If nothing wanted anything to do with the caught exception,
3209 // kill the extract call.
3210 if (Caught->use_empty())
3211 Caught->eraseFromParent();
3212
3213 if (!AllMatched)
3214 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3215
3216 if (HasFinally) {
3217 // Emit the exception handler for the @catch blocks.
3218 CGF.EmitBlock(CatchHandler);
3219
3220 // In theory we might now need a write hazard, but actually it's
3221 // unnecessary because there's no local-accessing code between
3222 // the try's write hazard and here.
3223 //Hazards.emitWriteHazard();
3224
John McCall9916e3f2010-10-04 23:42:51 +00003225 // Extract the new exception and save it to the
3226 // propagating-exception slot.
3227 assert(PropagatingExnVar);
3228 llvm::CallInst *NewCaught =
3229 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3230 ExceptionData, "caught");
3231 NewCaught->setDoesNotThrow();
3232 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3233
John McCall2dd7d442010-08-04 05:59:32 +00003234 // Don't pop the catch handler; the throw already did.
3235 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003236 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003237 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003238 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003239
John McCall42227ed2010-07-31 23:20:56 +00003240 // Insert read hazards as required in the new blocks.
John McCall2dd7d442010-08-04 05:59:32 +00003241 Hazards.emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00003242
John McCallbd309292010-07-06 01:34:17 +00003243 // Pop the cleanup.
John McCall2dd7d442010-08-04 05:59:32 +00003244 CGF.Builder.restoreIP(TryFallthroughIP);
3245 if (CGF.HaveInsertPoint())
3246 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallbd309292010-07-06 01:34:17 +00003247 CGF.PopCleanupBlock();
John McCall2dd7d442010-08-04 05:59:32 +00003248 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003249
John McCallbd309292010-07-06 01:34:17 +00003250 // Emit the rethrow block.
John McCall42227ed2010-07-31 23:20:56 +00003251 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallad5d61e2010-07-23 21:56:41 +00003252 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallbd309292010-07-06 01:34:17 +00003253 if (CGF.HaveInsertPoint()) {
John McCall9916e3f2010-10-04 23:42:51 +00003254 // If we have a propagating-exception variable, check it.
3255 llvm::Value *PropagatingExn;
3256 if (PropagatingExnVar) {
3257 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall2dd7d442010-08-04 05:59:32 +00003258
John McCall9916e3f2010-10-04 23:42:51 +00003259 // Otherwise, just look in the buffer for the exception to throw.
3260 } else {
3261 llvm::CallInst *Caught =
3262 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3263 ExceptionData);
3264 Caught->setDoesNotThrow();
3265 PropagatingExn = Caught;
3266 }
3267
3268 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallbd309292010-07-06 01:34:17 +00003269 ->setDoesNotThrow();
3270 CGF.Builder.CreateUnreachable();
Fariborz Jahaniane2caaaa2008-11-21 19:21:53 +00003271 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003272
John McCall42227ed2010-07-31 23:20:56 +00003273 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003274}
3275
3276void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003277 const ObjCAtThrowStmt &S) {
Anders Carlssone005aa12008-09-09 16:16:55 +00003278 llvm::Value *ExceptionAsObject;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003279
Anders Carlssone005aa12008-09-09 16:16:55 +00003280 if (const Expr *ThrowExpr = S.getThrowExpr()) {
3281 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003282 ExceptionAsObject =
Anders Carlssone005aa12008-09-09 16:16:55 +00003283 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
3284 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003285 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003286 "Unexpected rethrow outside @catch block.");
Anders Carlssonbf8a1be2009-02-07 21:37:21 +00003287 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlssone005aa12008-09-09 16:16:55 +00003288 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003289
John McCallbd309292010-07-06 01:34:17 +00003290 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3291 ->setDoesNotReturn();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003292 CGF.Builder.CreateUnreachable();
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003293
3294 // Clear the insertion point to indicate we are in unreachable code.
3295 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003296}
3297
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003298/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003299/// object: objc_read_weak (id *src)
3300///
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003301llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003302 llvm::Value *AddrWeakObj) {
Eli Friedmana374b682009-03-07 03:57:15 +00003303 const llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003304 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3305 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3306 ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00003307 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003308 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00003309 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003310 return read_weak;
3311}
3312
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003313/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3314/// objc_assign_weak (id src, id *dst)
3315///
3316void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003317 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003318 const llvm::Type * SrcTy = src->getType();
3319 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003320 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003321 assert(Size <= 8 && "does not support size > 8");
3322 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003323 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003324 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3325 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003326 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3327 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00003328 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003329 src, dst, "weakassign");
3330 return;
3331}
3332
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003333/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3334/// objc_assign_global (id src, id *dst)
3335///
3336void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00003337 llvm::Value *src, llvm::Value *dst,
3338 bool threadlocal) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003339 const llvm::Type * SrcTy = src->getType();
3340 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003341 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003342 assert(Size <= 8 && "does not support size > 8");
3343 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003344 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003345 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3346 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003347 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3348 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00003349 if (!threadlocal)
3350 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3351 src, dst, "globalassign");
3352 else
3353 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3354 src, dst, "threadlocalassign");
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003355 return;
3356}
3357
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003358/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003359/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003360///
3361void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003362 llvm::Value *src, llvm::Value *dst,
3363 llvm::Value *ivarOffset) {
3364 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003365 const llvm::Type * SrcTy = src->getType();
3366 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003367 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003368 assert(Size <= 8 && "does not support size > 8");
3369 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003370 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003371 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3372 }
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003373 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3374 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003375 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3376 src, dst, ivarOffset);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003377 return;
3378}
3379
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003380/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3381/// objc_assign_strongCast (id src, id *dst)
3382///
3383void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003384 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003385 const llvm::Type * SrcTy = src->getType();
3386 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003387 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003388 assert(Size <= 8 && "does not support size > 8");
3389 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003390 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003391 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3392 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003393 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3394 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00003395 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003396 src, dst, "weakassign");
3397 return;
3398}
3399
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003400void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003401 llvm::Value *DestPtr,
3402 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003403 llvm::Value *size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003404 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3405 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003406 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003407 DestPtr, SrcPtr, size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003408 return;
3409}
3410
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003411/// EmitObjCValueForIvar - Code Gen for ivar reference.
3412///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003413LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3414 QualType ObjectTy,
3415 llvm::Value *BaseValue,
3416 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003417 unsigned CVRQualifiers) {
John McCall8b07ec22010-05-15 11:32:37 +00003418 const ObjCInterfaceDecl *ID =
3419 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003420 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3421 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003422}
3423
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003424llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00003425 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003426 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003427 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003428 return llvm::ConstantInt::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003429 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3430 Offset);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003431}
3432
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003433/* *** Private Interface *** */
3434
3435/// EmitImageInfo - Emit the image info marker used to encode some module
3436/// level information.
3437///
3438/// See: <rdr://4810609&4810587&4810587>
3439/// struct IMAGE_INFO {
3440/// unsigned version;
3441/// unsigned flags;
3442/// };
3443enum ImageInfoFlags {
Daniel Dunbar5e639272010-04-25 20:39:01 +00003444 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003445 eImageInfo_GarbageCollected = (1 << 1),
3446 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003447 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3448
Daniel Dunbar5e639272010-04-25 20:39:01 +00003449 // A flag indicating that the module has no instances of a @synthesize of a
3450 // superclass variable. <rdar://problem/6803242>
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003451 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003452};
3453
Daniel Dunbar5e639272010-04-25 20:39:01 +00003454void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003455 unsigned version = 0; // Version is unused?
3456 unsigned flags = 0;
3457
3458 // FIXME: Fix and continue?
3459 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3460 flags |= eImageInfo_GarbageCollected;
3461 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3462 flags |= eImageInfo_GCOnly;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003463
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003464 // We never allow @synthesize of a superclass property.
3465 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003466
Chris Lattner5e016ae2010-06-27 07:15:29 +00003467 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3468
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003469 // Emitted as int[2];
3470 llvm::Constant *values[2] = {
Chris Lattner5e016ae2010-06-27 07:15:29 +00003471 llvm::ConstantInt::get(Int32Ty, version),
3472 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003473 };
Chris Lattner5e016ae2010-06-27 07:15:29 +00003474 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003475
3476 const char *Section;
3477 if (ObjCABI == 1)
3478 Section = "__OBJC, __image_info,regular";
3479 else
3480 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003481 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003482 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson47034e12009-07-28 18:33:04 +00003483 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003484 Section,
3485 0,
3486 true);
3487 GV->setConstant(true);
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003488}
3489
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003490
3491// struct objc_module {
3492// unsigned long version;
3493// unsigned long size;
3494// const char *name;
3495// Symtab symtab;
3496// };
3497
3498// FIXME: Get from somewhere
3499static const int ModuleVersion = 7;
3500
3501void CGObjCMac::EmitModuleInfo() {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003502 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003503
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003504 std::vector<llvm::Constant*> Values(4);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003505 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3506 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar92992502008-08-15 22:20:32 +00003507 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbarb036db82008-08-13 03:21:16 +00003508 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003509 Values[3] = EmitModuleSymbols();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003510 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson0e0189d2009-07-27 22:29:56 +00003511 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003512 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00003513 4, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003514}
3515
3516llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003517 unsigned NumClasses = DefinedClasses.size();
3518 unsigned NumCategories = DefinedCategories.size();
3519
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003520 // Return null if no symbols were defined.
3521 if (!NumClasses && !NumCategories)
Owen Anderson0b75f232009-07-31 20:28:54 +00003522 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003523
3524 std::vector<llvm::Constant*> Values(5);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003525 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson0b75f232009-07-31 20:28:54 +00003526 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003527 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3528 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003529
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003530 // The runtime expects exactly the list of defined classes followed
3531 // by the list of defined categories, in a single array.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003532 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003533 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00003534 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003535 ObjCTypes.Int8PtrTy);
3536 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003537 Symbols[NumClasses + i] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003538 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003539 ObjCTypes.Int8PtrTy);
3540
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003541 Values[4] =
Owen Anderson9793f0e2009-07-29 22:16:19 +00003542 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003543 NumClasses + NumCategories),
3544 Symbols);
3545
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00003546 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003547
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003548 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003549 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3550 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003551 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00003552 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003553}
3554
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003555llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003556 const ObjCInterfaceDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003557 LazySymbols.insert(ID->getIdentifier());
3558
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003559 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003560
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003561 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003562 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003563 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003564 ObjCTypes.ClassPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003565 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003566 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3567 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003568 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003569 }
3570
Daniel Dunbarc76493a2009-11-29 21:23:36 +00003571 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003572}
3573
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003574llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3575 bool lvalue) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003576 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003577
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003578 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003579 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003580 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003581 ObjCTypes.SelectorPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003582 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003583 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3584 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003585 4, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003586 }
3587
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003588 if (lvalue)
3589 return Entry;
Daniel Dunbarc76493a2009-11-29 21:23:36 +00003590 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003591}
3592
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00003593llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00003594 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003595
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003596 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003597 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003598 llvm::ConstantArray::get(VMContext,
3599 Ident->getNameStart()),
Daniel Dunbarcda43072010-07-29 22:57:21 +00003600 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003601 1, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003602
Owen Anderson170229f2009-07-14 23:10:40 +00003603 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003604}
3605
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003606llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3607 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3608 I = MethodDefinitions.find(MD);
3609 if (I != MethodDefinitions.end())
3610 return I->second;
3611
3612 if (MD->hasBody() && MD->getPCHLevel() > 0) {
3613 // MD isn't emitted yet because it comes from PCH.
3614 CGM.EmitTopLevelDecl(const_cast<ObjCMethodDecl*>(MD));
3615 assert(MethodDefinitions[MD] && "EmitTopLevelDecl didn't emit the method!");
3616 return MethodDefinitions[MD];
3617 }
3618
3619 return NULL;
3620}
3621
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003622/// GetIvarLayoutName - Returns a unique constant for the given
3623/// ivar layout bitmap.
3624llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003625 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson0b75f232009-07-31 20:28:54 +00003626 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003627}
3628
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003629void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003630 unsigned int BytePos,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003631 bool ForStrongLayout,
3632 bool &HasUnion) {
3633 const RecordDecl *RD = RT->getDecl();
3634 // FIXME - Use iterator.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003635 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003636 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003637 const llvm::StructLayout *RecLayout =
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003638 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003639
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003640 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3641 ForStrongLayout, HasUnion);
3642}
3643
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +00003644void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003645 const llvm::StructLayout *Layout,
3646 const RecordDecl *RD,
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003647 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003648 unsigned int BytePos, bool ForStrongLayout,
3649 bool &HasUnion) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003650 bool IsUnion = (RD && RD->isUnion());
3651 uint64_t MaxUnionIvarSize = 0;
3652 uint64_t MaxSkippedUnionIvarSize = 0;
3653 FieldDecl *MaxField = 0;
3654 FieldDecl *MaxSkippedField = 0;
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003655 FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003656 uint64_t MaxFieldOffset = 0;
3657 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003658 uint64_t LastBitfieldOrUnnamedOffset = 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003659
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003660 if (RecFields.empty())
3661 return;
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003662 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3663 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3664
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003665 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003666 FieldDecl *Field = RecFields[i];
Daniel Dunbar62b10702009-05-03 23:35:23 +00003667 uint64_t FieldOffset;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003668 if (RD) {
Daniel Dunbard51c1a62010-04-14 17:02:21 +00003669 // Note that 'i' here is actually the field index inside RD of Field,
3670 // although this dependency is hidden.
3671 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
3672 FieldOffset = RL.getFieldOffset(i) / 8;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003673 } else
Daniel Dunbar62b10702009-05-03 23:35:23 +00003674 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003675
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003676 // Skip over unnamed or bitfields
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003677 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003678 LastFieldBitfieldOrUnnamed = Field;
3679 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003680 continue;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003681 }
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003682
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003683 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003684 QualType FQT = Field->getType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003685 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003686 if (FQT->isUnionType())
3687 HasUnion = true;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003688
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003689 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003690 BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003691 ForStrongLayout, HasUnion);
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003692 continue;
3693 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003694
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003695 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003696 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003697 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003698 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003699 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003700 FQT = CArray->getElementType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003701 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3702 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003703 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003704 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003705 FQT = CArray->getElementType();
3706 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003707
3708 assert(!FQT->isUnionType() &&
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003709 "layout for array of unions not supported");
Fariborz Jahanian9a7d57d2011-01-03 19:23:18 +00003710 if (FQT->isRecordType() && ElCount) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003711 int OldIndex = IvarsInfo.size() - 1;
3712 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003713
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003714 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003715 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003716 ForStrongLayout, HasUnion);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003717
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003718 // Replicate layout information for each array element. Note that
3719 // one element is already done.
3720 uint64_t ElIx = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003721 for (int FirstIndex = IvarsInfo.size() - 1,
3722 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003723 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003724 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3725 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3726 IvarsInfo[i].ivar_size));
3727 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3728 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3729 SkipIvars[i].ivar_size));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003730 }
3731 continue;
3732 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003733 }
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003734 // At this point, we are done with Record/Union and array there of.
3735 // For other arrays we are down to its element type.
John McCall8ccfcb52009-09-24 19:53:00 +00003736 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003737
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003738 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall8ccfcb52009-09-24 19:53:00 +00003739 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3740 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003741 if (IsUnion) {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003742 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003743 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003744 MaxUnionIvarSize = UnionIvarSize;
3745 MaxField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003746 MaxFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003747 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003748 } else {
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003749 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003750 FieldSize / WordSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003751 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003752 } else if ((ForStrongLayout &&
John McCall8ccfcb52009-09-24 19:53:00 +00003753 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3754 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003755 if (IsUnion) {
Mike Stump18bb9282009-05-16 07:57:57 +00003756 // FIXME: Why the asymmetry? We divide by word size in bits on other
3757 // side.
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003758 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003759 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003760 MaxSkippedUnionIvarSize = UnionIvarSize;
3761 MaxSkippedField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003762 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003763 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003764 } else {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003765 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003766 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003767 FieldSize / ByteSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003768 }
3769 }
3770 }
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003771
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003772 if (LastFieldBitfieldOrUnnamed) {
3773 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3774 // Last field was a bitfield. Must update skip info.
3775 Expr *BitWidth = LastFieldBitfieldOrUnnamed->getBitWidth();
3776 uint64_t BitFieldSize =
3777 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
3778 GC_IVAR skivar;
3779 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3780 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3781 + ((BitFieldSize % ByteSizeInBits) != 0);
3782 SkipIvars.push_back(skivar);
3783 } else {
3784 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3785 // Last field was unnamed. Must update skip info.
3786 unsigned FieldSize
3787 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3788 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3789 FieldSize / ByteSizeInBits));
3790 }
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003791 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003792
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003793 if (MaxField)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003794 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003795 MaxUnionIvarSize));
3796 if (MaxSkippedField)
Daniel Dunbar5b743912009-05-03 23:31:46 +00003797 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003798 MaxSkippedUnionIvarSize));
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003799}
3800
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003801/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3802/// the computations and returning the layout bitmap (for ivar or blocks) in
3803/// the given argument BitMap string container. Routine reads
3804/// two containers, IvarsInfo and SkipIvars which are assumed to be
3805/// filled already by the caller.
3806llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003807 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramerabd5b902009-10-13 10:07:13 +00003808 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003809
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003810 // Build the string of skip/scan nibbles
Fariborz Jahaniance5676572009-04-24 17:15:27 +00003811 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003812 unsigned int WordSize =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003813 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003814 if (IvarsInfo[0].ivar_bytepos == 0) {
3815 WordsToSkip = 0;
3816 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003817 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003818 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3819 WordsToScan = IvarsInfo[0].ivar_size;
3820 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003821 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003822 unsigned int TailPrevGCObjC =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003823 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003824 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003825 // consecutive 'scanned' object pointers.
3826 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003827 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003828 // Skip over 'gc'able object pointer which lay over each other.
3829 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3830 continue;
3831 // Must skip over 1 or more words. We save current skip/scan values
3832 // and start a new pair.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003833 SKIP_SCAN SkScan;
3834 SkScan.skip = WordsToSkip;
3835 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003836 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003837
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003838 // Skip the hole.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003839 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3840 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003841 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003842 WordsToSkip = 0;
3843 WordsToScan = IvarsInfo[i].ivar_size;
3844 }
3845 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003846 if (WordsToScan > 0) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003847 SKIP_SCAN SkScan;
3848 SkScan.skip = WordsToSkip;
3849 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003850 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003851 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003852
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003853 if (!SkipIvars.empty()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003854 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003855 int LastByteSkipped =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003856 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003857 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003858 int LastByteScanned =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003859 IvarsInfo[LastIndex].ivar_bytepos +
3860 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003861 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramerd20ef752009-12-25 15:43:36 +00003862 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003863 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003864 SKIP_SCAN SkScan;
3865 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3866 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003867 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003868 }
3869 }
3870 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3871 // as 0xMN.
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003872 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003873 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003874 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3875 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3876 // 0xM0 followed by 0x0N detected.
3877 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3878 for (int j = i+1; j < SkipScan; j++)
3879 SkipScanIvars[j] = SkipScanIvars[j+1];
3880 --SkipScan;
3881 }
3882 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003883
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003884 // Generate the string.
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003885 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003886 unsigned char byte;
3887 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3888 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3889 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3890 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003891
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003892 // first skip big.
3893 for (unsigned int ix = 0; ix < skip_big; ix++)
3894 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003895
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003896 // next (skip small, scan)
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003897 if (skip_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003898 byte = skip_small << 4;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003899 if (scan_big > 0) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003900 byte |= 0xf;
3901 --scan_big;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003902 } else if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003903 byte |= scan_small;
3904 scan_small = 0;
3905 }
3906 BitMap += byte;
3907 }
3908 // next scan big
3909 for (unsigned int ix = 0; ix < scan_big; ix++)
3910 BitMap += (unsigned char)(0x0f);
3911 // last scan small
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003912 if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003913 byte = scan_small;
3914 BitMap += byte;
3915 }
3916 }
3917 // null terminate string.
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003918 unsigned char zero = 0;
3919 BitMap += zero;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003920
3921 llvm::GlobalVariable * Entry =
3922 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3923 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
3924 "__TEXT,__cstring,cstring_literals",
3925 1, true);
3926 return getConstantGEP(VMContext, Entry, 0, 0);
3927}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003928
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003929/// BuildIvarLayout - Builds ivar layout bitmap for the class
3930/// implementation for the __strong or __weak case.
3931/// The layout map displays which words in ivar list must be skipped
3932/// and which must be scanned by GC (see below). String is built of bytes.
3933/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3934/// of words to skip and right nibble is count of words to scan. So, each
3935/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3936/// represented by a 0x00 byte which also ends the string.
3937/// 1. when ForStrongLayout is true, following ivars are scanned:
3938/// - id, Class
3939/// - object *
3940/// - __strong anything
3941///
3942/// 2. When ForStrongLayout is false, following ivars are scanned:
3943/// - __weak anything
3944///
3945llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3946 const ObjCImplementationDecl *OMD,
3947 bool ForStrongLayout) {
3948 bool hasUnion = false;
3949
3950 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
3951 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
3952 return llvm::Constant::getNullValue(PtrTy);
3953
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003954 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003955 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003956 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003957
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003958 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003959 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3960 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3961
3962 if (RecFields.empty())
3963 return llvm::Constant::getNullValue(PtrTy);
3964
3965 SkipIvars.clear();
3966 IvarsInfo.clear();
3967
3968 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
3969 if (IvarsInfo.empty())
3970 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003971 // Sort on byte position in case we encounterred a union nested in
3972 // the ivar list.
3973 if (hasUnion && !IvarsInfo.empty())
3974 std::sort(IvarsInfo.begin(), IvarsInfo.end());
3975 if (hasUnion && !SkipIvars.empty())
3976 std::sort(SkipIvars.begin(), SkipIvars.end());
3977
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003978 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003979 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003980
3981 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003982 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003983 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar56df9772010-08-17 22:39:59 +00003984 OMD->getClassInterface()->getName().data());
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003985 const unsigned char *s = (unsigned char*)BitMap.c_str();
3986 for (unsigned i = 0; i < BitMap.size(); i++)
3987 if (!(s[i] & 0xf0))
3988 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3989 else
3990 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3991 printf("\n");
3992 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003993 return C;
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003994}
3995
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003996llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003997 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3998
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003999 // FIXME: Avoid std::string copying.
4000 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004001 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson41a75022009-08-13 21:57:51 +00004002 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbarcda43072010-07-29 22:57:21 +00004003 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004004 1, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004005
Owen Anderson170229f2009-07-14 23:10:40 +00004006 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004007}
4008
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004009// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004010llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004011 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4012}
4013
Daniel Dunbarf5c18462009-04-20 06:54:31 +00004014llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004015 std::string TypeStr;
4016 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4017
4018 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbarb036db82008-08-13 03:21:16 +00004019
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004020 if (!Entry)
4021 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00004022 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbarcda43072010-07-29 22:57:21 +00004023 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004024 1, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004025
Owen Anderson170229f2009-07-14 23:10:40 +00004026 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004027}
4028
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004029llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004030 std::string TypeStr;
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00004031 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
4032 TypeStr);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004033
4034 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4035
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004036 if (!Entry)
4037 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00004038 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbarcda43072010-07-29 22:57:21 +00004039 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004040 1, true);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004041
Owen Anderson170229f2009-07-14 23:10:40 +00004042 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004043}
4044
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004045// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004046llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004047 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004048
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004049 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004050 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004051 llvm::ConstantArray::get(VMContext,
4052 Ident->getNameStart()),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004053 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004054 1, true);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004055
Owen Anderson170229f2009-07-14 23:10:40 +00004056 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004057}
4058
4059// FIXME: Merge into a single cstring creation function.
Daniel Dunbar4932b362008-08-28 04:38:10 +00004060// FIXME: This Decl should be more precise.
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004061llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004062CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4063 const Decl *Container) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00004064 std::string TypeStr;
4065 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004066 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4067}
4068
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004069void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004070 const ObjCContainerDecl *CD,
Daniel Dunbard2386812009-10-19 01:21:19 +00004071 llvm::SmallVectorImpl<char> &Name) {
4072 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00004073 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbard2386812009-10-19 01:21:19 +00004074 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4075 << '[' << CD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004076 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbard2386812009-10-19 01:21:19 +00004077 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerb11416d2010-04-17 09:33:03 +00004078 OS << '(' << CID << ')';
Daniel Dunbard2386812009-10-19 01:21:19 +00004079 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbara94ecd22008-08-16 03:19:19 +00004080}
4081
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004082void CGObjCMac::FinishModule() {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004083 EmitModuleInfo();
4084
Daniel Dunbarc475d422008-10-29 22:36:39 +00004085 // Emit the dummy bodies for any protocols which were referenced but
4086 // never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004087 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerf56501c2009-07-17 23:57:13 +00004088 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4089 if (I->second->hasInitializer())
Daniel Dunbarc475d422008-10-29 22:36:39 +00004090 continue;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004091
Daniel Dunbarc475d422008-10-29 22:36:39 +00004092 std::vector<llvm::Constant*> Values(5);
Owen Anderson0b75f232009-07-31 20:28:54 +00004093 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004094 Values[1] = GetClassName(I->first);
Owen Anderson0b75f232009-07-31 20:28:54 +00004095 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004096 Values[3] = Values[4] =
Owen Anderson0b75f232009-07-31 20:28:54 +00004097 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004098 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004099 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarc475d422008-10-29 22:36:39 +00004100 Values));
Chris Lattnerf56501c2009-07-17 23:57:13 +00004101 CGM.AddUsedGlobal(I->second);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004102 }
4103
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004104 // Add assembler directives to add lazy undefined symbol references
4105 // for classes which are referenced but not defined. This is
4106 // important for correct linker interaction.
Daniel Dunbard027a922009-09-07 00:20:42 +00004107 //
4108 // FIXME: It would be nice if we had an LLVM construct for this.
4109 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4110 llvm::SmallString<256> Asm;
4111 Asm += CGM.getModule().getModuleInlineAsm();
4112 if (!Asm.empty() && Asm.back() != '\n')
4113 Asm += '\n';
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004114
Daniel Dunbard027a922009-09-07 00:20:42 +00004115 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbard027a922009-09-07 00:20:42 +00004116 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4117 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar07d07852009-10-18 21:17:35 +00004118 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4119 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnera299f2c2010-04-17 18:26:20 +00004120 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004121 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnera299f2c2010-04-17 18:26:20 +00004122 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004123 }
4124
4125 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4126 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4127 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4128 }
Chris Lattnera299f2c2010-04-17 18:26:20 +00004129
Daniel Dunbard027a922009-09-07 00:20:42 +00004130 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004131 }
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004132}
4133
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004134CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004135 : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00004136 ObjCTypes(cgm) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004137 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004138 ObjCABI = 2;
4139}
4140
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004141/* *** */
4142
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004143ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004144 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004145 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4146 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004147
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004148 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004149 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004150 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004151 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004152 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004153
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004154 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004155 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004156 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004157
Mike Stump18bb9282009-05-16 07:57:57 +00004158 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4159 // comes out a bit cleaner.
Daniel Dunbarb036db82008-08-13 03:21:16 +00004160 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004161 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004162
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004163 // I'm not sure I like this. The implicit coordination is a bit
4164 // gross. We should solve this in a reasonable fashion because this
4165 // is a pretty common task (match some runtime data structure with
4166 // an LLVM data structure).
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004167
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004168 // FIXME: This is leaked.
4169 // FIXME: Merge with rewriter code?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004170
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004171 // struct _objc_super {
4172 // id self;
4173 // Class cls;
4174 // }
Abramo Bagnara6150c882010-05-11 21:36:43 +00004175 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004176 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004177 SourceLocation(), SourceLocation(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004178 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004179 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004180 Ctx.getObjCIdType(), 0, 0, false));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004181 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004182 Ctx.getObjCClassType(), 0, 0, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004183 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004184
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004185 SuperCTy = Ctx.getTagDeclType(RD);
4186 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004187
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004188 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004189 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4190
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004191 // struct _prop_t {
4192 // char *name;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004193 // char *attributes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004194 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004195 PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004196 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004197 PropertyTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004198
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004199 // struct _prop_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004200 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004201 // uint32_t count_of_properties;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004202 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004203 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004204 PropertyListTy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004205 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004206 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004207 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004208 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004209 PropertyListTy);
4210 // struct _prop_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004211 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004212
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004213 // struct _objc_method {
4214 // SEL _cmd;
4215 // char *method_type;
4216 // char *_imp;
4217 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004218 MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy,
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004219 Int8PtrTy,
4220 Int8PtrTy,
4221 NULL);
4222 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004223
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004224 // struct _objc_cache *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004225 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004226 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004227 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004228}
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004229
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004230ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004231 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004232 // struct _objc_method_description {
4233 // SEL name;
4234 // char *types;
4235 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004236 MethodDescriptionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004237 llvm::StructType::get(VMContext, SelectorPtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004238 Int8PtrTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00004239 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004240 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004241 MethodDescriptionTy);
4242
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004243 // struct _objc_method_description_list {
4244 // int count;
4245 // struct _objc_method_description[1];
4246 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004247 MethodDescriptionListTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004248 llvm::StructType::get(VMContext, IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004249 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbarb036db82008-08-13 03:21:16 +00004250 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004251 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004252 MethodDescriptionListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004253
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004254 // struct _objc_method_description_list *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004255 MethodDescriptionListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004256 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004257
Daniel Dunbarb036db82008-08-13 03:21:16 +00004258 // Protocol description structures
4259
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004260 // struct _objc_protocol_extension {
4261 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4262 // struct _objc_method_description_list *optional_instance_methods;
4263 // struct _objc_method_description_list *optional_class_methods;
4264 // struct _objc_property_list *instance_properties;
4265 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004266 ProtocolExtensionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004267 llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004268 MethodDescriptionListPtrTy,
4269 MethodDescriptionListPtrTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00004270 PropertyListPtrTy,
4271 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004272 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004273 ProtocolExtensionTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004274
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004275 // struct _objc_protocol_extension *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004276 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004277
Daniel Dunbarc475d422008-10-29 22:36:39 +00004278 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarb036db82008-08-13 03:21:16 +00004279
Owen Andersonc36edfe2009-08-13 23:27:53 +00004280 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
4281 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004282
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004283 const llvm::Type *T =
Mike Stump11289f42009-09-09 15:08:12 +00004284 llvm::StructType::get(VMContext,
Owen Anderson758428f2009-08-05 23:18:46 +00004285 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004286 LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004287 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004288 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004289 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
4290
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004291 // struct _objc_protocol {
4292 // struct _objc_protocol_extension *isa;
4293 // char *protocol_name;
4294 // struct _objc_protocol **_objc_protocol_list;
4295 // struct _objc_method_description_list *instance_methods;
4296 // struct _objc_method_description_list *class_methods;
4297 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004298 T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004299 Int8PtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004300 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbarb036db82008-08-13 03:21:16 +00004301 MethodDescriptionListPtrTy,
4302 MethodDescriptionListPtrTy,
4303 NULL);
4304 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
4305
4306 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004307 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004308 ProtocolListTy);
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004309 // struct _objc_protocol_list *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004310 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004311
4312 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004313 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004314 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004315
4316 // Class description structures
4317
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004318 // struct _objc_ivar {
4319 // char *ivar_name;
4320 // char *ivar_type;
4321 // int ivar_offset;
4322 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004323 IvarTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004324 Int8PtrTy,
4325 IntTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004326 NULL);
4327 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
4328
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004329 // struct _objc_ivar_list *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004330 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004331 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004332 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004333
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004334 // struct _objc_method_list *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004335 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004336 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004337 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004338
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004339 // struct _objc_class_extension *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004340 ClassExtensionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004341 llvm::StructType::get(VMContext, IntTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004342 Int8PtrTy,
4343 PropertyListPtrTy,
4344 NULL);
4345 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004346 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004347
Owen Andersonc36edfe2009-08-13 23:27:53 +00004348 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004349
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004350 // struct _objc_class {
4351 // Class isa;
4352 // Class super_class;
4353 // char *name;
4354 // long version;
4355 // long info;
4356 // long instance_size;
4357 // struct _objc_ivar_list *ivars;
4358 // struct _objc_method_list *methods;
4359 // struct _objc_cache *cache;
4360 // struct _objc_protocol_list *protocols;
4361 // char *ivar_layout;
4362 // struct _objc_class_ext *ext;
4363 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004364 T = llvm::StructType::get(VMContext,
4365 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson9793f0e2009-07-29 22:16:19 +00004366 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004367 Int8PtrTy,
4368 LongTy,
4369 LongTy,
4370 LongTy,
4371 IvarListPtrTy,
4372 MethodListPtrTy,
4373 CachePtrTy,
4374 ProtocolListPtrTy,
4375 Int8PtrTy,
4376 ClassExtensionPtrTy,
4377 NULL);
4378 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004379
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004380 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
4381 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004382 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004383
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004384 // struct _objc_category {
4385 // char *category_name;
4386 // char *class_name;
4387 // struct _objc_method_list *instance_method;
4388 // struct _objc_method_list *class_method;
4389 // uint32_t size; // sizeof(struct _objc_category)
4390 // struct _objc_property_list *instance_properties;// category's @property
4391 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004392 CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004393 Int8PtrTy,
4394 MethodListPtrTy,
4395 MethodListPtrTy,
4396 ProtocolListPtrTy,
4397 IntTy,
4398 PropertyListPtrTy,
4399 NULL);
4400 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
4401
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004402 // Global metadata structures
4403
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004404 // struct _objc_symtab {
4405 // long sel_ref_cnt;
4406 // SEL *refs;
4407 // short cls_def_cnt;
4408 // short cat_def_cnt;
4409 // char *defs[cls_def_cnt + cat_def_cnt];
4410 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004411 SymtabTy = llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004412 SelectorPtrTy,
4413 ShortTy,
4414 ShortTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004415 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004416 NULL);
4417 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004418 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004419
Fariborz Jahanianeee54df2009-01-22 00:37:21 +00004420 // struct _objc_module {
4421 // long version;
4422 // long size; // sizeof(struct _objc_module)
4423 // char *name;
4424 // struct _objc_symtab* symtab;
4425 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004426 ModuleTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004427 llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004428 LongTy,
4429 Int8PtrTy,
4430 SymtabPtrTy,
4431 NULL);
4432 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00004433
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004434
Mike Stump18bb9282009-05-16 07:57:57 +00004435 // FIXME: This is the size of the setjmp buffer and should be target
4436 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson9ff22482008-09-09 10:10:21 +00004437 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004438
Anders Carlsson9ff22482008-09-09 10:10:21 +00004439 // Exceptions
Owen Anderson9793f0e2009-07-29 22:16:19 +00004440 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004441 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004442
4443 ExceptionDataTy =
Chris Lattner5e016ae2010-06-27 07:15:29 +00004444 llvm::StructType::get(VMContext,
4445 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4446 SetJmpBufferSize),
Anders Carlsson9ff22482008-09-09 10:10:21 +00004447 StackPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004448 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson9ff22482008-09-09 10:10:21 +00004449 ExceptionDataTy);
4450
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004451}
4452
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004453ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004454 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004455 // struct _method_list_t {
4456 // uint32_t entsize; // sizeof(struct _objc_method)
4457 // uint32_t method_count;
4458 // struct _objc_method method_list[method_count];
4459 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004460 MethodListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004461 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004462 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004463 NULL);
4464 CGM.getModule().addTypeName("struct.__method_list_t",
4465 MethodListnfABITy);
4466 // struct method_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004467 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004468
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004469 // struct _protocol_t {
4470 // id isa; // NULL
4471 // const char * const protocol_name;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004472 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004473 // const struct method_list_t * const instance_methods;
4474 // const struct method_list_t * const class_methods;
4475 // const struct method_list_t *optionalInstanceMethods;
4476 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004477 // const struct _prop_list_t * properties;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004478 // const uint32_t size; // sizeof(struct _protocol_t)
4479 // const uint32_t flags; // = 0
4480 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004481
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004482 // Holder for struct _protocol_list_t *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004483 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004484
Owen Anderson758428f2009-08-05 23:18:46 +00004485 ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004486 Int8PtrTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004487 llvm::PointerType::getUnqual(
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004488 ProtocolListTyHolder),
4489 MethodListnfABIPtrTy,
4490 MethodListnfABIPtrTy,
4491 MethodListnfABIPtrTy,
4492 MethodListnfABIPtrTy,
4493 PropertyListPtrTy,
4494 IntTy,
4495 IntTy,
4496 NULL);
4497 CGM.getModule().addTypeName("struct._protocol_t",
4498 ProtocolnfABITy);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004499
4500 // struct _protocol_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004501 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004502
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004503 // struct _protocol_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004504 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004505 // struct _protocol_t *[protocol_count];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004506 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004507 ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004508 llvm::ArrayType::get(
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004509 ProtocolnfABIPtrTy, 0),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004510 NULL);
4511 CGM.getModule().addTypeName("struct._objc_protocol_list",
4512 ProtocolListnfABITy);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004513 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004514 ProtocolListnfABITy);
4515
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004516 // struct _objc_protocol_list*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004517 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004518
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004519 // struct _ivar_t {
4520 // unsigned long int *offset; // pointer to ivar offset location
4521 // char *name;
4522 // char *type;
4523 // uint32_t alignment;
4524 // uint32_t size;
4525 // }
Mike Stump11289f42009-09-09 15:08:12 +00004526 IvarnfABITy = llvm::StructType::get(VMContext,
Owen Anderson758428f2009-08-05 23:18:46 +00004527 llvm::PointerType::getUnqual(LongTy),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004528 Int8PtrTy,
4529 Int8PtrTy,
4530 IntTy,
4531 IntTy,
4532 NULL);
4533 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004534
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004535 // struct _ivar_list_t {
4536 // uint32 entsize; // sizeof(struct _ivar_t)
4537 // uint32 count;
4538 // struct _iver_t list[count];
4539 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004540 IvarListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004541 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004542 llvm::ArrayType::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004543 IvarnfABITy, 0),
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004544 NULL);
4545 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004546
Owen Anderson9793f0e2009-07-29 22:16:19 +00004547 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004548
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004549 // struct _class_ro_t {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004550 // uint32_t const flags;
4551 // uint32_t const instanceStart;
4552 // uint32_t const instanceSize;
4553 // uint32_t const reserved; // only when building for 64bit targets
4554 // const uint8_t * const ivarLayout;
4555 // const char *const name;
4556 // const struct _method_list_t * const baseMethods;
4557 // const struct _objc_protocol_list *const baseProtocols;
4558 // const struct _ivar_list_t *const ivars;
4559 // const uint8_t * const weakIvarLayout;
4560 // const struct _prop_list_t * const properties;
4561 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004562
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004563 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson758428f2009-08-05 23:18:46 +00004564 ClassRonfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004565 IntTy,
4566 IntTy,
4567 Int8PtrTy,
4568 Int8PtrTy,
4569 MethodListnfABIPtrTy,
4570 ProtocolListnfABIPtrTy,
4571 IvarListnfABIPtrTy,
4572 Int8PtrTy,
4573 PropertyListPtrTy,
4574 NULL);
4575 CGM.getModule().addTypeName("struct._class_ro_t",
4576 ClassRonfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004577
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004578 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
4579 std::vector<const llvm::Type*> Params;
4580 Params.push_back(ObjectPtrTy);
4581 Params.push_back(SelectorPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004582 ImpnfABITy = llvm::PointerType::getUnqual(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004583 llvm::FunctionType::get(ObjectPtrTy, Params, false));
4584
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004585 // struct _class_t {
4586 // struct _class_t *isa;
4587 // struct _class_t * const superclass;
4588 // void *cache;
4589 // IMP *vtable;
4590 // struct class_ro_t *ro;
4591 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004592
Owen Andersonc36edfe2009-08-13 23:27:53 +00004593 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Anderson170229f2009-07-14 23:10:40 +00004594 ClassnfABITy =
Owen Anderson758428f2009-08-05 23:18:46 +00004595 llvm::StructType::get(VMContext,
4596 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004597 llvm::PointerType::getUnqual(ClassTyHolder),
4598 CachePtrTy,
4599 llvm::PointerType::getUnqual(ImpnfABITy),
4600 llvm::PointerType::getUnqual(ClassRonfABITy),
4601 NULL);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004602 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4603
4604 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004605 ClassnfABITy);
4606
Fariborz Jahanian71394042009-01-23 23:53:38 +00004607 // LLVM for struct _class_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004608 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004609
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004610 // struct _category_t {
4611 // const char * const name;
4612 // struct _class_t *const cls;
4613 // const struct _method_list_t * const instance_methods;
4614 // const struct _method_list_t * const class_methods;
4615 // const struct _protocol_list_t * const protocols;
4616 // const struct _prop_list_t * const properties;
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004617 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004618 CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy,
Fariborz Jahanian71394042009-01-23 23:53:38 +00004619 ClassnfABIPtrTy,
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004620 MethodListnfABIPtrTy,
4621 MethodListnfABIPtrTy,
4622 ProtocolListnfABIPtrTy,
4623 PropertyListPtrTy,
4624 NULL);
4625 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004626
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004627 // New types for nonfragile abi messaging.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004628 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4629 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004630
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004631 // MessageRefTy - LLVM for:
4632 // struct _message_ref_t {
4633 // IMP messenger;
4634 // SEL name;
4635 // };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004636
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004637 // First the clang type for struct _message_ref_t
Abramo Bagnara6150c882010-05-11 21:36:43 +00004638 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004639 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004640 SourceLocation(), SourceLocation(),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004641 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004642 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004643 Ctx.VoidPtrTy, 0, 0, false));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004644 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004645 Ctx.getObjCSelType(), 0, 0, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004646 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004647
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004648 MessageRefCTy = Ctx.getTagDeclType(RD);
4649 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4650 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004651
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004652 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004653 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004654
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004655 // SuperMessageRefTy - LLVM for:
4656 // struct _super_message_ref_t {
4657 // SUPER_IMP messenger;
4658 // SEL name;
4659 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004660 SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy,
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004661 SelectorPtrTy,
4662 NULL);
4663 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004664
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004665 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004666 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4667
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004668
4669 // struct objc_typeinfo {
4670 // const void** vtable; // objc_ehtype_vtable + 2
4671 // const char* name; // c++ typeinfo string
4672 // Class cls;
4673 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004674 EHTypeTy = llvm::StructType::get(VMContext,
4675 llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004676 Int8PtrTy,
4677 ClassnfABIPtrTy,
4678 NULL);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00004679 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004680 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004681}
4682
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004683llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004684 FinishNonFragileABIModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004685
Fariborz Jahanian71394042009-01-23 23:53:38 +00004686 return NULL;
4687}
4688
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004689void CGObjCNonFragileABIMac::AddModuleClassList(const
4690 std::vector<llvm::GlobalValue*>
4691 &Container,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004692 const char *SymbolName,
4693 const char *SectionName) {
4694 unsigned NumClasses = Container.size();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004695
Daniel Dunbar19573e72009-05-15 21:48:48 +00004696 if (!NumClasses)
4697 return;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004698
Daniel Dunbar19573e72009-05-15 21:48:48 +00004699 std::vector<llvm::Constant*> Symbols(NumClasses);
4700 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00004701 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar19573e72009-05-15 21:48:48 +00004702 ObjCTypes.Int8PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004703 llvm::Constant* Init =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004704 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004705 NumClasses),
4706 Symbols);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004707
Daniel Dunbar19573e72009-05-15 21:48:48 +00004708 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00004709 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004710 llvm::GlobalValue::InternalLinkage,
4711 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00004712 SymbolName);
Daniel Dunbar710cb202010-04-25 20:39:32 +00004713 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar19573e72009-05-15 21:48:48 +00004714 GV->setSection(SectionName);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004715 CGM.AddUsedGlobal(GV);
Daniel Dunbar19573e72009-05-15 21:48:48 +00004716}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004717
Fariborz Jahanian71394042009-01-23 23:53:38 +00004718void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4719 // nonfragile abi has no module definition.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004720
Daniel Dunbar19573e72009-05-15 21:48:48 +00004721 // Build list of all implemented class addresses in array
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004722 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004723 AddModuleClassList(DefinedClasses,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004724 "\01L_OBJC_LABEL_CLASS_$",
4725 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahanian67260552009-11-17 21:37:35 +00004726
Fariborz Jahanian67260552009-11-17 21:37:35 +00004727 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4728 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4729 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4730 continue;
4731 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004732 }
4733
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004734 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4735 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4736 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4737 continue;
4738 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4739 }
Fariborz Jahanian67260552009-11-17 21:37:35 +00004740
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004741 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004742 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4743 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004744
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004745 // Build list of all implemented category addresses in array
4746 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004747 AddModuleClassList(DefinedCategories,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004748 "\01L_OBJC_LABEL_CATEGORY_$",
4749 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004750 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004751 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4752 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004753
Daniel Dunbar5e639272010-04-25 20:39:01 +00004754 EmitImageInfo();
Fariborz Jahanian71394042009-01-23 23:53:38 +00004755}
4756
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004757/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004758/// NonLegacyDispatchMethods; false otherwise. What this means is that
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004759/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004760/// message dispatch call for all the rest.
4761///
4762bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004763 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4764 default:
4765 assert(0 && "Invalid dispatch method!");
4766 case CodeGenOptions::Legacy:
Daniel Dunbarca5e3eb2010-02-01 21:07:33 +00004767 return true;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004768 case CodeGenOptions::NonLegacy:
Fariborz Jahaniandfb39832010-04-19 17:53:30 +00004769 return false;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004770 case CodeGenOptions::Mixed:
4771 break;
4772 }
4773
4774 // If so, see whether this selector is in the white-list of things which must
4775 // use the new dispatch convention. We lazily build a dense set for this.
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004776 if (NonLegacyDispatchMethods.empty()) {
4777 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4778 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4779 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4780 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4781 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4782 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4783 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4784 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4785 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4786 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004787
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004788 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4789 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4790 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4791 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4792 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4793 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4794 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4795 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004796 // "countByEnumeratingWithState:objects:count"
Fariborz Jahanian18801362009-05-13 16:19:02 +00004797 IdentifierInfo *KeyIdents[] = {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004798 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4799 &CGM.getContext().Idents.get("objects"),
4800 &CGM.getContext().Idents.get("count")
Fariborz Jahanian18801362009-05-13 16:19:02 +00004801 };
4802 NonLegacyDispatchMethods.insert(
4803 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004804 }
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004805
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004806 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004807}
4808
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004809// Metadata flags
4810enum MetaDataDlags {
4811 CLS = 0x0,
4812 CLS_META = 0x1,
4813 CLS_ROOT = 0x2,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004814 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004815 CLS_EXCEPTION = 0x20
4816};
4817/// BuildClassRoTInitializer - generate meta-data for:
4818/// struct _class_ro_t {
4819/// uint32_t const flags;
4820/// uint32_t const instanceStart;
4821/// uint32_t const instanceSize;
4822/// uint32_t const reserved; // only when building for 64bit targets
4823/// const uint8_t * const ivarLayout;
4824/// const char *const name;
4825/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004826/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004827/// const struct _ivar_list_t *const ivars;
4828/// const uint8_t * const weakIvarLayout;
4829/// const struct _prop_list_t * const properties;
4830/// }
4831///
4832llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004833 unsigned flags,
4834 unsigned InstanceStart,
4835 unsigned InstanceSize,
4836 const ObjCImplementationDecl *ID) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004837 std::string ClassName = ID->getNameAsString();
4838 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004839 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4840 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4841 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004842 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004843 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4844 : BuildIvarLayout(ID, true);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004845 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004846 // const struct _method_list_t * const baseMethods;
4847 std::vector<llvm::Constant*> Methods;
4848 std::string MethodListName("\01l_OBJC_$_");
4849 if (flags & CLS_META) {
4850 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004851 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004852 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004853 // Class methods should always be defined.
4854 Methods.push_back(GetMethodConstant(*i));
4855 }
4856 } else {
4857 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004858 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004859 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004860 // Instance methods should always be defined.
4861 Methods.push_back(GetMethodConstant(*i));
4862 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004863 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004864 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004865 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004866
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004867 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4868 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004869
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004870 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4871 if (llvm::Constant *C = GetMethodConstant(MD))
4872 Methods.push_back(C);
4873 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4874 if (llvm::Constant *C = GetMethodConstant(MD))
4875 Methods.push_back(C);
4876 }
4877 }
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004878 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004879 Values[ 5] = EmitMethodList(MethodListName,
4880 "__DATA, __objc_const", Methods);
4881
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004882 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4883 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004884 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004885 + OID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00004886 OID->all_referenced_protocol_begin(),
4887 OID->all_referenced_protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004888
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004889 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004890 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004891 else
4892 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004893 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4894 : BuildIvarLayout(ID, false);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004895 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004896 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004897 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004898 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4899 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004900 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004901 Values);
4902 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004903 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4904 llvm::GlobalValue::InternalLinkage,
4905 Init,
4906 (flags & CLS_META) ?
4907 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4908 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004909 CLASS_RO_GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004910 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00004911 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004912 return CLASS_RO_GV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004913
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004914}
4915
4916/// BuildClassMetaData - This routine defines that to-level meta-data
4917/// for the given ClassName for:
4918/// struct _class_t {
4919/// struct _class_t *isa;
4920/// struct _class_t * const superclass;
4921/// void *cache;
4922/// IMP *vtable;
4923/// struct class_ro_t *ro;
4924/// }
4925///
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004926llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004927 std::string &ClassName,
4928 llvm::Constant *IsAGV,
4929 llvm::Constant *SuperClassGV,
4930 llvm::Constant *ClassRoGV,
4931 bool HiddenVisibility) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004932 std::vector<llvm::Constant*> Values(5);
4933 Values[0] = IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004934 Values[1] = SuperClassGV;
4935 if (!Values[1])
4936 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004937 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4938 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4939 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004940 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004941 Values);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004942 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4943 GV->setInitializer(Init);
Fariborz Jahanian04087232009-01-31 01:07:39 +00004944 GV->setSection("__DATA, __objc_data");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004945 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004946 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian82208252009-01-31 00:59:10 +00004947 if (HiddenVisibility)
4948 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004949 return GV;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004950}
4951
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004952bool
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00004953CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004954 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004955}
4956
Daniel Dunbar961202372009-05-03 12:57:56 +00004957void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00004958 uint32_t &InstanceStart,
4959 uint32_t &InstanceSize) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004960 const ASTRecordLayout &RL =
Daniel Dunbar9252ee12009-05-04 21:26:30 +00004961 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004962
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004963 // InstanceSize is really instance end.
Ken Dyckd5090c12011-02-11 02:20:09 +00004964 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004965
4966 // If there are no fields, the start is the same as the end.
4967 if (!RL.getFieldCount())
4968 InstanceStart = InstanceSize;
4969 else
4970 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbar554fd792009-04-19 23:41:48 +00004971}
4972
Fariborz Jahanian71394042009-01-23 23:53:38 +00004973void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4974 std::string ClassName = ID->getNameAsString();
4975 if (!ObjCEmptyCacheVar) {
4976 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004977 CGM.getModule(),
4978 ObjCTypes.CacheTy,
4979 false,
4980 llvm::GlobalValue::ExternalLinkage,
4981 0,
4982 "_objc_empty_cache");
4983
Fariborz Jahanian71394042009-01-23 23:53:38 +00004984 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004985 CGM.getModule(),
4986 ObjCTypes.ImpnfABITy,
4987 false,
4988 llvm::GlobalValue::ExternalLinkage,
4989 0,
4990 "_objc_empty_vtable");
Fariborz Jahanian71394042009-01-23 23:53:38 +00004991 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004992 assert(ID->getClassInterface() &&
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004993 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbare3f5cfc2009-04-20 20:18:54 +00004994 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004995 uint32_t InstanceStart =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00004996 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004997 uint32_t InstanceSize = InstanceStart;
4998 uint32_t flags = CLS_META;
Daniel Dunbar15894b72009-04-07 05:48:37 +00004999 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
5000 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005001
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005002 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005003
5004 bool classIsHidden =
John McCall457a04e2010-10-22 21:05:15 +00005005 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahanian82208252009-01-31 00:59:10 +00005006 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005007 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00005008 if (ID->getNumIvarInitializers())
5009 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005010 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005011 // class is root
5012 flags |= CLS_ROOT;
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005013 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005014 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005015 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005016 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005017 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5018 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5019 Root = Super;
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005020 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00005021 if (Root->hasAttr<WeakImportAttr>())
5022 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005023 // work on super class metadata symbol.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005024 std::string SuperClassName =
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00005025 ObjCMetaClassName +
5026 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005027 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahanian67260552009-11-17 21:37:35 +00005028 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
5029 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005030 }
5031 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5032 InstanceStart,
5033 InstanceSize,ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005034 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005035 llvm::GlobalVariable *MetaTClass =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005036 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5037 classIsHidden);
Fariborz Jahanian67260552009-11-17 21:37:35 +00005038 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar15894b72009-04-07 05:48:37 +00005039
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005040 // Metadata for the class
5041 flags = CLS;
Fariborz Jahanian82208252009-01-31 00:59:10 +00005042 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005043 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00005044 if (ID->getNumIvarInitializers())
5045 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005046
Douglas Gregor78bd61f2009-06-18 16:11:24 +00005047 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005048 flags |= CLS_EXCEPTION;
5049
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005050 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005051 flags |= CLS_ROOT;
5052 SuperClassGV = 0;
Chris Lattnerb433b272009-04-19 06:02:28 +00005053 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005054 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005055 std::string RootClassName =
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005056 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005057 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahanian67260552009-11-17 21:37:35 +00005058 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
5059 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005060 }
Daniel Dunbar961202372009-05-03 12:57:56 +00005061 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005062 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005063 InstanceStart,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005064 InstanceSize,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005065 ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005066
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005067 TClassName = ObjCClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005068 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005069 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5070 classIsHidden);
Fariborz Jahanian279abd32009-01-30 20:55:31 +00005071 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005072
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005073 // Determine if this class is also "non-lazy".
5074 if (ImplementationIsNonLazy(ID))
5075 DefinedNonLazyClasses.push_back(ClassMD);
5076
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005077 // Force the definition of the EHType if necessary.
5078 if (flags & CLS_EXCEPTION)
5079 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanian71394042009-01-23 23:53:38 +00005080}
5081
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005082/// GenerateProtocolRef - This routine is called to generate code for
5083/// a protocol reference expression; as in:
5084/// @code
5085/// @protocol(Proto1);
5086/// @endcode
5087/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5088/// which will hold address of the protocol meta-data.
5089///
5090llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005091 const ObjCProtocolDecl *PD) {
5092
Fariborz Jahanian464423d2009-04-10 18:47:34 +00005093 // This routine is called for @protocol only. So, we must build definition
5094 // of protocol's meta-data (not a reference to it!)
5095 //
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005096 llvm::Constant *Init =
5097 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
5098 ObjCTypes.ExternalProtocolPtrTy);
5099
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005100 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar56df9772010-08-17 22:39:59 +00005101 ProtocolName += PD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005102
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005103 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5104 if (PTGV)
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005105 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005106 PTGV = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005107 CGM.getModule(),
5108 Init->getType(), false,
5109 llvm::GlobalValue::WeakAnyLinkage,
5110 Init,
5111 ProtocolName);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005112 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5113 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005114 CGM.AddUsedGlobal(PTGV);
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005115 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005116}
5117
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005118/// GenerateCategory - Build metadata for a category implementation.
5119/// struct _category_t {
5120/// const char * const name;
5121/// struct _class_t *const cls;
5122/// const struct _method_list_t * const instance_methods;
5123/// const struct _method_list_t * const class_methods;
5124/// const struct _protocol_list_t * const protocols;
5125/// const struct _prop_list_t * const properties;
5126/// }
5127///
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005128void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005129 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005130 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005131 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5132 "_$_" + OCD->getNameAsString());
5133 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar15894b72009-04-07 05:48:37 +00005134 Interface->getNameAsString());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005135
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005136 std::vector<llvm::Constant*> Values(6);
5137 Values[0] = GetClassName(OCD->getIdentifier());
5138 // meta-class entry symbol
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005139 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanian3ad8dcf2009-11-17 22:02:21 +00005140 if (Interface->hasAttr<WeakImportAttr>())
5141 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5142
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005143 Values[1] = ClassGV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005144 std::vector<llvm::Constant*> Methods;
5145 std::string MethodListName(Prefix);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005146 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005147 "_$_" + OCD->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005148
5149 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005150 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005151 // Instance methods should always be defined.
5152 Methods.push_back(GetMethodConstant(*i));
5153 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005154
5155 Values[2] = EmitMethodList(MethodListName,
5156 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005157 Methods);
5158
5159 MethodListName = Prefix;
5160 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5161 OCD->getNameAsString();
5162 Methods.clear();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005163 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005164 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005165 // Class methods should always be defined.
5166 Methods.push_back(GetMethodConstant(*i));
5167 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005168
5169 Values[3] = EmitMethodList(MethodListName,
5170 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005171 Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005172 const ObjCCategoryDecl *Category =
Fariborz Jahanian066347e2009-01-28 22:18:42 +00005173 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005174 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005175 llvm::SmallString<256> ExtName;
5176 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5177 << OCD->getName();
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005178 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005179 + Interface->getName() + "_$_"
5180 + Category->getName(),
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005181 Category->protocol_begin(),
5182 Category->protocol_end());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005183 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5184 OCD, Category, ObjCTypes);
Mike Stump658fe022009-07-30 22:28:39 +00005185 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00005186 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5187 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005188 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005189
5190 llvm::Constant *Init =
5191 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005192 Values);
5193 llvm::GlobalVariable *GCATV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005194 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005195 false,
5196 llvm::GlobalValue::InternalLinkage,
5197 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005198 ExtCatName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005199 GCATV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005200 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005201 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005202 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005203 DefinedCategories.push_back(GCATV);
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005204
5205 // Determine if this category is also "non-lazy".
5206 if (ImplementationIsNonLazy(OCD))
5207 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005208}
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005209
5210/// GetMethodConstant - Return a struct objc_method constant for the
5211/// given method if it has been defined. The result is null if the
5212/// method has not been defined. The return value has type MethodPtrTy.
5213llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005214 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00005215 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005216 if (!Fn)
5217 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005218
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005219 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005220 Method[0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00005221 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005222 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005223 Method[1] = GetMethodVarType(MD);
Owen Andersonade90fd2009-07-29 18:54:39 +00005224 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005225 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005226}
5227
5228/// EmitMethodList - Build meta-data for method declarations
5229/// struct _method_list_t {
5230/// uint32_t entsize; // sizeof(struct _objc_method)
5231/// uint32_t method_count;
5232/// struct _objc_method method_list[method_count];
5233/// }
5234///
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005235llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
5236 const char *Section,
5237 const ConstantVector &Methods) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005238 // Return null for empty list.
5239 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005240 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005241
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005242 std::vector<llvm::Constant*> Values(3);
5243 // sizeof(struct _objc_method)
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005244 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005245 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005246 // method_count
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005247 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005248 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005249 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005250 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005251 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005252
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005253 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005254 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005255 llvm::GlobalValue::InternalLinkage,
5256 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005257 Name);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005258 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005259 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005260 GV->setSection(Section);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005261 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005262 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005263 ObjCTypes.MethodListnfABIPtrTy);
5264}
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005265
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005266/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5267/// the given ivar.
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005268llvm::GlobalVariable *
5269CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5270 const ObjCIvarDecl *Ivar) {
5271 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar3f86b9c2009-05-05 00:36:57 +00005272 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregorbcced4e2009-04-09 21:40:53 +00005273 '.' + Ivar->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005274 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005275 CGM.getModule().getGlobalVariable(Name);
5276 if (!IvarOffsetGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005277 IvarOffsetGV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005278 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005279 false,
5280 llvm::GlobalValue::ExternalLinkage,
5281 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005282 Name);
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005283 return IvarOffsetGV;
5284}
5285
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005286llvm::Constant *
5287CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5288 const ObjCIvarDecl *Ivar,
5289 unsigned long int Offset) {
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005290 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005291 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005292 Offset));
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005293 IvarOffsetGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005294 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005295
Mike Stump18bb9282009-05-16 07:57:57 +00005296 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5297 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005298 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5299 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall457a04e2010-10-22 21:05:15 +00005300 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00005301 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00005302 else
Fariborz Jahanianbc3c77b2009-04-06 18:30:00 +00005303 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005304 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005305 return IvarOffsetGV;
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005306}
5307
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005308/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005309/// implementation. The return value has type
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005310/// IvarListnfABIPtrTy.
5311/// struct _ivar_t {
5312/// unsigned long int *offset; // pointer to ivar offset location
5313/// char *name;
5314/// char *type;
5315/// uint32_t alignment;
5316/// uint32_t size;
5317/// }
5318/// struct _ivar_list_t {
5319/// uint32 entsize; // sizeof(struct _ivar_t)
5320/// uint32 count;
5321/// struct _iver_t list[count];
5322/// }
5323///
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005324
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005325llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005326 const ObjCImplementationDecl *ID) {
5327
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005328 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005329
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005330 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5331 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005332
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005333 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005334
Daniel Dunbarae032262009-04-20 00:33:43 +00005335 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian63a224a2009-03-31 18:11:23 +00005336 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005337 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005338
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005339 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
5340 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005341 // Ignore unnamed bit-fields.
5342 if (!IVD->getDeclName())
5343 continue;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005344 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar961202372009-05-03 12:57:56 +00005345 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005346 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5347 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005348 const llvm::Type *FieldTy =
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005349 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005350 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005351 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005352 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005353 Align = llvm::Log2_32(Align);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005354 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbarae032262009-04-20 00:33:43 +00005355 // NOTE. Size of a bitfield does not match gcc's, because of the
5356 // way bitfields are treated special in each. But I am told that
5357 // 'size' for bitfield ivars is ignored by the runtime so it does
5358 // not matter. If it matters, there is enough info to get the
5359 // bitfield right!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005360 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005361 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005362 }
5363 // Return null for empty list.
5364 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005365 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005366 std::vector<llvm::Constant*> Values(3);
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005367 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005368 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5369 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005370 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005371 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005372 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005373 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005374 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5375 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005376 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005377 llvm::GlobalValue::InternalLinkage,
5378 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005379 Prefix + OID->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005380 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005381 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005382 GV->setSection("__DATA, __objc_const");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005383
Chris Lattnerf56501c2009-07-17 23:57:13 +00005384 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005385 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005386}
5387
5388llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005389 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005390 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005391
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005392 if (!Entry) {
5393 // We use the initializer as a marker of whether this is a forward
5394 // reference or not. At module finalization we add the empty
5395 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005396 Entry =
5397 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5398 llvm::GlobalValue::ExternalLinkage,
5399 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005400 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005401 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005402 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005403
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005404 return Entry;
5405}
5406
5407/// GetOrEmitProtocol - Generate the protocol meta-data:
5408/// @code
5409/// struct _protocol_t {
5410/// id isa; // NULL
5411/// const char * const protocol_name;
5412/// const struct _protocol_list_t * protocol_list; // super protocols
5413/// const struct method_list_t * const instance_methods;
5414/// const struct method_list_t * const class_methods;
5415/// const struct method_list_t *optionalInstanceMethods;
5416/// const struct method_list_t *optionalClassMethods;
5417/// const struct _prop_list_t * properties;
5418/// const uint32_t size; // sizeof(struct _protocol_t)
5419/// const uint32_t flags; // = 0
5420/// }
5421/// @endcode
5422///
5423
5424llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005425 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005426 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005427
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005428 // Early exit if a defining object has already been generated.
5429 if (Entry && Entry->hasInitializer())
5430 return Entry;
5431
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005432 // Construct method lists.
5433 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5434 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005435 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005436 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005437 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005438 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005439 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5440 OptInstanceMethods.push_back(C);
5441 } else {
5442 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005443 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005444 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005445
5446 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005447 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005448 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005449 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005450 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5451 OptClassMethods.push_back(C);
5452 } else {
5453 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005454 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005455 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005456
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005457 std::vector<llvm::Constant*> Values(10);
5458 // isa is NULL
Owen Anderson0b75f232009-07-31 20:28:54 +00005459 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005460 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005461 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5462 PD->protocol_begin(),
5463 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005464
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005465 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005466 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005467 "__DATA, __objc_const",
5468 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005469 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005470 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005471 "__DATA, __objc_const",
5472 ClassMethods);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005473 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005474 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005475 "__DATA, __objc_const",
5476 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005477 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005478 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005479 "__DATA, __objc_const",
5480 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005481 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005482 0, PD, ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005483 uint32_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005484 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005485 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0b75f232009-07-31 20:28:54 +00005486 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005487 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005488 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005489
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005490 if (Entry) {
5491 // Already created, fix the linkage and update the initializer.
Mike Stumpa6ca3342009-03-07 16:33:28 +00005492 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005493 Entry->setInitializer(Init);
5494 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005495 Entry =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005496 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5497 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5498 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005499 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005500 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005501 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005502 }
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005503 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005504 CGM.AddUsedGlobal(Entry);
5505
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005506 // Use this protocol meta-data to build protocol list table in section
5507 // __DATA, __objc_protolist
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005508 llvm::GlobalVariable *PTGV =
5509 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5510 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5511 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005512 PTGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005513 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbarb25452a2009-04-15 02:56:18 +00005514 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005515 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005516 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005517 return Entry;
5518}
5519
5520/// EmitProtocolList - Generate protocol list meta-data:
5521/// @code
5522/// struct _protocol_list_t {
5523/// long protocol_count; // Note, this is 32/64 bit
5524/// struct _protocol_t[protocol_count];
5525/// }
5526/// @endcode
5527///
5528llvm::Constant *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005529CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5530 ObjCProtocolDecl::protocol_iterator begin,
5531 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005532 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005533
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005534 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005535 if (begin == end)
Owen Anderson0b75f232009-07-31 20:28:54 +00005536 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005537
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005538 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005539 llvm::SmallString<256> TmpName;
5540 Name.toVector(TmpName);
5541 llvm::GlobalVariable *GV =
5542 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005543 if (GV)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005544 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005545
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005546 for (; begin != end; ++begin)
5547 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5548
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005549 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00005550 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005551 ObjCTypes.ProtocolnfABIPtrTy));
5552
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005553 std::vector<llvm::Constant*> Values(2);
Owen Anderson170229f2009-07-14 23:10:40 +00005554 Values[0] =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005555 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005556 Values[1] =
Owen Anderson47034e12009-07-28 18:33:04 +00005557 llvm::ConstantArray::get(
Owen Anderson9793f0e2009-07-29 22:16:19 +00005558 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005559 ProtocolRefs.size()),
5560 ProtocolRefs);
5561
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005562 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005563 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005564 llvm::GlobalValue::InternalLinkage,
5565 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005566 Name);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005567 GV->setSection("__DATA, __objc_const");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005568 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005569 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerf56501c2009-07-17 23:57:13 +00005570 CGM.AddUsedGlobal(GV);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005571 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005572 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005573}
5574
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005575/// GetMethodDescriptionConstant - This routine build following meta-data:
5576/// struct _objc_method {
5577/// SEL _cmd;
5578/// char *method_type;
5579/// char *_imp;
5580/// }
5581
5582llvm::Constant *
5583CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5584 std::vector<llvm::Constant*> Desc(3);
Owen Anderson170229f2009-07-14 23:10:40 +00005585 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005586 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5587 ObjCTypes.SelectorPtrTy);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005588 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005589 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00005590 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005591 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005592}
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005593
5594/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5595/// This code gen. amounts to generating code for:
5596/// @code
5597/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5598/// @encode
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005599///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00005600LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall96fa4842010-05-17 21:00:27 +00005601 CodeGen::CodeGenFunction &CGF,
5602 QualType ObjectTy,
5603 llvm::Value *BaseValue,
5604 const ObjCIvarDecl *Ivar,
5605 unsigned CVRQualifiers) {
5606 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00005607 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5608 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005609}
5610
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005611llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005612 CodeGen::CodeGenFunction &CGF,
5613 const ObjCInterfaceDecl *Interface,
5614 const ObjCIvarDecl *Ivar) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005615 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005616}
5617
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005618CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005619 CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005620 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005621 QualType ResultType,
5622 Selector Sel,
5623 llvm::Value *Receiver,
5624 QualType Arg0Ty,
5625 bool IsSuper,
Fariborz Jahaniancf7f66f2011-03-01 17:28:13 +00005626 const CallArgList &CallArgs,
5627 const ObjCMethodDecl *Method) {
Mike Stump18bb9282009-05-16 07:57:57 +00005628 // FIXME. Even though IsSuper is passes. This function doese not handle calls
5629 // to 'super' receivers.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005630 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005631 llvm::Value *Arg0 = Receiver;
5632 if (!IsSuper)
5633 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005634
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005635 // Find the message function name.
Mike Stump18bb9282009-05-16 07:57:57 +00005636 // FIXME. This is too much work to get the ABI-specific result type needed to
5637 // find the message name.
John McCall2da83a32010-02-26 00:48:12 +00005638 const CGFunctionInfo &FnInfo
Rafael Espindolac50c27c2010-03-30 20:24:48 +00005639 = Types.getFunctionInfo(ResultType, CallArgList(),
5640 FunctionType::ExtInfo());
Fariborz Jahaniane29b4f02009-04-30 23:08:58 +00005641 llvm::Constant *Fn = 0;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005642 std::string Name("\01l_");
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00005643 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
John McCallc5799442011-02-26 09:48:59 +00005644 EmitNullReturnInitialization(CGF, Return, ResultType);
Chris Lattner396639d2010-08-18 16:09:06 +00005645 if (IsSuper) {
5646 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5647 Name += "objc_msgSendSuper2_stret_fixup";
5648 } else {
5649 Fn = ObjCTypes.getMessageSendStretFixupFn();
5650 Name += "objc_msgSend_stret_fixup";
5651 }
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00005652 } else if (!IsSuper && CGM.ReturnTypeUsesFPRet(ResultType)) {
5653 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5654 Name += "objc_msgSend_fpret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00005655 } else {
Chris Lattner396639d2010-08-18 16:09:06 +00005656 if (IsSuper) {
5657 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
5658 Name += "objc_msgSendSuper2_fixup";
5659 } else {
5660 Fn = ObjCTypes.getMessageSendFixupFn();
5661 Name += "objc_msgSend_fixup";
5662 }
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005663 }
Fariborz Jahaniane29b4f02009-04-30 23:08:58 +00005664 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005665 Name += '_';
5666 std::string SelName(Sel.getAsString());
5667 // Replace all ':' in selector name with '_' ouch!
Mike Stump11289f42009-09-09 15:08:12 +00005668 for (unsigned i = 0; i < SelName.size(); i++)
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005669 if (SelName[i] == ':')
5670 SelName[i] = '_';
5671 Name += SelName;
5672 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5673 if (!GV) {
Daniel Dunbare60aa052009-04-15 19:03:14 +00005674 // Build message ref table entry.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005675 std::vector<llvm::Constant*> Values(2);
5676 Values[0] = Fn;
5677 Values[1] = GetMethodVarName(Sel);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005678 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005679 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stumpa6ca3342009-03-07 16:33:28 +00005680 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005681 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005682 Name);
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005683 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar24645c92009-04-15 19:04:46 +00005684 GV->setAlignment(16);
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005685 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005686 }
5687 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005688
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005689 CallArgList ActualArgs;
5690 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005691 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005692 ObjCTypes.MessageRefCPtrTy));
5693 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
John McCallab26cfa2010-02-05 21:31:56 +00005694 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00005695 FunctionType::ExtInfo());
Fariborz Jahanian4e87c832009-02-05 01:13:09 +00005696 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5697 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahaniancf7f66f2011-03-01 17:28:13 +00005698 const llvm::FunctionType *FTy =
5699 Types.GetFunctionType(FnInfo1, Method ? Method->isVariadic() : false);
Fariborz Jahanian4e87c832009-02-05 01:13:09 +00005700 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Anderson9793f0e2009-07-29 22:16:19 +00005701 llvm::PointerType::getUnqual(FTy));
John McCall78a15112010-05-22 01:48:05 +00005702 return CGF.EmitCall(FnInfo1, Callee, Return, ActualArgs);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005703}
5704
5705/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005706CodeGen::RValue
5707CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005708 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005709 QualType ResultType,
5710 Selector Sel,
5711 llvm::Value *Receiver,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005712 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00005713 const ObjCInterfaceDecl *Class,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005714 const ObjCMethodDecl *Method) {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005715 return LegacyDispatchedSelector(Sel)
John McCall78a15112010-05-22 01:48:05 +00005716 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5717 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005718 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005719 false, CallArgs, Method, ObjCTypes)
John McCall78a15112010-05-22 01:48:05 +00005720 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005721 Receiver, CGF.getContext().getObjCIdType(),
Fariborz Jahaniancf7f66f2011-03-01 17:28:13 +00005722 false, CallArgs, Method);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005723}
5724
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005725llvm::GlobalVariable *
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005726CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005727 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5728
Daniel Dunbara6468342009-03-02 05:18:14 +00005729 if (!GV) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005730 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005731 false, llvm::GlobalValue::ExternalLinkage,
5732 0, Name);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005733 }
5734
5735 return GV;
5736}
5737
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005738llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5739 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005740 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005741
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005742 if (!Entry) {
Daniel Dunbar15894b72009-04-07 05:48:37 +00005743 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005744 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005745 Entry =
5746 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005747 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005748 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005749 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005750 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005751 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005752 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005753 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005754 CGM.AddUsedGlobal(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005755 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005756
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005757 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005758}
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005759
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005760llvm::Value *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005761CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005762 const ObjCInterfaceDecl *ID) {
5763 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005764
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005765 if (!Entry) {
5766 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5767 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005768 Entry =
5769 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005770 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005771 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005772 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005773 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005774 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005775 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005776 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005777 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005778 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005779
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005780 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005781}
5782
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005783/// EmitMetaClassRef - Return a Value * of the address of _class_t
5784/// meta-data
5785///
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005786llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5787 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005788 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5789 if (Entry)
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005790 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005791
Daniel Dunbar15894b72009-04-07 05:48:37 +00005792 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005793 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005794 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005795 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005796 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005797 MetaClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005798 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005799 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005800 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005801 ObjCTypes.ClassnfABIPtrTy));
5802
Daniel Dunbare60aa052009-04-15 19:03:14 +00005803 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005804 CGM.AddUsedGlobal(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005805
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005806 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005807}
5808
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005809/// GetClass - Return a reference to the class for the given interface
5810/// decl.
5811llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5812 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian95ace552009-11-17 22:42:00 +00005813 if (ID->hasAttr<WeakImportAttr>()) {
5814 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5815 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5816 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5817 }
5818
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005819 return EmitClassRef(Builder, ID);
5820}
5821
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005822/// Generates a message send where the super is the receiver. This is
5823/// a message send to self with special delivery semantics indicating
5824/// which class's method should be called.
5825CodeGen::RValue
5826CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005827 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005828 QualType ResultType,
5829 Selector Sel,
5830 const ObjCInterfaceDecl *Class,
5831 bool isCategoryImpl,
5832 llvm::Value *Receiver,
5833 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005834 const CodeGen::CallArgList &CallArgs,
5835 const ObjCMethodDecl *Method) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005836 // ...
5837 // Create and init a super structure; this is a (receiver, class)
5838 // pair we will pass to objc_msgSendSuper.
5839 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00005840 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005841
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005842 llvm::Value *ReceiverAsObject =
5843 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5844 CGF.Builder.CreateStore(ReceiverAsObject,
5845 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005846
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005847 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005848 llvm::Value *Target;
5849 if (IsClassMessage) {
5850 if (isCategoryImpl) {
5851 // Message sent to "super' in a class method defined in
5852 // a category implementation.
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005853 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005854 Target = CGF.Builder.CreateStructGEP(Target, 0);
5855 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00005856 } else
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005857 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stump658fe022009-07-30 22:28:39 +00005858 } else
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005859 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005860
Mike Stump18bb9282009-05-16 07:57:57 +00005861 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5862 // ObjCTypes types.
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005863 const llvm::Type *ClassTy =
5864 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5865 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5866 CGF.Builder.CreateStore(Target,
5867 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005868
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005869 return (LegacyDispatchedSelector(Sel))
John McCall78a15112010-05-22 01:48:05 +00005870 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5871 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005872 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005873 true, CallArgs, Method, ObjCTypes)
John McCall78a15112010-05-22 01:48:05 +00005874 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005875 ObjCSuper, ObjCTypes.SuperPtrCTy,
Fariborz Jahaniancf7f66f2011-03-01 17:28:13 +00005876 true, CallArgs, Method);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005877}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005878
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005879llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005880 Selector Sel, bool lval) {
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005881 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005882
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005883 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005884 llvm::Constant *Casted =
5885 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5886 ObjCTypes.SelectorPtrTy);
5887 Entry =
5888 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5889 llvm::GlobalValue::InternalLinkage,
5890 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005891 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005892 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005893 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005894
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005895 if (lval)
5896 return Entry;
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005897 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005898}
Fariborz Jahanian06292952009-02-16 22:52:32 +00005899/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005900/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian06292952009-02-16 22:52:32 +00005901///
5902void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005903 llvm::Value *src,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005904 llvm::Value *dst,
5905 llvm::Value *ivarOffset) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005906 const llvm::Type * SrcTy = src->getType();
5907 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005908 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005909 assert(Size <= 8 && "does not support size > 8");
5910 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5911 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005912 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5913 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005914 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5915 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005916 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5917 src, dst, ivarOffset);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005918 return;
5919}
5920
5921/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5922/// objc_assign_strongCast (id src, id *dst)
5923///
5924void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005925 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005926 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005927 const llvm::Type * SrcTy = src->getType();
5928 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005929 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005930 assert(Size <= 8 && "does not support size > 8");
5931 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005932 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005933 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5934 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005935 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5936 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00005937 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005938 src, dst, "weakassign");
5939 return;
5940}
5941
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005942void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005943 CodeGen::CodeGenFunction &CGF,
5944 llvm::Value *DestPtr,
5945 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005946 llvm::Value *Size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005947 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5948 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005949 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005950 DestPtr, SrcPtr, Size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005951 return;
5952}
5953
Fariborz Jahanian06292952009-02-16 22:52:32 +00005954/// EmitObjCWeakRead - Code gen for loading value of a __weak
5955/// object: objc_read_weak (id *src)
5956///
5957llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005958 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005959 llvm::Value *AddrWeakObj) {
Eli Friedmana374b682009-03-07 03:57:15 +00005960 const llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005961 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5962 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00005963 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005964 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00005965 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005966 return read_weak;
5967}
5968
5969/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5970/// objc_assign_weak (id src, id *dst)
5971///
5972void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005973 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005974 const llvm::Type * SrcTy = src->getType();
5975 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005976 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005977 assert(Size <= 8 && "does not support size > 8");
5978 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5979 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005980 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5981 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005982 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5983 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00005984 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005985 src, dst, "weakassign");
5986 return;
5987}
5988
5989/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5990/// objc_assign_global (id src, id *dst)
5991///
5992void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00005993 llvm::Value *src, llvm::Value *dst,
5994 bool threadlocal) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005995 const llvm::Type * SrcTy = src->getType();
5996 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005997 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005998 assert(Size <= 8 && "does not support size > 8");
5999 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6000 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006001 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6002 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006003 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6004 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00006005 if (!threadlocal)
6006 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
6007 src, dst, "globalassign");
6008 else
6009 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
6010 src, dst, "threadlocalassign");
Fariborz Jahanian06292952009-02-16 22:52:32 +00006011 return;
6012}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006013
John McCallc20acd32010-07-21 00:41:47 +00006014namespace {
John McCallcda666c2010-07-21 07:22:38 +00006015 struct CallSyncExit : EHScopeStack::Cleanup {
John McCallc20acd32010-07-21 00:41:47 +00006016 llvm::Value *SyncExitFn;
6017 llvm::Value *SyncArg;
6018 CallSyncExit(llvm::Value *SyncExitFn, llvm::Value *SyncArg)
6019 : SyncExitFn(SyncExitFn), SyncArg(SyncArg) {}
6020
6021 void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
6022 CGF.Builder.CreateCall(SyncExitFn, SyncArg)->setDoesNotThrow();
6023 }
6024 };
6025}
6026
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006027void
John McCallbd309292010-07-06 01:34:17 +00006028CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6029 const ObjCAtSynchronizedStmt &S) {
6030 // Evaluate the lock operand. This should dominate the cleanup.
6031 llvm::Value *SyncArg = CGF.EmitScalarExpr(S.getSynchExpr());
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006032
John McCallbd309292010-07-06 01:34:17 +00006033 // Acquire the lock.
6034 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
6035 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
6036 ->setDoesNotThrow();
6037
6038 // Register an all-paths cleanup to release the lock.
John McCallcda666c2010-07-21 07:22:38 +00006039 CGF.EHStack.pushCleanup<CallSyncExit>(NormalAndEHCleanup,
6040 ObjCTypes.getSyncExitFn(),
6041 SyncArg);
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006042
John McCallbd309292010-07-06 01:34:17 +00006043 // Emit the body of the statement.
6044 CGF.EmitStmt(S.getSynchBody());
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006045
John McCallbd309292010-07-06 01:34:17 +00006046 // Pop the lock-release cleanup.
6047 CGF.PopCleanupBlock();
6048}
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006049
John McCallbd309292010-07-06 01:34:17 +00006050namespace {
6051 struct CatchHandler {
6052 const VarDecl *Variable;
6053 const Stmt *Body;
6054 llvm::BasicBlock *Block;
6055 llvm::Value *TypeInfo;
6056 };
John McCall5c08ab92010-07-13 22:12:14 +00006057
John McCallcda666c2010-07-21 07:22:38 +00006058 struct CallObjCEndCatch : EHScopeStack::Cleanup {
John McCall5c08ab92010-07-13 22:12:14 +00006059 CallObjCEndCatch(bool MightThrow, llvm::Value *Fn) :
6060 MightThrow(MightThrow), Fn(Fn) {}
6061 bool MightThrow;
6062 llvm::Value *Fn;
6063
6064 void Emit(CodeGenFunction &CGF, bool IsForEH) {
6065 if (!MightThrow) {
6066 CGF.Builder.CreateCall(Fn)->setDoesNotThrow();
6067 return;
6068 }
6069
6070 CGF.EmitCallOrInvoke(Fn, 0, 0);
6071 }
6072 };
John McCallbd309292010-07-06 01:34:17 +00006073}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006074
John McCall2ca705e2010-07-24 00:37:23 +00006075llvm::Constant *
6076CGObjCNonFragileABIMac::GetEHType(QualType T) {
6077 // There's a particular fixed type info for 'id'.
6078 if (T->isObjCIdType() ||
6079 T->isObjCQualifiedIdType()) {
6080 llvm::Constant *IDEHType =
6081 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6082 if (!IDEHType)
6083 IDEHType =
6084 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6085 false,
6086 llvm::GlobalValue::ExternalLinkage,
6087 0, "OBJC_EHTYPE_id");
6088 return IDEHType;
6089 }
6090
6091 // All other types should be Objective-C interface pointer types.
6092 const ObjCObjectPointerType *PT =
6093 T->getAs<ObjCObjectPointerType>();
6094 assert(PT && "Invalid @catch type.");
6095 const ObjCInterfaceType *IT = PT->getInterfaceType();
6096 assert(IT && "Invalid @catch type.");
6097 return GetInterfaceEHType(IT->getDecl(), false);
6098}
6099
John McCallbd309292010-07-06 01:34:17 +00006100void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6101 const ObjCAtTryStmt &S) {
6102 // Jump destination for falling out of catch bodies.
6103 CodeGenFunction::JumpDest Cont;
6104 if (S.getNumCatchStmts())
6105 Cont = CGF.getJumpDestInCurrentScope("eh.cont");
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006106
John McCallbd309292010-07-06 01:34:17 +00006107 CodeGenFunction::FinallyInfo FinallyInfo;
6108 if (const ObjCAtFinallyStmt *Finally = S.getFinallyStmt())
6109 FinallyInfo = CGF.EnterFinallyBlock(Finally->getFinallyBody(),
6110 ObjCTypes.getObjCBeginCatchFn(),
6111 ObjCTypes.getObjCEndCatchFn(),
6112 ObjCTypes.getExceptionRethrowFn());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006113
John McCallbd309292010-07-06 01:34:17 +00006114 llvm::SmallVector<CatchHandler, 8> Handlers;
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006115
John McCallbd309292010-07-06 01:34:17 +00006116 // Enter the catch, if there is one.
6117 if (S.getNumCatchStmts()) {
6118 for (unsigned I = 0, N = S.getNumCatchStmts(); I != N; ++I) {
6119 const ObjCAtCatchStmt *CatchStmt = S.getCatchStmt(I);
Douglas Gregor46a572b2010-04-26 16:46:50 +00006120 const VarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006121
John McCallbd309292010-07-06 01:34:17 +00006122 Handlers.push_back(CatchHandler());
6123 CatchHandler &Handler = Handlers.back();
6124 Handler.Variable = CatchDecl;
6125 Handler.Body = CatchStmt->getCatchBody();
6126 Handler.Block = CGF.createBasicBlock("catch");
6127
6128 // @catch(...) always matches.
Douglas Gregor96c79492010-04-23 22:50:49 +00006129 if (!CatchDecl) {
John McCallbd309292010-07-06 01:34:17 +00006130 Handler.TypeInfo = 0; // catch-all
6131 // Don't consider any other catches.
Douglas Gregor96c79492010-04-23 22:50:49 +00006132 break;
6133 }
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006134
John McCall2ca705e2010-07-24 00:37:23 +00006135 Handler.TypeInfo = GetEHType(CatchDecl->getType());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006136 }
John McCallbd309292010-07-06 01:34:17 +00006137
6138 EHCatchScope *Catch = CGF.EHStack.pushCatch(Handlers.size());
6139 for (unsigned I = 0, E = Handlers.size(); I != E; ++I)
6140 Catch->setHandler(I, Handlers[I].TypeInfo, Handlers[I].Block);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006141 }
John McCallbd309292010-07-06 01:34:17 +00006142
6143 // Emit the try body.
6144 CGF.EmitStmt(S.getTryBody());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006145
John McCallbd309292010-07-06 01:34:17 +00006146 // Leave the try.
6147 if (S.getNumCatchStmts())
6148 CGF.EHStack.popCatch();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006149
John McCallbd309292010-07-06 01:34:17 +00006150 // Remember where we were.
6151 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006152
John McCallbd309292010-07-06 01:34:17 +00006153 // Emit the handlers.
6154 for (unsigned I = 0, E = Handlers.size(); I != E; ++I) {
6155 CatchHandler &Handler = Handlers[I];
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006156
John McCallbd309292010-07-06 01:34:17 +00006157 CGF.EmitBlock(Handler.Block);
6158 llvm::Value *RawExn = CGF.Builder.CreateLoad(CGF.getExceptionSlot());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006159
John McCallbd309292010-07-06 01:34:17 +00006160 // Enter the catch.
6161 llvm::CallInst *Exn =
6162 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), RawExn,
6163 "exn.adjusted");
6164 Exn->setDoesNotThrow();
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006165
John McCallbd309292010-07-06 01:34:17 +00006166 // Add a cleanup to leave the catch.
John McCall5c08ab92010-07-13 22:12:14 +00006167 bool EndCatchMightThrow = (Handler.Variable == 0);
John McCallcda666c2010-07-21 07:22:38 +00006168 CGF.EHStack.pushCleanup<CallObjCEndCatch>(NormalAndEHCleanup,
6169 EndCatchMightThrow,
6170 ObjCTypes.getObjCEndCatchFn());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006171
John McCallbd309292010-07-06 01:34:17 +00006172 // Bind the catch parameter if it exists.
6173 if (const VarDecl *CatchParam = Handler.Variable) {
6174 const llvm::Type *CatchType = CGF.ConvertType(CatchParam->getType());
6175 llvm::Value *CastExn = CGF.Builder.CreateBitCast(Exn, CatchType);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006176
John McCall1c9c3fd2010-10-15 04:57:14 +00006177 CGF.EmitAutoVarDecl(*CatchParam);
John McCallbd309292010-07-06 01:34:17 +00006178 CGF.Builder.CreateStore(CastExn, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006179 }
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006180
John McCallbd309292010-07-06 01:34:17 +00006181 CGF.ObjCEHValueStack.push_back(Exn);
6182 CGF.EmitStmt(Handler.Body);
6183 CGF.ObjCEHValueStack.pop_back();
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006184
John McCallbd309292010-07-06 01:34:17 +00006185 // Leave the earlier cleanup.
6186 CGF.PopCleanupBlock();
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006187
John McCallbd309292010-07-06 01:34:17 +00006188 CGF.EmitBranchThroughCleanup(Cont);
6189 }
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006190
John McCallbd309292010-07-06 01:34:17 +00006191 // Go back to the try-statement fallthrough.
6192 CGF.Builder.restoreIP(SavedIP);
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006193
John McCallbd309292010-07-06 01:34:17 +00006194 // Pop out of the normal cleanup on the finally.
6195 if (S.getFinallyStmt())
6196 CGF.ExitFinallyBlock(FinallyInfo);
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006197
John McCallad5d61e2010-07-23 21:56:41 +00006198 if (Cont.isValid())
6199 CGF.EmitBlock(Cont.getBlock());
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006200}
6201
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006202/// EmitThrowStmt - Generate code for a throw statement.
6203void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6204 const ObjCAtThrowStmt &S) {
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006205 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall17afe452010-10-16 08:21:07 +00006206 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
John McCalld5091822010-10-16 16:34:08 +00006207 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy,
6208 "tmp");
John McCall17afe452010-10-16 08:21:07 +00006209 llvm::Value *Args[] = { Exception };
6210 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(),
6211 Args, Args+1)
6212 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006213 } else {
John McCall17afe452010-10-16 08:21:07 +00006214 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn(), 0, 0)
6215 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006216 }
6217
John McCall17afe452010-10-16 08:21:07 +00006218 CGF.Builder.CreateUnreachable();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006219 CGF.Builder.ClearInsertionPoint();
6220}
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006221
John McCall2ca705e2010-07-24 00:37:23 +00006222llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006223CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006224 bool ForDefinition) {
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006225 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006226
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006227 // If we don't need a definition, return the entry if found or check
6228 // if we use an external reference.
6229 if (!ForDefinition) {
6230 if (Entry)
6231 return Entry;
Daniel Dunbard7beeea2009-04-07 06:43:45 +00006232
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006233 // If this type (or a super class) has the __objc_exception__
6234 // attribute, emit an external reference.
Douglas Gregor78bd61f2009-06-18 16:11:24 +00006235 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006236 return Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006237 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006238 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006239 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006240 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006241 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006242 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006243
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006244 // Otherwise we need to either make a new entry or fill in the
6245 // initializer.
6246 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar15894b72009-04-07 05:48:37 +00006247 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006248 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006249 llvm::GlobalVariable *VTableGV =
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006250 CGM.getModule().getGlobalVariable(VTableName);
6251 if (!VTableGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006252 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6253 false,
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006254 llvm::GlobalValue::ExternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006255 0, VTableName);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006256
Chris Lattner5e016ae2010-06-27 07:15:29 +00006257 llvm::Value *VTableIdx =
6258 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006259
6260 std::vector<llvm::Constant*> Values(3);
Owen Andersonade90fd2009-07-29 18:54:39 +00006261 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006262 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00006263 Values[2] = GetClassGlobal(ClassName);
Owen Anderson170229f2009-07-14 23:10:40 +00006264 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00006265 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006266
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006267 if (Entry) {
6268 Entry->setInitializer(Init);
6269 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00006270 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006271 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006272 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006273 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006274 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006275 }
6276
John McCall457a04e2010-10-22 21:05:15 +00006277 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar15894b72009-04-07 05:48:37 +00006278 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar710cb202010-04-25 20:39:32 +00006279 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6280 ObjCTypes.EHTypeTy));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006281
6282 if (ForDefinition) {
6283 Entry->setSection("__DATA,__objc_const");
6284 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6285 } else {
6286 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6287 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006288
6289 return Entry;
6290}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006291
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00006292/* *** */
6293
Daniel Dunbarb036db82008-08-13 03:21:16 +00006294CodeGen::CGObjCRuntime *
6295CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbar303e2c22008-08-11 02:45:11 +00006296 return new CGObjCMac(CGM);
6297}
Fariborz Jahanian279eda62009-01-21 22:04:16 +00006298
6299CodeGen::CGObjCRuntime *
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00006300CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00006301 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00006302}