blob: 8dbd85f8b738798867ea75602fd4d3d3a147ac97 [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);
Daniel Dunbar9fd114d2009-04-22 07:32:20 +0000118 V = CGF.Builder.CreateGEP(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,
186 Params, true),
187 "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),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000197 Params, true),
198 "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,
212 true),
213 "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 Jahanian5d5ed2d2009-05-11 19:25:47 +0000224 Params, true),
225 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 Jahanian5d5ed2d2009-05-11 19:25:47 +0000235 Params, true),
236 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),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000248 Params, true),
249 "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),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000261 Params, true),
262 "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,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000724 Params, true),
725 "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,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000734 Params, true),
735 "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,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000744 Params, true),
745 "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,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000755 Params, true),
756 "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,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000766 Params, true),
767 "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,
1297 const CallArgList &CallArgs);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00001298
1299 /// GetClassGlobal - Return the global variable for the Objective-C
1300 /// class of the given name.
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00001301 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001302
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001303 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001304 /// for the given class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001305 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001306 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001307
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001308 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1309 /// for the given super class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001310 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1311 const ObjCInterfaceDecl *ID);
1312
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001313 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1314 /// meta-data
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001315 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001316 const ObjCInterfaceDecl *ID);
1317
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001318 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1319 /// the given ivar.
1320 ///
Daniel Dunbara1060522009-04-19 00:31:15 +00001321 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001322 const ObjCInterfaceDecl *ID,
1323 const ObjCIvarDecl *Ivar);
1324
Fariborz Jahanian74b77222009-02-11 20:51:17 +00001325 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1326 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001327 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1328 bool lval=false);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001329
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001330 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001331 /// interface. The return value has type EHTypePtrTy.
John McCall2ca705e2010-07-24 00:37:23 +00001332 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001333 bool ForDefinition);
Daniel Dunbar15894b72009-04-07 05:48:37 +00001334
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001335 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar15894b72009-04-07 05:48:37 +00001336 return "OBJC_METACLASS_$_";
1337 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001338
Daniel Dunbar15894b72009-04-07 05:48:37 +00001339 const char *getClassSymbolPrefix() const {
1340 return "OBJC_CLASS_$_";
1341 }
1342
Daniel Dunbar961202372009-05-03 12:57:56 +00001343 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00001344 uint32_t &InstanceStart,
1345 uint32_t &InstanceSize);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001346
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001347 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001348 Selector GetNullarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001349 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1350 return CGM.getContext().Selectors.getSelector(0, &II);
1351 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001352
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001353 Selector GetUnarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001354 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1355 return CGM.getContext().Selectors.getSelector(1, &II);
1356 }
Daniel Dunbar554fd792009-04-19 23:41:48 +00001357
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001358 /// ImplementationIsNonLazy - Check whether the given category or
1359 /// class implementation is "non-lazy".
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00001360 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001361
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001362public:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001363 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001364 // FIXME. All stubs for now!
1365 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001366
Fariborz Jahanian71394042009-01-23 23:53:38 +00001367 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001368 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001369 QualType ResultType,
1370 Selector Sel,
1371 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001372 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001373 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001374 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001375
1376 virtual CodeGen::RValue
Fariborz Jahanian71394042009-01-23 23:53:38 +00001377 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001378 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001379 QualType ResultType,
1380 Selector Sel,
1381 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001382 bool isCategoryImpl,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001383 llvm::Value *Receiver,
1384 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001385 const CallArgList &CallArgs,
1386 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001387
Fariborz Jahanian71394042009-01-23 23:53:38 +00001388 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001389 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001390
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001391 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1392 bool lvalue = false)
1393 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001394
1395 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1396 /// untyped one.
1397 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1398 const ObjCMethodDecl *Method)
1399 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001400
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00001401 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001402
Fariborz Jahanian71394042009-01-23 23:53:38 +00001403 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001404 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian097feda2009-01-30 18:58:59 +00001405 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001406
John McCall2ca705e2010-07-24 00:37:23 +00001407 virtual llvm::Constant *GetEHType(QualType T);
1408
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001409 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001410 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001411 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001412 virtual llvm::Constant *GetPropertySetFunction() {
1413 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001414 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001415
David Chisnall168b80f2010-12-26 22:13:16 +00001416 virtual llvm::Constant *GetSetStructFunction() {
1417 return ObjCTypes.getCopyStructFn();
1418 }
1419 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001420 return ObjCTypes.getCopyStructFn();
1421 }
1422
Chris Lattnerd4808922009-03-22 21:03:39 +00001423 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001424 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbard73ea8162009-02-16 18:48:45 +00001425 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001426
John McCallbd309292010-07-06 01:34:17 +00001427 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1428 const ObjCAtTryStmt &S);
1429 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1430 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001431 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlsson9ab53d12009-02-16 22:59:18 +00001432 const ObjCAtThrowStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001433 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001434 llvm::Value *AddrWeakObj);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001435 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001436 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001437 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001438 llvm::Value *src, llvm::Value *dest,
1439 bool threadlocal = false);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001440 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001441 llvm::Value *src, llvm::Value *dest,
1442 llvm::Value *ivarOffset);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001443 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001444 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001445 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1446 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001447 llvm::Value *size);
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001448 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1449 QualType ObjectTy,
1450 llvm::Value *BaseValue,
1451 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001452 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001453 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001454 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001455 const ObjCIvarDecl *Ivar);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001456};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001457
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001458} // end anonymous namespace
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001459
1460/* *** Helper Functions *** */
1461
1462/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Anderson170229f2009-07-14 23:10:40 +00001463static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001464 llvm::Constant *C,
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001465 unsigned idx0,
1466 unsigned idx1) {
1467 llvm::Value *Idxs[] = {
Owen Anderson41a75022009-08-13 21:57:51 +00001468 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1469 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001470 };
Owen Andersonade90fd2009-07-29 18:54:39 +00001471 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001472}
1473
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001474/// hasObjCExceptionAttribute - Return true if this class or any super
1475/// class has the __objc_exception__ attribute.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001476static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001477 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001478 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001479 return true;
1480 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001481 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001482 return false;
1483}
1484
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001485/* *** CGObjCMac Public Interface *** */
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001486
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001487CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00001488 ObjCTypes(cgm) {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001489 ObjCABI = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001490 EmitImageInfo();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001491}
1492
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +00001493/// GetClass - Return a reference to the class for the given interface
1494/// decl.
Daniel Dunbarcb463852008-11-01 01:53:16 +00001495llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001496 const ObjCInterfaceDecl *ID) {
1497 return EmitClassRef(Builder, ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001498}
1499
1500/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001501llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1502 bool lval) {
1503 return EmitSelector(Builder, Sel, lval);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001504}
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001505llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001506 *Method) {
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001507 return EmitSelector(Builder, Method->getSelector());
1508}
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001509
John McCall2ca705e2010-07-24 00:37:23 +00001510llvm::Constant *CGObjCMac::GetEHType(QualType T) {
1511 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1512 return 0;
1513}
1514
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001515/// Generate a constant CFString object.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001516/*
1517 struct __builtin_CFString {
1518 const int *isa; // point to __CFConstantStringClassReference
1519 int flags;
1520 const char *str;
1521 long length;
1522 };
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001523*/
1524
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001525/// or Generate a constant NSString object.
1526/*
1527 struct __builtin_NSString {
1528 const int *isa; // point to __NSConstantStringClassReference
1529 const char *str;
1530 unsigned int length;
1531 };
1532*/
1533
Fariborz Jahanian71394042009-01-23 23:53:38 +00001534llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall481e3a82010-01-23 02:40:42 +00001535 const StringLiteral *SL) {
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001536 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1537 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian50c925f2010-10-19 17:19:29 +00001538 CGM.GetAddrOfConstantString(SL));
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001539}
1540
1541/// Generates a message send where the super is the receiver. This is
1542/// a message send to self with special delivery semantics indicating
1543/// which class's method should be called.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001544CodeGen::RValue
1545CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001546 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001547 QualType ResultType,
1548 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001549 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001550 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001551 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001552 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001553 const CodeGen::CallArgList &CallArgs,
1554 const ObjCMethodDecl *Method) {
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001555 // Create and init a super structure; this is a (receiver, class)
1556 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001557 llvm::Value *ObjCSuper =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001558 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001559 llvm::Value *ReceiverAsObject =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001560 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001561 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001562 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001563
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001564 // If this is a class message the metaclass is passed as the target.
1565 llvm::Value *Target;
1566 if (IsClassMessage) {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001567 if (isCategoryImpl) {
1568 // Message sent to 'super' in a class method defined in a category
1569 // implementation requires an odd treatment.
1570 // If we are in a class method, we must retrieve the
1571 // _metaclass_ for the current class, pointed at by
1572 // the class's "isa" pointer. The following assumes that
1573 // isa" is the first ivar in a class (which it must be).
1574 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1575 Target = CGF.Builder.CreateStructGEP(Target, 0);
1576 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00001577 } else {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001578 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1579 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1580 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1581 Target = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001582 }
Fariborz Jahanianda2efb02009-11-14 02:18:31 +00001583 }
1584 else if (isCategoryImpl)
1585 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1586 else {
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001587 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1588 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1589 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001590 }
Mike Stump18bb9282009-05-16 07:57:57 +00001591 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1592 // ObjCTypes types.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001593 const llvm::Type *ClassTy =
Daniel Dunbarc722b852008-08-30 03:02:31 +00001594 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbarc475d422008-10-29 22:36:39 +00001595 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001596 CGF.Builder.CreateStore(Target,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001597 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall78a15112010-05-22 01:48:05 +00001598 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001599 EmitSelector(CGF.Builder, Sel),
1600 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001601 true, CallArgs, Method, ObjCTypes);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001602}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001603
1604/// Generate code for a message send expression.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001605CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001606 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001607 QualType ResultType,
1608 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001609 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001610 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001611 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001612 const ObjCMethodDecl *Method) {
John McCall78a15112010-05-22 01:48:05 +00001613 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001614 EmitSelector(CGF.Builder, Sel),
1615 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001616 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001617}
1618
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001619CodeGen::RValue
1620CGObjCCommonMac::EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001621 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001622 QualType ResultType,
1623 llvm::Value *Sel,
1624 llvm::Value *Arg0,
1625 QualType Arg0Ty,
1626 bool IsSuper,
1627 const CallArgList &CallArgs,
1628 const ObjCMethodDecl *Method,
1629 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc722b852008-08-30 03:02:31 +00001630 CallArgList ActualArgs;
Fariborz Jahanian969bc682009-04-24 21:07:43 +00001631 if (!IsSuper)
1632 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar41cf9de2008-09-09 01:06:48 +00001633 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001634 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbarc722b852008-08-30 03:02:31 +00001635 CGF.getContext().getObjCSelType()));
1636 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001637
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00001638 CodeGenTypes &Types = CGM.getTypes();
John McCallab26cfa2010-02-05 21:31:56 +00001639 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001640 FunctionType::ExtInfo());
Daniel Dunbardf0e62d2009-09-17 04:01:40 +00001641 const llvm::FunctionType *FTy =
1642 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001643
Anders Carlsson280e61f12010-06-21 20:59:55 +00001644 if (Method)
1645 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1646 CGM.getContext().getCanonicalType(ResultType) &&
1647 "Result type mismatch!");
1648
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001649 llvm::Constant *Fn = NULL;
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001650 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
John McCallc5799442011-02-26 09:48:59 +00001651 EmitNullReturnInitialization(CGF, Return, ResultType);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001652 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001653 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001654 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1655 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1656 : ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001657 } else {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001658 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001659 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001660 }
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001661 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCall78a15112010-05-22 01:48:05 +00001662 return CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001663}
1664
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001665static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1666 if (FQT.isObjCGCStrong())
1667 return Qualifiers::Strong;
1668
1669 if (FQT.isObjCGCWeak())
1670 return Qualifiers::Weak;
1671
1672 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1673 return Qualifiers::Strong;
1674
1675 if (const PointerType *PT = FQT->getAs<PointerType>())
1676 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1677
1678 return Qualifiers::GCNone;
1679}
1680
John McCall351762c2011-02-07 10:33:21 +00001681llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1682 const CGBlockInfo &blockInfo) {
1683 llvm::Constant *nullPtr =
Fariborz Jahanianafa3c0a2010-08-04 18:44:59 +00001684 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
John McCall351762c2011-02-07 10:33:21 +00001685
Fariborz Jahanian0aa35b92010-09-13 16:09:44 +00001686 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
John McCall351762c2011-02-07 10:33:21 +00001687 return nullPtr;
1688
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001689 bool hasUnion = false;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001690 SkipIvars.clear();
1691 IvarsInfo.clear();
1692 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
1693 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
1694
Fariborz Jahaniancfddabf2010-09-09 00:21:45 +00001695 // __isa is the first field in block descriptor and must assume by runtime's
1696 // convention that it is GC'able.
1697 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall351762c2011-02-07 10:33:21 +00001698
1699 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1700
1701 // Calculate the basic layout of the block structure.
1702 const llvm::StructLayout *layout =
1703 CGM.getTargetData().getStructLayout(blockInfo.StructureType);
1704
1705 // Ignore the optional 'this' capture: C++ objects are not assumed
1706 // to be GC'ed.
1707
1708 // Walk the captured variables.
1709 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1710 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1711 const VarDecl *variable = ci->getVariable();
1712 QualType type = variable->getType();
1713
1714 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1715
1716 // Ignore constant captures.
1717 if (capture.isConstant()) continue;
1718
1719 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1720
1721 // __block variables are passed by their descriptor address.
1722 if (ci->isByRef()) {
1723 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanian933c6722010-09-11 01:27:29 +00001724 continue;
John McCall351762c2011-02-07 10:33:21 +00001725 }
1726
1727 assert(!type->isArrayType() && "array variable should not be caught");
1728 if (const RecordType *record = type->getAs<RecordType>()) {
1729 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahanian903aba32010-08-05 21:00:25 +00001730 continue;
1731 }
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001732
John McCall351762c2011-02-07 10:33:21 +00001733 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1734 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1735
1736 if (GCAttr == Qualifiers::Strong)
1737 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1738 fieldSize / WordSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001739 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall351762c2011-02-07 10:33:21 +00001740 SkipIvars.push_back(GC_IVAR(fieldOffset,
1741 fieldSize / ByteSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001742 }
1743
1744 if (IvarsInfo.empty())
John McCall351762c2011-02-07 10:33:21 +00001745 return nullPtr;
1746
1747 // Sort on byte position; captures might not be allocated in order,
1748 // and unions can do funny things.
1749 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1750 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001751
1752 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00001753 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001754 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1755 printf("\n block variable layout for block: ");
1756 const unsigned char *s = (unsigned char*)BitMap.c_str();
1757 for (unsigned i = 0; i < BitMap.size(); i++)
1758 if (!(s[i] & 0xf0))
1759 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1760 else
1761 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1762 printf("\n");
1763 }
1764
1765 return C;
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001766}
1767
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001768llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001769 const ObjCProtocolDecl *PD) {
Daniel Dunbar7050c552008-09-04 04:33:15 +00001770 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001771 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar7050c552008-09-04 04:33:15 +00001772 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1773
Owen Andersonade90fd2009-07-29 18:54:39 +00001774 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001775 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001776}
1777
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001778void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stump18bb9282009-05-16 07:57:57 +00001779 // FIXME: We shouldn't need this, the protocol decl should contain enough
1780 // information to tell us whether this was a declaration or a definition.
Daniel Dunbarc475d422008-10-29 22:36:39 +00001781 DefinedProtocols.insert(PD->getIdentifier());
1782
1783 // If we have generated a forward reference to this protocol, emit
1784 // it now. Otherwise do nothing, the protocol objects are lazily
1785 // emitted.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001786 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbarc475d422008-10-29 22:36:39 +00001787 GetOrEmitProtocol(PD);
1788}
1789
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001790llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001791 if (DefinedProtocols.count(PD->getIdentifier()))
1792 return GetOrEmitProtocol(PD);
1793 return GetOrEmitProtocolRef(PD);
1794}
1795
Daniel Dunbarb036db82008-08-13 03:21:16 +00001796/*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001797// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1798struct _objc_protocol {
1799struct _objc_protocol_extension *isa;
1800char *protocol_name;
1801struct _objc_protocol_list *protocol_list;
1802struct _objc__method_prototype_list *instance_methods;
1803struct _objc__method_prototype_list *class_methods
1804};
Daniel Dunbarb036db82008-08-13 03:21:16 +00001805
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001806See EmitProtocolExtension().
Daniel Dunbarb036db82008-08-13 03:21:16 +00001807*/
Daniel Dunbarc475d422008-10-29 22:36:39 +00001808llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1809 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1810
1811 // Early exit if a defining object has already been generated.
1812 if (Entry && Entry->hasInitializer())
1813 return Entry;
1814
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001815 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001816 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001817 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1818
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001819 // Construct method lists.
1820 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1821 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001822 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001823 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001824 ObjCMethodDecl *MD = *i;
1825 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1826 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1827 OptInstanceMethods.push_back(C);
1828 } else {
1829 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001830 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001831 }
1832
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001833 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001834 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001835 ObjCMethodDecl *MD = *i;
1836 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1837 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1838 OptClassMethods.push_back(C);
1839 } else {
1840 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001841 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001842 }
1843
Daniel Dunbarb036db82008-08-13 03:21:16 +00001844 std::vector<llvm::Constant*> Values(5);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001845 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001846 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001847 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001848 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00001849 PD->protocol_begin(),
1850 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001851 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001852 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001853 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1854 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001855 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001856 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001857 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1858 ClassMethods);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001859 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001860 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001861
Daniel Dunbarb036db82008-08-13 03:21:16 +00001862 if (Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001863 // Already created, fix the linkage and update the initializer.
1864 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001865 Entry->setInitializer(Init);
1866 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001867 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001868 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001869 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001870 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001871 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001872 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001873 // FIXME: Is this necessary? Why only for protocol?
1874 Entry->setAlignment(4);
1875 }
Chris Lattnerf56501c2009-07-17 23:57:13 +00001876 CGM.AddUsedGlobal(Entry);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001877
1878 return Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001879}
1880
Daniel Dunbarc475d422008-10-29 22:36:39 +00001881llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001882 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1883
1884 if (!Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001885 // We use the initializer as a marker of whether this is a forward
1886 // reference or not. At module finalization we add the empty
1887 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001888 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001889 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarc475d422008-10-29 22:36:39 +00001890 llvm::GlobalValue::ExternalLinkage,
1891 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001892 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001893 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001894 // FIXME: Is this necessary? Why only for protocol?
1895 Entry->setAlignment(4);
1896 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001897
Daniel Dunbarb036db82008-08-13 03:21:16 +00001898 return Entry;
1899}
1900
1901/*
1902 struct _objc_protocol_extension {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001903 uint32_t size;
1904 struct objc_method_description_list *optional_instance_methods;
1905 struct objc_method_description_list *optional_class_methods;
1906 struct objc_property_list *instance_properties;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001907 };
1908*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001909llvm::Constant *
1910CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1911 const ConstantVector &OptInstanceMethods,
1912 const ConstantVector &OptClassMethods) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001913 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00001914 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001915 std::vector<llvm::Constant*> Values(4);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001916 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001917 Values[1] =
1918 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001919 + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001920 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1921 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001922 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001923 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001924 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1925 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001926 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00001927 0, PD, ObjCTypes);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001928
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001929 // Return null if no extension bits are used.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001930 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbarb036db82008-08-13 03:21:16 +00001931 Values[3]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00001932 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001933
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001934 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00001935 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001936
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001937 // No special section, but goes in llvm.used
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001938 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001939 Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001940 0, 0, true);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001941}
1942
1943/*
1944 struct objc_protocol_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001945 struct objc_protocol_list *next;
1946 long count;
1947 Protocol *list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00001948 };
1949*/
Daniel Dunbardec75f82008-08-21 21:57:41 +00001950llvm::Constant *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001951CGObjCMac::EmitProtocolList(llvm::Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001952 ObjCProtocolDecl::protocol_iterator begin,
1953 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001954 std::vector<llvm::Constant*> ProtocolRefs;
1955
Daniel Dunbardec75f82008-08-21 21:57:41 +00001956 for (; begin != end; ++begin)
1957 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001958
1959 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001960 if (ProtocolRefs.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001961 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001962
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001963 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00001964 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001965
1966 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001967 // This field is only used by the runtime.
Owen Anderson0b75f232009-07-31 20:28:54 +00001968 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001969 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001970 ProtocolRefs.size() - 1);
1971 Values[2] =
1972 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1973 ProtocolRefs.size()),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001974 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001975
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00001976 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001977 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001978 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00001979 4, false);
Owen Andersonade90fd2009-07-29 18:54:39 +00001980 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001981}
1982
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001983void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1984 std::vector<llvm::Constant*> &Properties,
1985 const Decl *Container,
1986 const ObjCProtocolDecl *PROTO,
1987 const ObjCCommonTypesHelper &ObjCTypes) {
1988 std::vector<llvm::Constant*> Prop(2);
1989 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1990 E = PROTO->protocol_end(); P != E; ++P)
1991 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1992 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1993 E = PROTO->prop_end(); I != E; ++I) {
1994 const ObjCPropertyDecl *PD = *I;
1995 if (!PropertySet.insert(PD->getIdentifier()))
1996 continue;
1997 Prop[0] = GetPropertyName(PD->getIdentifier());
1998 Prop[1] = GetPropertyTypeString(PD, Container);
1999 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
2000 }
2001}
2002
Daniel Dunbarb036db82008-08-13 03:21:16 +00002003/*
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002004 struct _objc_property {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002005 const char * const name;
2006 const char * const attributes;
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002007 };
2008
2009 struct _objc_property_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002010 uint32_t entsize; // sizeof (struct _objc_property)
2011 uint32_t prop_count;
2012 struct _objc_property[prop_count];
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002013 };
2014*/
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002015llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002016 const Decl *Container,
2017 const ObjCContainerDecl *OCD,
2018 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002019 std::vector<llvm::Constant*> Properties, Prop(2);
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002020 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002021 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2022 E = OCD->prop_end(); I != E; ++I) {
Steve Naroffba3dc382009-01-11 12:47:58 +00002023 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002024 PropertySet.insert(PD->getIdentifier());
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002025 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbar4932b362008-08-28 04:38:10 +00002026 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002027 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002028 Prop));
2029 }
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002030 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002031 for (ObjCInterfaceDecl::all_protocol_iterator
2032 P = OID->all_referenced_protocol_begin(),
2033 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002034 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2035 ObjCTypes);
2036 }
2037 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2038 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2039 E = CD->protocol_end(); P != E; ++P)
2040 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2041 ObjCTypes);
2042 }
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002043
2044 // Return null for empty list.
2045 if (Properties.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002046 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002047
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002048 unsigned PropertySize =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002049 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002050 std::vector<llvm::Constant*> Values(3);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002051 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2052 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002053 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002054 Properties.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002055 Values[2] = llvm::ConstantArray::get(AT, Properties);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002056 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002057
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002058 llvm::GlobalVariable *GV =
2059 CreateMetadataVar(Name, Init,
2060 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002061 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002062 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002063 true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002064 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002065}
2066
2067/*
Daniel Dunbarb036db82008-08-13 03:21:16 +00002068 struct objc_method_description_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002069 int count;
2070 struct objc_method_description list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00002071 };
2072*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002073llvm::Constant *
2074CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
2075 std::vector<llvm::Constant*> Desc(2);
Owen Anderson170229f2009-07-14 23:10:40 +00002076 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002077 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2078 ObjCTypes.SelectorPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002079 Desc[1] = GetMethodVarType(MD);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002080 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002081 Desc);
2082}
Daniel Dunbarb036db82008-08-13 03:21:16 +00002083
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002084llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002085 const char *Section,
2086 const ConstantVector &Methods) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00002087 // Return null for empty list.
2088 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002089 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002090
2091 std::vector<llvm::Constant*> Values(2);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002092 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002093 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002094 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002095 Values[1] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002096 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002097
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002098 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002099 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002100 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002101}
2102
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002103/*
2104 struct _objc_category {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002105 char *category_name;
2106 char *class_name;
2107 struct _objc_method_list *instance_methods;
2108 struct _objc_method_list *class_methods;
2109 struct _objc_protocol_list *protocols;
2110 uint32_t size; // <rdar://4585769>
2111 struct _objc_property_list *instance_properties;
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002112 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002113*/
Daniel Dunbar92992502008-08-15 22:20:32 +00002114void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002115 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002116
Mike Stump18bb9282009-05-16 07:57:57 +00002117 // FIXME: This is poor design, the OCD should have a pointer to the category
2118 // decl. Additionally, note that Category can be null for the @implementation
2119 // w/o an @interface case. Sema should just create one for us as it does for
2120 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002121 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002122 const ObjCCategoryDecl *Category =
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002123 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002124
2125 llvm::SmallString<256> ExtName;
2126 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2127 << OCD->getName();
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002128
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002129 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002130 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002131 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002132 // Instance methods should always be defined.
2133 InstanceMethods.push_back(GetMethodConstant(*i));
2134 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002135 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002136 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002137 // Class methods should always be defined.
2138 ClassMethods.push_back(GetMethodConstant(*i));
2139 }
2140
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002141 std::vector<llvm::Constant*> Values(7);
2142 Values[0] = GetClassName(OCD->getIdentifier());
2143 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahaniane55f8662009-04-29 20:40:05 +00002144 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002145 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002146 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002147 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002148 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002149 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002150 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002151 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002152 ClassMethods);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002153 if (Category) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002154 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002155 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002156 Category->protocol_begin(),
2157 Category->protocol_end());
2158 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002159 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002160 }
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002161 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002162
2163 // If there is no category @interface then there can be no properties.
2164 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002165 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002166 OCD, Category, ObjCTypes);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002167 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002168 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002169 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002170
Owen Anderson0e0189d2009-07-27 22:29:56 +00002171 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002172 Values);
2173
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002174 llvm::GlobalVariable *GV =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002175 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002176 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002177 4, true);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002178 DefinedCategories.push_back(GV);
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00002179 DefinedCategoryNames.insert(ExtName.str());
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002180}
2181
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002182// FIXME: Get from somewhere?
2183enum ClassFlags {
2184 eClassFlags_Factory = 0x00001,
2185 eClassFlags_Meta = 0x00002,
2186 // <rdr://5142207>
2187 eClassFlags_HasCXXStructors = 0x02000,
2188 eClassFlags_Hidden = 0x20000,
2189 eClassFlags_ABI2_Hidden = 0x00010,
2190 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2191};
2192
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002193/*
2194 struct _objc_class {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002195 Class isa;
2196 Class super_class;
2197 const char *name;
2198 long version;
2199 long info;
2200 long instance_size;
2201 struct _objc_ivar_list *ivars;
2202 struct _objc_method_list *methods;
2203 struct _objc_cache *cache;
2204 struct _objc_protocol_list *protocols;
2205 // Objective-C 1.0 extensions (<rdr://4585769>)
2206 const char *ivar_layout;
2207 struct _objc_class_ext *ext;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002208 };
2209
2210 See EmitClassExtension();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002211*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002212void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002213 DefinedSymbols.insert(ID->getIdentifier());
2214
Chris Lattner86d7d912008-11-24 03:54:41 +00002215 std::string ClassName = ID->getNameAsString();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002216 // FIXME: Gross
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002217 ObjCInterfaceDecl *Interface =
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002218 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002219 llvm::Constant *Protocols =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002220 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002221 Interface->all_referenced_protocol_begin(),
2222 Interface->all_referenced_protocol_end());
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002223 unsigned Flags = eClassFlags_Factory;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00002224 if (ID->getNumIvarInitializers())
2225 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002226 unsigned Size =
Ken Dyckc8ae5502011-02-09 01:59:34 +00002227 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002228
2229 // FIXME: Set CXX-structors flag.
John McCall457a04e2010-10-22 21:05:15 +00002230 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002231 Flags |= eClassFlags_Hidden;
2232
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002233 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002234 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002235 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002236 // Instance methods should always be defined.
2237 InstanceMethods.push_back(GetMethodConstant(*i));
2238 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002239 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002240 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002241 // Class methods should always be defined.
2242 ClassMethods.push_back(GetMethodConstant(*i));
2243 }
2244
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002245 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002246 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002247 ObjCPropertyImplDecl *PID = *i;
2248
2249 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2250 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2251
2252 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2253 if (llvm::Constant *C = GetMethodConstant(MD))
2254 InstanceMethods.push_back(C);
2255 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2256 if (llvm::Constant *C = GetMethodConstant(MD))
2257 InstanceMethods.push_back(C);
2258 }
2259 }
2260
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002261 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarccf61832009-05-03 08:56:52 +00002262 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002263 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002264 // Record a reference to the super class.
2265 LazySymbols.insert(Super->getIdentifier());
2266
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002267 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002268 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002269 ObjCTypes.ClassPtrTy);
2270 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002271 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002272 }
2273 Values[ 2] = GetClassName(ID->getIdentifier());
2274 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002275 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2276 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2277 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002278 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002279 Values[ 7] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002280 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002281 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002282 InstanceMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002283 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002284 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002285 Values[ 9] = Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002286 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002287 Values[11] = EmitClassExtension(ID);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002288 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002289 Values);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002290 std::string Name("\01L_OBJC_CLASS_");
2291 Name += ClassName;
2292 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2293 // Check for a forward reference.
2294 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2295 if (GV) {
2296 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2297 "Forward metaclass reference has incorrect type.");
2298 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2299 GV->setInitializer(Init);
2300 GV->setSection(Section);
2301 GV->setAlignment(4);
2302 CGM.AddUsedGlobal(GV);
2303 }
2304 else
2305 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002306 DefinedClasses.push_back(GV);
2307}
2308
2309llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2310 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002311 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002312 unsigned Flags = eClassFlags_Meta;
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002313 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002314
John McCall457a04e2010-10-22 21:05:15 +00002315 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002316 Flags |= eClassFlags_Hidden;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002317
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002318 std::vector<llvm::Constant*> Values(12);
2319 // The isa for the metaclass is the root of the hierarchy.
2320 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2321 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2322 Root = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002323 Values[ 0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002324 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002325 ObjCTypes.ClassPtrTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002326 // The super class for the metaclass is emitted as the name of the
2327 // super class. The runtime fixes this up to point to the
2328 // *metaclass* for the super class.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002329 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002330 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002331 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002332 ObjCTypes.ClassPtrTy);
2333 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002334 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002335 }
2336 Values[ 2] = GetClassName(ID->getIdentifier());
2337 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002338 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2339 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2340 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002341 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002342 Values[ 7] =
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002343 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002344 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002345 Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002346 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002347 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002348 Values[ 9] = Protocols;
2349 // ivar_layout for metaclass is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002350 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002351 // The class extension is always unused for metaclasses.
Owen Anderson0b75f232009-07-31 20:28:54 +00002352 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002353 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002354 Values);
2355
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002356 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner86d7d912008-11-24 03:54:41 +00002357 Name += ID->getNameAsCString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002358
2359 // Check for a forward reference.
2360 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2361 if (GV) {
2362 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2363 "Forward metaclass reference has incorrect type.");
2364 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2365 GV->setInitializer(Init);
2366 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00002367 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002368 llvm::GlobalValue::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002369 Init, Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002370 }
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002371 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbarae333842009-03-09 22:18:41 +00002372 GV->setAlignment(4);
Chris Lattnerf56501c2009-07-17 23:57:13 +00002373 CGM.AddUsedGlobal(GV);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002374
2375 return GV;
2376}
2377
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002378llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002379 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002380
Mike Stump18bb9282009-05-16 07:57:57 +00002381 // FIXME: Should we look these up somewhere other than the module. Its a bit
2382 // silly since we only generate these while processing an implementation, so
2383 // exactly one pointer would work if know when we entered/exitted an
2384 // implementation block.
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002385
2386 // Check for an existing forward reference.
Fariborz Jahanian475831b2009-01-07 20:11:22 +00002387 // Previously, metaclass with internal linkage may have been defined.
2388 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002389 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2390 true)) {
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002391 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2392 "Forward metaclass reference has incorrect type.");
2393 return GV;
2394 } else {
2395 // Generate as an external reference to keep a consistent
2396 // module. This will be patched up when we emit the metaclass.
Owen Andersonc10c8d32009-07-08 19:05:04 +00002397 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002398 llvm::GlobalValue::ExternalLinkage,
2399 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002400 Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002401 }
2402}
2403
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002404llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2405 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2406
2407 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2408 true)) {
2409 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2410 "Forward class metadata reference has incorrect type.");
2411 return GV;
2412 } else {
2413 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2414 llvm::GlobalValue::ExternalLinkage,
2415 0,
2416 Name);
2417 }
2418}
2419
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002420/*
2421 struct objc_class_ext {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002422 uint32_t size;
2423 const char *weak_ivar_layout;
2424 struct _objc_property_list *properties;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002425 };
2426*/
2427llvm::Constant *
2428CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002429 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002430 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002431
2432 std::vector<llvm::Constant*> Values(3);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002433 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian6df69862009-04-22 23:00:43 +00002434 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002435 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002436 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002437
2438 // Return null if no extension bits are used.
2439 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002440 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002441
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002442 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002443 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002444 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002445 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002446 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002447}
2448
2449/*
2450 struct objc_ivar {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002451 char *ivar_name;
2452 char *ivar_type;
2453 int ivar_offset;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002454 };
2455
2456 struct objc_ivar_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002457 int ivar_count;
2458 struct objc_ivar list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002459 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002460*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002461llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002462 bool ForClass) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002463 std::vector<llvm::Constant*> Ivars, Ivar(3);
2464
2465 // When emitting the root class GCC emits ivar entries for the
2466 // actual class structure. It is not clear if we need to follow this
2467 // behavior; for now lets try and get away with not doing it. If so,
2468 // the cleanest solution would be to make up an ObjCInterfaceDecl
2469 // for the class.
2470 if (ForClass)
Owen Anderson0b75f232009-07-31 20:28:54 +00002471 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002472
2473 ObjCInterfaceDecl *OID =
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002474 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002475
Daniel Dunbarf5c18462009-04-20 06:54:31 +00002476 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002477 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002478
Daniel Dunbarf5c18462009-04-20 06:54:31 +00002479 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2480 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002481 // Ignore unnamed bit-fields.
2482 if (!IVD->getDeclName())
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002483 continue;
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00002484 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2485 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002486 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00002487 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson0e0189d2009-07-27 22:29:56 +00002488 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002489 }
2490
2491 // Return null for empty list.
2492 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002493 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002494
2495 std::vector<llvm::Constant*> Values(2);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002496 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002497 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002498 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002499 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002500 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002501
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002502 llvm::GlobalVariable *GV;
2503 if (ForClass)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002504 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002505 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002506 4, true);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002507 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002508 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002509 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002510 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002511 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002512}
2513
2514/*
2515 struct objc_method {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002516 SEL method_name;
2517 char *method_types;
2518 void *method;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002519 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002520
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002521 struct objc_method_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002522 struct objc_method_list *obsolete;
2523 int count;
2524 struct objc_method methods_list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002525 };
2526*/
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002527
2528/// GetMethodConstant - Return a struct objc_method constant for the
2529/// given method if it has been defined. The result is null if the
2530/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002531llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00002532 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002533 if (!Fn)
2534 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002535
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002536 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002537 Method[0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002538 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002539 ObjCTypes.SelectorPtrTy);
2540 Method[1] = GetMethodVarType(MD);
Owen Andersonade90fd2009-07-29 18:54:39 +00002541 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002542 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002543}
2544
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002545llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002546 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002547 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002548 // Return null for empty list.
2549 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002550 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002551
2552 std::vector<llvm::Constant*> Values(3);
Owen Anderson0b75f232009-07-31 20:28:54 +00002553 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002554 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002555 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002556 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002557 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002558 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002559
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002560 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002561 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002562 ObjCTypes.MethodListPtrTy);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002563}
2564
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00002565llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002566 const ObjCContainerDecl *CD) {
Daniel Dunbard2386812009-10-19 01:21:19 +00002567 llvm::SmallString<256> Name;
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00002568 GetNameForMethod(OMD, CD, Name);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002569
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002570 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002571 const llvm::FunctionType *MethodTy =
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002572 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002573 llvm::Function *Method =
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002574 llvm::Function::Create(MethodTy,
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002575 llvm::GlobalValue::InternalLinkage,
Daniel Dunbard2386812009-10-19 01:21:19 +00002576 Name.str(),
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002577 &CGM.getModule());
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002578 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002579
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002580 return Method;
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002581}
2582
Daniel Dunbar30c65362009-03-09 20:09:19 +00002583llvm::GlobalVariable *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002584CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +00002585 llvm::Constant *Init,
2586 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002587 unsigned Align,
2588 bool AddToUsed) {
Daniel Dunbar30c65362009-03-09 20:09:19 +00002589 const llvm::Type *Ty = Init->getType();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002590 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002591 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerf56501c2009-07-17 23:57:13 +00002592 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002593 if (Section)
2594 GV->setSection(Section);
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002595 if (Align)
2596 GV->setAlignment(Align);
2597 if (AddToUsed)
Chris Lattnerf56501c2009-07-17 23:57:13 +00002598 CGM.AddUsedGlobal(GV);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002599 return GV;
2600}
2601
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002602llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00002603 // Abuse this interface function as a place to finalize.
2604 FinishModule();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002605 return NULL;
2606}
2607
Chris Lattnerd4808922009-03-22 21:03:39 +00002608llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002609 return ObjCTypes.getGetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002610}
2611
Chris Lattnerd4808922009-03-22 21:03:39 +00002612llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002613 return ObjCTypes.getSetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002614}
2615
David Chisnall168b80f2010-12-26 22:13:16 +00002616llvm::Constant *CGObjCMac::GetGetStructFunction() {
2617 return ObjCTypes.getCopyStructFn();
2618}
2619llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00002620 return ObjCTypes.getCopyStructFn();
2621}
2622
Chris Lattnerd4808922009-03-22 21:03:39 +00002623llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002624 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson3f35a262008-08-31 04:05:03 +00002625}
2626
John McCallbd309292010-07-06 01:34:17 +00002627void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2628 return EmitTryOrSynchronizedStmt(CGF, S);
2629}
2630
2631void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2632 const ObjCAtSynchronizedStmt &S) {
2633 return EmitTryOrSynchronizedStmt(CGF, S);
2634}
2635
John McCall65bea082010-07-21 06:59:36 +00002636namespace {
John McCallcda666c2010-07-21 07:22:38 +00002637 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCall65bea082010-07-21 06:59:36 +00002638 const Stmt &S;
John McCall2dd7d442010-08-04 05:59:32 +00002639 llvm::Value *SyncArgSlot;
John McCall65bea082010-07-21 06:59:36 +00002640 llvm::Value *CallTryExitVar;
2641 llvm::Value *ExceptionData;
2642 ObjCTypesHelper &ObjCTypes;
2643 PerformFragileFinally(const Stmt *S,
John McCall2dd7d442010-08-04 05:59:32 +00002644 llvm::Value *SyncArgSlot,
John McCall65bea082010-07-21 06:59:36 +00002645 llvm::Value *CallTryExitVar,
2646 llvm::Value *ExceptionData,
2647 ObjCTypesHelper *ObjCTypes)
John McCall2dd7d442010-08-04 05:59:32 +00002648 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCall65bea082010-07-21 06:59:36 +00002649 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2650
2651 void Emit(CodeGenFunction &CGF, bool IsForEH) {
2652 // Check whether we need to call objc_exception_try_exit.
2653 // In optimized code, this branch will always be folded.
2654 llvm::BasicBlock *FinallyCallExit =
2655 CGF.createBasicBlock("finally.call_exit");
2656 llvm::BasicBlock *FinallyNoCallExit =
2657 CGF.createBasicBlock("finally.no_call_exit");
2658 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2659 FinallyCallExit, FinallyNoCallExit);
2660
2661 CGF.EmitBlock(FinallyCallExit);
2662 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2663 ->setDoesNotThrow();
2664
2665 CGF.EmitBlock(FinallyNoCallExit);
2666
2667 if (isa<ObjCAtTryStmt>(S)) {
2668 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCallcebe0ca2010-08-11 00:16:14 +00002669 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2670 // Save the current cleanup destination in case there's
2671 // control flow inside the finally statement.
2672 llvm::Value *CurCleanupDest =
2673 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2674
John McCall65bea082010-07-21 06:59:36 +00002675 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2676
John McCallcebe0ca2010-08-11 00:16:14 +00002677 if (CGF.HaveInsertPoint()) {
2678 CGF.Builder.CreateStore(CurCleanupDest,
2679 CGF.getNormalCleanupDestSlot());
2680 } else {
2681 // Currently, the end of the cleanup must always exist.
2682 CGF.EnsureInsertPoint();
2683 }
2684 }
John McCall65bea082010-07-21 06:59:36 +00002685 } else {
2686 // Emit objc_sync_exit(expr); as finally's sole statement for
2687 // @synchronized.
John McCall2dd7d442010-08-04 05:59:32 +00002688 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCall65bea082010-07-21 06:59:36 +00002689 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2690 ->setDoesNotThrow();
2691 }
2692 }
2693 };
John McCall42227ed2010-07-31 23:20:56 +00002694
2695 class FragileHazards {
2696 CodeGenFunction &CGF;
2697 llvm::SmallVector<llvm::Value*, 20> Locals;
2698 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2699
2700 llvm::InlineAsm *ReadHazard;
2701 llvm::InlineAsm *WriteHazard;
2702
2703 llvm::FunctionType *GetAsmFnType();
2704
2705 void collectLocals();
2706 void emitReadHazard(CGBuilderTy &Builder);
2707
2708 public:
2709 FragileHazards(CodeGenFunction &CGF);
John McCall2dd7d442010-08-04 05:59:32 +00002710
John McCall42227ed2010-07-31 23:20:56 +00002711 void emitWriteHazard();
John McCall2dd7d442010-08-04 05:59:32 +00002712 void emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00002713 };
2714}
2715
2716/// Create the fragile-ABI read and write hazards based on the current
2717/// state of the function, which is presumed to be immediately prior
2718/// to a @try block. These hazards are used to maintain correct
2719/// semantics in the face of optimization and the fragile ABI's
2720/// cavalier use of setjmp/longjmp.
2721FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2722 collectLocals();
2723
2724 if (Locals.empty()) return;
2725
2726 // Collect all the blocks in the function.
2727 for (llvm::Function::iterator
2728 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2729 BlocksBeforeTry.insert(&*I);
2730
2731 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2732
2733 // Create a read hazard for the allocas. This inhibits dead-store
2734 // optimizations and forces the values to memory. This hazard is
2735 // inserted before any 'throwing' calls in the protected scope to
2736 // reflect the possibility that the variables might be read from the
2737 // catch block if the call throws.
2738 {
2739 std::string Constraint;
2740 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2741 if (I) Constraint += ',';
2742 Constraint += "*m";
2743 }
2744
2745 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2746 }
2747
2748 // Create a write hazard for the allocas. This inhibits folding
2749 // loads across the hazard. This hazard is inserted at the
2750 // beginning of the catch path to reflect the possibility that the
2751 // variables might have been written within the protected scope.
2752 {
2753 std::string Constraint;
2754 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2755 if (I) Constraint += ',';
2756 Constraint += "=*m";
2757 }
2758
2759 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2760 }
2761}
2762
2763/// Emit a write hazard at the current location.
2764void FragileHazards::emitWriteHazard() {
2765 if (Locals.empty()) return;
2766
2767 CGF.Builder.CreateCall(WriteHazard, Locals.begin(), Locals.end())
2768 ->setDoesNotThrow();
2769}
2770
John McCall42227ed2010-07-31 23:20:56 +00002771void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2772 assert(!Locals.empty());
2773 Builder.CreateCall(ReadHazard, Locals.begin(), Locals.end())
2774 ->setDoesNotThrow();
2775}
2776
2777/// Emit read hazards in all the protected blocks, i.e. all the blocks
2778/// which have been inserted since the beginning of the try.
John McCall2dd7d442010-08-04 05:59:32 +00002779void FragileHazards::emitHazardsInNewBlocks() {
John McCall42227ed2010-07-31 23:20:56 +00002780 if (Locals.empty()) return;
2781
2782 CGBuilderTy Builder(CGF.getLLVMContext());
2783
2784 // Iterate through all blocks, skipping those prior to the try.
2785 for (llvm::Function::iterator
2786 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2787 llvm::BasicBlock &BB = *FI;
2788 if (BlocksBeforeTry.count(&BB)) continue;
2789
2790 // Walk through all the calls in the block.
2791 for (llvm::BasicBlock::iterator
2792 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2793 llvm::Instruction &I = *BI;
2794
2795 // Ignore instructions that aren't non-intrinsic calls.
2796 // These are the only calls that can possibly call longjmp.
2797 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2798 if (isa<llvm::IntrinsicInst>(I))
2799 continue;
2800
2801 // Ignore call sites marked nounwind. This may be questionable,
2802 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2803 llvm::CallSite CS(&I);
2804 if (CS.doesNotThrow()) continue;
2805
John McCall2dd7d442010-08-04 05:59:32 +00002806 // Insert a read hazard before the call. This will ensure that
2807 // any writes to the locals are performed before making the
2808 // call. If the call throws, then this is sufficient to
2809 // guarantee correctness as long as it doesn't also write to any
2810 // locals.
John McCall42227ed2010-07-31 23:20:56 +00002811 Builder.SetInsertPoint(&BB, BI);
2812 emitReadHazard(Builder);
2813 }
2814 }
2815}
2816
2817static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2818 if (V) S.insert(V);
2819}
2820
2821void FragileHazards::collectLocals() {
2822 // Compute a set of allocas to ignore.
2823 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2824 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2825 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
2826 addIfPresent(AllocasToIgnore, CGF.EHCleanupDest);
2827
2828 // Collect all the allocas currently in the function. This is
2829 // probably way too aggressive.
2830 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2831 for (llvm::BasicBlock::iterator
2832 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2833 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2834 Locals.push_back(&*I);
2835}
2836
2837llvm::FunctionType *FragileHazards::GetAsmFnType() {
2838 std::vector<const llvm::Type *> Tys(Locals.size());
2839 for (unsigned I = 0, E = Locals.size(); I != E; ++I)
2840 Tys[I] = Locals[I]->getType();
2841 return llvm::FunctionType::get(CGF.Builder.getVoidTy(), Tys, false);
John McCall65bea082010-07-21 06:59:36 +00002842}
2843
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002844/*
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002845
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002846 Objective-C setjmp-longjmp (sjlj) Exception Handling
2847 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002848
John McCallbd309292010-07-06 01:34:17 +00002849 A catch buffer is a setjmp buffer plus:
2850 - a pointer to the exception that was caught
2851 - a pointer to the previous exception data buffer
2852 - two pointers of reserved storage
2853 Therefore catch buffers form a stack, with a pointer to the top
2854 of the stack kept in thread-local storage.
2855
2856 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2857 objc_exception_try_exit pops the given catch buffer, which is
2858 required to be the top of the EH stack.
2859 objc_exception_throw pops the top of the EH stack, writes the
2860 thrown exception into the appropriate field, and longjmps
2861 to the setjmp buffer. It crashes the process (with a printf
2862 and an abort()) if there are no catch buffers on the stack.
2863 objc_exception_extract just reads the exception pointer out of the
2864 catch buffer.
2865
2866 There's no reason an implementation couldn't use a light-weight
2867 setjmp here --- something like __builtin_setjmp, but API-compatible
2868 with the heavyweight setjmp. This will be more important if we ever
2869 want to implement correct ObjC/C++ exception interactions for the
2870 fragile ABI.
2871
2872 Note that for this use of setjmp/longjmp to be correct, we may need
2873 to mark some local variables volatile: if a non-volatile local
2874 variable is modified between the setjmp and the longjmp, it has
2875 indeterminate value. For the purposes of LLVM IR, it may be
2876 sufficient to make loads and stores within the @try (to variables
2877 declared outside the @try) volatile. This is necessary for
2878 optimized correctness, but is not currently being done; this is
2879 being tracked as rdar://problem/8160285
2880
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002881 The basic framework for a @try-catch-finally is as follows:
2882 {
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002883 objc_exception_data d;
2884 id _rethrow = null;
Anders Carlssonda0e4562009-02-07 21:26:04 +00002885 bool _call_try_exit = true;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002886
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002887 objc_exception_try_enter(&d);
2888 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002889 ... try body ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002890 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002891 // exception path
2892 id _caught = objc_exception_extract(&d);
2893
2894 // enter new try scope for handlers
2895 if (!setjmp(d.jmp_buf)) {
2896 ... match exception and execute catch blocks ...
2897
2898 // fell off end, rethrow.
2899 _rethrow = _caught;
2900 ... jump-through-finally to finally_rethrow ...
2901 } else {
2902 // exception in catch block
2903 _rethrow = objc_exception_extract(&d);
2904 _call_try_exit = false;
2905 ... jump-through-finally to finally_rethrow ...
2906 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002907 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002908 ... jump-through-finally to finally_end ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002909
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002910 finally:
Anders Carlssonda0e4562009-02-07 21:26:04 +00002911 if (_call_try_exit)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002912 objc_exception_try_exit(&d);
Anders Carlssonda0e4562009-02-07 21:26:04 +00002913
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002914 ... finally block ....
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002915 ... dispatch to finally destination ...
2916
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002917 finally_rethrow:
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002918 objc_exception_throw(_rethrow);
2919
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002920 finally_end:
2921 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002922
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002923 This framework differs slightly from the one gcc uses, in that gcc
2924 uses _rethrow to determine if objc_exception_try_exit should be called
2925 and if the object should be rethrown. This breaks in the face of
2926 throwing nil and introduces unnecessary branches.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002927
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002928 We specialize this framework for a few particular circumstances:
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002929
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002930 - If there are no catch blocks, then we avoid emitting the second
2931 exception handling context.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002932
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002933 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2934 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002935
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002936 - FIXME: If there is no @finally block we can do a few more
2937 simplifications.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002938
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002939 Rethrows and Jumps-Through-Finally
2940 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002941
John McCallbd309292010-07-06 01:34:17 +00002942 '@throw;' is supported by pushing the currently-caught exception
2943 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002944
John McCallbd309292010-07-06 01:34:17 +00002945 Branches through the @finally block are handled with an ordinary
2946 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2947 exceptions are not compatible with C++ exceptions, and this is
2948 hardly the only place where this will go wrong.
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002949
John McCallbd309292010-07-06 01:34:17 +00002950 @synchronized(expr) { stmt; } is emitted as if it were:
2951 id synch_value = expr;
2952 objc_sync_enter(synch_value);
2953 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002954*/
2955
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +00002956void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2957 const Stmt &S) {
2958 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallbd309292010-07-06 01:34:17 +00002959
2960 // A destination for the fall-through edges of the catch handlers to
2961 // jump to.
2962 CodeGenFunction::JumpDest FinallyEnd =
2963 CGF.getJumpDestInCurrentScope("finally.end");
2964
2965 // A destination for the rethrow edge of the catch handlers to jump
2966 // to.
2967 CodeGenFunction::JumpDest FinallyRethrow =
2968 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002969
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002970 // For @synchronized, call objc_sync_enter(sync.expr). The
2971 // evaluation of the expression must occur before we enter the
John McCall2dd7d442010-08-04 05:59:32 +00002972 // @synchronized. We can't avoid a temp here because we need the
2973 // value to be preserved. If the backend ever does liveness
2974 // correctly after setjmp, this will be unnecessary.
2975 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002976 if (!isTry) {
John McCall2dd7d442010-08-04 05:59:32 +00002977 llvm::Value *SyncArg =
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002978 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2979 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallbd309292010-07-06 01:34:17 +00002980 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2981 ->setDoesNotThrow();
John McCall2dd7d442010-08-04 05:59:32 +00002982
2983 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2984 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002985 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002986
John McCall2dd7d442010-08-04 05:59:32 +00002987 // Allocate memory for the setjmp buffer. This needs to be kept
2988 // live throughout the try and catch blocks.
2989 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2990 "exceptiondata.ptr");
2991
John McCall42227ed2010-07-31 23:20:56 +00002992 // Create the fragile hazards. Note that this will not capture any
2993 // of the allocas required for exception processing, but will
2994 // capture the current basic block (which extends all the way to the
2995 // setjmp call) as "before the @try".
2996 FragileHazards Hazards(CGF);
2997
John McCallbd309292010-07-06 01:34:17 +00002998 // Create a flag indicating whether the cleanup needs to call
2999 // objc_exception_try_exit. This is true except when
3000 // - no catches match and we're branching through the cleanup
3001 // just to rethrow the exception, or
3002 // - a catch matched and we're falling out of the catch handler.
John McCall2dd7d442010-08-04 05:59:32 +00003003 // The setjmp-safety rule here is that we should always store to this
3004 // variable in a place that dominates the branch through the cleanup
3005 // without passing through any setjmps.
John McCallbd309292010-07-06 01:34:17 +00003006 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlssonda0e4562009-02-07 21:26:04 +00003007 "_call_try_exit");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003008
John McCall9916e3f2010-10-04 23:42:51 +00003009 // A slot containing the exception to rethrow. Only needed when we
3010 // have both a @catch and a @finally.
3011 llvm::Value *PropagatingExnVar = 0;
3012
John McCallbd309292010-07-06 01:34:17 +00003013 // Push a normal cleanup to leave the try scope.
John McCallcda666c2010-07-21 07:22:38 +00003014 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall2dd7d442010-08-04 05:59:32 +00003015 SyncArgSlot,
John McCallcda666c2010-07-21 07:22:38 +00003016 CallTryExitVar,
3017 ExceptionData,
3018 &ObjCTypes);
John McCallbd309292010-07-06 01:34:17 +00003019
3020 // Enter a try block:
3021 // - Call objc_exception_try_enter to push ExceptionData on top of
3022 // the EH stack.
3023 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3024 ->setDoesNotThrow();
3025
3026 // - Call setjmp on the exception data buffer.
3027 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3028 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3029 llvm::Value *SetJmpBuffer =
3030 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer");
3031 llvm::CallInst *SetJmpResult =
3032 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3033 SetJmpResult->setDoesNotThrow();
3034
3035 // If setjmp returned 0, enter the protected block; otherwise,
3036 // branch to the handler.
Daniel Dunbar75283ff2008-11-11 02:29:29 +00003037 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3038 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallbd309292010-07-06 01:34:17 +00003039 llvm::Value *DidCatch =
John McCallcebe0ca2010-08-11 00:16:14 +00003040 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3041 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003042
John McCallbd309292010-07-06 01:34:17 +00003043 // Emit the protected block.
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003044 CGF.EmitBlock(TryBlock);
John McCall2dd7d442010-08-04 05:59:32 +00003045 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003046 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallbd309292010-07-06 01:34:17 +00003047 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall2dd7d442010-08-04 05:59:32 +00003048
3049 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003050
John McCallbd309292010-07-06 01:34:17 +00003051 // Emit the exception handler block.
Daniel Dunbar7f086782008-09-27 23:30:04 +00003052 CGF.EmitBlock(TryHandler);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003053
John McCall42227ed2010-07-31 23:20:56 +00003054 // Don't optimize loads of the in-scope locals across this point.
3055 Hazards.emitWriteHazard();
3056
John McCallbd309292010-07-06 01:34:17 +00003057 // For a @synchronized (or a @try with no catches), just branch
3058 // through the cleanup to the rethrow block.
3059 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3060 // Tell the cleanup not to re-pop the exit.
John McCall2dd7d442010-08-04 05:59:32 +00003061 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003062 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallbd309292010-07-06 01:34:17 +00003063
3064 // Otherwise, we have to match against the caught exceptions.
3065 } else {
John McCall2dd7d442010-08-04 05:59:32 +00003066 // Retrieve the exception object. We may emit multiple blocks but
3067 // nothing can cross this so the value is already in SSA form.
3068 llvm::CallInst *Caught =
3069 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3070 ExceptionData, "caught");
3071 Caught->setDoesNotThrow();
3072
John McCallbd309292010-07-06 01:34:17 +00003073 // Push the exception to rethrow onto the EH value stack for the
3074 // benefit of any @throws in the handlers.
3075 CGF.ObjCEHValueStack.push_back(Caught);
3076
Douglas Gregor96c79492010-04-23 22:50:49 +00003077 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003078
John McCall2dd7d442010-08-04 05:59:32 +00003079 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallbd309292010-07-06 01:34:17 +00003080
John McCall2dd7d442010-08-04 05:59:32 +00003081 llvm::BasicBlock *CatchBlock = 0;
3082 llvm::BasicBlock *CatchHandler = 0;
3083 if (HasFinally) {
John McCall9916e3f2010-10-04 23:42:51 +00003084 // Save the currently-propagating exception before
3085 // objc_exception_try_enter clears the exception slot.
3086 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3087 "propagating_exception");
3088 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3089
John McCall2dd7d442010-08-04 05:59:32 +00003090 // Enter a new exception try block (in case a @catch block
3091 // throws an exception).
3092 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3093 ->setDoesNotThrow();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003094
John McCall2dd7d442010-08-04 05:59:32 +00003095 llvm::CallInst *SetJmpResult =
3096 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3097 "setjmp.result");
3098 SetJmpResult->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003099
John McCall2dd7d442010-08-04 05:59:32 +00003100 llvm::Value *Threw =
3101 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3102
3103 CatchBlock = CGF.createBasicBlock("catch");
3104 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3105 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3106
3107 CGF.EmitBlock(CatchBlock);
3108 }
3109
3110 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003111
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003112 // Handle catch list. As a special case we check if everything is
3113 // matched and avoid generating code for falling off the end if
3114 // so.
3115 bool AllMatched = false;
Douglas Gregor96c79492010-04-23 22:50:49 +00003116 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3117 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003118
Douglas Gregor46a572b2010-04-26 16:46:50 +00003119 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +00003120 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar523208f2008-09-27 07:36:24 +00003121
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003122 // catch(...) always matches.
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003123 if (!CatchParam) {
3124 AllMatched = true;
3125 } else {
John McCall9dd450b2009-09-21 23:43:11 +00003126 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003127
John McCallbd309292010-07-06 01:34:17 +00003128 // catch(id e) always matches under this ABI, since only
3129 // ObjC exceptions end up here in the first place.
Daniel Dunbar86919f42008-09-27 22:21:14 +00003130 // FIXME: For the time being we also match id<X>; this should
3131 // be rejected by Sema instead.
Eli Friedman55179ca2009-07-11 00:57:02 +00003132 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003133 AllMatched = true;
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003134 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003135
John McCallbd309292010-07-06 01:34:17 +00003136 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003137 if (AllMatched) {
John McCallbd309292010-07-06 01:34:17 +00003138 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3139
Anders Carlsson9396a892008-09-11 09:15:33 +00003140 if (CatchParam) {
John McCall1c9c3fd2010-10-15 04:57:14 +00003141 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003142 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallbd309292010-07-06 01:34:17 +00003143
3144 // These types work out because ConvertType(id) == i8*.
Steve Naroff371b8fb2009-03-03 19:52:17 +00003145 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson9396a892008-09-11 09:15:33 +00003146 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003147
Anders Carlsson9396a892008-09-11 09:15:33 +00003148 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003149
3150 // The scope of the catch variable ends right here.
3151 CatchVarCleanups.ForceCleanup();
3152
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003153 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003154 break;
3155 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003156
Steve Naroff7cae42b2009-07-10 23:34:53 +00003157 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall96fa4842010-05-17 21:00:27 +00003158 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallbd309292010-07-06 01:34:17 +00003159
3160 // FIXME: @catch (Class c) ?
John McCall96fa4842010-05-17 21:00:27 +00003161 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3162 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003163
3164 // Check if the @catch block matches the exception object.
John McCall96fa4842010-05-17 21:00:27 +00003165 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003166
John McCallbd309292010-07-06 01:34:17 +00003167 llvm::CallInst *Match =
Chris Lattnerc6406db2009-04-22 02:26:14 +00003168 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3169 Class, Caught, "match");
John McCallbd309292010-07-06 01:34:17 +00003170 Match->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003171
John McCallbd309292010-07-06 01:34:17 +00003172 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3173 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003174
3175 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar7f086782008-09-27 23:30:04 +00003176 MatchedBlock, NextCatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003177
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003178 // Emit the @catch block.
3179 CGF.EmitBlock(MatchedBlock);
John McCallbd309292010-07-06 01:34:17 +00003180
3181 // Collect any cleanups for the catch variable. The scope lasts until
3182 // the end of the catch body.
John McCall2dd7d442010-08-04 05:59:32 +00003183 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallbd309292010-07-06 01:34:17 +00003184
John McCall1c9c3fd2010-10-15 04:57:14 +00003185 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003186 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003187
John McCallbd309292010-07-06 01:34:17 +00003188 // Initialize the catch variable.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003189 llvm::Value *Tmp =
3190 CGF.Builder.CreateBitCast(Caught,
3191 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003192 "tmp");
Steve Naroff371b8fb2009-03-03 19:52:17 +00003193 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003194
Anders Carlsson9396a892008-09-11 09:15:33 +00003195 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003196
3197 // We're done with the catch variable.
3198 CatchVarCleanups.ForceCleanup();
3199
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003200 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003201
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003202 CGF.EmitBlock(NextCatchBlock);
3203 }
3204
John McCallbd309292010-07-06 01:34:17 +00003205 CGF.ObjCEHValueStack.pop_back();
3206
John McCall2dd7d442010-08-04 05:59:32 +00003207 // If nothing wanted anything to do with the caught exception,
3208 // kill the extract call.
3209 if (Caught->use_empty())
3210 Caught->eraseFromParent();
3211
3212 if (!AllMatched)
3213 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3214
3215 if (HasFinally) {
3216 // Emit the exception handler for the @catch blocks.
3217 CGF.EmitBlock(CatchHandler);
3218
3219 // In theory we might now need a write hazard, but actually it's
3220 // unnecessary because there's no local-accessing code between
3221 // the try's write hazard and here.
3222 //Hazards.emitWriteHazard();
3223
John McCall9916e3f2010-10-04 23:42:51 +00003224 // Extract the new exception and save it to the
3225 // propagating-exception slot.
3226 assert(PropagatingExnVar);
3227 llvm::CallInst *NewCaught =
3228 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3229 ExceptionData, "caught");
3230 NewCaught->setDoesNotThrow();
3231 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3232
John McCall2dd7d442010-08-04 05:59:32 +00003233 // Don't pop the catch handler; the throw already did.
3234 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003235 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003236 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003237 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003238
John McCall42227ed2010-07-31 23:20:56 +00003239 // Insert read hazards as required in the new blocks.
John McCall2dd7d442010-08-04 05:59:32 +00003240 Hazards.emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00003241
John McCallbd309292010-07-06 01:34:17 +00003242 // Pop the cleanup.
John McCall2dd7d442010-08-04 05:59:32 +00003243 CGF.Builder.restoreIP(TryFallthroughIP);
3244 if (CGF.HaveInsertPoint())
3245 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallbd309292010-07-06 01:34:17 +00003246 CGF.PopCleanupBlock();
John McCall2dd7d442010-08-04 05:59:32 +00003247 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003248
John McCallbd309292010-07-06 01:34:17 +00003249 // Emit the rethrow block.
John McCall42227ed2010-07-31 23:20:56 +00003250 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallad5d61e2010-07-23 21:56:41 +00003251 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallbd309292010-07-06 01:34:17 +00003252 if (CGF.HaveInsertPoint()) {
John McCall9916e3f2010-10-04 23:42:51 +00003253 // If we have a propagating-exception variable, check it.
3254 llvm::Value *PropagatingExn;
3255 if (PropagatingExnVar) {
3256 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall2dd7d442010-08-04 05:59:32 +00003257
John McCall9916e3f2010-10-04 23:42:51 +00003258 // Otherwise, just look in the buffer for the exception to throw.
3259 } else {
3260 llvm::CallInst *Caught =
3261 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3262 ExceptionData);
3263 Caught->setDoesNotThrow();
3264 PropagatingExn = Caught;
3265 }
3266
3267 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallbd309292010-07-06 01:34:17 +00003268 ->setDoesNotThrow();
3269 CGF.Builder.CreateUnreachable();
Fariborz Jahaniane2caaaa2008-11-21 19:21:53 +00003270 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003271
John McCall42227ed2010-07-31 23:20:56 +00003272 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003273}
3274
3275void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003276 const ObjCAtThrowStmt &S) {
Anders Carlssone005aa12008-09-09 16:16:55 +00003277 llvm::Value *ExceptionAsObject;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003278
Anders Carlssone005aa12008-09-09 16:16:55 +00003279 if (const Expr *ThrowExpr = S.getThrowExpr()) {
3280 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003281 ExceptionAsObject =
Anders Carlssone005aa12008-09-09 16:16:55 +00003282 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
3283 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003284 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003285 "Unexpected rethrow outside @catch block.");
Anders Carlssonbf8a1be2009-02-07 21:37:21 +00003286 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlssone005aa12008-09-09 16:16:55 +00003287 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003288
John McCallbd309292010-07-06 01:34:17 +00003289 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3290 ->setDoesNotReturn();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003291 CGF.Builder.CreateUnreachable();
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003292
3293 // Clear the insertion point to indicate we are in unreachable code.
3294 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003295}
3296
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003297/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003298/// object: objc_read_weak (id *src)
3299///
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003300llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003301 llvm::Value *AddrWeakObj) {
Eli Friedmana374b682009-03-07 03:57:15 +00003302 const llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003303 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3304 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3305 ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00003306 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003307 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00003308 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003309 return read_weak;
3310}
3311
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003312/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3313/// objc_assign_weak (id src, id *dst)
3314///
3315void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003316 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003317 const llvm::Type * SrcTy = src->getType();
3318 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003319 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003320 assert(Size <= 8 && "does not support size > 8");
3321 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003322 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003323 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3324 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003325 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3326 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00003327 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003328 src, dst, "weakassign");
3329 return;
3330}
3331
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003332/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3333/// objc_assign_global (id src, id *dst)
3334///
3335void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00003336 llvm::Value *src, llvm::Value *dst,
3337 bool threadlocal) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003338 const llvm::Type * SrcTy = src->getType();
3339 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003340 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003341 assert(Size <= 8 && "does not support size > 8");
3342 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003343 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003344 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3345 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003346 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3347 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00003348 if (!threadlocal)
3349 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3350 src, dst, "globalassign");
3351 else
3352 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3353 src, dst, "threadlocalassign");
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003354 return;
3355}
3356
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003357/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003358/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003359///
3360void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003361 llvm::Value *src, llvm::Value *dst,
3362 llvm::Value *ivarOffset) {
3363 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003364 const llvm::Type * SrcTy = src->getType();
3365 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003366 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003367 assert(Size <= 8 && "does not support size > 8");
3368 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003369 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003370 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3371 }
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003372 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3373 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003374 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3375 src, dst, ivarOffset);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003376 return;
3377}
3378
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003379/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3380/// objc_assign_strongCast (id src, id *dst)
3381///
3382void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003383 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003384 const llvm::Type * SrcTy = src->getType();
3385 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003386 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003387 assert(Size <= 8 && "does not support size > 8");
3388 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003389 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003390 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3391 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003392 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3393 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00003394 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003395 src, dst, "weakassign");
3396 return;
3397}
3398
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003399void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003400 llvm::Value *DestPtr,
3401 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003402 llvm::Value *size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003403 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3404 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003405 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003406 DestPtr, SrcPtr, size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003407 return;
3408}
3409
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003410/// EmitObjCValueForIvar - Code Gen for ivar reference.
3411///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003412LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3413 QualType ObjectTy,
3414 llvm::Value *BaseValue,
3415 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003416 unsigned CVRQualifiers) {
John McCall8b07ec22010-05-15 11:32:37 +00003417 const ObjCInterfaceDecl *ID =
3418 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003419 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3420 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003421}
3422
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003423llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00003424 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003425 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003426 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003427 return llvm::ConstantInt::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003428 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3429 Offset);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003430}
3431
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003432/* *** Private Interface *** */
3433
3434/// EmitImageInfo - Emit the image info marker used to encode some module
3435/// level information.
3436///
3437/// See: <rdr://4810609&4810587&4810587>
3438/// struct IMAGE_INFO {
3439/// unsigned version;
3440/// unsigned flags;
3441/// };
3442enum ImageInfoFlags {
Daniel Dunbar5e639272010-04-25 20:39:01 +00003443 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003444 eImageInfo_GarbageCollected = (1 << 1),
3445 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003446 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3447
Daniel Dunbar5e639272010-04-25 20:39:01 +00003448 // A flag indicating that the module has no instances of a @synthesize of a
3449 // superclass variable. <rdar://problem/6803242>
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003450 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003451};
3452
Daniel Dunbar5e639272010-04-25 20:39:01 +00003453void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003454 unsigned version = 0; // Version is unused?
3455 unsigned flags = 0;
3456
3457 // FIXME: Fix and continue?
3458 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3459 flags |= eImageInfo_GarbageCollected;
3460 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3461 flags |= eImageInfo_GCOnly;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003462
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003463 // We never allow @synthesize of a superclass property.
3464 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003465
Chris Lattner5e016ae2010-06-27 07:15:29 +00003466 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3467
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003468 // Emitted as int[2];
3469 llvm::Constant *values[2] = {
Chris Lattner5e016ae2010-06-27 07:15:29 +00003470 llvm::ConstantInt::get(Int32Ty, version),
3471 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003472 };
Chris Lattner5e016ae2010-06-27 07:15:29 +00003473 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003474
3475 const char *Section;
3476 if (ObjCABI == 1)
3477 Section = "__OBJC, __image_info,regular";
3478 else
3479 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003480 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003481 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson47034e12009-07-28 18:33:04 +00003482 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003483 Section,
3484 0,
3485 true);
3486 GV->setConstant(true);
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003487}
3488
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003489
3490// struct objc_module {
3491// unsigned long version;
3492// unsigned long size;
3493// const char *name;
3494// Symtab symtab;
3495// };
3496
3497// FIXME: Get from somewhere
3498static const int ModuleVersion = 7;
3499
3500void CGObjCMac::EmitModuleInfo() {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003501 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003502
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003503 std::vector<llvm::Constant*> Values(4);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003504 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3505 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar92992502008-08-15 22:20:32 +00003506 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbarb036db82008-08-13 03:21:16 +00003507 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003508 Values[3] = EmitModuleSymbols();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003509 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson0e0189d2009-07-27 22:29:56 +00003510 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003511 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00003512 4, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003513}
3514
3515llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003516 unsigned NumClasses = DefinedClasses.size();
3517 unsigned NumCategories = DefinedCategories.size();
3518
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003519 // Return null if no symbols were defined.
3520 if (!NumClasses && !NumCategories)
Owen Anderson0b75f232009-07-31 20:28:54 +00003521 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003522
3523 std::vector<llvm::Constant*> Values(5);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003524 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson0b75f232009-07-31 20:28:54 +00003525 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003526 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3527 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003528
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003529 // The runtime expects exactly the list of defined classes followed
3530 // by the list of defined categories, in a single array.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003531 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003532 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00003533 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003534 ObjCTypes.Int8PtrTy);
3535 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003536 Symbols[NumClasses + i] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003537 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003538 ObjCTypes.Int8PtrTy);
3539
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003540 Values[4] =
Owen Anderson9793f0e2009-07-29 22:16:19 +00003541 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003542 NumClasses + NumCategories),
3543 Symbols);
3544
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00003545 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003546
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003547 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003548 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3549 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003550 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00003551 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003552}
3553
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003554llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003555 const ObjCInterfaceDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003556 LazySymbols.insert(ID->getIdentifier());
3557
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003558 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003559
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003560 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003561 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003562 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003563 ObjCTypes.ClassPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003564 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003565 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3566 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003567 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003568 }
3569
Daniel Dunbarc76493a2009-11-29 21:23:36 +00003570 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003571}
3572
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003573llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3574 bool lvalue) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003575 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003576
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003577 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003578 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003579 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003580 ObjCTypes.SelectorPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003581 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003582 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3583 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003584 4, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003585 }
3586
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003587 if (lvalue)
3588 return Entry;
Daniel Dunbarc76493a2009-11-29 21:23:36 +00003589 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003590}
3591
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00003592llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00003593 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003594
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003595 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003596 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003597 llvm::ConstantArray::get(VMContext,
3598 Ident->getNameStart()),
Daniel Dunbarcda43072010-07-29 22:57:21 +00003599 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003600 1, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003601
Owen Anderson170229f2009-07-14 23:10:40 +00003602 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003603}
3604
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003605llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3606 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3607 I = MethodDefinitions.find(MD);
3608 if (I != MethodDefinitions.end())
3609 return I->second;
3610
3611 if (MD->hasBody() && MD->getPCHLevel() > 0) {
3612 // MD isn't emitted yet because it comes from PCH.
3613 CGM.EmitTopLevelDecl(const_cast<ObjCMethodDecl*>(MD));
3614 assert(MethodDefinitions[MD] && "EmitTopLevelDecl didn't emit the method!");
3615 return MethodDefinitions[MD];
3616 }
3617
3618 return NULL;
3619}
3620
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003621/// GetIvarLayoutName - Returns a unique constant for the given
3622/// ivar layout bitmap.
3623llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003624 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson0b75f232009-07-31 20:28:54 +00003625 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003626}
3627
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003628void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003629 unsigned int BytePos,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003630 bool ForStrongLayout,
3631 bool &HasUnion) {
3632 const RecordDecl *RD = RT->getDecl();
3633 // FIXME - Use iterator.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003634 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003635 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003636 const llvm::StructLayout *RecLayout =
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003637 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003638
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003639 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3640 ForStrongLayout, HasUnion);
3641}
3642
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +00003643void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003644 const llvm::StructLayout *Layout,
3645 const RecordDecl *RD,
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003646 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003647 unsigned int BytePos, bool ForStrongLayout,
3648 bool &HasUnion) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003649 bool IsUnion = (RD && RD->isUnion());
3650 uint64_t MaxUnionIvarSize = 0;
3651 uint64_t MaxSkippedUnionIvarSize = 0;
3652 FieldDecl *MaxField = 0;
3653 FieldDecl *MaxSkippedField = 0;
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003654 FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003655 uint64_t MaxFieldOffset = 0;
3656 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003657 uint64_t LastBitfieldOrUnnamedOffset = 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003658
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003659 if (RecFields.empty())
3660 return;
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003661 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3662 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3663
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003664 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003665 FieldDecl *Field = RecFields[i];
Daniel Dunbar62b10702009-05-03 23:35:23 +00003666 uint64_t FieldOffset;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003667 if (RD) {
Daniel Dunbard51c1a62010-04-14 17:02:21 +00003668 // Note that 'i' here is actually the field index inside RD of Field,
3669 // although this dependency is hidden.
3670 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
3671 FieldOffset = RL.getFieldOffset(i) / 8;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003672 } else
Daniel Dunbar62b10702009-05-03 23:35:23 +00003673 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003674
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003675 // Skip over unnamed or bitfields
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003676 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003677 LastFieldBitfieldOrUnnamed = Field;
3678 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003679 continue;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003680 }
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003681
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003682 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003683 QualType FQT = Field->getType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003684 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003685 if (FQT->isUnionType())
3686 HasUnion = true;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003687
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003688 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003689 BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003690 ForStrongLayout, HasUnion);
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003691 continue;
3692 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003693
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003694 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003695 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003696 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003697 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003698 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003699 FQT = CArray->getElementType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003700 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3701 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003702 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003703 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003704 FQT = CArray->getElementType();
3705 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003706
3707 assert(!FQT->isUnionType() &&
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003708 "layout for array of unions not supported");
Fariborz Jahanian9a7d57d2011-01-03 19:23:18 +00003709 if (FQT->isRecordType() && ElCount) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003710 int OldIndex = IvarsInfo.size() - 1;
3711 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003712
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003713 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003714 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003715 ForStrongLayout, HasUnion);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003716
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003717 // Replicate layout information for each array element. Note that
3718 // one element is already done.
3719 uint64_t ElIx = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003720 for (int FirstIndex = IvarsInfo.size() - 1,
3721 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003722 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003723 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3724 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3725 IvarsInfo[i].ivar_size));
3726 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3727 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3728 SkipIvars[i].ivar_size));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003729 }
3730 continue;
3731 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003732 }
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003733 // At this point, we are done with Record/Union and array there of.
3734 // For other arrays we are down to its element type.
John McCall8ccfcb52009-09-24 19:53:00 +00003735 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003736
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003737 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall8ccfcb52009-09-24 19:53:00 +00003738 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3739 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003740 if (IsUnion) {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003741 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003742 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003743 MaxUnionIvarSize = UnionIvarSize;
3744 MaxField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003745 MaxFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003746 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003747 } else {
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003748 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003749 FieldSize / WordSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003750 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003751 } else if ((ForStrongLayout &&
John McCall8ccfcb52009-09-24 19:53:00 +00003752 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3753 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003754 if (IsUnion) {
Mike Stump18bb9282009-05-16 07:57:57 +00003755 // FIXME: Why the asymmetry? We divide by word size in bits on other
3756 // side.
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003757 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003758 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003759 MaxSkippedUnionIvarSize = UnionIvarSize;
3760 MaxSkippedField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003761 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003762 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003763 } else {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003764 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003765 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003766 FieldSize / ByteSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003767 }
3768 }
3769 }
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003770
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003771 if (LastFieldBitfieldOrUnnamed) {
3772 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3773 // Last field was a bitfield. Must update skip info.
3774 Expr *BitWidth = LastFieldBitfieldOrUnnamed->getBitWidth();
3775 uint64_t BitFieldSize =
3776 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
3777 GC_IVAR skivar;
3778 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3779 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3780 + ((BitFieldSize % ByteSizeInBits) != 0);
3781 SkipIvars.push_back(skivar);
3782 } else {
3783 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3784 // Last field was unnamed. Must update skip info.
3785 unsigned FieldSize
3786 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3787 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3788 FieldSize / ByteSizeInBits));
3789 }
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003790 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003791
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003792 if (MaxField)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003793 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003794 MaxUnionIvarSize));
3795 if (MaxSkippedField)
Daniel Dunbar5b743912009-05-03 23:31:46 +00003796 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003797 MaxSkippedUnionIvarSize));
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003798}
3799
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003800/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3801/// the computations and returning the layout bitmap (for ivar or blocks) in
3802/// the given argument BitMap string container. Routine reads
3803/// two containers, IvarsInfo and SkipIvars which are assumed to be
3804/// filled already by the caller.
3805llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003806 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramerabd5b902009-10-13 10:07:13 +00003807 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003808
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003809 // Build the string of skip/scan nibbles
Fariborz Jahaniance5676572009-04-24 17:15:27 +00003810 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003811 unsigned int WordSize =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003812 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003813 if (IvarsInfo[0].ivar_bytepos == 0) {
3814 WordsToSkip = 0;
3815 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003816 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003817 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3818 WordsToScan = IvarsInfo[0].ivar_size;
3819 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003820 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003821 unsigned int TailPrevGCObjC =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003822 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003823 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003824 // consecutive 'scanned' object pointers.
3825 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003826 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003827 // Skip over 'gc'able object pointer which lay over each other.
3828 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3829 continue;
3830 // Must skip over 1 or more words. We save current skip/scan values
3831 // and start a new pair.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003832 SKIP_SCAN SkScan;
3833 SkScan.skip = WordsToSkip;
3834 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003835 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003836
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003837 // Skip the hole.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003838 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3839 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003840 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003841 WordsToSkip = 0;
3842 WordsToScan = IvarsInfo[i].ivar_size;
3843 }
3844 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003845 if (WordsToScan > 0) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003846 SKIP_SCAN SkScan;
3847 SkScan.skip = WordsToSkip;
3848 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003849 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003850 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003851
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003852 if (!SkipIvars.empty()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003853 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003854 int LastByteSkipped =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003855 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003856 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003857 int LastByteScanned =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003858 IvarsInfo[LastIndex].ivar_bytepos +
3859 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003860 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramerd20ef752009-12-25 15:43:36 +00003861 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003862 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003863 SKIP_SCAN SkScan;
3864 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3865 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003866 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003867 }
3868 }
3869 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3870 // as 0xMN.
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003871 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003872 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003873 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3874 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3875 // 0xM0 followed by 0x0N detected.
3876 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3877 for (int j = i+1; j < SkipScan; j++)
3878 SkipScanIvars[j] = SkipScanIvars[j+1];
3879 --SkipScan;
3880 }
3881 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003882
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003883 // Generate the string.
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003884 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003885 unsigned char byte;
3886 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3887 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3888 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3889 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003890
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003891 // first skip big.
3892 for (unsigned int ix = 0; ix < skip_big; ix++)
3893 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003894
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003895 // next (skip small, scan)
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003896 if (skip_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003897 byte = skip_small << 4;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003898 if (scan_big > 0) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003899 byte |= 0xf;
3900 --scan_big;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003901 } else if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003902 byte |= scan_small;
3903 scan_small = 0;
3904 }
3905 BitMap += byte;
3906 }
3907 // next scan big
3908 for (unsigned int ix = 0; ix < scan_big; ix++)
3909 BitMap += (unsigned char)(0x0f);
3910 // last scan small
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003911 if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003912 byte = scan_small;
3913 BitMap += byte;
3914 }
3915 }
3916 // null terminate string.
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003917 unsigned char zero = 0;
3918 BitMap += zero;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003919
3920 llvm::GlobalVariable * Entry =
3921 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3922 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
3923 "__TEXT,__cstring,cstring_literals",
3924 1, true);
3925 return getConstantGEP(VMContext, Entry, 0, 0);
3926}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003927
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003928/// BuildIvarLayout - Builds ivar layout bitmap for the class
3929/// implementation for the __strong or __weak case.
3930/// The layout map displays which words in ivar list must be skipped
3931/// and which must be scanned by GC (see below). String is built of bytes.
3932/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3933/// of words to skip and right nibble is count of words to scan. So, each
3934/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3935/// represented by a 0x00 byte which also ends the string.
3936/// 1. when ForStrongLayout is true, following ivars are scanned:
3937/// - id, Class
3938/// - object *
3939/// - __strong anything
3940///
3941/// 2. When ForStrongLayout is false, following ivars are scanned:
3942/// - __weak anything
3943///
3944llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3945 const ObjCImplementationDecl *OMD,
3946 bool ForStrongLayout) {
3947 bool hasUnion = false;
3948
3949 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
3950 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
3951 return llvm::Constant::getNullValue(PtrTy);
3952
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003953 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003954 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003955 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003956
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003957 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003958 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3959 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3960
3961 if (RecFields.empty())
3962 return llvm::Constant::getNullValue(PtrTy);
3963
3964 SkipIvars.clear();
3965 IvarsInfo.clear();
3966
3967 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
3968 if (IvarsInfo.empty())
3969 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003970 // Sort on byte position in case we encounterred a union nested in
3971 // the ivar list.
3972 if (hasUnion && !IvarsInfo.empty())
3973 std::sort(IvarsInfo.begin(), IvarsInfo.end());
3974 if (hasUnion && !SkipIvars.empty())
3975 std::sort(SkipIvars.begin(), SkipIvars.end());
3976
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003977 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003978 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003979
3980 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003981 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003982 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar56df9772010-08-17 22:39:59 +00003983 OMD->getClassInterface()->getName().data());
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003984 const unsigned char *s = (unsigned char*)BitMap.c_str();
3985 for (unsigned i = 0; i < BitMap.size(); i++)
3986 if (!(s[i] & 0xf0))
3987 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3988 else
3989 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3990 printf("\n");
3991 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003992 return C;
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003993}
3994
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003995llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003996 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3997
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003998 // FIXME: Avoid std::string copying.
3999 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004000 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson41a75022009-08-13 21:57:51 +00004001 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbarcda43072010-07-29 22:57:21 +00004002 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004003 1, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004004
Owen Anderson170229f2009-07-14 23:10:40 +00004005 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004006}
4007
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004008// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004009llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004010 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4011}
4012
Daniel Dunbarf5c18462009-04-20 06:54:31 +00004013llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004014 std::string TypeStr;
4015 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4016
4017 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbarb036db82008-08-13 03:21:16 +00004018
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004019 if (!Entry)
4020 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00004021 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbarcda43072010-07-29 22:57:21 +00004022 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004023 1, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004024
Owen Anderson170229f2009-07-14 23:10:40 +00004025 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004026}
4027
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004028llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004029 std::string TypeStr;
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00004030 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
4031 TypeStr);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004032
4033 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4034
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004035 if (!Entry)
4036 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00004037 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbarcda43072010-07-29 22:57:21 +00004038 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004039 1, true);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004040
Owen Anderson170229f2009-07-14 23:10:40 +00004041 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004042}
4043
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004044// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004045llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004046 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004047
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004048 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004049 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004050 llvm::ConstantArray::get(VMContext,
4051 Ident->getNameStart()),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004052 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004053 1, true);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004054
Owen Anderson170229f2009-07-14 23:10:40 +00004055 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004056}
4057
4058// FIXME: Merge into a single cstring creation function.
Daniel Dunbar4932b362008-08-28 04:38:10 +00004059// FIXME: This Decl should be more precise.
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004060llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004061CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4062 const Decl *Container) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00004063 std::string TypeStr;
4064 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004065 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4066}
4067
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004068void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004069 const ObjCContainerDecl *CD,
Daniel Dunbard2386812009-10-19 01:21:19 +00004070 llvm::SmallVectorImpl<char> &Name) {
4071 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00004072 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbard2386812009-10-19 01:21:19 +00004073 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4074 << '[' << CD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004075 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbard2386812009-10-19 01:21:19 +00004076 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerb11416d2010-04-17 09:33:03 +00004077 OS << '(' << CID << ')';
Daniel Dunbard2386812009-10-19 01:21:19 +00004078 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbara94ecd22008-08-16 03:19:19 +00004079}
4080
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004081void CGObjCMac::FinishModule() {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004082 EmitModuleInfo();
4083
Daniel Dunbarc475d422008-10-29 22:36:39 +00004084 // Emit the dummy bodies for any protocols which were referenced but
4085 // never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004086 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerf56501c2009-07-17 23:57:13 +00004087 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4088 if (I->second->hasInitializer())
Daniel Dunbarc475d422008-10-29 22:36:39 +00004089 continue;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004090
Daniel Dunbarc475d422008-10-29 22:36:39 +00004091 std::vector<llvm::Constant*> Values(5);
Owen Anderson0b75f232009-07-31 20:28:54 +00004092 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004093 Values[1] = GetClassName(I->first);
Owen Anderson0b75f232009-07-31 20:28:54 +00004094 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004095 Values[3] = Values[4] =
Owen Anderson0b75f232009-07-31 20:28:54 +00004096 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004097 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004098 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarc475d422008-10-29 22:36:39 +00004099 Values));
Chris Lattnerf56501c2009-07-17 23:57:13 +00004100 CGM.AddUsedGlobal(I->second);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004101 }
4102
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004103 // Add assembler directives to add lazy undefined symbol references
4104 // for classes which are referenced but not defined. This is
4105 // important for correct linker interaction.
Daniel Dunbard027a922009-09-07 00:20:42 +00004106 //
4107 // FIXME: It would be nice if we had an LLVM construct for this.
4108 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4109 llvm::SmallString<256> Asm;
4110 Asm += CGM.getModule().getModuleInlineAsm();
4111 if (!Asm.empty() && Asm.back() != '\n')
4112 Asm += '\n';
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004113
Daniel Dunbard027a922009-09-07 00:20:42 +00004114 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbard027a922009-09-07 00:20:42 +00004115 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4116 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar07d07852009-10-18 21:17:35 +00004117 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4118 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnera299f2c2010-04-17 18:26:20 +00004119 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004120 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnera299f2c2010-04-17 18:26:20 +00004121 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004122 }
4123
4124 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4125 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4126 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4127 }
Chris Lattnera299f2c2010-04-17 18:26:20 +00004128
Daniel Dunbard027a922009-09-07 00:20:42 +00004129 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004130 }
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004131}
4132
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004133CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004134 : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00004135 ObjCTypes(cgm) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004136 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004137 ObjCABI = 2;
4138}
4139
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004140/* *** */
4141
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004142ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004143 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004144 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4145 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004146
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004147 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004148 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004149 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004150 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004151 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004152
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004153 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004154 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004155 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004156
Mike Stump18bb9282009-05-16 07:57:57 +00004157 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4158 // comes out a bit cleaner.
Daniel Dunbarb036db82008-08-13 03:21:16 +00004159 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004160 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004161
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004162 // I'm not sure I like this. The implicit coordination is a bit
4163 // gross. We should solve this in a reasonable fashion because this
4164 // is a pretty common task (match some runtime data structure with
4165 // an LLVM data structure).
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004166
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004167 // FIXME: This is leaked.
4168 // FIXME: Merge with rewriter code?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004169
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004170 // struct _objc_super {
4171 // id self;
4172 // Class cls;
4173 // }
Abramo Bagnara6150c882010-05-11 21:36:43 +00004174 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004175 Ctx.getTranslationUnitDecl(),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004176 SourceLocation(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004177 &Ctx.Idents.get("_objc_super"));
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004178 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004179 Ctx.getObjCIdType(), 0, 0, false));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004180 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004181 Ctx.getObjCClassType(), 0, 0, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004182 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004183
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004184 SuperCTy = Ctx.getTagDeclType(RD);
4185 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004186
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004187 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004188 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4189
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004190 // struct _prop_t {
4191 // char *name;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004192 // char *attributes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004193 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004194 PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004195 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004196 PropertyTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004197
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004198 // struct _prop_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004199 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004200 // uint32_t count_of_properties;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004201 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004202 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004203 PropertyListTy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004204 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004205 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004206 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004207 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004208 PropertyListTy);
4209 // struct _prop_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004210 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004211
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004212 // struct _objc_method {
4213 // SEL _cmd;
4214 // char *method_type;
4215 // char *_imp;
4216 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004217 MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy,
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004218 Int8PtrTy,
4219 Int8PtrTy,
4220 NULL);
4221 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004222
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004223 // struct _objc_cache *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004224 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004225 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004226 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004227}
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004228
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004229ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004230 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004231 // struct _objc_method_description {
4232 // SEL name;
4233 // char *types;
4234 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004235 MethodDescriptionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004236 llvm::StructType::get(VMContext, SelectorPtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004237 Int8PtrTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00004238 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004239 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004240 MethodDescriptionTy);
4241
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004242 // struct _objc_method_description_list {
4243 // int count;
4244 // struct _objc_method_description[1];
4245 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004246 MethodDescriptionListTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004247 llvm::StructType::get(VMContext, IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004248 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbarb036db82008-08-13 03:21:16 +00004249 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004250 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004251 MethodDescriptionListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004252
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004253 // struct _objc_method_description_list *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004254 MethodDescriptionListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004255 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004256
Daniel Dunbarb036db82008-08-13 03:21:16 +00004257 // Protocol description structures
4258
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004259 // struct _objc_protocol_extension {
4260 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4261 // struct _objc_method_description_list *optional_instance_methods;
4262 // struct _objc_method_description_list *optional_class_methods;
4263 // struct _objc_property_list *instance_properties;
4264 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004265 ProtocolExtensionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004266 llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004267 MethodDescriptionListPtrTy,
4268 MethodDescriptionListPtrTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00004269 PropertyListPtrTy,
4270 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004271 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004272 ProtocolExtensionTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004273
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004274 // struct _objc_protocol_extension *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004275 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004276
Daniel Dunbarc475d422008-10-29 22:36:39 +00004277 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarb036db82008-08-13 03:21:16 +00004278
Owen Andersonc36edfe2009-08-13 23:27:53 +00004279 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
4280 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004281
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004282 const llvm::Type *T =
Mike Stump11289f42009-09-09 15:08:12 +00004283 llvm::StructType::get(VMContext,
Owen Anderson758428f2009-08-05 23:18:46 +00004284 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004285 LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004286 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004287 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004288 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
4289
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004290 // struct _objc_protocol {
4291 // struct _objc_protocol_extension *isa;
4292 // char *protocol_name;
4293 // struct _objc_protocol **_objc_protocol_list;
4294 // struct _objc_method_description_list *instance_methods;
4295 // struct _objc_method_description_list *class_methods;
4296 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004297 T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004298 Int8PtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004299 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbarb036db82008-08-13 03:21:16 +00004300 MethodDescriptionListPtrTy,
4301 MethodDescriptionListPtrTy,
4302 NULL);
4303 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
4304
4305 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004306 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004307 ProtocolListTy);
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004308 // struct _objc_protocol_list *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004309 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004310
4311 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004312 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004313 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004314
4315 // Class description structures
4316
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004317 // struct _objc_ivar {
4318 // char *ivar_name;
4319 // char *ivar_type;
4320 // int ivar_offset;
4321 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004322 IvarTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004323 Int8PtrTy,
4324 IntTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004325 NULL);
4326 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
4327
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004328 // struct _objc_ivar_list *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004329 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004330 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004331 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004332
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004333 // struct _objc_method_list *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004334 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004335 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004336 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004337
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004338 // struct _objc_class_extension *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004339 ClassExtensionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004340 llvm::StructType::get(VMContext, IntTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004341 Int8PtrTy,
4342 PropertyListPtrTy,
4343 NULL);
4344 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004345 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004346
Owen Andersonc36edfe2009-08-13 23:27:53 +00004347 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004348
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004349 // struct _objc_class {
4350 // Class isa;
4351 // Class super_class;
4352 // char *name;
4353 // long version;
4354 // long info;
4355 // long instance_size;
4356 // struct _objc_ivar_list *ivars;
4357 // struct _objc_method_list *methods;
4358 // struct _objc_cache *cache;
4359 // struct _objc_protocol_list *protocols;
4360 // char *ivar_layout;
4361 // struct _objc_class_ext *ext;
4362 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004363 T = llvm::StructType::get(VMContext,
4364 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson9793f0e2009-07-29 22:16:19 +00004365 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004366 Int8PtrTy,
4367 LongTy,
4368 LongTy,
4369 LongTy,
4370 IvarListPtrTy,
4371 MethodListPtrTy,
4372 CachePtrTy,
4373 ProtocolListPtrTy,
4374 Int8PtrTy,
4375 ClassExtensionPtrTy,
4376 NULL);
4377 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004378
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004379 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
4380 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004381 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004382
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004383 // struct _objc_category {
4384 // char *category_name;
4385 // char *class_name;
4386 // struct _objc_method_list *instance_method;
4387 // struct _objc_method_list *class_method;
4388 // uint32_t size; // sizeof(struct _objc_category)
4389 // struct _objc_property_list *instance_properties;// category's @property
4390 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004391 CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004392 Int8PtrTy,
4393 MethodListPtrTy,
4394 MethodListPtrTy,
4395 ProtocolListPtrTy,
4396 IntTy,
4397 PropertyListPtrTy,
4398 NULL);
4399 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
4400
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004401 // Global metadata structures
4402
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004403 // struct _objc_symtab {
4404 // long sel_ref_cnt;
4405 // SEL *refs;
4406 // short cls_def_cnt;
4407 // short cat_def_cnt;
4408 // char *defs[cls_def_cnt + cat_def_cnt];
4409 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004410 SymtabTy = llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004411 SelectorPtrTy,
4412 ShortTy,
4413 ShortTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004414 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004415 NULL);
4416 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004417 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004418
Fariborz Jahanianeee54df2009-01-22 00:37:21 +00004419 // struct _objc_module {
4420 // long version;
4421 // long size; // sizeof(struct _objc_module)
4422 // char *name;
4423 // struct _objc_symtab* symtab;
4424 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004425 ModuleTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004426 llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004427 LongTy,
4428 Int8PtrTy,
4429 SymtabPtrTy,
4430 NULL);
4431 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00004432
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004433
Mike Stump18bb9282009-05-16 07:57:57 +00004434 // FIXME: This is the size of the setjmp buffer and should be target
4435 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson9ff22482008-09-09 10:10:21 +00004436 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004437
Anders Carlsson9ff22482008-09-09 10:10:21 +00004438 // Exceptions
Owen Anderson9793f0e2009-07-29 22:16:19 +00004439 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004440 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004441
4442 ExceptionDataTy =
Chris Lattner5e016ae2010-06-27 07:15:29 +00004443 llvm::StructType::get(VMContext,
4444 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4445 SetJmpBufferSize),
Anders Carlsson9ff22482008-09-09 10:10:21 +00004446 StackPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004447 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson9ff22482008-09-09 10:10:21 +00004448 ExceptionDataTy);
4449
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004450}
4451
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004452ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004453 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004454 // struct _method_list_t {
4455 // uint32_t entsize; // sizeof(struct _objc_method)
4456 // uint32_t method_count;
4457 // struct _objc_method method_list[method_count];
4458 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004459 MethodListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004460 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004461 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004462 NULL);
4463 CGM.getModule().addTypeName("struct.__method_list_t",
4464 MethodListnfABITy);
4465 // struct method_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004466 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004467
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004468 // struct _protocol_t {
4469 // id isa; // NULL
4470 // const char * const protocol_name;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004471 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004472 // const struct method_list_t * const instance_methods;
4473 // const struct method_list_t * const class_methods;
4474 // const struct method_list_t *optionalInstanceMethods;
4475 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004476 // const struct _prop_list_t * properties;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004477 // const uint32_t size; // sizeof(struct _protocol_t)
4478 // const uint32_t flags; // = 0
4479 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004480
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004481 // Holder for struct _protocol_list_t *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004482 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004483
Owen Anderson758428f2009-08-05 23:18:46 +00004484 ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004485 Int8PtrTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004486 llvm::PointerType::getUnqual(
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004487 ProtocolListTyHolder),
4488 MethodListnfABIPtrTy,
4489 MethodListnfABIPtrTy,
4490 MethodListnfABIPtrTy,
4491 MethodListnfABIPtrTy,
4492 PropertyListPtrTy,
4493 IntTy,
4494 IntTy,
4495 NULL);
4496 CGM.getModule().addTypeName("struct._protocol_t",
4497 ProtocolnfABITy);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004498
4499 // struct _protocol_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004500 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004501
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004502 // struct _protocol_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004503 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004504 // struct _protocol_t *[protocol_count];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004505 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004506 ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004507 llvm::ArrayType::get(
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004508 ProtocolnfABIPtrTy, 0),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004509 NULL);
4510 CGM.getModule().addTypeName("struct._objc_protocol_list",
4511 ProtocolListnfABITy);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004512 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004513 ProtocolListnfABITy);
4514
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004515 // struct _objc_protocol_list*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004516 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004517
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004518 // struct _ivar_t {
4519 // unsigned long int *offset; // pointer to ivar offset location
4520 // char *name;
4521 // char *type;
4522 // uint32_t alignment;
4523 // uint32_t size;
4524 // }
Mike Stump11289f42009-09-09 15:08:12 +00004525 IvarnfABITy = llvm::StructType::get(VMContext,
Owen Anderson758428f2009-08-05 23:18:46 +00004526 llvm::PointerType::getUnqual(LongTy),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004527 Int8PtrTy,
4528 Int8PtrTy,
4529 IntTy,
4530 IntTy,
4531 NULL);
4532 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004533
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004534 // struct _ivar_list_t {
4535 // uint32 entsize; // sizeof(struct _ivar_t)
4536 // uint32 count;
4537 // struct _iver_t list[count];
4538 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004539 IvarListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004540 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004541 llvm::ArrayType::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004542 IvarnfABITy, 0),
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004543 NULL);
4544 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004545
Owen Anderson9793f0e2009-07-29 22:16:19 +00004546 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004547
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004548 // struct _class_ro_t {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004549 // uint32_t const flags;
4550 // uint32_t const instanceStart;
4551 // uint32_t const instanceSize;
4552 // uint32_t const reserved; // only when building for 64bit targets
4553 // const uint8_t * const ivarLayout;
4554 // const char *const name;
4555 // const struct _method_list_t * const baseMethods;
4556 // const struct _objc_protocol_list *const baseProtocols;
4557 // const struct _ivar_list_t *const ivars;
4558 // const uint8_t * const weakIvarLayout;
4559 // const struct _prop_list_t * const properties;
4560 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004561
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004562 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson758428f2009-08-05 23:18:46 +00004563 ClassRonfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004564 IntTy,
4565 IntTy,
4566 Int8PtrTy,
4567 Int8PtrTy,
4568 MethodListnfABIPtrTy,
4569 ProtocolListnfABIPtrTy,
4570 IvarListnfABIPtrTy,
4571 Int8PtrTy,
4572 PropertyListPtrTy,
4573 NULL);
4574 CGM.getModule().addTypeName("struct._class_ro_t",
4575 ClassRonfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004576
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004577 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
4578 std::vector<const llvm::Type*> Params;
4579 Params.push_back(ObjectPtrTy);
4580 Params.push_back(SelectorPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004581 ImpnfABITy = llvm::PointerType::getUnqual(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004582 llvm::FunctionType::get(ObjectPtrTy, Params, false));
4583
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004584 // struct _class_t {
4585 // struct _class_t *isa;
4586 // struct _class_t * const superclass;
4587 // void *cache;
4588 // IMP *vtable;
4589 // struct class_ro_t *ro;
4590 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004591
Owen Andersonc36edfe2009-08-13 23:27:53 +00004592 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Anderson170229f2009-07-14 23:10:40 +00004593 ClassnfABITy =
Owen Anderson758428f2009-08-05 23:18:46 +00004594 llvm::StructType::get(VMContext,
4595 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004596 llvm::PointerType::getUnqual(ClassTyHolder),
4597 CachePtrTy,
4598 llvm::PointerType::getUnqual(ImpnfABITy),
4599 llvm::PointerType::getUnqual(ClassRonfABITy),
4600 NULL);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004601 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4602
4603 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004604 ClassnfABITy);
4605
Fariborz Jahanian71394042009-01-23 23:53:38 +00004606 // LLVM for struct _class_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004607 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004608
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004609 // struct _category_t {
4610 // const char * const name;
4611 // struct _class_t *const cls;
4612 // const struct _method_list_t * const instance_methods;
4613 // const struct _method_list_t * const class_methods;
4614 // const struct _protocol_list_t * const protocols;
4615 // const struct _prop_list_t * const properties;
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004616 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004617 CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy,
Fariborz Jahanian71394042009-01-23 23:53:38 +00004618 ClassnfABIPtrTy,
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004619 MethodListnfABIPtrTy,
4620 MethodListnfABIPtrTy,
4621 ProtocolListnfABIPtrTy,
4622 PropertyListPtrTy,
4623 NULL);
4624 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004625
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004626 // New types for nonfragile abi messaging.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004627 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4628 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004629
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004630 // MessageRefTy - LLVM for:
4631 // struct _message_ref_t {
4632 // IMP messenger;
4633 // SEL name;
4634 // };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004635
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004636 // First the clang type for struct _message_ref_t
Abramo Bagnara6150c882010-05-11 21:36:43 +00004637 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004638 Ctx.getTranslationUnitDecl(),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004639 SourceLocation(),
4640 &Ctx.Idents.get("_message_ref_t"));
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004641 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004642 Ctx.VoidPtrTy, 0, 0, false));
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004643 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004644 Ctx.getObjCSelType(), 0, 0, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004645 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004646
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004647 MessageRefCTy = Ctx.getTagDeclType(RD);
4648 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4649 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004650
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004651 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004652 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004653
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004654 // SuperMessageRefTy - LLVM for:
4655 // struct _super_message_ref_t {
4656 // SUPER_IMP messenger;
4657 // SEL name;
4658 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004659 SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy,
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004660 SelectorPtrTy,
4661 NULL);
4662 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004663
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004664 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004665 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4666
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004667
4668 // struct objc_typeinfo {
4669 // const void** vtable; // objc_ehtype_vtable + 2
4670 // const char* name; // c++ typeinfo string
4671 // Class cls;
4672 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004673 EHTypeTy = llvm::StructType::get(VMContext,
4674 llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004675 Int8PtrTy,
4676 ClassnfABIPtrTy,
4677 NULL);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00004678 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004679 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004680}
4681
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004682llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004683 FinishNonFragileABIModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004684
Fariborz Jahanian71394042009-01-23 23:53:38 +00004685 return NULL;
4686}
4687
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004688void CGObjCNonFragileABIMac::AddModuleClassList(const
4689 std::vector<llvm::GlobalValue*>
4690 &Container,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004691 const char *SymbolName,
4692 const char *SectionName) {
4693 unsigned NumClasses = Container.size();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004694
Daniel Dunbar19573e72009-05-15 21:48:48 +00004695 if (!NumClasses)
4696 return;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004697
Daniel Dunbar19573e72009-05-15 21:48:48 +00004698 std::vector<llvm::Constant*> Symbols(NumClasses);
4699 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00004700 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar19573e72009-05-15 21:48:48 +00004701 ObjCTypes.Int8PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004702 llvm::Constant* Init =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004703 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004704 NumClasses),
4705 Symbols);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004706
Daniel Dunbar19573e72009-05-15 21:48:48 +00004707 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00004708 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004709 llvm::GlobalValue::InternalLinkage,
4710 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00004711 SymbolName);
Daniel Dunbar710cb202010-04-25 20:39:32 +00004712 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar19573e72009-05-15 21:48:48 +00004713 GV->setSection(SectionName);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004714 CGM.AddUsedGlobal(GV);
Daniel Dunbar19573e72009-05-15 21:48:48 +00004715}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004716
Fariborz Jahanian71394042009-01-23 23:53:38 +00004717void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4718 // nonfragile abi has no module definition.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004719
Daniel Dunbar19573e72009-05-15 21:48:48 +00004720 // Build list of all implemented class addresses in array
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004721 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004722 AddModuleClassList(DefinedClasses,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004723 "\01L_OBJC_LABEL_CLASS_$",
4724 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahanian67260552009-11-17 21:37:35 +00004725
Fariborz Jahanian67260552009-11-17 21:37:35 +00004726 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4727 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4728 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4729 continue;
4730 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004731 }
4732
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004733 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4734 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4735 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4736 continue;
4737 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4738 }
Fariborz Jahanian67260552009-11-17 21:37:35 +00004739
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004740 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004741 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4742 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004743
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004744 // Build list of all implemented category addresses in array
4745 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004746 AddModuleClassList(DefinedCategories,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004747 "\01L_OBJC_LABEL_CATEGORY_$",
4748 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004749 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004750 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4751 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004752
Daniel Dunbar5e639272010-04-25 20:39:01 +00004753 EmitImageInfo();
Fariborz Jahanian71394042009-01-23 23:53:38 +00004754}
4755
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004756/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004757/// NonLegacyDispatchMethods; false otherwise. What this means is that
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004758/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004759/// message dispatch call for all the rest.
4760///
4761bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004762 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4763 default:
4764 assert(0 && "Invalid dispatch method!");
4765 case CodeGenOptions::Legacy:
Daniel Dunbarca5e3eb2010-02-01 21:07:33 +00004766 return true;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004767 case CodeGenOptions::NonLegacy:
Fariborz Jahaniandfb39832010-04-19 17:53:30 +00004768 return false;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004769 case CodeGenOptions::Mixed:
4770 break;
4771 }
4772
4773 // If so, see whether this selector is in the white-list of things which must
4774 // use the new dispatch convention. We lazily build a dense set for this.
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004775 if (NonLegacyDispatchMethods.empty()) {
4776 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4777 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4778 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4779 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4780 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4781 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4782 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4783 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4784 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4785 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004786
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004787 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4788 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4789 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4790 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4791 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4792 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4793 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4794 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004795 // "countByEnumeratingWithState:objects:count"
Fariborz Jahanian18801362009-05-13 16:19:02 +00004796 IdentifierInfo *KeyIdents[] = {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004797 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4798 &CGM.getContext().Idents.get("objects"),
4799 &CGM.getContext().Idents.get("count")
Fariborz Jahanian18801362009-05-13 16:19:02 +00004800 };
4801 NonLegacyDispatchMethods.insert(
4802 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004803 }
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004804
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004805 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004806}
4807
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004808// Metadata flags
4809enum MetaDataDlags {
4810 CLS = 0x0,
4811 CLS_META = 0x1,
4812 CLS_ROOT = 0x2,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004813 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004814 CLS_EXCEPTION = 0x20
4815};
4816/// BuildClassRoTInitializer - generate meta-data for:
4817/// struct _class_ro_t {
4818/// uint32_t const flags;
4819/// uint32_t const instanceStart;
4820/// uint32_t const instanceSize;
4821/// uint32_t const reserved; // only when building for 64bit targets
4822/// const uint8_t * const ivarLayout;
4823/// const char *const name;
4824/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004825/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004826/// const struct _ivar_list_t *const ivars;
4827/// const uint8_t * const weakIvarLayout;
4828/// const struct _prop_list_t * const properties;
4829/// }
4830///
4831llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004832 unsigned flags,
4833 unsigned InstanceStart,
4834 unsigned InstanceSize,
4835 const ObjCImplementationDecl *ID) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004836 std::string ClassName = ID->getNameAsString();
4837 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004838 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4839 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4840 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004841 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004842 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4843 : BuildIvarLayout(ID, true);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004844 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004845 // const struct _method_list_t * const baseMethods;
4846 std::vector<llvm::Constant*> Methods;
4847 std::string MethodListName("\01l_OBJC_$_");
4848 if (flags & CLS_META) {
4849 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004850 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004851 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004852 // Class methods should always be defined.
4853 Methods.push_back(GetMethodConstant(*i));
4854 }
4855 } else {
4856 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004857 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004858 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004859 // Instance methods should always be defined.
4860 Methods.push_back(GetMethodConstant(*i));
4861 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004862 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004863 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004864 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004865
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004866 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4867 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004868
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004869 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4870 if (llvm::Constant *C = GetMethodConstant(MD))
4871 Methods.push_back(C);
4872 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4873 if (llvm::Constant *C = GetMethodConstant(MD))
4874 Methods.push_back(C);
4875 }
4876 }
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004877 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004878 Values[ 5] = EmitMethodList(MethodListName,
4879 "__DATA, __objc_const", Methods);
4880
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004881 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4882 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004883 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004884 + OID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00004885 OID->all_referenced_protocol_begin(),
4886 OID->all_referenced_protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004887
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004888 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004889 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004890 else
4891 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004892 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4893 : BuildIvarLayout(ID, false);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004894 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004895 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004896 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004897 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4898 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004899 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004900 Values);
4901 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004902 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4903 llvm::GlobalValue::InternalLinkage,
4904 Init,
4905 (flags & CLS_META) ?
4906 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4907 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004908 CLASS_RO_GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004909 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00004910 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004911 return CLASS_RO_GV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004912
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004913}
4914
4915/// BuildClassMetaData - This routine defines that to-level meta-data
4916/// for the given ClassName for:
4917/// struct _class_t {
4918/// struct _class_t *isa;
4919/// struct _class_t * const superclass;
4920/// void *cache;
4921/// IMP *vtable;
4922/// struct class_ro_t *ro;
4923/// }
4924///
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004925llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004926 std::string &ClassName,
4927 llvm::Constant *IsAGV,
4928 llvm::Constant *SuperClassGV,
4929 llvm::Constant *ClassRoGV,
4930 bool HiddenVisibility) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004931 std::vector<llvm::Constant*> Values(5);
4932 Values[0] = IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004933 Values[1] = SuperClassGV;
4934 if (!Values[1])
4935 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004936 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4937 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4938 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004939 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004940 Values);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004941 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4942 GV->setInitializer(Init);
Fariborz Jahanian04087232009-01-31 01:07:39 +00004943 GV->setSection("__DATA, __objc_data");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004944 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004945 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian82208252009-01-31 00:59:10 +00004946 if (HiddenVisibility)
4947 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004948 return GV;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004949}
4950
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004951bool
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00004952CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004953 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004954}
4955
Daniel Dunbar961202372009-05-03 12:57:56 +00004956void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00004957 uint32_t &InstanceStart,
4958 uint32_t &InstanceSize) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004959 const ASTRecordLayout &RL =
Daniel Dunbar9252ee12009-05-04 21:26:30 +00004960 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004961
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004962 // InstanceSize is really instance end.
Ken Dyckd5090c12011-02-11 02:20:09 +00004963 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004964
4965 // If there are no fields, the start is the same as the end.
4966 if (!RL.getFieldCount())
4967 InstanceStart = InstanceSize;
4968 else
4969 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbar554fd792009-04-19 23:41:48 +00004970}
4971
Fariborz Jahanian71394042009-01-23 23:53:38 +00004972void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4973 std::string ClassName = ID->getNameAsString();
4974 if (!ObjCEmptyCacheVar) {
4975 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004976 CGM.getModule(),
4977 ObjCTypes.CacheTy,
4978 false,
4979 llvm::GlobalValue::ExternalLinkage,
4980 0,
4981 "_objc_empty_cache");
4982
Fariborz Jahanian71394042009-01-23 23:53:38 +00004983 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004984 CGM.getModule(),
4985 ObjCTypes.ImpnfABITy,
4986 false,
4987 llvm::GlobalValue::ExternalLinkage,
4988 0,
4989 "_objc_empty_vtable");
Fariborz Jahanian71394042009-01-23 23:53:38 +00004990 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004991 assert(ID->getClassInterface() &&
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004992 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbare3f5cfc2009-04-20 20:18:54 +00004993 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004994 uint32_t InstanceStart =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00004995 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004996 uint32_t InstanceSize = InstanceStart;
4997 uint32_t flags = CLS_META;
Daniel Dunbar15894b72009-04-07 05:48:37 +00004998 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4999 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005000
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005001 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005002
5003 bool classIsHidden =
John McCall457a04e2010-10-22 21:05:15 +00005004 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahanian82208252009-01-31 00:59:10 +00005005 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005006 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00005007 if (ID->getNumIvarInitializers())
5008 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005009 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005010 // class is root
5011 flags |= CLS_ROOT;
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005012 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005013 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005014 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005015 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005016 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5017 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5018 Root = Super;
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005019 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00005020 if (Root->hasAttr<WeakImportAttr>())
5021 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005022 // work on super class metadata symbol.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005023 std::string SuperClassName =
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00005024 ObjCMetaClassName +
5025 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005026 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahanian67260552009-11-17 21:37:35 +00005027 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
5028 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005029 }
5030 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5031 InstanceStart,
5032 InstanceSize,ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005033 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005034 llvm::GlobalVariable *MetaTClass =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005035 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5036 classIsHidden);
Fariborz Jahanian67260552009-11-17 21:37:35 +00005037 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar15894b72009-04-07 05:48:37 +00005038
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005039 // Metadata for the class
5040 flags = CLS;
Fariborz Jahanian82208252009-01-31 00:59:10 +00005041 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005042 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00005043 if (ID->getNumIvarInitializers())
5044 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005045
Douglas Gregor78bd61f2009-06-18 16:11:24 +00005046 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005047 flags |= CLS_EXCEPTION;
5048
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005049 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005050 flags |= CLS_ROOT;
5051 SuperClassGV = 0;
Chris Lattnerb433b272009-04-19 06:02:28 +00005052 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005053 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005054 std::string RootClassName =
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005055 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005056 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahanian67260552009-11-17 21:37:35 +00005057 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
5058 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005059 }
Daniel Dunbar961202372009-05-03 12:57:56 +00005060 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005061 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005062 InstanceStart,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005063 InstanceSize,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005064 ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005065
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005066 TClassName = ObjCClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005067 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005068 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5069 classIsHidden);
Fariborz Jahanian279abd32009-01-30 20:55:31 +00005070 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005071
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005072 // Determine if this class is also "non-lazy".
5073 if (ImplementationIsNonLazy(ID))
5074 DefinedNonLazyClasses.push_back(ClassMD);
5075
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005076 // Force the definition of the EHType if necessary.
5077 if (flags & CLS_EXCEPTION)
5078 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanian71394042009-01-23 23:53:38 +00005079}
5080
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005081/// GenerateProtocolRef - This routine is called to generate code for
5082/// a protocol reference expression; as in:
5083/// @code
5084/// @protocol(Proto1);
5085/// @endcode
5086/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5087/// which will hold address of the protocol meta-data.
5088///
5089llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005090 const ObjCProtocolDecl *PD) {
5091
Fariborz Jahanian464423d2009-04-10 18:47:34 +00005092 // This routine is called for @protocol only. So, we must build definition
5093 // of protocol's meta-data (not a reference to it!)
5094 //
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005095 llvm::Constant *Init =
5096 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
5097 ObjCTypes.ExternalProtocolPtrTy);
5098
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005099 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar56df9772010-08-17 22:39:59 +00005100 ProtocolName += PD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005101
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005102 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5103 if (PTGV)
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005104 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005105 PTGV = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005106 CGM.getModule(),
5107 Init->getType(), false,
5108 llvm::GlobalValue::WeakAnyLinkage,
5109 Init,
5110 ProtocolName);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005111 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5112 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005113 CGM.AddUsedGlobal(PTGV);
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005114 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005115}
5116
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005117/// GenerateCategory - Build metadata for a category implementation.
5118/// struct _category_t {
5119/// const char * const name;
5120/// struct _class_t *const cls;
5121/// const struct _method_list_t * const instance_methods;
5122/// const struct _method_list_t * const class_methods;
5123/// const struct _protocol_list_t * const protocols;
5124/// const struct _prop_list_t * const properties;
5125/// }
5126///
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005127void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005128 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005129 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005130 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5131 "_$_" + OCD->getNameAsString());
5132 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar15894b72009-04-07 05:48:37 +00005133 Interface->getNameAsString());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005134
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005135 std::vector<llvm::Constant*> Values(6);
5136 Values[0] = GetClassName(OCD->getIdentifier());
5137 // meta-class entry symbol
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005138 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanian3ad8dcf2009-11-17 22:02:21 +00005139 if (Interface->hasAttr<WeakImportAttr>())
5140 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5141
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005142 Values[1] = ClassGV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005143 std::vector<llvm::Constant*> Methods;
5144 std::string MethodListName(Prefix);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005145 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005146 "_$_" + OCD->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005147
5148 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005149 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005150 // Instance methods should always be defined.
5151 Methods.push_back(GetMethodConstant(*i));
5152 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005153
5154 Values[2] = EmitMethodList(MethodListName,
5155 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005156 Methods);
5157
5158 MethodListName = Prefix;
5159 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5160 OCD->getNameAsString();
5161 Methods.clear();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005162 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005163 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005164 // Class methods should always be defined.
5165 Methods.push_back(GetMethodConstant(*i));
5166 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005167
5168 Values[3] = EmitMethodList(MethodListName,
5169 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005170 Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005171 const ObjCCategoryDecl *Category =
Fariborz Jahanian066347e2009-01-28 22:18:42 +00005172 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005173 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005174 llvm::SmallString<256> ExtName;
5175 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5176 << OCD->getName();
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005177 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005178 + Interface->getName() + "_$_"
5179 + Category->getName(),
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005180 Category->protocol_begin(),
5181 Category->protocol_end());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005182 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5183 OCD, Category, ObjCTypes);
Mike Stump658fe022009-07-30 22:28:39 +00005184 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00005185 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5186 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005187 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005188
5189 llvm::Constant *Init =
5190 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005191 Values);
5192 llvm::GlobalVariable *GCATV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005193 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005194 false,
5195 llvm::GlobalValue::InternalLinkage,
5196 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005197 ExtCatName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005198 GCATV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005199 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005200 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005201 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005202 DefinedCategories.push_back(GCATV);
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005203
5204 // Determine if this category is also "non-lazy".
5205 if (ImplementationIsNonLazy(OCD))
5206 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005207}
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005208
5209/// GetMethodConstant - Return a struct objc_method constant for the
5210/// given method if it has been defined. The result is null if the
5211/// method has not been defined. The return value has type MethodPtrTy.
5212llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005213 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00005214 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005215 if (!Fn)
5216 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005217
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005218 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005219 Method[0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00005220 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005221 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005222 Method[1] = GetMethodVarType(MD);
Owen Andersonade90fd2009-07-29 18:54:39 +00005223 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005224 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005225}
5226
5227/// EmitMethodList - Build meta-data for method declarations
5228/// struct _method_list_t {
5229/// uint32_t entsize; // sizeof(struct _objc_method)
5230/// uint32_t method_count;
5231/// struct _objc_method method_list[method_count];
5232/// }
5233///
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005234llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
5235 const char *Section,
5236 const ConstantVector &Methods) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005237 // Return null for empty list.
5238 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005239 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005240
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005241 std::vector<llvm::Constant*> Values(3);
5242 // sizeof(struct _objc_method)
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005243 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005244 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005245 // method_count
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005246 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005247 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005248 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005249 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005250 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005251
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005252 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005253 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005254 llvm::GlobalValue::InternalLinkage,
5255 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005256 Name);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005257 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005258 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005259 GV->setSection(Section);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005260 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005261 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005262 ObjCTypes.MethodListnfABIPtrTy);
5263}
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005264
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005265/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5266/// the given ivar.
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005267llvm::GlobalVariable *
5268CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5269 const ObjCIvarDecl *Ivar) {
5270 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar3f86b9c2009-05-05 00:36:57 +00005271 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregorbcced4e2009-04-09 21:40:53 +00005272 '.' + Ivar->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005273 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005274 CGM.getModule().getGlobalVariable(Name);
5275 if (!IvarOffsetGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005276 IvarOffsetGV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005277 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005278 false,
5279 llvm::GlobalValue::ExternalLinkage,
5280 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005281 Name);
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005282 return IvarOffsetGV;
5283}
5284
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005285llvm::Constant *
5286CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5287 const ObjCIvarDecl *Ivar,
5288 unsigned long int Offset) {
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005289 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005290 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005291 Offset));
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005292 IvarOffsetGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005293 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005294
Mike Stump18bb9282009-05-16 07:57:57 +00005295 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5296 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005297 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5298 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall457a04e2010-10-22 21:05:15 +00005299 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00005300 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00005301 else
Fariborz Jahanianbc3c77b2009-04-06 18:30:00 +00005302 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005303 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005304 return IvarOffsetGV;
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005305}
5306
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005307/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005308/// implementation. The return value has type
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005309/// IvarListnfABIPtrTy.
5310/// struct _ivar_t {
5311/// unsigned long int *offset; // pointer to ivar offset location
5312/// char *name;
5313/// char *type;
5314/// uint32_t alignment;
5315/// uint32_t size;
5316/// }
5317/// struct _ivar_list_t {
5318/// uint32 entsize; // sizeof(struct _ivar_t)
5319/// uint32 count;
5320/// struct _iver_t list[count];
5321/// }
5322///
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005323
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005324llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005325 const ObjCImplementationDecl *ID) {
5326
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005327 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005328
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005329 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5330 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005331
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005332 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005333
Daniel Dunbarae032262009-04-20 00:33:43 +00005334 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian63a224a2009-03-31 18:11:23 +00005335 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005336 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005337
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005338 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
5339 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005340 // Ignore unnamed bit-fields.
5341 if (!IVD->getDeclName())
5342 continue;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005343 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar961202372009-05-03 12:57:56 +00005344 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005345 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5346 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005347 const llvm::Type *FieldTy =
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005348 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005349 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005350 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005351 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005352 Align = llvm::Log2_32(Align);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005353 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbarae032262009-04-20 00:33:43 +00005354 // NOTE. Size of a bitfield does not match gcc's, because of the
5355 // way bitfields are treated special in each. But I am told that
5356 // 'size' for bitfield ivars is ignored by the runtime so it does
5357 // not matter. If it matters, there is enough info to get the
5358 // bitfield right!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005359 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005360 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005361 }
5362 // Return null for empty list.
5363 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005364 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005365 std::vector<llvm::Constant*> Values(3);
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005366 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005367 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5368 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005369 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005370 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005371 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005372 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005373 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5374 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005375 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005376 llvm::GlobalValue::InternalLinkage,
5377 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005378 Prefix + OID->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005379 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005380 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005381 GV->setSection("__DATA, __objc_const");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005382
Chris Lattnerf56501c2009-07-17 23:57:13 +00005383 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005384 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005385}
5386
5387llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005388 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005389 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005390
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005391 if (!Entry) {
5392 // We use the initializer as a marker of whether this is a forward
5393 // reference or not. At module finalization we add the empty
5394 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005395 Entry =
5396 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5397 llvm::GlobalValue::ExternalLinkage,
5398 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005399 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005400 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005401 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005402
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005403 return Entry;
5404}
5405
5406/// GetOrEmitProtocol - Generate the protocol meta-data:
5407/// @code
5408/// struct _protocol_t {
5409/// id isa; // NULL
5410/// const char * const protocol_name;
5411/// const struct _protocol_list_t * protocol_list; // super protocols
5412/// const struct method_list_t * const instance_methods;
5413/// const struct method_list_t * const class_methods;
5414/// const struct method_list_t *optionalInstanceMethods;
5415/// const struct method_list_t *optionalClassMethods;
5416/// const struct _prop_list_t * properties;
5417/// const uint32_t size; // sizeof(struct _protocol_t)
5418/// const uint32_t flags; // = 0
5419/// }
5420/// @endcode
5421///
5422
5423llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005424 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005425 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005426
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005427 // Early exit if a defining object has already been generated.
5428 if (Entry && Entry->hasInitializer())
5429 return Entry;
5430
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005431 // Construct method lists.
5432 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5433 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005434 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005435 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005436 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005437 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005438 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5439 OptInstanceMethods.push_back(C);
5440 } else {
5441 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005442 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005443 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005444
5445 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005446 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005447 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005448 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005449 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5450 OptClassMethods.push_back(C);
5451 } else {
5452 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005453 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005454 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005455
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005456 std::vector<llvm::Constant*> Values(10);
5457 // isa is NULL
Owen Anderson0b75f232009-07-31 20:28:54 +00005458 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005459 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005460 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5461 PD->protocol_begin(),
5462 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005463
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005464 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005465 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005466 "__DATA, __objc_const",
5467 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005468 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005469 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005470 "__DATA, __objc_const",
5471 ClassMethods);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005472 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005473 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005474 "__DATA, __objc_const",
5475 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005476 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005477 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005478 "__DATA, __objc_const",
5479 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005480 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005481 0, PD, ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005482 uint32_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005483 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005484 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0b75f232009-07-31 20:28:54 +00005485 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005486 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005487 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005488
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005489 if (Entry) {
5490 // Already created, fix the linkage and update the initializer.
Mike Stumpa6ca3342009-03-07 16:33:28 +00005491 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005492 Entry->setInitializer(Init);
5493 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005494 Entry =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005495 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5496 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5497 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005498 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005499 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005500 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005501 }
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005502 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005503 CGM.AddUsedGlobal(Entry);
5504
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005505 // Use this protocol meta-data to build protocol list table in section
5506 // __DATA, __objc_protolist
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005507 llvm::GlobalVariable *PTGV =
5508 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5509 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5510 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005511 PTGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005512 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbarb25452a2009-04-15 02:56:18 +00005513 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005514 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005515 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005516 return Entry;
5517}
5518
5519/// EmitProtocolList - Generate protocol list meta-data:
5520/// @code
5521/// struct _protocol_list_t {
5522/// long protocol_count; // Note, this is 32/64 bit
5523/// struct _protocol_t[protocol_count];
5524/// }
5525/// @endcode
5526///
5527llvm::Constant *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005528CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5529 ObjCProtocolDecl::protocol_iterator begin,
5530 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005531 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005532
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005533 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005534 if (begin == end)
Owen Anderson0b75f232009-07-31 20:28:54 +00005535 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005536
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005537 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005538 llvm::SmallString<256> TmpName;
5539 Name.toVector(TmpName);
5540 llvm::GlobalVariable *GV =
5541 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005542 if (GV)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005543 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005544
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005545 for (; begin != end; ++begin)
5546 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5547
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005548 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00005549 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005550 ObjCTypes.ProtocolnfABIPtrTy));
5551
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005552 std::vector<llvm::Constant*> Values(2);
Owen Anderson170229f2009-07-14 23:10:40 +00005553 Values[0] =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005554 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005555 Values[1] =
Owen Anderson47034e12009-07-28 18:33:04 +00005556 llvm::ConstantArray::get(
Owen Anderson9793f0e2009-07-29 22:16:19 +00005557 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005558 ProtocolRefs.size()),
5559 ProtocolRefs);
5560
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005561 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005562 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005563 llvm::GlobalValue::InternalLinkage,
5564 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005565 Name);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005566 GV->setSection("__DATA, __objc_const");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005567 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005568 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerf56501c2009-07-17 23:57:13 +00005569 CGM.AddUsedGlobal(GV);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005570 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005571 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005572}
5573
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005574/// GetMethodDescriptionConstant - This routine build following meta-data:
5575/// struct _objc_method {
5576/// SEL _cmd;
5577/// char *method_type;
5578/// char *_imp;
5579/// }
5580
5581llvm::Constant *
5582CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5583 std::vector<llvm::Constant*> Desc(3);
Owen Anderson170229f2009-07-14 23:10:40 +00005584 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005585 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5586 ObjCTypes.SelectorPtrTy);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005587 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005588 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00005589 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005590 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005591}
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005592
5593/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5594/// This code gen. amounts to generating code for:
5595/// @code
5596/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5597/// @encode
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005598///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00005599LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall96fa4842010-05-17 21:00:27 +00005600 CodeGen::CodeGenFunction &CGF,
5601 QualType ObjectTy,
5602 llvm::Value *BaseValue,
5603 const ObjCIvarDecl *Ivar,
5604 unsigned CVRQualifiers) {
5605 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00005606 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5607 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005608}
5609
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005610llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005611 CodeGen::CodeGenFunction &CGF,
5612 const ObjCInterfaceDecl *Interface,
5613 const ObjCIvarDecl *Ivar) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005614 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005615}
5616
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005617CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005618 CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005619 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005620 QualType ResultType,
5621 Selector Sel,
5622 llvm::Value *Receiver,
5623 QualType Arg0Ty,
5624 bool IsSuper,
5625 const CallArgList &CallArgs) {
Mike Stump18bb9282009-05-16 07:57:57 +00005626 // FIXME. Even though IsSuper is passes. This function doese not handle calls
5627 // to 'super' receivers.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005628 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005629 llvm::Value *Arg0 = Receiver;
5630 if (!IsSuper)
5631 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005632
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005633 // Find the message function name.
Mike Stump18bb9282009-05-16 07:57:57 +00005634 // FIXME. This is too much work to get the ABI-specific result type needed to
5635 // find the message name.
John McCall2da83a32010-02-26 00:48:12 +00005636 const CGFunctionInfo &FnInfo
Rafael Espindolac50c27c2010-03-30 20:24:48 +00005637 = Types.getFunctionInfo(ResultType, CallArgList(),
5638 FunctionType::ExtInfo());
Fariborz Jahaniane29b4f02009-04-30 23:08:58 +00005639 llvm::Constant *Fn = 0;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005640 std::string Name("\01l_");
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00005641 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
John McCallc5799442011-02-26 09:48:59 +00005642 EmitNullReturnInitialization(CGF, Return, ResultType);
Chris Lattner396639d2010-08-18 16:09:06 +00005643 if (IsSuper) {
5644 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5645 Name += "objc_msgSendSuper2_stret_fixup";
5646 } else {
5647 Fn = ObjCTypes.getMessageSendStretFixupFn();
5648 Name += "objc_msgSend_stret_fixup";
5649 }
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00005650 } else if (!IsSuper && CGM.ReturnTypeUsesFPRet(ResultType)) {
5651 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5652 Name += "objc_msgSend_fpret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00005653 } else {
Chris Lattner396639d2010-08-18 16:09:06 +00005654 if (IsSuper) {
5655 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
5656 Name += "objc_msgSendSuper2_fixup";
5657 } else {
5658 Fn = ObjCTypes.getMessageSendFixupFn();
5659 Name += "objc_msgSend_fixup";
5660 }
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005661 }
Fariborz Jahaniane29b4f02009-04-30 23:08:58 +00005662 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005663 Name += '_';
5664 std::string SelName(Sel.getAsString());
5665 // Replace all ':' in selector name with '_' ouch!
Mike Stump11289f42009-09-09 15:08:12 +00005666 for (unsigned i = 0; i < SelName.size(); i++)
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005667 if (SelName[i] == ':')
5668 SelName[i] = '_';
5669 Name += SelName;
5670 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5671 if (!GV) {
Daniel Dunbare60aa052009-04-15 19:03:14 +00005672 // Build message ref table entry.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005673 std::vector<llvm::Constant*> Values(2);
5674 Values[0] = Fn;
5675 Values[1] = GetMethodVarName(Sel);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005676 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005677 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stumpa6ca3342009-03-07 16:33:28 +00005678 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005679 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005680 Name);
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005681 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar24645c92009-04-15 19:04:46 +00005682 GV->setAlignment(16);
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005683 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005684 }
5685 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005686
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005687 CallArgList ActualArgs;
5688 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005689 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005690 ObjCTypes.MessageRefCPtrTy));
5691 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
John McCallab26cfa2010-02-05 21:31:56 +00005692 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00005693 FunctionType::ExtInfo());
Fariborz Jahanian4e87c832009-02-05 01:13:09 +00005694 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5695 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian35afdfc2009-02-14 21:25:36 +00005696 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanian4e87c832009-02-05 01:13:09 +00005697 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Anderson9793f0e2009-07-29 22:16:19 +00005698 llvm::PointerType::getUnqual(FTy));
John McCall78a15112010-05-22 01:48:05 +00005699 return CGF.EmitCall(FnInfo1, Callee, Return, ActualArgs);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005700}
5701
5702/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005703CodeGen::RValue
5704CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005705 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005706 QualType ResultType,
5707 Selector Sel,
5708 llvm::Value *Receiver,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005709 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00005710 const ObjCInterfaceDecl *Class,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005711 const ObjCMethodDecl *Method) {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005712 return LegacyDispatchedSelector(Sel)
John McCall78a15112010-05-22 01:48:05 +00005713 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5714 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005715 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005716 false, CallArgs, Method, ObjCTypes)
John McCall78a15112010-05-22 01:48:05 +00005717 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005718 Receiver, CGF.getContext().getObjCIdType(),
5719 false, CallArgs);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005720}
5721
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005722llvm::GlobalVariable *
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005723CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005724 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5725
Daniel Dunbara6468342009-03-02 05:18:14 +00005726 if (!GV) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005727 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005728 false, llvm::GlobalValue::ExternalLinkage,
5729 0, Name);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005730 }
5731
5732 return GV;
5733}
5734
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005735llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5736 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005737 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005738
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005739 if (!Entry) {
Daniel Dunbar15894b72009-04-07 05:48:37 +00005740 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005741 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005742 Entry =
5743 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005744 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005745 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005746 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005747 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005748 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005749 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005750 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005751 CGM.AddUsedGlobal(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005752 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005753
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005754 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005755}
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005756
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005757llvm::Value *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005758CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005759 const ObjCInterfaceDecl *ID) {
5760 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005761
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005762 if (!Entry) {
5763 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5764 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005765 Entry =
5766 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005767 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005768 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005769 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005770 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005771 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005772 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005773 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005774 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005775 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005776
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005777 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005778}
5779
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005780/// EmitMetaClassRef - Return a Value * of the address of _class_t
5781/// meta-data
5782///
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005783llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5784 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005785 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5786 if (Entry)
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005787 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005788
Daniel Dunbar15894b72009-04-07 05:48:37 +00005789 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005790 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005791 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005792 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005793 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005794 MetaClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005795 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005796 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005797 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005798 ObjCTypes.ClassnfABIPtrTy));
5799
Daniel Dunbare60aa052009-04-15 19:03:14 +00005800 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005801 CGM.AddUsedGlobal(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005802
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005803 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005804}
5805
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005806/// GetClass - Return a reference to the class for the given interface
5807/// decl.
5808llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5809 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian95ace552009-11-17 22:42:00 +00005810 if (ID->hasAttr<WeakImportAttr>()) {
5811 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5812 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5813 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5814 }
5815
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005816 return EmitClassRef(Builder, ID);
5817}
5818
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005819/// Generates a message send where the super is the receiver. This is
5820/// a message send to self with special delivery semantics indicating
5821/// which class's method should be called.
5822CodeGen::RValue
5823CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005824 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005825 QualType ResultType,
5826 Selector Sel,
5827 const ObjCInterfaceDecl *Class,
5828 bool isCategoryImpl,
5829 llvm::Value *Receiver,
5830 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005831 const CodeGen::CallArgList &CallArgs,
5832 const ObjCMethodDecl *Method) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005833 // ...
5834 // Create and init a super structure; this is a (receiver, class)
5835 // pair we will pass to objc_msgSendSuper.
5836 llvm::Value *ObjCSuper =
5837 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005838
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005839 llvm::Value *ReceiverAsObject =
5840 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5841 CGF.Builder.CreateStore(ReceiverAsObject,
5842 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005843
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005844 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005845 llvm::Value *Target;
5846 if (IsClassMessage) {
5847 if (isCategoryImpl) {
5848 // Message sent to "super' in a class method defined in
5849 // a category implementation.
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005850 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005851 Target = CGF.Builder.CreateStructGEP(Target, 0);
5852 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00005853 } else
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005854 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stump658fe022009-07-30 22:28:39 +00005855 } else
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005856 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005857
Mike Stump18bb9282009-05-16 07:57:57 +00005858 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5859 // ObjCTypes types.
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005860 const llvm::Type *ClassTy =
5861 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5862 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5863 CGF.Builder.CreateStore(Target,
5864 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005865
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005866 return (LegacyDispatchedSelector(Sel))
John McCall78a15112010-05-22 01:48:05 +00005867 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5868 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005869 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005870 true, CallArgs, Method, ObjCTypes)
John McCall78a15112010-05-22 01:48:05 +00005871 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005872 ObjCSuper, ObjCTypes.SuperPtrCTy,
5873 true, CallArgs);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005874}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005875
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005876llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005877 Selector Sel, bool lval) {
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005878 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005879
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005880 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005881 llvm::Constant *Casted =
5882 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5883 ObjCTypes.SelectorPtrTy);
5884 Entry =
5885 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5886 llvm::GlobalValue::InternalLinkage,
5887 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005888 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005889 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005890 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005891
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005892 if (lval)
5893 return Entry;
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005894 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005895}
Fariborz Jahanian06292952009-02-16 22:52:32 +00005896/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005897/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian06292952009-02-16 22:52:32 +00005898///
5899void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005900 llvm::Value *src,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005901 llvm::Value *dst,
5902 llvm::Value *ivarOffset) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005903 const llvm::Type * SrcTy = src->getType();
5904 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005905 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005906 assert(Size <= 8 && "does not support size > 8");
5907 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5908 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005909 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5910 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005911 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5912 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005913 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5914 src, dst, ivarOffset);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005915 return;
5916}
5917
5918/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5919/// objc_assign_strongCast (id src, id *dst)
5920///
5921void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005922 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005923 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005924 const llvm::Type * SrcTy = src->getType();
5925 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005926 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005927 assert(Size <= 8 && "does not support size > 8");
5928 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005929 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005930 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5931 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005932 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5933 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00005934 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005935 src, dst, "weakassign");
5936 return;
5937}
5938
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005939void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005940 CodeGen::CodeGenFunction &CGF,
5941 llvm::Value *DestPtr,
5942 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005943 llvm::Value *Size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005944 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5945 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005946 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005947 DestPtr, SrcPtr, Size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005948 return;
5949}
5950
Fariborz Jahanian06292952009-02-16 22:52:32 +00005951/// EmitObjCWeakRead - Code gen for loading value of a __weak
5952/// object: objc_read_weak (id *src)
5953///
5954llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005955 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005956 llvm::Value *AddrWeakObj) {
Eli Friedmana374b682009-03-07 03:57:15 +00005957 const llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005958 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5959 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00005960 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005961 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00005962 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005963 return read_weak;
5964}
5965
5966/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5967/// objc_assign_weak (id src, id *dst)
5968///
5969void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005970 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005971 const llvm::Type * SrcTy = src->getType();
5972 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005973 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005974 assert(Size <= 8 && "does not support size > 8");
5975 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5976 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005977 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5978 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005979 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5980 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00005981 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005982 src, dst, "weakassign");
5983 return;
5984}
5985
5986/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5987/// objc_assign_global (id src, id *dst)
5988///
5989void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00005990 llvm::Value *src, llvm::Value *dst,
5991 bool threadlocal) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005992 const llvm::Type * SrcTy = src->getType();
5993 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005994 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005995 assert(Size <= 8 && "does not support size > 8");
5996 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5997 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005998 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5999 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006000 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6001 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00006002 if (!threadlocal)
6003 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
6004 src, dst, "globalassign");
6005 else
6006 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
6007 src, dst, "threadlocalassign");
Fariborz Jahanian06292952009-02-16 22:52:32 +00006008 return;
6009}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006010
John McCallc20acd32010-07-21 00:41:47 +00006011namespace {
John McCallcda666c2010-07-21 07:22:38 +00006012 struct CallSyncExit : EHScopeStack::Cleanup {
John McCallc20acd32010-07-21 00:41:47 +00006013 llvm::Value *SyncExitFn;
6014 llvm::Value *SyncArg;
6015 CallSyncExit(llvm::Value *SyncExitFn, llvm::Value *SyncArg)
6016 : SyncExitFn(SyncExitFn), SyncArg(SyncArg) {}
6017
6018 void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
6019 CGF.Builder.CreateCall(SyncExitFn, SyncArg)->setDoesNotThrow();
6020 }
6021 };
6022}
6023
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006024void
John McCallbd309292010-07-06 01:34:17 +00006025CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6026 const ObjCAtSynchronizedStmt &S) {
6027 // Evaluate the lock operand. This should dominate the cleanup.
6028 llvm::Value *SyncArg = CGF.EmitScalarExpr(S.getSynchExpr());
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006029
John McCallbd309292010-07-06 01:34:17 +00006030 // Acquire the lock.
6031 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
6032 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
6033 ->setDoesNotThrow();
6034
6035 // Register an all-paths cleanup to release the lock.
John McCallcda666c2010-07-21 07:22:38 +00006036 CGF.EHStack.pushCleanup<CallSyncExit>(NormalAndEHCleanup,
6037 ObjCTypes.getSyncExitFn(),
6038 SyncArg);
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006039
John McCallbd309292010-07-06 01:34:17 +00006040 // Emit the body of the statement.
6041 CGF.EmitStmt(S.getSynchBody());
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006042
John McCallbd309292010-07-06 01:34:17 +00006043 // Pop the lock-release cleanup.
6044 CGF.PopCleanupBlock();
6045}
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006046
John McCallbd309292010-07-06 01:34:17 +00006047namespace {
6048 struct CatchHandler {
6049 const VarDecl *Variable;
6050 const Stmt *Body;
6051 llvm::BasicBlock *Block;
6052 llvm::Value *TypeInfo;
6053 };
John McCall5c08ab92010-07-13 22:12:14 +00006054
John McCallcda666c2010-07-21 07:22:38 +00006055 struct CallObjCEndCatch : EHScopeStack::Cleanup {
John McCall5c08ab92010-07-13 22:12:14 +00006056 CallObjCEndCatch(bool MightThrow, llvm::Value *Fn) :
6057 MightThrow(MightThrow), Fn(Fn) {}
6058 bool MightThrow;
6059 llvm::Value *Fn;
6060
6061 void Emit(CodeGenFunction &CGF, bool IsForEH) {
6062 if (!MightThrow) {
6063 CGF.Builder.CreateCall(Fn)->setDoesNotThrow();
6064 return;
6065 }
6066
6067 CGF.EmitCallOrInvoke(Fn, 0, 0);
6068 }
6069 };
John McCallbd309292010-07-06 01:34:17 +00006070}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006071
John McCall2ca705e2010-07-24 00:37:23 +00006072llvm::Constant *
6073CGObjCNonFragileABIMac::GetEHType(QualType T) {
6074 // There's a particular fixed type info for 'id'.
6075 if (T->isObjCIdType() ||
6076 T->isObjCQualifiedIdType()) {
6077 llvm::Constant *IDEHType =
6078 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6079 if (!IDEHType)
6080 IDEHType =
6081 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6082 false,
6083 llvm::GlobalValue::ExternalLinkage,
6084 0, "OBJC_EHTYPE_id");
6085 return IDEHType;
6086 }
6087
6088 // All other types should be Objective-C interface pointer types.
6089 const ObjCObjectPointerType *PT =
6090 T->getAs<ObjCObjectPointerType>();
6091 assert(PT && "Invalid @catch type.");
6092 const ObjCInterfaceType *IT = PT->getInterfaceType();
6093 assert(IT && "Invalid @catch type.");
6094 return GetInterfaceEHType(IT->getDecl(), false);
6095}
6096
John McCallbd309292010-07-06 01:34:17 +00006097void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6098 const ObjCAtTryStmt &S) {
6099 // Jump destination for falling out of catch bodies.
6100 CodeGenFunction::JumpDest Cont;
6101 if (S.getNumCatchStmts())
6102 Cont = CGF.getJumpDestInCurrentScope("eh.cont");
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006103
John McCallbd309292010-07-06 01:34:17 +00006104 CodeGenFunction::FinallyInfo FinallyInfo;
6105 if (const ObjCAtFinallyStmt *Finally = S.getFinallyStmt())
6106 FinallyInfo = CGF.EnterFinallyBlock(Finally->getFinallyBody(),
6107 ObjCTypes.getObjCBeginCatchFn(),
6108 ObjCTypes.getObjCEndCatchFn(),
6109 ObjCTypes.getExceptionRethrowFn());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006110
John McCallbd309292010-07-06 01:34:17 +00006111 llvm::SmallVector<CatchHandler, 8> Handlers;
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006112
John McCallbd309292010-07-06 01:34:17 +00006113 // Enter the catch, if there is one.
6114 if (S.getNumCatchStmts()) {
6115 for (unsigned I = 0, N = S.getNumCatchStmts(); I != N; ++I) {
6116 const ObjCAtCatchStmt *CatchStmt = S.getCatchStmt(I);
Douglas Gregor46a572b2010-04-26 16:46:50 +00006117 const VarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006118
John McCallbd309292010-07-06 01:34:17 +00006119 Handlers.push_back(CatchHandler());
6120 CatchHandler &Handler = Handlers.back();
6121 Handler.Variable = CatchDecl;
6122 Handler.Body = CatchStmt->getCatchBody();
6123 Handler.Block = CGF.createBasicBlock("catch");
6124
6125 // @catch(...) always matches.
Douglas Gregor96c79492010-04-23 22:50:49 +00006126 if (!CatchDecl) {
John McCallbd309292010-07-06 01:34:17 +00006127 Handler.TypeInfo = 0; // catch-all
6128 // Don't consider any other catches.
Douglas Gregor96c79492010-04-23 22:50:49 +00006129 break;
6130 }
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006131
John McCall2ca705e2010-07-24 00:37:23 +00006132 Handler.TypeInfo = GetEHType(CatchDecl->getType());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006133 }
John McCallbd309292010-07-06 01:34:17 +00006134
6135 EHCatchScope *Catch = CGF.EHStack.pushCatch(Handlers.size());
6136 for (unsigned I = 0, E = Handlers.size(); I != E; ++I)
6137 Catch->setHandler(I, Handlers[I].TypeInfo, Handlers[I].Block);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006138 }
John McCallbd309292010-07-06 01:34:17 +00006139
6140 // Emit the try body.
6141 CGF.EmitStmt(S.getTryBody());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006142
John McCallbd309292010-07-06 01:34:17 +00006143 // Leave the try.
6144 if (S.getNumCatchStmts())
6145 CGF.EHStack.popCatch();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006146
John McCallbd309292010-07-06 01:34:17 +00006147 // Remember where we were.
6148 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006149
John McCallbd309292010-07-06 01:34:17 +00006150 // Emit the handlers.
6151 for (unsigned I = 0, E = Handlers.size(); I != E; ++I) {
6152 CatchHandler &Handler = Handlers[I];
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006153
John McCallbd309292010-07-06 01:34:17 +00006154 CGF.EmitBlock(Handler.Block);
6155 llvm::Value *RawExn = CGF.Builder.CreateLoad(CGF.getExceptionSlot());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006156
John McCallbd309292010-07-06 01:34:17 +00006157 // Enter the catch.
6158 llvm::CallInst *Exn =
6159 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), RawExn,
6160 "exn.adjusted");
6161 Exn->setDoesNotThrow();
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006162
John McCallbd309292010-07-06 01:34:17 +00006163 // Add a cleanup to leave the catch.
John McCall5c08ab92010-07-13 22:12:14 +00006164 bool EndCatchMightThrow = (Handler.Variable == 0);
John McCallcda666c2010-07-21 07:22:38 +00006165 CGF.EHStack.pushCleanup<CallObjCEndCatch>(NormalAndEHCleanup,
6166 EndCatchMightThrow,
6167 ObjCTypes.getObjCEndCatchFn());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006168
John McCallbd309292010-07-06 01:34:17 +00006169 // Bind the catch parameter if it exists.
6170 if (const VarDecl *CatchParam = Handler.Variable) {
6171 const llvm::Type *CatchType = CGF.ConvertType(CatchParam->getType());
6172 llvm::Value *CastExn = CGF.Builder.CreateBitCast(Exn, CatchType);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006173
John McCall1c9c3fd2010-10-15 04:57:14 +00006174 CGF.EmitAutoVarDecl(*CatchParam);
John McCallbd309292010-07-06 01:34:17 +00006175 CGF.Builder.CreateStore(CastExn, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006176 }
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006177
John McCallbd309292010-07-06 01:34:17 +00006178 CGF.ObjCEHValueStack.push_back(Exn);
6179 CGF.EmitStmt(Handler.Body);
6180 CGF.ObjCEHValueStack.pop_back();
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006181
John McCallbd309292010-07-06 01:34:17 +00006182 // Leave the earlier cleanup.
6183 CGF.PopCleanupBlock();
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006184
John McCallbd309292010-07-06 01:34:17 +00006185 CGF.EmitBranchThroughCleanup(Cont);
6186 }
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006187
John McCallbd309292010-07-06 01:34:17 +00006188 // Go back to the try-statement fallthrough.
6189 CGF.Builder.restoreIP(SavedIP);
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006190
John McCallbd309292010-07-06 01:34:17 +00006191 // Pop out of the normal cleanup on the finally.
6192 if (S.getFinallyStmt())
6193 CGF.ExitFinallyBlock(FinallyInfo);
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006194
John McCallad5d61e2010-07-23 21:56:41 +00006195 if (Cont.isValid())
6196 CGF.EmitBlock(Cont.getBlock());
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006197}
6198
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006199/// EmitThrowStmt - Generate code for a throw statement.
6200void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6201 const ObjCAtThrowStmt &S) {
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006202 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall17afe452010-10-16 08:21:07 +00006203 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
John McCalld5091822010-10-16 16:34:08 +00006204 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy,
6205 "tmp");
John McCall17afe452010-10-16 08:21:07 +00006206 llvm::Value *Args[] = { Exception };
6207 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(),
6208 Args, Args+1)
6209 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006210 } else {
John McCall17afe452010-10-16 08:21:07 +00006211 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn(), 0, 0)
6212 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006213 }
6214
John McCall17afe452010-10-16 08:21:07 +00006215 CGF.Builder.CreateUnreachable();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006216 CGF.Builder.ClearInsertionPoint();
6217}
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006218
John McCall2ca705e2010-07-24 00:37:23 +00006219llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006220CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006221 bool ForDefinition) {
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006222 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006223
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006224 // If we don't need a definition, return the entry if found or check
6225 // if we use an external reference.
6226 if (!ForDefinition) {
6227 if (Entry)
6228 return Entry;
Daniel Dunbard7beeea2009-04-07 06:43:45 +00006229
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006230 // If this type (or a super class) has the __objc_exception__
6231 // attribute, emit an external reference.
Douglas Gregor78bd61f2009-06-18 16:11:24 +00006232 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006233 return Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006234 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006235 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006236 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006237 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006238 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006239 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006240
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006241 // Otherwise we need to either make a new entry or fill in the
6242 // initializer.
6243 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar15894b72009-04-07 05:48:37 +00006244 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006245 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006246 llvm::GlobalVariable *VTableGV =
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006247 CGM.getModule().getGlobalVariable(VTableName);
6248 if (!VTableGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006249 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6250 false,
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006251 llvm::GlobalValue::ExternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006252 0, VTableName);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006253
Chris Lattner5e016ae2010-06-27 07:15:29 +00006254 llvm::Value *VTableIdx =
6255 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006256
6257 std::vector<llvm::Constant*> Values(3);
Owen Andersonade90fd2009-07-29 18:54:39 +00006258 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006259 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00006260 Values[2] = GetClassGlobal(ClassName);
Owen Anderson170229f2009-07-14 23:10:40 +00006261 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00006262 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006263
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006264 if (Entry) {
6265 Entry->setInitializer(Init);
6266 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00006267 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006268 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006269 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006270 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006271 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006272 }
6273
John McCall457a04e2010-10-22 21:05:15 +00006274 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar15894b72009-04-07 05:48:37 +00006275 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar710cb202010-04-25 20:39:32 +00006276 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6277 ObjCTypes.EHTypeTy));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006278
6279 if (ForDefinition) {
6280 Entry->setSection("__DATA,__objc_const");
6281 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6282 } else {
6283 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6284 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006285
6286 return Entry;
6287}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006288
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00006289/* *** */
6290
Daniel Dunbarb036db82008-08-13 03:21:16 +00006291CodeGen::CGObjCRuntime *
6292CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbar303e2c22008-08-11 02:45:11 +00006293 return new CGObjCMac(CGM);
6294}
Fariborz Jahanian279eda62009-01-21 22:04:16 +00006295
6296CodeGen::CGObjCRuntime *
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00006297CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00006298 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00006299}