blob: 3cbb296b0b20c7bca06fb73fb72fcbb0241feff8 [file] [log] [blame]
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001//===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides Objective-C code generation targetting the Apple runtime.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000015
Daniel Dunbar198bcb42010-03-31 01:09:11 +000016#include "CGRecordLayout.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000017#include "CodeGenModule.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000018#include "CodeGenFunction.h"
John McCalld16c2cf2011-02-08 08:22:06 +000019#include "CGBlocks.h"
John McCall36f893c2011-01-28 11:13:47 +000020#include "CGCleanup.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000021#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000022#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000023#include "clang/AST/DeclObjC.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Chris Lattner16f00492009-04-26 01:32:48 +000025#include "clang/AST/StmtObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000026#include "clang/Basic/LangOptions.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000027#include "clang/Frontend/CodeGenOptions.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000028
John McCall87bb5822010-07-31 23:20:56 +000029#include "llvm/InlineAsm.h"
30#include "llvm/IntrinsicInst.h"
Owen Anderson69243822009-07-13 04:10:07 +000031#include "llvm/LLVMContext.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000032#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000033#include "llvm/ADT/DenseSet.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000034#include "llvm/ADT/SetVector.h"
35#include "llvm/ADT/SmallString.h"
Fariborz Jahanian191dcd72009-12-12 21:26:21 +000036#include "llvm/ADT/SmallPtrSet.h"
John McCall8e3f8612010-07-13 22:12:14 +000037#include "llvm/Support/CallSite.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000038#include "llvm/Support/raw_ostream.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000039#include "llvm/Target/TargetData.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000040#include <cstdio>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000041
42using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000043using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000044
Daniel Dunbar97776872009-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
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000049static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM,
50 const ObjCInterfaceDecl *OID,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000051 const ObjCImplementationDecl *ID,
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000052 const ObjCIvarDecl *Ivar) {
Daniel Dunbare83be122010-04-02 21:14:02 +000053 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar532d4da2009-05-03 13:15:50 +000054
Daniel Dunbar61ac1d22010-04-02 22:29:40 +000055 // FIXME: We should eliminate the need to have ObjCImplementationDecl passed
56 // in here; it should never be necessary because that should be the lexical
57 // decl context for the ivar.
Daniel Dunbar3a2c80f2010-04-02 15:43:29 +000058
Daniel Dunbar532d4da2009-05-03 13:15:50 +000059 // If we know have an implementation (and the ivar is in it) then
60 // look up in the implementation layout.
Daniel Dunbar6bff2512009-08-03 17:06:42 +000061 const ASTRecordLayout *RL;
Daniel Dunbar532d4da2009-05-03 13:15:50 +000062 if (ID && ID->getClassInterface() == Container)
63 RL = &CGM.getContext().getASTObjCImplementationLayout(ID);
64 else
65 RL = &CGM.getContext().getASTObjCInterfaceLayout(Container);
Daniel Dunbare83be122010-04-02 21:14:02 +000066
67 // Compute field index.
68 //
69 // FIXME: The index here is closely tied to how ASTContext::getObjCLayout is
70 // implemented. This should be fixed to get the information from the layout
71 // directly.
72 unsigned Index = 0;
73 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
74 CGM.getContext().ShallowCollectObjCIvars(Container, Ivars);
75 for (unsigned k = 0, e = Ivars.size(); k != e; ++k) {
76 if (Ivar == Ivars[k])
77 break;
78 ++Index;
79 }
80 assert(Index != Ivars.size() && "Ivar is not inside container!");
David Chisnalle0d98762010-11-03 16:12:44 +000081 assert(Index < RL->getFieldCount() && "Ivar is not inside record layout!");
Daniel Dunbare83be122010-04-02 21:14:02 +000082
Daniel Dunbar532d4da2009-05-03 13:15:50 +000083 return RL->getFieldOffset(Index);
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000084}
85
86uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
87 const ObjCInterfaceDecl *OID,
88 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000089 return LookupFieldBitOffset(CGM, OID, 0, Ivar) / 8;
90}
91
92uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
93 const ObjCImplementationDecl *OID,
94 const ObjCIvarDecl *Ivar) {
95 return LookupFieldBitOffset(CGM, OID->getClassInterface(), OID, Ivar) / 8;
Daniel Dunbar97776872009-04-22 07:32:20 +000096}
97
98LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
99 const ObjCInterfaceDecl *OID,
100 llvm::Value *BaseValue,
101 const ObjCIvarDecl *Ivar,
102 unsigned CVRQualifiers,
103 llvm::Value *Offset) {
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000104 // Compute (type*) ( (char *) BaseValue + Offset)
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000105 const llvm::Type *I8Ptr = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000106 QualType IvarTy = Ivar->getType();
107 const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy);
Daniel Dunbar97776872009-04-22 07:32:20 +0000108 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr);
Daniel Dunbar97776872009-04-22 07:32:20 +0000109 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000110 V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000111
Daniel Dunbar6d5eb762010-08-21 03:44:13 +0000112 if (!Ivar->isBitField()) {
113 LValue LV = CGF.MakeAddrLValue(V, IvarTy);
114 LV.getQuals().addCVRQualifiers(CVRQualifiers);
115 return LV;
116 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +0000117
Daniel Dunbarc5c446c2010-09-02 23:53:31 +0000118 // We need to compute an access strategy for this bit-field. We are given the
119 // offset to the first byte in the bit-field, the sub-byte offset is taken
120 // from the original layout. We reuse the normal bit-field access strategy by
121 // treating this as an access to a struct where the bit-field is in byte 0,
122 // and adjust the containing type size as appropriate.
123 //
124 // FIXME: Note that currently we make a very conservative estimate of the
125 // alignment of the bit-field, because (a) it is not clear what guarantees the
126 // runtime makes us, and (b) we don't have a way to specify that the struct is
127 // at an alignment plus offset.
128 //
129 // Note, there is a subtle invariant here: we can only call this routine on
130 // non-synthesized ivars but we may be called for synthesized ivars. However,
131 // a synthesized ivar can never be a bit-field, so this is safe.
132 const ASTRecordLayout &RL =
133 CGF.CGM.getContext().getASTObjCInterfaceLayout(OID);
Ken Dyck5f022d82011-02-09 01:59:34 +0000134 uint64_t TypeSizeInBits =
135 RL.getSize().getQuantity() * CGF.CGM.getContext().getCharWidth();
Daniel Dunbarc5c446c2010-09-02 23:53:31 +0000136 uint64_t FieldBitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar);
137 uint64_t BitOffset = FieldBitOffset % 8;
138 uint64_t ContainingTypeAlign = 8;
139 uint64_t ContainingTypeSize = TypeSizeInBits - (FieldBitOffset - BitOffset);
Daniel Dunbar56229f52010-04-05 16:20:33 +0000140 uint64_t BitFieldSize =
141 Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue();
Daniel Dunbar97776872009-04-22 07:32:20 +0000142
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000143 // Allocate a new CGBitFieldInfo object to describe this access.
144 //
145 // FIXME: This is incredibly wasteful, these should be uniqued or part of some
146 // layout object. However, this is blocked on other cleanups to the
147 // Objective-C code, so for now we just live with allocating a bunch of these
148 // objects.
Daniel Dunbarc5c446c2010-09-02 23:53:31 +0000149 CGBitFieldInfo *Info = new (CGF.CGM.getContext()) CGBitFieldInfo(
150 CGBitFieldInfo::MakeInfo(CGF.CGM.getTypes(), Ivar, BitOffset, BitFieldSize,
151 ContainingTypeSize, ContainingTypeAlign));
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000152
Daniel Dunbar76695ca2010-08-21 04:05:54 +0000153 return LValue::MakeBitfield(V, *Info,
154 IvarTy.getCVRQualifiers() | CVRQualifiers);
Daniel Dunbar97776872009-04-22 07:32:20 +0000155}
156
157///
158
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000159namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000160
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000161typedef std::vector<llvm::Constant*> ConstantVector;
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000162
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000163// FIXME: We should find a nicer way to make the labels for metadata, string
164// concatenation is lame.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000165
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000166class ObjCCommonTypesHelper {
Owen Andersona1cf15f2009-07-14 23:10:40 +0000167protected:
168 llvm::LLVMContext &VMContext;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000169
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000170private:
171 llvm::Constant *getMessageSendFn() const {
172 // id objc_msgSend (id, SEL, ...)
173 std::vector<const llvm::Type*> Params;
174 Params.push_back(ObjectPtrTy);
175 Params.push_back(SelectorPtrTy);
176 return
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000177 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
178 Params, true),
179 "objc_msgSend");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000180 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000181
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000182 llvm::Constant *getMessageSendStretFn() const {
183 // id objc_msgSend_stret (id, SEL, ...)
184 std::vector<const llvm::Type*> Params;
185 Params.push_back(ObjectPtrTy);
186 Params.push_back(SelectorPtrTy);
187 return
Owen Anderson0032b272009-08-13 21:57:51 +0000188 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000189 Params, true),
190 "objc_msgSend_stret");
191
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000192 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000193
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000194 llvm::Constant *getMessageSendFpretFn() const {
195 // FIXME: This should be long double on x86_64?
196 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
197 std::vector<const llvm::Type*> Params;
198 Params.push_back(ObjectPtrTy);
199 Params.push_back(SelectorPtrTy);
200 return
Owen Anderson0032b272009-08-13 21:57:51 +0000201 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
202 llvm::Type::getDoubleTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000203 Params,
204 true),
205 "objc_msgSend_fpret");
206
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000207 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000208
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000209 llvm::Constant *getMessageSendSuperFn() const {
210 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
211 const char *SuperName = "objc_msgSendSuper";
212 std::vector<const llvm::Type*> Params;
213 Params.push_back(SuperPtrTy);
214 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000215 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000216 Params, true),
217 SuperName);
218 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000219
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000220 llvm::Constant *getMessageSendSuperFn2() const {
221 // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
222 const char *SuperName = "objc_msgSendSuper2";
223 std::vector<const llvm::Type*> Params;
224 Params.push_back(SuperPtrTy);
225 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000226 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000227 Params, true),
228 SuperName);
229 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000230
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000231 llvm::Constant *getMessageSendSuperStretFn() const {
232 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
233 // SEL op, ...)
234 std::vector<const llvm::Type*> Params;
235 Params.push_back(Int8PtrTy);
236 Params.push_back(SuperPtrTy);
237 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000238 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000239 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000240 Params, true),
241 "objc_msgSendSuper_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000242 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000243
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000244 llvm::Constant *getMessageSendSuperStretFn2() const {
245 // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
246 // SEL op, ...)
247 std::vector<const llvm::Type*> Params;
248 Params.push_back(Int8PtrTy);
249 Params.push_back(SuperPtrTy);
250 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000251 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000252 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000253 Params, true),
254 "objc_msgSendSuper2_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000255 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000256
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000257 llvm::Constant *getMessageSendSuperFpretFn() const {
258 // There is no objc_msgSendSuper_fpret? How can that work?
259 return getMessageSendSuperFn();
260 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000261
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000262 llvm::Constant *getMessageSendSuperFpretFn2() const {
263 // There is no objc_msgSendSuper_fpret? How can that work?
264 return getMessageSendSuperFn2();
265 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000266
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000267protected:
268 CodeGen::CodeGenModule &CGM;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000269
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000270public:
Fariborz Jahanian0a855d02009-03-23 19:10:40 +0000271 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000272 const llvm::Type *Int8PtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000273
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000274 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
275 const llvm::Type *ObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000276
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000277 /// PtrObjectPtrTy - LLVM type for id *
278 const llvm::Type *PtrObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000279
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000280 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000281 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000282 /// ProtocolPtrTy - LLVM type for external protocol handles
283 /// (typeof(Protocol))
284 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000285
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000286 // SuperCTy - clang type for struct objc_super.
287 QualType SuperCTy;
288 // SuperPtrCTy - clang type for struct objc_super *.
289 QualType SuperPtrCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000290
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000291 /// SuperTy - LLVM type for struct objc_super.
292 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000293 /// SuperPtrTy - LLVM type for struct objc_super *.
294 const llvm::Type *SuperPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000295
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000296 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
297 /// in GCC parlance).
298 const llvm::StructType *PropertyTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000299
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000300 /// PropertyListTy - LLVM type for struct objc_property_list
301 /// (_prop_list_t in GCC parlance).
302 const llvm::StructType *PropertyListTy;
303 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
304 const llvm::Type *PropertyListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000305
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000306 // MethodTy - LLVM type for struct objc_method.
307 const llvm::StructType *MethodTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000308
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000309 /// CacheTy - LLVM type for struct objc_cache.
310 const llvm::Type *CacheTy;
311 /// CachePtrTy - LLVM type for struct objc_cache *.
312 const llvm::Type *CachePtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000313
Chris Lattner72db6c32009-04-22 02:44:54 +0000314 llvm::Constant *getGetPropertyFn() {
315 CodeGen::CodeGenTypes &Types = CGM.getTypes();
316 ASTContext &Ctx = CGM.getContext();
317 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
John McCallead608a2010-02-26 00:48:12 +0000318 llvm::SmallVector<CanQualType,4> Params;
319 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
320 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000321 Params.push_back(IdType);
322 Params.push_back(SelType);
323 Params.push_back(Ctx.LongTy);
324 Params.push_back(Ctx.BoolTy);
325 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000326 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000327 FunctionType::ExtInfo()),
328 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000329 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
330 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000331
Chris Lattner72db6c32009-04-22 02:44:54 +0000332 llvm::Constant *getSetPropertyFn() {
333 CodeGen::CodeGenTypes &Types = CGM.getTypes();
334 ASTContext &Ctx = CGM.getContext();
335 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
John McCallead608a2010-02-26 00:48:12 +0000336 llvm::SmallVector<CanQualType,6> Params;
337 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
338 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000339 Params.push_back(IdType);
340 Params.push_back(SelType);
341 Params.push_back(Ctx.LongTy);
342 Params.push_back(IdType);
343 Params.push_back(Ctx.BoolTy);
344 Params.push_back(Ctx.BoolTy);
345 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000346 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000347 FunctionType::ExtInfo()),
348 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000349 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
350 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000351
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000352
353 llvm::Constant *getCopyStructFn() {
354 CodeGen::CodeGenTypes &Types = CGM.getTypes();
355 ASTContext &Ctx = CGM.getContext();
356 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
357 llvm::SmallVector<CanQualType,5> Params;
358 Params.push_back(Ctx.VoidPtrTy);
359 Params.push_back(Ctx.VoidPtrTy);
360 Params.push_back(Ctx.LongTy);
361 Params.push_back(Ctx.BoolTy);
362 Params.push_back(Ctx.BoolTy);
363 const llvm::FunctionType *FTy =
364 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
365 FunctionType::ExtInfo()),
366 false);
367 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
368 }
369
Chris Lattner72db6c32009-04-22 02:44:54 +0000370 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000371 CodeGen::CodeGenTypes &Types = CGM.getTypes();
372 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000373 // void objc_enumerationMutation (id)
John McCallead608a2010-02-26 00:48:12 +0000374 llvm::SmallVector<CanQualType,1> Params;
375 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000376 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000377 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000378 FunctionType::ExtInfo()),
379 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000380 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
381 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000382
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000383 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000384 llvm::Constant *getGcReadWeakFn() {
385 // id objc_read_weak (id *)
386 std::vector<const llvm::Type*> Args;
387 Args.push_back(ObjectPtrTy->getPointerTo());
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000388 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000389 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000390 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000391 }
392
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000393 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000394 llvm::Constant *getGcAssignWeakFn() {
395 // id objc_assign_weak (id, id *)
396 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
397 Args.push_back(ObjectPtrTy->getPointerTo());
398 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000399 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner96508e12009-04-17 22:12:36 +0000400 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
401 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000402
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000403 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000404 llvm::Constant *getGcAssignGlobalFn() {
405 // id objc_assign_global(id, id *)
406 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
407 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000408 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000409 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000410 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
411 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000412
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000413 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
414 llvm::Constant *getGcAssignThreadLocalFn() {
415 // id objc_assign_threadlocal(id src, id * dest)
416 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
417 Args.push_back(ObjectPtrTy->getPointerTo());
418 llvm::FunctionType *FTy =
419 llvm::FunctionType::get(ObjectPtrTy, Args, false);
420 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
421 }
422
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000423 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000424 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000425 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattnerbbccd612009-04-22 02:38:11 +0000426 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
427 Args.push_back(ObjectPtrTy->getPointerTo());
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000428 Args.push_back(LongTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000429 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000430 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000431 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
432 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000433
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000434 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
435 llvm::Constant *GcMemmoveCollectableFn() {
436 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
437 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
438 Args.push_back(Int8PtrTy);
439 Args.push_back(LongTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000440 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000441 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
442 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000443
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000444 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000445 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000446 // id objc_assign_strongCast(id, id *)
Chris Lattnerbbccd612009-04-22 02:38:11 +0000447 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
448 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000449 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000450 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000451 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
452 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000453
454 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000455 llvm::Constant *getExceptionThrowFn() {
456 // void objc_exception_throw(id)
457 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
458 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000459 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000460 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
461 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000462
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000463 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
464 llvm::Constant *getExceptionRethrowFn() {
465 // void objc_exception_rethrow(void)
466 std::vector<const llvm::Type*> Args;
467 llvm::FunctionType *FTy =
John McCall7ec404c2010-10-16 08:21:07 +0000468 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000469 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
470 }
471
Daniel Dunbar1c566672009-02-24 01:43:46 +0000472 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000473 llvm::Constant *getSyncEnterFn() {
474 // void objc_sync_enter (id)
475 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
476 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000477 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000478 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
479 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000480
Daniel Dunbar1c566672009-02-24 01:43:46 +0000481 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000482 llvm::Constant *getSyncExitFn() {
483 // void objc_sync_exit (id)
484 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
485 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000486 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000487 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
488 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000489
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000490 llvm::Constant *getSendFn(bool IsSuper) const {
491 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
492 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000493
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000494 llvm::Constant *getSendFn2(bool IsSuper) const {
495 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
496 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000497
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000498 llvm::Constant *getSendStretFn(bool IsSuper) const {
499 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
500 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000501
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000502 llvm::Constant *getSendStretFn2(bool IsSuper) const {
503 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
504 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000505
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000506 llvm::Constant *getSendFpretFn(bool IsSuper) const {
507 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
508 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000509
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000510 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
511 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
512 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000513
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000514 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
515 ~ObjCCommonTypesHelper(){}
516};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000517
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000518/// ObjCTypesHelper - Helper class that encapsulates lazy
519/// construction of varies types used during ObjC generation.
520class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000521public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000522 /// SymtabTy - LLVM type for struct objc_symtab.
523 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000524 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
525 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000526 /// ModuleTy - LLVM type for struct objc_module.
527 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000528
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000529 /// ProtocolTy - LLVM type for struct objc_protocol.
530 const llvm::StructType *ProtocolTy;
531 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
532 const llvm::Type *ProtocolPtrTy;
533 /// ProtocolExtensionTy - LLVM type for struct
534 /// objc_protocol_extension.
535 const llvm::StructType *ProtocolExtensionTy;
536 /// ProtocolExtensionTy - LLVM type for struct
537 /// objc_protocol_extension *.
538 const llvm::Type *ProtocolExtensionPtrTy;
539 /// MethodDescriptionTy - LLVM type for struct
540 /// objc_method_description.
541 const llvm::StructType *MethodDescriptionTy;
542 /// MethodDescriptionListTy - LLVM type for struct
543 /// objc_method_description_list.
544 const llvm::StructType *MethodDescriptionListTy;
545 /// MethodDescriptionListPtrTy - LLVM type for struct
546 /// objc_method_description_list *.
547 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000548 /// ProtocolListTy - LLVM type for struct objc_property_list.
549 const llvm::Type *ProtocolListTy;
550 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
551 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000552 /// CategoryTy - LLVM type for struct objc_category.
553 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000554 /// ClassTy - LLVM type for struct objc_class.
555 const llvm::StructType *ClassTy;
556 /// ClassPtrTy - LLVM type for struct objc_class *.
557 const llvm::Type *ClassPtrTy;
558 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
559 const llvm::StructType *ClassExtensionTy;
560 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
561 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000562 // IvarTy - LLVM type for struct objc_ivar.
563 const llvm::StructType *IvarTy;
564 /// IvarListTy - LLVM type for struct objc_ivar_list.
565 const llvm::Type *IvarListTy;
566 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
567 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000568 /// MethodListTy - LLVM type for struct objc_method_list.
569 const llvm::Type *MethodListTy;
570 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
571 const llvm::Type *MethodListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000572
Anders Carlsson124526b2008-09-09 10:10:21 +0000573 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
574 const llvm::Type *ExceptionDataTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000575
Anders Carlsson124526b2008-09-09 10:10:21 +0000576 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000577 llvm::Constant *getExceptionTryEnterFn() {
578 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000579 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +0000580 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000581 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000582 Params, false),
583 "objc_exception_try_enter");
Chris Lattner34b02a12009-04-22 02:26:14 +0000584 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000585
586 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000587 llvm::Constant *getExceptionTryExitFn() {
588 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000589 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +0000590 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000591 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000592 Params, false),
593 "objc_exception_try_exit");
Chris Lattner34b02a12009-04-22 02:26:14 +0000594 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000595
596 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000597 llvm::Constant *getExceptionExtractFn() {
598 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000599 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
600 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner34b02a12009-04-22 02:26:14 +0000601 Params, false),
602 "objc_exception_extract");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000603
Chris Lattner34b02a12009-04-22 02:26:14 +0000604 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000605
Anders Carlsson124526b2008-09-09 10:10:21 +0000606 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000607 llvm::Constant *getExceptionMatchFn() {
608 std::vector<const llvm::Type*> Params;
609 Params.push_back(ClassPtrTy);
610 Params.push_back(ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000611 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000612 llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000613 Params, false),
614 "objc_exception_match");
615
Chris Lattner34b02a12009-04-22 02:26:14 +0000616 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000617
Anders Carlsson124526b2008-09-09 10:10:21 +0000618 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000619 llvm::Constant *getSetJmpFn() {
620 std::vector<const llvm::Type*> Params;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000621 Params.push_back(llvm::Type::getInt32PtrTy(VMContext));
Chris Lattner34b02a12009-04-22 02:26:14 +0000622 return
Owen Anderson0032b272009-08-13 21:57:51 +0000623 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattner34b02a12009-04-22 02:26:14 +0000624 Params, false),
625 "_setjmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000626
Chris Lattner34b02a12009-04-22 02:26:14 +0000627 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000628
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000629public:
630 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000631 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000632};
633
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000634/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000635/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000636class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000637public:
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000638
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000639 // MethodListnfABITy - LLVM for struct _method_list_t
640 const llvm::StructType *MethodListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000641
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000642 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
643 const llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000644
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000645 // ProtocolnfABITy = LLVM for struct _protocol_t
646 const llvm::StructType *ProtocolnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000647
Daniel Dunbar948e2582009-02-15 07:36:20 +0000648 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
649 const llvm::Type *ProtocolnfABIPtrTy;
650
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000651 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
652 const llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000653
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000654 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
655 const llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000656
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000657 // ClassnfABITy - LLVM for struct _class_t
658 const llvm::StructType *ClassnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000659
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000660 // ClassnfABIPtrTy - LLVM for struct _class_t*
661 const llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000662
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000663 // IvarnfABITy - LLVM for struct _ivar_t
664 const llvm::StructType *IvarnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000665
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000666 // IvarListnfABITy - LLVM for struct _ivar_list_t
667 const llvm::StructType *IvarListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000668
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000669 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
670 const llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000671
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000672 // ClassRonfABITy - LLVM for struct _class_ro_t
673 const llvm::StructType *ClassRonfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000674
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000675 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
676 const llvm::Type *ImpnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000677
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000678 // CategorynfABITy - LLVM for struct _category_t
679 const llvm::StructType *CategorynfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000680
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000681 // New types for nonfragile abi messaging.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000682
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000683 // MessageRefTy - LLVM for:
684 // struct _message_ref_t {
685 // IMP messenger;
686 // SEL name;
687 // };
688 const llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000689 // MessageRefCTy - clang type for struct _message_ref_t
690 QualType MessageRefCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000691
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000692 // MessageRefPtrTy - LLVM for struct _message_ref_t*
693 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000694 // MessageRefCPtrTy - clang type for struct _message_ref_t*
695 QualType MessageRefCPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000696
Fariborz Jahanianef163782009-02-05 01:13:09 +0000697 // MessengerTy - Type of the messenger (shown as IMP above)
698 const llvm::FunctionType *MessengerTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000699
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000700 // SuperMessageRefTy - LLVM for:
701 // struct _super_message_ref_t {
702 // SUPER_IMP messenger;
703 // SEL name;
704 // };
705 const llvm::StructType *SuperMessageRefTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000706
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000707 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
708 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000709
Chris Lattner1c02f862009-04-22 02:53:24 +0000710 llvm::Constant *getMessageSendFixupFn() {
711 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
712 std::vector<const llvm::Type*> Params;
713 Params.push_back(ObjectPtrTy);
714 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000715 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000716 Params, true),
717 "objc_msgSend_fixup");
718 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000719
Chris Lattner1c02f862009-04-22 02:53:24 +0000720 llvm::Constant *getMessageSendFpretFixupFn() {
721 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
722 std::vector<const llvm::Type*> Params;
723 Params.push_back(ObjectPtrTy);
724 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000725 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000726 Params, true),
727 "objc_msgSend_fpret_fixup");
728 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000729
Chris Lattner1c02f862009-04-22 02:53:24 +0000730 llvm::Constant *getMessageSendStretFixupFn() {
731 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
732 std::vector<const llvm::Type*> Params;
733 Params.push_back(ObjectPtrTy);
734 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000735 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000736 Params, true),
737 "objc_msgSend_stret_fixup");
738 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000739
Chris Lattner1c02f862009-04-22 02:53:24 +0000740 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000741 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000742 // struct _super_message_ref_t*, ...)
743 std::vector<const llvm::Type*> Params;
744 Params.push_back(SuperPtrTy);
745 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000746 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000747 Params, true),
748 "objc_msgSendSuper2_fixup");
749 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000750
Chris Lattner1c02f862009-04-22 02:53:24 +0000751 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000752 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000753 // struct _super_message_ref_t*, ...)
754 std::vector<const llvm::Type*> Params;
755 Params.push_back(SuperPtrTy);
756 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000757 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000758 Params, true),
759 "objc_msgSendSuper2_stret_fixup");
760 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000761
Chris Lattner8a569112009-04-22 02:15:23 +0000762 llvm::Constant *getObjCEndCatchFn() {
Owen Anderson0032b272009-08-13 21:57:51 +0000763 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Chris Lattnerb59761b2009-07-01 04:13:52 +0000764 false),
Chris Lattner8a569112009-04-22 02:15:23 +0000765 "objc_end_catch");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000766
Chris Lattner8a569112009-04-22 02:15:23 +0000767 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000768
Chris Lattner8a569112009-04-22 02:15:23 +0000769 llvm::Constant *getObjCBeginCatchFn() {
770 std::vector<const llvm::Type*> Params;
771 Params.push_back(Int8PtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000772 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
Chris Lattner8a569112009-04-22 02:15:23 +0000773 Params, false),
774 "objc_begin_catch");
775 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000776
777 const llvm::StructType *EHTypeTy;
778 const llvm::Type *EHTypePtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000779
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000780 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
781 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000782};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000783
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000784class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000785public:
786 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000787 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000788 public:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000789 unsigned ivar_bytepos;
790 unsigned ivar_size;
791 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000792 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000793
794 // Allow sorting based on byte pos.
795 bool operator<(const GC_IVAR &b) const {
796 return ivar_bytepos < b.ivar_bytepos;
797 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000798 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000799
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000800 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000801 public:
802 unsigned skip;
803 unsigned scan;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000804 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000805 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000806 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000807
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000808protected:
809 CodeGen::CodeGenModule &CGM;
Owen Anderson69243822009-07-13 04:10:07 +0000810 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000811 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000812 unsigned ObjCABI;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000813
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000814 // gc ivar layout bitmap calculation helper caches.
815 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
816 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000817
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000818 /// LazySymbols - Symbols to generate a lazy reference for. See
819 /// DefinedSymbols and FinishModule().
Daniel Dunbar33063492009-09-07 00:20:42 +0000820 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000821
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000822 /// DefinedSymbols - External symbols which are defined by this
823 /// module. The symbols in this list and LazySymbols are used to add
824 /// special linker symbols which ensure that Objective-C modules are
825 /// linked properly.
Daniel Dunbar33063492009-09-07 00:20:42 +0000826 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000827
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000828 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000829 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000830
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000831 /// MethodVarNames - uniqued method variable names.
832 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000833
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +0000834 /// DefinedCategoryNames - list of category names in form Class_Category.
835 llvm::SetVector<std::string> DefinedCategoryNames;
836
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000837 /// MethodVarTypes - uniqued method type signatures. We have to use
838 /// a StringMap here because have no other unique reference.
839 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000840
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000841 /// MethodDefinitions - map of methods which have been defined in
842 /// this translation unit.
843 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000844
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000845 /// PropertyNames - uniqued method variable names.
846 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000847
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000848 /// ClassReferences - uniqued class references.
849 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000850
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000851 /// SelectorReferences - uniqued selector references.
852 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000853
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000854 /// Protocols - Protocols for which an objc_protocol structure has
855 /// been emitted. Forward declarations are handled by creating an
856 /// empty structure whose initializer is filled in when/if defined.
857 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000858
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000859 /// DefinedProtocols - Protocols which have actually been
860 /// defined. We should not need this, see FIXME in GenerateProtocol.
861 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000862
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000863 /// DefinedClasses - List of defined classes.
864 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000865
866 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
867 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000868
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000869 /// DefinedCategories - List of defined categories.
870 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000871
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000872 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
873 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000874
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000875 /// GetNameForMethod - Return a name for the given method.
876 /// \param[out] NameOut - The return value.
877 void GetNameForMethod(const ObjCMethodDecl *OMD,
878 const ObjCContainerDecl *CD,
Daniel Dunbarc575ce72009-10-19 01:21:19 +0000879 llvm::SmallVectorImpl<char> &NameOut);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000880
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000881 /// GetMethodVarName - Return a unique constant for the given
882 /// selector's name. The return value has type char *.
883 llvm::Constant *GetMethodVarName(Selector Sel);
884 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000885
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000886 /// GetMethodVarType - Return a unique constant for the given
887 /// selector's name. The return value has type char *.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000888
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000889 // FIXME: This is a horrible name.
890 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000891 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000892
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000893 /// GetPropertyName - Return a unique constant for the given
894 /// name. The return value has type char *.
895 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000896
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000897 // FIXME: This can be dropped once string functions are unified.
898 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
899 const Decl *Container);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000900
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000901 /// GetClassName - Return a unique constant for the given selector's
902 /// name. The return value has type char *.
903 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000904
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +0000905 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
906
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000907 /// BuildIvarLayout - Builds ivar layout bitmap for the class
908 /// implementation for the __strong or __weak case.
909 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000910 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
911 bool ForStrongLayout);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +0000912
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +0000913 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000914
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000915 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000916 unsigned int BytePos, bool ForStrongLayout,
917 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000918 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000919 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000920 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +0000921 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000922 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000923 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000924
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000925 /// GetIvarLayoutName - Returns a unique constant for the given
926 /// ivar layout bitmap.
927 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
928 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000929
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000930 /// EmitPropertyList - Emit the given property list. The return
931 /// value has type PropertyListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +0000932 llvm::Constant *EmitPropertyList(llvm::Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000933 const Decl *Container,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000934 const ObjCContainerDecl *OCD,
935 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000936
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000937 /// PushProtocolProperties - Push protocol's property on the input stack.
938 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
939 std::vector<llvm::Constant*> &Properties,
940 const Decl *Container,
941 const ObjCProtocolDecl *PROTO,
942 const ObjCCommonTypesHelper &ObjCTypes);
943
Fariborz Jahanianda320092009-01-29 19:24:30 +0000944 /// GetProtocolRef - Return a reference to the internal protocol
945 /// description, creating an empty one if it has not been
946 /// defined. The return value has type ProtocolPtrTy.
947 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000948
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000949 /// CreateMetadataVar - Create a global variable with internal
950 /// linkage for use by the Objective-C runtime.
951 ///
952 /// This is a convenience wrapper which not only creates the
953 /// variable, but also sets the section and alignment and adds the
Chris Lattnerad64e022009-07-17 23:57:13 +0000954 /// global to the "llvm.used" list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000955 ///
956 /// \param Name - The variable name.
957 /// \param Init - The variable initializer; this is also used to
958 /// define the type of the variable.
959 /// \param Section - The section the variable should go into, or 0.
960 /// \param Align - The alignment for the variable, or 0.
961 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000962 /// "llvm.used".
Daniel Dunbar9c29bf52009-10-18 20:48:59 +0000963 llvm::GlobalVariable *CreateMetadataVar(llvm::Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000964 llvm::Constant *Init,
965 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000966 unsigned Align,
967 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000968
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000969 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +0000970 ReturnValueSlot Return,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000971 QualType ResultType,
972 llvm::Value *Sel,
973 llvm::Value *Arg0,
974 QualType Arg0Ty,
975 bool IsSuper,
976 const CallArgList &CallArgs,
Daniel Dunbard6c93d72009-09-17 04:01:22 +0000977 const ObjCMethodDecl *OMD,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000978 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000979
Daniel Dunbarfce176b2010-04-25 20:39:01 +0000980 /// EmitImageInfo - Emit the image info marker used to encode some module
981 /// level information.
982 void EmitImageInfo();
983
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000984public:
Owen Anderson69243822009-07-13 04:10:07 +0000985 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump1eb44332009-09-09 15:08:12 +0000986 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000987
David Chisnall0d13f6f2010-01-23 02:40:42 +0000988 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000989
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000990 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
991 const ObjCContainerDecl *CD=0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000992
Fariborz Jahanianda320092009-01-29 19:24:30 +0000993 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000994
Fariborz Jahanianda320092009-01-29 19:24:30 +0000995 /// GetOrEmitProtocol - Get the protocol object for the given
996 /// declaration, emitting it if necessary. The return value has type
997 /// ProtocolPtrTy.
998 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000999
Fariborz Jahanianda320092009-01-29 19:24:30 +00001000 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1001 /// object for the given declaration, emitting it if needed. These
1002 /// forward references will be filled in with empty bodies if no
1003 /// definition is seen. The return value has type ProtocolPtrTy.
1004 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall6b5a61b2011-02-07 10:33:21 +00001005 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
1006 const CGBlockInfo &blockInfo);
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001007
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001008};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001009
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001010class CGObjCMac : public CGObjCCommonMac {
1011private:
1012 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001013
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001014 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001015 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001016 void EmitModuleInfo();
1017
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001018 /// EmitModuleSymols - Emit module symbols, the list of defined
1019 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001020 llvm::Constant *EmitModuleSymbols();
1021
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001022 /// FinishModule - Write out global data structures at the end of
1023 /// processing a translation unit.
1024 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001025
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001026 /// EmitClassExtension - Generate the class extension structure used
1027 /// to store the weak ivar layout and properties. The return value
1028 /// has type ClassExtensionPtrTy.
1029 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
1030
1031 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1032 /// for the given class.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001033 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001034 const ObjCInterfaceDecl *ID);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001035
1036 /// EmitSuperClassRef - Emits reference to class's main metadata class.
1037 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001038
1039 /// EmitIvarList - Emit the ivar list for the given
1040 /// implementation. If ForClass is true the list of class ivars
1041 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1042 /// interface ivars will be emitted. The return value has type
1043 /// IvarListPtrTy.
1044 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001045 bool ForClass);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001046
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001047 /// EmitMetaClass - Emit a forward reference to the class structure
1048 /// for the metaclass of the given interface. The return value has
1049 /// type ClassPtrTy.
1050 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1051
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001052 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001053 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001054 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1055 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001056 const ConstantVector &Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001057
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001058 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001059
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001060 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001061
1062 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001063 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001064 llvm::Constant *EmitMethodList(llvm::Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001065 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001066 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001067
1068 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001069 /// method declarations.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001070 /// - TypeName: The name for the type containing the methods.
1071 /// - IsProtocol: True iff these methods are for a protocol.
1072 /// - ClassMethds: True iff these are class methods.
1073 /// - Required: When true, only "required" methods are
1074 /// listed. Similarly, when false only "optional" methods are
1075 /// listed. For classes this should always be true.
1076 /// - begin, end: The method list to output.
1077 ///
1078 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001079 llvm::Constant *EmitMethodDescList(llvm::Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001080 const char *Section,
1081 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001082
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001083 /// GetOrEmitProtocol - Get the protocol object for the given
1084 /// declaration, emitting it if necessary. The return value has type
1085 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001086 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001087
1088 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1089 /// object for the given declaration, emitting it if needed. These
1090 /// forward references will be filled in with empty bodies if no
1091 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001092 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001093
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001094 /// EmitProtocolExtension - Generate the protocol extension
1095 /// structure used to store optional instance and class methods, and
1096 /// protocol properties. The return value has type
1097 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001098 llvm::Constant *
1099 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1100 const ConstantVector &OptInstanceMethods,
1101 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001102
1103 /// EmitProtocolList - Generate the list of referenced
1104 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001105 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001106 ObjCProtocolDecl::protocol_iterator begin,
1107 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001108
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001109 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1110 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001111 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1112 bool lval=false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001113
1114public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001115 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001116
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001117 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001118
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001119 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001120 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001121 QualType ResultType,
1122 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001123 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001124 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001125 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001126 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001127
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001128 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001129 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001130 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001131 QualType ResultType,
1132 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001133 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001134 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001135 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001136 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001137 const CallArgList &CallArgs,
1138 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001139
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001140 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001141 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001142
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001143 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1144 bool lval = false);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001145
1146 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1147 /// untyped one.
1148 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1149 const ObjCMethodDecl *Method);
1150
John McCall5a180392010-07-24 00:37:23 +00001151 virtual llvm::Constant *GetEHType(QualType T);
1152
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001153 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001154
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001155 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001156
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001157 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001158 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001159
Chris Lattner74391b42009-03-22 21:03:39 +00001160 virtual llvm::Constant *GetPropertyGetFunction();
1161 virtual llvm::Constant *GetPropertySetFunction();
David Chisnall8fac25d2010-12-26 22:13:16 +00001162 virtual llvm::Constant *GetGetStructFunction();
1163 virtual llvm::Constant *GetSetStructFunction();
Chris Lattner74391b42009-03-22 21:03:39 +00001164 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001165
John McCallf1549f62010-07-06 01:34:17 +00001166 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1167 const ObjCAtTryStmt &S);
1168 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1169 const ObjCAtSynchronizedStmt &S);
1170 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001171 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1172 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001173 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001174 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001175 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001176 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001177 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001178 llvm::Value *src, llvm::Value *dest,
1179 bool threadlocal = false);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001180 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001181 llvm::Value *src, llvm::Value *dest,
1182 llvm::Value *ivarOffset);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001183 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1184 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001185 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1186 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001187 llvm::Value *size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001188
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001189 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1190 QualType ObjectTy,
1191 llvm::Value *BaseValue,
1192 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001193 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001194 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001195 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001196 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001197};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001198
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001199class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001200private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001201 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001202 llvm::GlobalVariable* ObjCEmptyCacheVar;
1203 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001204
Daniel Dunbar11394522009-04-18 08:51:00 +00001205 /// SuperClassReferences - uniqued super class references.
1206 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001207
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001208 /// MetaClassReferences - uniqued meta class references.
1209 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001210
1211 /// EHTypeReferences - uniqued class ehtype references.
1212 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001213
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001214 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001215 /// legacy messaging dispatch.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001216 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001217
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00001218 /// DefinedMetaClasses - List of defined meta-classes.
1219 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1220
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001221 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001222 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001223 bool LegacyDispatchedSelector(Selector Sel);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001224
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001225 /// FinishNonFragileABIModule - Write out global data structures at the end of
1226 /// processing a translation unit.
1227 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001228
Daniel Dunbar463b8762009-05-15 21:48:48 +00001229 /// AddModuleClassList - Add the given list of class pointers to the
1230 /// module with the provided symbol and section names.
1231 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1232 const char *SymbolName,
1233 const char *SectionName);
1234
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001235 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1236 unsigned InstanceStart,
1237 unsigned InstanceSize,
1238 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001239 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001240 llvm::Constant *IsAGV,
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001241 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001242 llvm::Constant *ClassRoGV,
1243 bool HiddenVisibility);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001244
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001245 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001246
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001247 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001248
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001249 /// EmitMethodList - Emit the method list for the given
1250 /// implementation. The return value has type MethodListnfABITy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001251 llvm::Constant *EmitMethodList(llvm::Twine Name,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001252 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001253 const ConstantVector &Methods);
1254 /// EmitIvarList - Emit the ivar list for the given
1255 /// implementation. If ForClass is true the list of class ivars
1256 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1257 /// interface ivars will be emitted. The return value has type
1258 /// IvarListnfABIPtrTy.
1259 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001260
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001261 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001262 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001263 unsigned long int offset);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001264
Fariborz Jahanianda320092009-01-29 19:24:30 +00001265 /// GetOrEmitProtocol - Get the protocol object for the given
1266 /// declaration, emitting it if necessary. The return value has type
1267 /// ProtocolPtrTy.
1268 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001269
Fariborz Jahanianda320092009-01-29 19:24:30 +00001270 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1271 /// object for the given declaration, emitting it if needed. These
1272 /// forward references will be filled in with empty bodies if no
1273 /// definition is seen. The return value has type ProtocolPtrTy.
1274 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001275
Fariborz Jahanianda320092009-01-29 19:24:30 +00001276 /// EmitProtocolList - Generate the list of referenced
1277 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001278 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Fariborz Jahanianda320092009-01-29 19:24:30 +00001279 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001280 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001281
Fariborz Jahanian46551122009-02-04 00:22:57 +00001282 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001283 ReturnValueSlot Return,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001284 QualType ResultType,
1285 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00001286 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001287 QualType Arg0Ty,
1288 bool IsSuper,
1289 const CallArgList &CallArgs);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001290
1291 /// GetClassGlobal - Return the global variable for the Objective-C
1292 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001293 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001294
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001295 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001296 /// for the given class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001297 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001298 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001299
Daniel Dunbar11394522009-04-18 08:51:00 +00001300 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1301 /// for the given super class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001302 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1303 const ObjCInterfaceDecl *ID);
1304
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001305 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1306 /// meta-data
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001307 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001308 const ObjCInterfaceDecl *ID);
1309
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001310 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1311 /// the given ivar.
1312 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001313 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001314 const ObjCInterfaceDecl *ID,
1315 const ObjCIvarDecl *Ivar);
1316
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001317 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1318 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001319 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1320 bool lval=false);
Daniel Dunbare588b992009-03-01 04:46:24 +00001321
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001322 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001323 /// interface. The return value has type EHTypePtrTy.
John McCall5a180392010-07-24 00:37:23 +00001324 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001325 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001326
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001327 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001328 return "OBJC_METACLASS_$_";
1329 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001330
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001331 const char *getClassSymbolPrefix() const {
1332 return "OBJC_CLASS_$_";
1333 }
1334
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001335 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001336 uint32_t &InstanceStart,
1337 uint32_t &InstanceSize);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001338
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001339 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001340 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001341 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1342 return CGM.getContext().Selectors.getSelector(0, &II);
1343 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001344
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001345 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001346 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1347 return CGM.getContext().Selectors.getSelector(1, &II);
1348 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001349
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001350 /// ImplementationIsNonLazy - Check whether the given category or
1351 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001352 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001353
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001354public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001355 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001356 // FIXME. All stubs for now!
1357 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001358
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001359 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001360 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001361 QualType ResultType,
1362 Selector Sel,
1363 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001364 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001365 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001366 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001367
1368 virtual CodeGen::RValue
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001369 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001370 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001371 QualType ResultType,
1372 Selector Sel,
1373 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001374 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001375 llvm::Value *Receiver,
1376 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001377 const CallArgList &CallArgs,
1378 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001379
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001380 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001381 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001382
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001383 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1384 bool lvalue = false)
1385 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001386
1387 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1388 /// untyped one.
1389 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1390 const ObjCMethodDecl *Method)
1391 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001392
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001393 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001394
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001395 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001396 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001397 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001398
John McCall5a180392010-07-24 00:37:23 +00001399 virtual llvm::Constant *GetEHType(QualType T);
1400
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001401 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001402 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001403 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001404 virtual llvm::Constant *GetPropertySetFunction() {
1405 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001406 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001407
David Chisnall8fac25d2010-12-26 22:13:16 +00001408 virtual llvm::Constant *GetSetStructFunction() {
1409 return ObjCTypes.getCopyStructFn();
1410 }
1411 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001412 return ObjCTypes.getCopyStructFn();
1413 }
1414
Chris Lattner74391b42009-03-22 21:03:39 +00001415 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001416 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001417 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001418
John McCallf1549f62010-07-06 01:34:17 +00001419 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1420 const ObjCAtTryStmt &S);
1421 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1422 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001423 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001424 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001425 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001426 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001427 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001428 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001429 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001430 llvm::Value *src, llvm::Value *dest,
1431 bool threadlocal = false);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001432 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001433 llvm::Value *src, llvm::Value *dest,
1434 llvm::Value *ivarOffset);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001435 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001436 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001437 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1438 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001439 llvm::Value *size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001440 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1441 QualType ObjectTy,
1442 llvm::Value *BaseValue,
1443 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001444 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001445 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001446 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001447 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001448};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001449
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001450} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001451
1452/* *** Helper Functions *** */
1453
1454/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001455static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001456 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001457 unsigned idx0,
1458 unsigned idx1) {
1459 llvm::Value *Idxs[] = {
Owen Anderson0032b272009-08-13 21:57:51 +00001460 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1461 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001462 };
Owen Anderson3c4972d2009-07-29 18:54:39 +00001463 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001464}
1465
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001466/// hasObjCExceptionAttribute - Return true if this class or any super
1467/// class has the __objc_exception__ attribute.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001468static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor68584ed2009-06-18 16:11:24 +00001469 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001470 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001471 return true;
1472 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001473 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001474 return false;
1475}
1476
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001477/* *** CGObjCMac Public Interface *** */
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001478
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001479CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00001480 ObjCTypes(cgm) {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001481 ObjCABI = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001482 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001483}
1484
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001485/// GetClass - Return a reference to the class for the given interface
1486/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001487llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001488 const ObjCInterfaceDecl *ID) {
1489 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001490}
1491
1492/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001493llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1494 bool lval) {
1495 return EmitSelector(Builder, Sel, lval);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001496}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001497llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001498 *Method) {
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001499 return EmitSelector(Builder, Method->getSelector());
1500}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001501
John McCall5a180392010-07-24 00:37:23 +00001502llvm::Constant *CGObjCMac::GetEHType(QualType T) {
1503 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1504 return 0;
1505}
1506
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001507/// Generate a constant CFString object.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001508/*
1509 struct __builtin_CFString {
1510 const int *isa; // point to __CFConstantStringClassReference
1511 int flags;
1512 const char *str;
1513 long length;
1514 };
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001515*/
1516
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001517/// or Generate a constant NSString object.
1518/*
1519 struct __builtin_NSString {
1520 const int *isa; // point to __NSConstantStringClassReference
1521 const char *str;
1522 unsigned int length;
1523 };
1524*/
1525
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001526llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall0d13f6f2010-01-23 02:40:42 +00001527 const StringLiteral *SL) {
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001528 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1529 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian4c733072010-10-19 17:19:29 +00001530 CGM.GetAddrOfConstantString(SL));
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001531}
1532
1533/// Generates a message send where the super is the receiver. This is
1534/// a message send to self with special delivery semantics indicating
1535/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001536CodeGen::RValue
1537CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001538 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001539 QualType ResultType,
1540 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001541 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001542 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001543 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001544 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001545 const CodeGen::CallArgList &CallArgs,
1546 const ObjCMethodDecl *Method) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001547 // Create and init a super structure; this is a (receiver, class)
1548 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001549 llvm::Value *ObjCSuper =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001550 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001551 llvm::Value *ReceiverAsObject =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001552 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001553 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001554 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001555
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001556 // If this is a class message the metaclass is passed as the target.
1557 llvm::Value *Target;
1558 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001559 if (isCategoryImpl) {
1560 // Message sent to 'super' in a class method defined in a category
1561 // implementation requires an odd treatment.
1562 // If we are in a class method, we must retrieve the
1563 // _metaclass_ for the current class, pointed at by
1564 // the class's "isa" pointer. The following assumes that
1565 // isa" is the first ivar in a class (which it must be).
1566 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1567 Target = CGF.Builder.CreateStructGEP(Target, 0);
1568 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00001569 } else {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001570 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1571 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1572 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1573 Target = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001574 }
Fariborz Jahanian182f2682009-11-14 02:18:31 +00001575 }
1576 else if (isCategoryImpl)
1577 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1578 else {
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001579 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1580 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1581 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001582 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001583 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1584 // ObjCTypes types.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001585 const llvm::Type *ClassTy =
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001586 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001587 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001588 CGF.Builder.CreateStore(Target,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001589 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCallef072fd2010-05-22 01:48:05 +00001590 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001591 EmitSelector(CGF.Builder, Sel),
1592 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001593 true, CallArgs, Method, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001594}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001595
1596/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001597CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001598 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001599 QualType ResultType,
1600 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001601 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001602 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001603 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001604 const ObjCMethodDecl *Method) {
John McCallef072fd2010-05-22 01:48:05 +00001605 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001606 EmitSelector(CGF.Builder, Sel),
1607 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001608 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001609}
1610
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001611CodeGen::RValue
1612CGObjCCommonMac::EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001613 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001614 QualType ResultType,
1615 llvm::Value *Sel,
1616 llvm::Value *Arg0,
1617 QualType Arg0Ty,
1618 bool IsSuper,
1619 const CallArgList &CallArgs,
1620 const ObjCMethodDecl *Method,
1621 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001622 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001623 if (!IsSuper)
1624 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001625 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001626 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001627 CGF.getContext().getObjCSelType()));
1628 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001629
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001630 CodeGenTypes &Types = CGM.getTypes();
John McCall04a67a62010-02-05 21:31:56 +00001631 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00001632 FunctionType::ExtInfo());
Daniel Dunbar67939662009-09-17 04:01:40 +00001633 const llvm::FunctionType *FTy =
1634 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001635
Anders Carlsson7e70fb22010-06-21 20:59:55 +00001636 if (Method)
1637 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1638 CGM.getContext().getCanonicalType(ResultType) &&
1639 "Result type mismatch!");
1640
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001641 llvm::Constant *Fn = NULL;
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001642 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001643 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001644 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001645 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1646 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1647 : ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001648 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001649 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001650 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001651 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001652 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCallef072fd2010-05-22 01:48:05 +00001653 return CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001654}
1655
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001656static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1657 if (FQT.isObjCGCStrong())
1658 return Qualifiers::Strong;
1659
1660 if (FQT.isObjCGCWeak())
1661 return Qualifiers::Weak;
1662
1663 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1664 return Qualifiers::Strong;
1665
1666 if (const PointerType *PT = FQT->getAs<PointerType>())
1667 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1668
1669 return Qualifiers::GCNone;
1670}
1671
John McCall6b5a61b2011-02-07 10:33:21 +00001672llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1673 const CGBlockInfo &blockInfo) {
1674 llvm::Constant *nullPtr =
Fariborz Jahanian44034db2010-08-04 18:44:59 +00001675 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
John McCall6b5a61b2011-02-07 10:33:21 +00001676
Fariborz Jahanianfb550312010-09-13 16:09:44 +00001677 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
John McCall6b5a61b2011-02-07 10:33:21 +00001678 return nullPtr;
1679
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001680 bool hasUnion = false;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001681 SkipIvars.clear();
1682 IvarsInfo.clear();
1683 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
1684 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
1685
Fariborz Jahanian81979822010-09-09 00:21:45 +00001686 // __isa is the first field in block descriptor and must assume by runtime's
1687 // convention that it is GC'able.
1688 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall6b5a61b2011-02-07 10:33:21 +00001689
1690 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1691
1692 // Calculate the basic layout of the block structure.
1693 const llvm::StructLayout *layout =
1694 CGM.getTargetData().getStructLayout(blockInfo.StructureType);
1695
1696 // Ignore the optional 'this' capture: C++ objects are not assumed
1697 // to be GC'ed.
1698
1699 // Walk the captured variables.
1700 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1701 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1702 const VarDecl *variable = ci->getVariable();
1703 QualType type = variable->getType();
1704
1705 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1706
1707 // Ignore constant captures.
1708 if (capture.isConstant()) continue;
1709
1710 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1711
1712 // __block variables are passed by their descriptor address.
1713 if (ci->isByRef()) {
1714 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanianc5904b42010-09-11 01:27:29 +00001715 continue;
John McCall6b5a61b2011-02-07 10:33:21 +00001716 }
1717
1718 assert(!type->isArrayType() && "array variable should not be caught");
1719 if (const RecordType *record = type->getAs<RecordType>()) {
1720 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001721 continue;
1722 }
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001723
John McCall6b5a61b2011-02-07 10:33:21 +00001724 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1725 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1726
1727 if (GCAttr == Qualifiers::Strong)
1728 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1729 fieldSize / WordSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001730 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall6b5a61b2011-02-07 10:33:21 +00001731 SkipIvars.push_back(GC_IVAR(fieldOffset,
1732 fieldSize / ByteSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001733 }
1734
1735 if (IvarsInfo.empty())
John McCall6b5a61b2011-02-07 10:33:21 +00001736 return nullPtr;
1737
1738 // Sort on byte position; captures might not be allocated in order,
1739 // and unions can do funny things.
1740 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1741 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001742
1743 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00001744 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001745 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1746 printf("\n block variable layout for block: ");
1747 const unsigned char *s = (unsigned char*)BitMap.c_str();
1748 for (unsigned i = 0; i < BitMap.size(); i++)
1749 if (!(s[i] & 0xf0))
1750 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1751 else
1752 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1753 printf("\n");
1754 }
1755
1756 return C;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001757}
1758
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001759llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001760 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001761 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001762 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001763 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1764
Owen Anderson3c4972d2009-07-29 18:54:39 +00001765 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001766 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001767}
1768
Fariborz Jahanianda320092009-01-29 19:24:30 +00001769void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001770 // FIXME: We shouldn't need this, the protocol decl should contain enough
1771 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001772 DefinedProtocols.insert(PD->getIdentifier());
1773
1774 // If we have generated a forward reference to this protocol, emit
1775 // it now. Otherwise do nothing, the protocol objects are lazily
1776 // emitted.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001777 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001778 GetOrEmitProtocol(PD);
1779}
1780
Fariborz Jahanianda320092009-01-29 19:24:30 +00001781llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001782 if (DefinedProtocols.count(PD->getIdentifier()))
1783 return GetOrEmitProtocol(PD);
1784 return GetOrEmitProtocolRef(PD);
1785}
1786
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001787/*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001788// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1789struct _objc_protocol {
1790struct _objc_protocol_extension *isa;
1791char *protocol_name;
1792struct _objc_protocol_list *protocol_list;
1793struct _objc__method_prototype_list *instance_methods;
1794struct _objc__method_prototype_list *class_methods
1795};
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001796
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001797See EmitProtocolExtension().
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001798*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001799llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1800 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1801
1802 // Early exit if a defining object has already been generated.
1803 if (Entry && Entry->hasInitializer())
1804 return Entry;
1805
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001806 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001807 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001808 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1809
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001810 // Construct method lists.
1811 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1812 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001813 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001814 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001815 ObjCMethodDecl *MD = *i;
1816 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1817 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1818 OptInstanceMethods.push_back(C);
1819 } else {
1820 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001821 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001822 }
1823
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001824 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001825 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001826 ObjCMethodDecl *MD = *i;
1827 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1828 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1829 OptClassMethods.push_back(C);
1830 } else {
1831 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001832 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001833 }
1834
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001835 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001836 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001837 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001838 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001839 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001840 PD->protocol_begin(),
1841 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001842 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001843 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001844 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1845 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001846 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001847 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001848 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1849 ClassMethods);
Owen Anderson08e25242009-07-27 22:29:56 +00001850 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001851 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001852
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001853 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001854 // Already created, fix the linkage and update the initializer.
1855 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001856 Entry->setInitializer(Init);
1857 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001858 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001859 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001860 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001861 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001862 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001863 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001864 // FIXME: Is this necessary? Why only for protocol?
1865 Entry->setAlignment(4);
1866 }
Chris Lattnerad64e022009-07-17 23:57:13 +00001867 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001868
1869 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001870}
1871
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001872llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001873 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1874
1875 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001876 // We use the initializer as a marker of whether this is a forward
1877 // reference or not. At module finalization we add the empty
1878 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001879 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001880 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001881 llvm::GlobalValue::ExternalLinkage,
1882 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001883 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001884 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001885 // FIXME: Is this necessary? Why only for protocol?
1886 Entry->setAlignment(4);
1887 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001888
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001889 return Entry;
1890}
1891
1892/*
1893 struct _objc_protocol_extension {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001894 uint32_t size;
1895 struct objc_method_description_list *optional_instance_methods;
1896 struct objc_method_description_list *optional_class_methods;
1897 struct objc_property_list *instance_properties;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001898 };
1899*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001900llvm::Constant *
1901CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1902 const ConstantVector &OptInstanceMethods,
1903 const ConstantVector &OptClassMethods) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001904 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00001905 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001906 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001907 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001908 Values[1] =
1909 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001910 + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001911 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1912 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001913 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001914 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001915 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1916 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001917 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001918 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001919
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001920 // Return null if no extension bits are used.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001921 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001922 Values[3]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001923 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001924
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001925 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00001926 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001927
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001928 // No special section, but goes in llvm.used
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001929 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001930 Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001931 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001932}
1933
1934/*
1935 struct objc_protocol_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001936 struct objc_protocol_list *next;
1937 long count;
1938 Protocol *list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001939 };
1940*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001941llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001942CGObjCMac::EmitProtocolList(llvm::Twine Name,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001943 ObjCProtocolDecl::protocol_iterator begin,
1944 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001945 std::vector<llvm::Constant*> ProtocolRefs;
1946
Daniel Dunbardbc933702008-08-21 21:57:41 +00001947 for (; begin != end; ++begin)
1948 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001949
1950 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001951 if (ProtocolRefs.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001952 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001953
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001954 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001955 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001956
1957 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001958 // This field is only used by the runtime.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001959 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001960 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001961 ProtocolRefs.size() - 1);
1962 Values[2] =
1963 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1964 ProtocolRefs.size()),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001965 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001966
Nick Lewycky0d36dd22009-09-19 20:00:52 +00001967 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001968 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001969 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001970 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001971 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001972}
1973
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001974void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1975 std::vector<llvm::Constant*> &Properties,
1976 const Decl *Container,
1977 const ObjCProtocolDecl *PROTO,
1978 const ObjCCommonTypesHelper &ObjCTypes) {
1979 std::vector<llvm::Constant*> Prop(2);
1980 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1981 E = PROTO->protocol_end(); P != E; ++P)
1982 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1983 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1984 E = PROTO->prop_end(); I != E; ++I) {
1985 const ObjCPropertyDecl *PD = *I;
1986 if (!PropertySet.insert(PD->getIdentifier()))
1987 continue;
1988 Prop[0] = GetPropertyName(PD->getIdentifier());
1989 Prop[1] = GetPropertyTypeString(PD, Container);
1990 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1991 }
1992}
1993
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001994/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001995 struct _objc_property {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001996 const char * const name;
1997 const char * const attributes;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001998 };
1999
2000 struct _objc_property_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002001 uint32_t entsize; // sizeof (struct _objc_property)
2002 uint32_t prop_count;
2003 struct _objc_property[prop_count];
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002004 };
2005*/
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002006llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002007 const Decl *Container,
2008 const ObjCContainerDecl *OCD,
2009 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002010 std::vector<llvm::Constant*> Properties, Prop(2);
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002011 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002012 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2013 E = OCD->prop_end(); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00002014 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002015 PropertySet.insert(PD->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002016 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002017 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson08e25242009-07-27 22:29:56 +00002018 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002019 Prop));
2020 }
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002021 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00002022 for (ObjCInterfaceDecl::all_protocol_iterator
2023 P = OID->all_referenced_protocol_begin(),
2024 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002025 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2026 ObjCTypes);
2027 }
2028 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2029 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2030 E = CD->protocol_end(); P != E; ++P)
2031 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2032 ObjCTypes);
2033 }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002034
2035 // Return null for empty list.
2036 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002037 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002038
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002039 unsigned PropertySize =
Duncan Sands9408c452009-05-09 07:08:47 +00002040 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002041 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002042 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2043 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002044 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002045 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002046 Values[2] = llvm::ConstantArray::get(AT, Properties);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002047 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002048
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002049 llvm::GlobalVariable *GV =
2050 CreateMetadataVar(Name, Init,
2051 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002052 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002053 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002054 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002055 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002056}
2057
2058/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002059 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002060 int count;
2061 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002062 };
2063*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002064llvm::Constant *
2065CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
2066 std::vector<llvm::Constant*> Desc(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002067 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002068 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2069 ObjCTypes.SelectorPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002070 Desc[1] = GetMethodVarType(MD);
Owen Anderson08e25242009-07-27 22:29:56 +00002071 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002072 Desc);
2073}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002074
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002075llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002076 const char *Section,
2077 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002078 // Return null for empty list.
2079 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002080 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002081
2082 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002083 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002084 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002085 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002086 Values[1] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002087 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002088
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002089 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002090 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002091 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002092}
2093
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002094/*
2095 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002096 char *category_name;
2097 char *class_name;
2098 struct _objc_method_list *instance_methods;
2099 struct _objc_method_list *class_methods;
2100 struct _objc_protocol_list *protocols;
2101 uint32_t size; // <rdar://4585769>
2102 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002103 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002104*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002105void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sands9408c452009-05-09 07:08:47 +00002106 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002107
Mike Stumpf5408fe2009-05-16 07:57:57 +00002108 // FIXME: This is poor design, the OCD should have a pointer to the category
2109 // decl. Additionally, note that Category can be null for the @implementation
2110 // w/o an @interface case. Sema should just create one for us as it does for
2111 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002112 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002113 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002114 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002115
2116 llvm::SmallString<256> ExtName;
2117 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2118 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002119
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002120 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002121 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002122 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002123 // Instance methods should always be defined.
2124 InstanceMethods.push_back(GetMethodConstant(*i));
2125 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002126 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002127 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002128 // Class methods should always be defined.
2129 ClassMethods.push_back(GetMethodConstant(*i));
2130 }
2131
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002132 std::vector<llvm::Constant*> Values(7);
2133 Values[0] = GetClassName(OCD->getIdentifier());
2134 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002135 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002136 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002137 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002138 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002139 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002140 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002141 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002142 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002143 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002144 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002145 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002146 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002147 Category->protocol_begin(),
2148 Category->protocol_end());
2149 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002150 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002151 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002152 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002153
2154 // If there is no category @interface then there can be no properties.
2155 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002156 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002157 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002158 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002159 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002160 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002161
Owen Anderson08e25242009-07-27 22:29:56 +00002162 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002163 Values);
2164
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002165 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002166 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002167 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002168 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002169 DefinedCategories.push_back(GV);
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00002170 DefinedCategoryNames.insert(ExtName.str());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002171}
2172
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002173// FIXME: Get from somewhere?
2174enum ClassFlags {
2175 eClassFlags_Factory = 0x00001,
2176 eClassFlags_Meta = 0x00002,
2177 // <rdr://5142207>
2178 eClassFlags_HasCXXStructors = 0x02000,
2179 eClassFlags_Hidden = 0x20000,
2180 eClassFlags_ABI2_Hidden = 0x00010,
2181 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2182};
2183
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002184/*
2185 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002186 Class isa;
2187 Class super_class;
2188 const char *name;
2189 long version;
2190 long info;
2191 long instance_size;
2192 struct _objc_ivar_list *ivars;
2193 struct _objc_method_list *methods;
2194 struct _objc_cache *cache;
2195 struct _objc_protocol_list *protocols;
2196 // Objective-C 1.0 extensions (<rdr://4585769>)
2197 const char *ivar_layout;
2198 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002199 };
2200
2201 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002202*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002203void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002204 DefinedSymbols.insert(ID->getIdentifier());
2205
Chris Lattner8ec03f52008-11-24 03:54:41 +00002206 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002207 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002208 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002209 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002210 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002211 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00002212 Interface->all_referenced_protocol_begin(),
2213 Interface->all_referenced_protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002214 unsigned Flags = eClassFlags_Factory;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00002215 if (ID->getNumIvarInitializers())
2216 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002217 unsigned Size =
Ken Dyck5f022d82011-02-09 01:59:34 +00002218 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002219
2220 // FIXME: Set CXX-structors flag.
John McCall1fb0caa2010-10-22 21:05:15 +00002221 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002222 Flags |= eClassFlags_Hidden;
2223
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002224 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002225 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002226 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002227 // Instance methods should always be defined.
2228 InstanceMethods.push_back(GetMethodConstant(*i));
2229 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002230 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002231 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002232 // Class methods should always be defined.
2233 ClassMethods.push_back(GetMethodConstant(*i));
2234 }
2235
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002236 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002237 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002238 ObjCPropertyImplDecl *PID = *i;
2239
2240 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2241 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2242
2243 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2244 if (llvm::Constant *C = GetMethodConstant(MD))
2245 InstanceMethods.push_back(C);
2246 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2247 if (llvm::Constant *C = GetMethodConstant(MD))
2248 InstanceMethods.push_back(C);
2249 }
2250 }
2251
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002252 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar5384b092009-05-03 08:56:52 +00002253 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002254 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002255 // Record a reference to the super class.
2256 LazySymbols.insert(Super->getIdentifier());
2257
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002258 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002259 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002260 ObjCTypes.ClassPtrTy);
2261 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002262 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002263 }
2264 Values[ 2] = GetClassName(ID->getIdentifier());
2265 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002266 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2267 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2268 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002269 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002270 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002271 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002272 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002273 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002274 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002275 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002276 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002277 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002278 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00002279 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002280 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002281 std::string Name("\01L_OBJC_CLASS_");
2282 Name += ClassName;
2283 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2284 // Check for a forward reference.
2285 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2286 if (GV) {
2287 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2288 "Forward metaclass reference has incorrect type.");
2289 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2290 GV->setInitializer(Init);
2291 GV->setSection(Section);
2292 GV->setAlignment(4);
2293 CGM.AddUsedGlobal(GV);
2294 }
2295 else
2296 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002297 DefinedClasses.push_back(GV);
2298}
2299
2300llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2301 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002302 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002303 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002304 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002305
John McCall1fb0caa2010-10-22 21:05:15 +00002306 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002307 Flags |= eClassFlags_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002308
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002309 std::vector<llvm::Constant*> Values(12);
2310 // The isa for the metaclass is the root of the hierarchy.
2311 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2312 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2313 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002314 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002315 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002316 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002317 // The super class for the metaclass is emitted as the name of the
2318 // super class. The runtime fixes this up to point to the
2319 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002320 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002321 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002322 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002323 ObjCTypes.ClassPtrTy);
2324 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002325 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002326 }
2327 Values[ 2] = GetClassName(ID->getIdentifier());
2328 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002329 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2330 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2331 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002332 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002333 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002334 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002335 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002336 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002337 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002338 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002339 Values[ 9] = Protocols;
2340 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002341 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002342 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002343 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002344 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002345 Values);
2346
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002347 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002348 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002349
2350 // Check for a forward reference.
2351 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2352 if (GV) {
2353 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2354 "Forward metaclass reference has incorrect type.");
2355 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2356 GV->setInitializer(Init);
2357 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002358 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002359 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002360 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002361 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002362 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002363 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002364 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002365
2366 return GV;
2367}
2368
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002369llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002370 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002371
Mike Stumpf5408fe2009-05-16 07:57:57 +00002372 // FIXME: Should we look these up somewhere other than the module. Its a bit
2373 // silly since we only generate these while processing an implementation, so
2374 // exactly one pointer would work if know when we entered/exitted an
2375 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002376
2377 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002378 // Previously, metaclass with internal linkage may have been defined.
2379 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002380 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2381 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002382 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2383 "Forward metaclass reference has incorrect type.");
2384 return GV;
2385 } else {
2386 // Generate as an external reference to keep a consistent
2387 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002388 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002389 llvm::GlobalValue::ExternalLinkage,
2390 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002391 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002392 }
2393}
2394
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002395llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2396 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2397
2398 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2399 true)) {
2400 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2401 "Forward class metadata reference has incorrect type.");
2402 return GV;
2403 } else {
2404 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2405 llvm::GlobalValue::ExternalLinkage,
2406 0,
2407 Name);
2408 }
2409}
2410
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002411/*
2412 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002413 uint32_t size;
2414 const char *weak_ivar_layout;
2415 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002416 };
2417*/
2418llvm::Constant *
2419CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002420 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002421 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002422
2423 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002424 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002425 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002426 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002427 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002428
2429 // Return null if no extension bits are used.
2430 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002431 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002432
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002433 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002434 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002435 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002436 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002437 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002438}
2439
2440/*
2441 struct objc_ivar {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002442 char *ivar_name;
2443 char *ivar_type;
2444 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002445 };
2446
2447 struct objc_ivar_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002448 int ivar_count;
2449 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002450 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002451*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002452llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002453 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002454 std::vector<llvm::Constant*> Ivars, Ivar(3);
2455
2456 // When emitting the root class GCC emits ivar entries for the
2457 // actual class structure. It is not clear if we need to follow this
2458 // behavior; for now lets try and get away with not doing it. If so,
2459 // the cleanest solution would be to make up an ObjCInterfaceDecl
2460 // for the class.
2461 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00002462 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002463
2464 ObjCInterfaceDecl *OID =
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002465 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002466
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002467 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002468 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002469
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002470 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2471 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002472 // Ignore unnamed bit-fields.
2473 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002474 continue;
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002475 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2476 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002477 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002478 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson08e25242009-07-27 22:29:56 +00002479 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002480 }
2481
2482 // Return null for empty list.
2483 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002484 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002485
2486 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002487 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002488 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002489 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002490 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002491 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002492
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002493 llvm::GlobalVariable *GV;
2494 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002495 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002496 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002497 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002498 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002499 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002500 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002501 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002502 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002503}
2504
2505/*
2506 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002507 SEL method_name;
2508 char *method_types;
2509 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002510 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002511
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002512 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002513 struct objc_method_list *obsolete;
2514 int count;
2515 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002516 };
2517*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002518
2519/// GetMethodConstant - Return a struct objc_method constant for the
2520/// given method if it has been defined. The result is null if the
2521/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002522llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00002523 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002524 if (!Fn)
2525 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002526
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002527 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002528 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002529 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002530 ObjCTypes.SelectorPtrTy);
2531 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002532 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002533 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002534}
2535
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002536llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002537 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002538 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002539 // Return null for empty list.
2540 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002541 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002542
2543 std::vector<llvm::Constant*> Values(3);
Owen Andersonc9c88b42009-07-31 20:28:54 +00002544 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002545 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002546 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002547 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002548 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002549 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002550
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002551 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002552 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002553 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002554}
2555
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002556llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002557 const ObjCContainerDecl *CD) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002558 llvm::SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002559 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002560
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002561 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002562 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002563 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002564 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002565 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002566 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002567 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002568 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002569 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002570
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002571 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002572}
2573
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002574llvm::GlobalVariable *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002575CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002576 llvm::Constant *Init,
2577 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002578 unsigned Align,
2579 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002580 const llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002581 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002582 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002583 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002584 if (Section)
2585 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002586 if (Align)
2587 GV->setAlignment(Align);
2588 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002589 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002590 return GV;
2591}
2592
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002593llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002594 // Abuse this interface function as a place to finalize.
2595 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002596 return NULL;
2597}
2598
Chris Lattner74391b42009-03-22 21:03:39 +00002599llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002600 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002601}
2602
Chris Lattner74391b42009-03-22 21:03:39 +00002603llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002604 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002605}
2606
David Chisnall8fac25d2010-12-26 22:13:16 +00002607llvm::Constant *CGObjCMac::GetGetStructFunction() {
2608 return ObjCTypes.getCopyStructFn();
2609}
2610llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00002611 return ObjCTypes.getCopyStructFn();
2612}
2613
Chris Lattner74391b42009-03-22 21:03:39 +00002614llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002615 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002616}
2617
John McCallf1549f62010-07-06 01:34:17 +00002618void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2619 return EmitTryOrSynchronizedStmt(CGF, S);
2620}
2621
2622void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2623 const ObjCAtSynchronizedStmt &S) {
2624 return EmitTryOrSynchronizedStmt(CGF, S);
2625}
2626
John McCallcc505292010-07-21 06:59:36 +00002627namespace {
John McCall1f0fca52010-07-21 07:22:38 +00002628 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00002629 const Stmt &S;
John McCall0b251722010-08-04 05:59:32 +00002630 llvm::Value *SyncArgSlot;
John McCallcc505292010-07-21 06:59:36 +00002631 llvm::Value *CallTryExitVar;
2632 llvm::Value *ExceptionData;
2633 ObjCTypesHelper &ObjCTypes;
2634 PerformFragileFinally(const Stmt *S,
John McCall0b251722010-08-04 05:59:32 +00002635 llvm::Value *SyncArgSlot,
John McCallcc505292010-07-21 06:59:36 +00002636 llvm::Value *CallTryExitVar,
2637 llvm::Value *ExceptionData,
2638 ObjCTypesHelper *ObjCTypes)
John McCall0b251722010-08-04 05:59:32 +00002639 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCallcc505292010-07-21 06:59:36 +00002640 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2641
2642 void Emit(CodeGenFunction &CGF, bool IsForEH) {
2643 // Check whether we need to call objc_exception_try_exit.
2644 // In optimized code, this branch will always be folded.
2645 llvm::BasicBlock *FinallyCallExit =
2646 CGF.createBasicBlock("finally.call_exit");
2647 llvm::BasicBlock *FinallyNoCallExit =
2648 CGF.createBasicBlock("finally.no_call_exit");
2649 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2650 FinallyCallExit, FinallyNoCallExit);
2651
2652 CGF.EmitBlock(FinallyCallExit);
2653 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2654 ->setDoesNotThrow();
2655
2656 CGF.EmitBlock(FinallyNoCallExit);
2657
2658 if (isa<ObjCAtTryStmt>(S)) {
2659 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCalld96a8e72010-08-11 00:16:14 +00002660 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2661 // Save the current cleanup destination in case there's
2662 // control flow inside the finally statement.
2663 llvm::Value *CurCleanupDest =
2664 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2665
John McCallcc505292010-07-21 06:59:36 +00002666 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2667
John McCalld96a8e72010-08-11 00:16:14 +00002668 if (CGF.HaveInsertPoint()) {
2669 CGF.Builder.CreateStore(CurCleanupDest,
2670 CGF.getNormalCleanupDestSlot());
2671 } else {
2672 // Currently, the end of the cleanup must always exist.
2673 CGF.EnsureInsertPoint();
2674 }
2675 }
John McCallcc505292010-07-21 06:59:36 +00002676 } else {
2677 // Emit objc_sync_exit(expr); as finally's sole statement for
2678 // @synchronized.
John McCall0b251722010-08-04 05:59:32 +00002679 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCallcc505292010-07-21 06:59:36 +00002680 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2681 ->setDoesNotThrow();
2682 }
2683 }
2684 };
John McCall87bb5822010-07-31 23:20:56 +00002685
2686 class FragileHazards {
2687 CodeGenFunction &CGF;
2688 llvm::SmallVector<llvm::Value*, 20> Locals;
2689 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2690
2691 llvm::InlineAsm *ReadHazard;
2692 llvm::InlineAsm *WriteHazard;
2693
2694 llvm::FunctionType *GetAsmFnType();
2695
2696 void collectLocals();
2697 void emitReadHazard(CGBuilderTy &Builder);
2698
2699 public:
2700 FragileHazards(CodeGenFunction &CGF);
John McCall0b251722010-08-04 05:59:32 +00002701
John McCall87bb5822010-07-31 23:20:56 +00002702 void emitWriteHazard();
John McCall0b251722010-08-04 05:59:32 +00002703 void emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00002704 };
2705}
2706
2707/// Create the fragile-ABI read and write hazards based on the current
2708/// state of the function, which is presumed to be immediately prior
2709/// to a @try block. These hazards are used to maintain correct
2710/// semantics in the face of optimization and the fragile ABI's
2711/// cavalier use of setjmp/longjmp.
2712FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2713 collectLocals();
2714
2715 if (Locals.empty()) return;
2716
2717 // Collect all the blocks in the function.
2718 for (llvm::Function::iterator
2719 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2720 BlocksBeforeTry.insert(&*I);
2721
2722 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2723
2724 // Create a read hazard for the allocas. This inhibits dead-store
2725 // optimizations and forces the values to memory. This hazard is
2726 // inserted before any 'throwing' calls in the protected scope to
2727 // reflect the possibility that the variables might be read from the
2728 // catch block if the call throws.
2729 {
2730 std::string Constraint;
2731 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2732 if (I) Constraint += ',';
2733 Constraint += "*m";
2734 }
2735
2736 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2737 }
2738
2739 // Create a write hazard for the allocas. This inhibits folding
2740 // loads across the hazard. This hazard is inserted at the
2741 // beginning of the catch path to reflect the possibility that the
2742 // variables might have been written within the protected scope.
2743 {
2744 std::string Constraint;
2745 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2746 if (I) Constraint += ',';
2747 Constraint += "=*m";
2748 }
2749
2750 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2751 }
2752}
2753
2754/// Emit a write hazard at the current location.
2755void FragileHazards::emitWriteHazard() {
2756 if (Locals.empty()) return;
2757
2758 CGF.Builder.CreateCall(WriteHazard, Locals.begin(), Locals.end())
2759 ->setDoesNotThrow();
2760}
2761
John McCall87bb5822010-07-31 23:20:56 +00002762void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2763 assert(!Locals.empty());
2764 Builder.CreateCall(ReadHazard, Locals.begin(), Locals.end())
2765 ->setDoesNotThrow();
2766}
2767
2768/// Emit read hazards in all the protected blocks, i.e. all the blocks
2769/// which have been inserted since the beginning of the try.
John McCall0b251722010-08-04 05:59:32 +00002770void FragileHazards::emitHazardsInNewBlocks() {
John McCall87bb5822010-07-31 23:20:56 +00002771 if (Locals.empty()) return;
2772
2773 CGBuilderTy Builder(CGF.getLLVMContext());
2774
2775 // Iterate through all blocks, skipping those prior to the try.
2776 for (llvm::Function::iterator
2777 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2778 llvm::BasicBlock &BB = *FI;
2779 if (BlocksBeforeTry.count(&BB)) continue;
2780
2781 // Walk through all the calls in the block.
2782 for (llvm::BasicBlock::iterator
2783 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2784 llvm::Instruction &I = *BI;
2785
2786 // Ignore instructions that aren't non-intrinsic calls.
2787 // These are the only calls that can possibly call longjmp.
2788 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2789 if (isa<llvm::IntrinsicInst>(I))
2790 continue;
2791
2792 // Ignore call sites marked nounwind. This may be questionable,
2793 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2794 llvm::CallSite CS(&I);
2795 if (CS.doesNotThrow()) continue;
2796
John McCall0b251722010-08-04 05:59:32 +00002797 // Insert a read hazard before the call. This will ensure that
2798 // any writes to the locals are performed before making the
2799 // call. If the call throws, then this is sufficient to
2800 // guarantee correctness as long as it doesn't also write to any
2801 // locals.
John McCall87bb5822010-07-31 23:20:56 +00002802 Builder.SetInsertPoint(&BB, BI);
2803 emitReadHazard(Builder);
2804 }
2805 }
2806}
2807
2808static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2809 if (V) S.insert(V);
2810}
2811
2812void FragileHazards::collectLocals() {
2813 // Compute a set of allocas to ignore.
2814 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2815 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2816 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
2817 addIfPresent(AllocasToIgnore, CGF.EHCleanupDest);
2818
2819 // Collect all the allocas currently in the function. This is
2820 // probably way too aggressive.
2821 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2822 for (llvm::BasicBlock::iterator
2823 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2824 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2825 Locals.push_back(&*I);
2826}
2827
2828llvm::FunctionType *FragileHazards::GetAsmFnType() {
2829 std::vector<const llvm::Type *> Tys(Locals.size());
2830 for (unsigned I = 0, E = Locals.size(); I != E; ++I)
2831 Tys[I] = Locals[I]->getType();
2832 return llvm::FunctionType::get(CGF.Builder.getVoidTy(), Tys, false);
John McCallcc505292010-07-21 06:59:36 +00002833}
2834
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002835/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002836
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002837 Objective-C setjmp-longjmp (sjlj) Exception Handling
2838 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002839
John McCallf1549f62010-07-06 01:34:17 +00002840 A catch buffer is a setjmp buffer plus:
2841 - a pointer to the exception that was caught
2842 - a pointer to the previous exception data buffer
2843 - two pointers of reserved storage
2844 Therefore catch buffers form a stack, with a pointer to the top
2845 of the stack kept in thread-local storage.
2846
2847 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2848 objc_exception_try_exit pops the given catch buffer, which is
2849 required to be the top of the EH stack.
2850 objc_exception_throw pops the top of the EH stack, writes the
2851 thrown exception into the appropriate field, and longjmps
2852 to the setjmp buffer. It crashes the process (with a printf
2853 and an abort()) if there are no catch buffers on the stack.
2854 objc_exception_extract just reads the exception pointer out of the
2855 catch buffer.
2856
2857 There's no reason an implementation couldn't use a light-weight
2858 setjmp here --- something like __builtin_setjmp, but API-compatible
2859 with the heavyweight setjmp. This will be more important if we ever
2860 want to implement correct ObjC/C++ exception interactions for the
2861 fragile ABI.
2862
2863 Note that for this use of setjmp/longjmp to be correct, we may need
2864 to mark some local variables volatile: if a non-volatile local
2865 variable is modified between the setjmp and the longjmp, it has
2866 indeterminate value. For the purposes of LLVM IR, it may be
2867 sufficient to make loads and stores within the @try (to variables
2868 declared outside the @try) volatile. This is necessary for
2869 optimized correctness, but is not currently being done; this is
2870 being tracked as rdar://problem/8160285
2871
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002872 The basic framework for a @try-catch-finally is as follows:
2873 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002874 objc_exception_data d;
2875 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002876 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002877
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002878 objc_exception_try_enter(&d);
2879 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002880 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002881 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002882 // exception path
2883 id _caught = objc_exception_extract(&d);
2884
2885 // enter new try scope for handlers
2886 if (!setjmp(d.jmp_buf)) {
2887 ... match exception and execute catch blocks ...
2888
2889 // fell off end, rethrow.
2890 _rethrow = _caught;
2891 ... jump-through-finally to finally_rethrow ...
2892 } else {
2893 // exception in catch block
2894 _rethrow = objc_exception_extract(&d);
2895 _call_try_exit = false;
2896 ... jump-through-finally to finally_rethrow ...
2897 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002898 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002899 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002900
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002901 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002902 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002903 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002904
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002905 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002906 ... dispatch to finally destination ...
2907
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002908 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00002909 objc_exception_throw(_rethrow);
2910
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002911 finally_end:
2912 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002913
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002914 This framework differs slightly from the one gcc uses, in that gcc
2915 uses _rethrow to determine if objc_exception_try_exit should be called
2916 and if the object should be rethrown. This breaks in the face of
2917 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002918
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002919 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002920
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002921 - If there are no catch blocks, then we avoid emitting the second
2922 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002923
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002924 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2925 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002926
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002927 - FIXME: If there is no @finally block we can do a few more
2928 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002929
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002930 Rethrows and Jumps-Through-Finally
2931 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002932
John McCallf1549f62010-07-06 01:34:17 +00002933 '@throw;' is supported by pushing the currently-caught exception
2934 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002935
John McCallf1549f62010-07-06 01:34:17 +00002936 Branches through the @finally block are handled with an ordinary
2937 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2938 exceptions are not compatible with C++ exceptions, and this is
2939 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00002940
John McCallf1549f62010-07-06 01:34:17 +00002941 @synchronized(expr) { stmt; } is emitted as if it were:
2942 id synch_value = expr;
2943 objc_sync_enter(synch_value);
2944 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002945*/
2946
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002947void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2948 const Stmt &S) {
2949 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00002950
2951 // A destination for the fall-through edges of the catch handlers to
2952 // jump to.
2953 CodeGenFunction::JumpDest FinallyEnd =
2954 CGF.getJumpDestInCurrentScope("finally.end");
2955
2956 // A destination for the rethrow edge of the catch handlers to jump
2957 // to.
2958 CodeGenFunction::JumpDest FinallyRethrow =
2959 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002960
Daniel Dunbar1c566672009-02-24 01:43:46 +00002961 // For @synchronized, call objc_sync_enter(sync.expr). The
2962 // evaluation of the expression must occur before we enter the
John McCall0b251722010-08-04 05:59:32 +00002963 // @synchronized. We can't avoid a temp here because we need the
2964 // value to be preserved. If the backend ever does liveness
2965 // correctly after setjmp, this will be unnecessary.
2966 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar1c566672009-02-24 01:43:46 +00002967 if (!isTry) {
John McCall0b251722010-08-04 05:59:32 +00002968 llvm::Value *SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00002969 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2970 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00002971 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2972 ->setDoesNotThrow();
John McCall0b251722010-08-04 05:59:32 +00002973
2974 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2975 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002976 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002977
John McCall0b251722010-08-04 05:59:32 +00002978 // Allocate memory for the setjmp buffer. This needs to be kept
2979 // live throughout the try and catch blocks.
2980 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2981 "exceptiondata.ptr");
2982
John McCall87bb5822010-07-31 23:20:56 +00002983 // Create the fragile hazards. Note that this will not capture any
2984 // of the allocas required for exception processing, but will
2985 // capture the current basic block (which extends all the way to the
2986 // setjmp call) as "before the @try".
2987 FragileHazards Hazards(CGF);
2988
John McCallf1549f62010-07-06 01:34:17 +00002989 // Create a flag indicating whether the cleanup needs to call
2990 // objc_exception_try_exit. This is true except when
2991 // - no catches match and we're branching through the cleanup
2992 // just to rethrow the exception, or
2993 // - a catch matched and we're falling out of the catch handler.
John McCall0b251722010-08-04 05:59:32 +00002994 // The setjmp-safety rule here is that we should always store to this
2995 // variable in a place that dominates the branch through the cleanup
2996 // without passing through any setjmps.
John McCallf1549f62010-07-06 01:34:17 +00002997 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00002998 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002999
John McCall9e2213d2010-10-04 23:42:51 +00003000 // A slot containing the exception to rethrow. Only needed when we
3001 // have both a @catch and a @finally.
3002 llvm::Value *PropagatingExnVar = 0;
3003
John McCallf1549f62010-07-06 01:34:17 +00003004 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00003005 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall0b251722010-08-04 05:59:32 +00003006 SyncArgSlot,
John McCall1f0fca52010-07-21 07:22:38 +00003007 CallTryExitVar,
3008 ExceptionData,
3009 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00003010
3011 // Enter a try block:
3012 // - Call objc_exception_try_enter to push ExceptionData on top of
3013 // the EH stack.
3014 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3015 ->setDoesNotThrow();
3016
3017 // - Call setjmp on the exception data buffer.
3018 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3019 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3020 llvm::Value *SetJmpBuffer =
3021 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer");
3022 llvm::CallInst *SetJmpResult =
3023 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3024 SetJmpResult->setDoesNotThrow();
3025
3026 // If setjmp returned 0, enter the protected block; otherwise,
3027 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00003028 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3029 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00003030 llvm::Value *DidCatch =
John McCalld96a8e72010-08-11 00:16:14 +00003031 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3032 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00003033
John McCallf1549f62010-07-06 01:34:17 +00003034 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00003035 CGF.EmitBlock(TryBlock);
John McCall0b251722010-08-04 05:59:32 +00003036 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003037 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00003038 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall0b251722010-08-04 05:59:32 +00003039
3040 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003041
John McCallf1549f62010-07-06 01:34:17 +00003042 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003043 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003044
John McCall87bb5822010-07-31 23:20:56 +00003045 // Don't optimize loads of the in-scope locals across this point.
3046 Hazards.emitWriteHazard();
3047
John McCallf1549f62010-07-06 01:34:17 +00003048 // For a @synchronized (or a @try with no catches), just branch
3049 // through the cleanup to the rethrow block.
3050 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3051 // Tell the cleanup not to re-pop the exit.
John McCall0b251722010-08-04 05:59:32 +00003052 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003053 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00003054
3055 // Otherwise, we have to match against the caught exceptions.
3056 } else {
John McCall0b251722010-08-04 05:59:32 +00003057 // Retrieve the exception object. We may emit multiple blocks but
3058 // nothing can cross this so the value is already in SSA form.
3059 llvm::CallInst *Caught =
3060 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3061 ExceptionData, "caught");
3062 Caught->setDoesNotThrow();
3063
John McCallf1549f62010-07-06 01:34:17 +00003064 // Push the exception to rethrow onto the EH value stack for the
3065 // benefit of any @throws in the handlers.
3066 CGF.ObjCEHValueStack.push_back(Caught);
3067
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003068 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003069
John McCall0b251722010-08-04 05:59:32 +00003070 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallf1549f62010-07-06 01:34:17 +00003071
John McCall0b251722010-08-04 05:59:32 +00003072 llvm::BasicBlock *CatchBlock = 0;
3073 llvm::BasicBlock *CatchHandler = 0;
3074 if (HasFinally) {
John McCall9e2213d2010-10-04 23:42:51 +00003075 // Save the currently-propagating exception before
3076 // objc_exception_try_enter clears the exception slot.
3077 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3078 "propagating_exception");
3079 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3080
John McCall0b251722010-08-04 05:59:32 +00003081 // Enter a new exception try block (in case a @catch block
3082 // throws an exception).
3083 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3084 ->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00003085
John McCall0b251722010-08-04 05:59:32 +00003086 llvm::CallInst *SetJmpResult =
3087 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3088 "setjmp.result");
3089 SetJmpResult->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003090
John McCall0b251722010-08-04 05:59:32 +00003091 llvm::Value *Threw =
3092 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3093
3094 CatchBlock = CGF.createBasicBlock("catch");
3095 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3096 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3097
3098 CGF.EmitBlock(CatchBlock);
3099 }
3100
3101 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003102
Daniel Dunbar55e40722008-09-27 07:03:52 +00003103 // Handle catch list. As a special case we check if everything is
3104 // matched and avoid generating code for falling off the end if
3105 // so.
3106 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003107 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3108 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00003109
Douglas Gregorc00d8e12010-04-26 16:46:50 +00003110 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00003111 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00003112
Anders Carlsson80f25672008-09-09 17:59:25 +00003113 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00003114 if (!CatchParam) {
3115 AllMatched = true;
3116 } else {
John McCall183700f2009-09-21 23:43:11 +00003117 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003118
John McCallf1549f62010-07-06 01:34:17 +00003119 // catch(id e) always matches under this ABI, since only
3120 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00003121 // FIXME: For the time being we also match id<X>; this should
3122 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00003123 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00003124 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00003125 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003126
John McCallf1549f62010-07-06 01:34:17 +00003127 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003128 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00003129 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3130
Anders Carlssondde0a942008-09-11 09:15:33 +00003131 if (CatchParam) {
John McCallb6bbcc92010-10-15 04:57:14 +00003132 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003133 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00003134
3135 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00003136 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00003137 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003138
Anders Carlssondde0a942008-09-11 09:15:33 +00003139 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003140
3141 // The scope of the catch variable ends right here.
3142 CatchVarCleanups.ForceCleanup();
3143
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003144 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00003145 break;
3146 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003147
Steve Naroff14108da2009-07-10 23:34:53 +00003148 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00003149 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00003150
3151 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00003152 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3153 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00003154
3155 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00003156 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003157
John McCallf1549f62010-07-06 01:34:17 +00003158 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00003159 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3160 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00003161 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003162
John McCallf1549f62010-07-06 01:34:17 +00003163 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3164 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003165
3166 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003167 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003168
Anders Carlsson80f25672008-09-09 17:59:25 +00003169 // Emit the @catch block.
3170 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00003171
3172 // Collect any cleanups for the catch variable. The scope lasts until
3173 // the end of the catch body.
John McCall0b251722010-08-04 05:59:32 +00003174 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallf1549f62010-07-06 01:34:17 +00003175
John McCallb6bbcc92010-10-15 04:57:14 +00003176 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003177 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003178
John McCallf1549f62010-07-06 01:34:17 +00003179 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003180 llvm::Value *Tmp =
3181 CGF.Builder.CreateBitCast(Caught,
3182 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003183 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00003184 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003185
Anders Carlssondde0a942008-09-11 09:15:33 +00003186 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003187
3188 // We're done with the catch variable.
3189 CatchVarCleanups.ForceCleanup();
3190
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003191 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003192
Anders Carlsson80f25672008-09-09 17:59:25 +00003193 CGF.EmitBlock(NextCatchBlock);
3194 }
3195
John McCallf1549f62010-07-06 01:34:17 +00003196 CGF.ObjCEHValueStack.pop_back();
3197
John McCall0b251722010-08-04 05:59:32 +00003198 // If nothing wanted anything to do with the caught exception,
3199 // kill the extract call.
3200 if (Caught->use_empty())
3201 Caught->eraseFromParent();
3202
3203 if (!AllMatched)
3204 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3205
3206 if (HasFinally) {
3207 // Emit the exception handler for the @catch blocks.
3208 CGF.EmitBlock(CatchHandler);
3209
3210 // In theory we might now need a write hazard, but actually it's
3211 // unnecessary because there's no local-accessing code between
3212 // the try's write hazard and here.
3213 //Hazards.emitWriteHazard();
3214
John McCall9e2213d2010-10-04 23:42:51 +00003215 // Extract the new exception and save it to the
3216 // propagating-exception slot.
3217 assert(PropagatingExnVar);
3218 llvm::CallInst *NewCaught =
3219 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3220 ExceptionData, "caught");
3221 NewCaught->setDoesNotThrow();
3222 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3223
John McCall0b251722010-08-04 05:59:32 +00003224 // Don't pop the catch handler; the throw already did.
3225 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003226 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003227 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003228 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003229
John McCall87bb5822010-07-31 23:20:56 +00003230 // Insert read hazards as required in the new blocks.
John McCall0b251722010-08-04 05:59:32 +00003231 Hazards.emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003232
John McCallf1549f62010-07-06 01:34:17 +00003233 // Pop the cleanup.
John McCall0b251722010-08-04 05:59:32 +00003234 CGF.Builder.restoreIP(TryFallthroughIP);
3235 if (CGF.HaveInsertPoint())
3236 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallf1549f62010-07-06 01:34:17 +00003237 CGF.PopCleanupBlock();
John McCall0b251722010-08-04 05:59:32 +00003238 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003239
John McCallf1549f62010-07-06 01:34:17 +00003240 // Emit the rethrow block.
John McCall87bb5822010-07-31 23:20:56 +00003241 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallff8e1152010-07-23 21:56:41 +00003242 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00003243 if (CGF.HaveInsertPoint()) {
John McCall9e2213d2010-10-04 23:42:51 +00003244 // If we have a propagating-exception variable, check it.
3245 llvm::Value *PropagatingExn;
3246 if (PropagatingExnVar) {
3247 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall0b251722010-08-04 05:59:32 +00003248
John McCall9e2213d2010-10-04 23:42:51 +00003249 // Otherwise, just look in the buffer for the exception to throw.
3250 } else {
3251 llvm::CallInst *Caught =
3252 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3253 ExceptionData);
3254 Caught->setDoesNotThrow();
3255 PropagatingExn = Caught;
3256 }
3257
3258 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallf1549f62010-07-06 01:34:17 +00003259 ->setDoesNotThrow();
3260 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00003261 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003262
John McCall87bb5822010-07-31 23:20:56 +00003263 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003264}
3265
3266void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00003267 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003268 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003269
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003270 if (const Expr *ThrowExpr = S.getThrowExpr()) {
3271 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003272 ExceptionAsObject =
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003273 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
3274 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003275 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003276 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00003277 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003278 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003279
John McCallf1549f62010-07-06 01:34:17 +00003280 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3281 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00003282 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00003283
3284 // Clear the insertion point to indicate we are in unreachable code.
3285 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003286}
3287
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003288/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003289/// object: objc_read_weak (id *src)
3290///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003291llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003292 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00003293 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003294 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3295 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3296 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00003297 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003298 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00003299 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003300 return read_weak;
3301}
3302
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003303/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3304/// objc_assign_weak (id src, id *dst)
3305///
3306void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003307 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003308 const llvm::Type * SrcTy = src->getType();
3309 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003310 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003311 assert(Size <= 8 && "does not support size > 8");
3312 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003313 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003314 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3315 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003316 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3317 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00003318 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003319 src, dst, "weakassign");
3320 return;
3321}
3322
Fariborz Jahanian58626502008-11-19 00:59:10 +00003323/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3324/// objc_assign_global (id src, id *dst)
3325///
3326void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003327 llvm::Value *src, llvm::Value *dst,
3328 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003329 const llvm::Type * SrcTy = src->getType();
3330 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003331 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003332 assert(Size <= 8 && "does not support size > 8");
3333 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003334 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003335 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3336 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003337 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3338 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003339 if (!threadlocal)
3340 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3341 src, dst, "globalassign");
3342 else
3343 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3344 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00003345 return;
3346}
3347
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003348/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003349/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003350///
3351void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003352 llvm::Value *src, llvm::Value *dst,
3353 llvm::Value *ivarOffset) {
3354 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003355 const llvm::Type * SrcTy = src->getType();
3356 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003357 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003358 assert(Size <= 8 && "does not support size > 8");
3359 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003360 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003361 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3362 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003363 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3364 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003365 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3366 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003367 return;
3368}
3369
Fariborz Jahanian58626502008-11-19 00:59:10 +00003370/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3371/// objc_assign_strongCast (id src, id *dst)
3372///
3373void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003374 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003375 const llvm::Type * SrcTy = src->getType();
3376 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003377 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003378 assert(Size <= 8 && "does not support size > 8");
3379 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003380 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003381 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3382 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003383 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3384 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00003385 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00003386 src, dst, "weakassign");
3387 return;
3388}
3389
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003390void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003391 llvm::Value *DestPtr,
3392 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003393 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003394 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3395 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003396 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003397 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003398 return;
3399}
3400
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003401/// EmitObjCValueForIvar - Code Gen for ivar reference.
3402///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003403LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3404 QualType ObjectTy,
3405 llvm::Value *BaseValue,
3406 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003407 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00003408 const ObjCInterfaceDecl *ID =
3409 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00003410 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3411 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003412}
3413
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003414llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00003415 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003416 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00003417 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003418 return llvm::ConstantInt::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003419 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3420 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003421}
3422
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003423/* *** Private Interface *** */
3424
3425/// EmitImageInfo - Emit the image info marker used to encode some module
3426/// level information.
3427///
3428/// See: <rdr://4810609&4810587&4810587>
3429/// struct IMAGE_INFO {
3430/// unsigned version;
3431/// unsigned flags;
3432/// };
3433enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003434 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003435 eImageInfo_GarbageCollected = (1 << 1),
3436 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003437 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3438
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003439 // A flag indicating that the module has no instances of a @synthesize of a
3440 // superclass variable. <rdar://problem/6803242>
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003441 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003442};
3443
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003444void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003445 unsigned version = 0; // Version is unused?
3446 unsigned flags = 0;
3447
3448 // FIXME: Fix and continue?
3449 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3450 flags |= eImageInfo_GarbageCollected;
3451 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3452 flags |= eImageInfo_GCOnly;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003453
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003454 // We never allow @synthesize of a superclass property.
3455 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003456
Chris Lattner77b89b82010-06-27 07:15:29 +00003457 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3458
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003459 // Emitted as int[2];
3460 llvm::Constant *values[2] = {
Chris Lattner77b89b82010-06-27 07:15:29 +00003461 llvm::ConstantInt::get(Int32Ty, version),
3462 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003463 };
Chris Lattner77b89b82010-06-27 07:15:29 +00003464 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003465
3466 const char *Section;
3467 if (ObjCABI == 1)
3468 Section = "__OBJC, __image_info,regular";
3469 else
3470 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003471 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003472 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson7db6d832009-07-28 18:33:04 +00003473 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003474 Section,
3475 0,
3476 true);
3477 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003478}
3479
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003480
3481// struct objc_module {
3482// unsigned long version;
3483// unsigned long size;
3484// const char *name;
3485// Symtab symtab;
3486// };
3487
3488// FIXME: Get from somewhere
3489static const int ModuleVersion = 7;
3490
3491void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00003492 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003493
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003494 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003495 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3496 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00003497 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003498 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003499 Values[3] = EmitModuleSymbols();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003500 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00003501 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003502 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003503 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003504}
3505
3506llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003507 unsigned NumClasses = DefinedClasses.size();
3508 unsigned NumCategories = DefinedCategories.size();
3509
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003510 // Return null if no symbols were defined.
3511 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003512 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003513
3514 std::vector<llvm::Constant*> Values(5);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003515 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003516 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003517 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3518 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003519
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003520 // The runtime expects exactly the list of defined classes followed
3521 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003522 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003523 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00003524 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003525 ObjCTypes.Int8PtrTy);
3526 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003527 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003528 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003529 ObjCTypes.Int8PtrTy);
3530
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003531 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003532 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003533 NumClasses + NumCategories),
3534 Symbols);
3535
Nick Lewycky0d36dd22009-09-19 20:00:52 +00003536 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003537
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003538 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003539 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3540 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003541 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003542 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003543}
3544
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003545llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003546 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003547 LazySymbols.insert(ID->getIdentifier());
3548
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003549 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003550
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003551 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003552 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003553 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003554 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003555 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003556 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3557 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003558 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003559 }
3560
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003561 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003562}
3563
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003564llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3565 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003566 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003567
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003568 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003569 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003570 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003571 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003572 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003573 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3574 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003575 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003576 }
3577
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003578 if (lvalue)
3579 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003580 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003581}
3582
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003583llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003584 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003585
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003586 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003587 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003588 llvm::ConstantArray::get(VMContext,
3589 Ident->getNameStart()),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003590 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003591 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003592
Owen Andersona1cf15f2009-07-14 23:10:40 +00003593 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003594}
3595
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003596llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3597 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3598 I = MethodDefinitions.find(MD);
3599 if (I != MethodDefinitions.end())
3600 return I->second;
3601
3602 if (MD->hasBody() && MD->getPCHLevel() > 0) {
3603 // MD isn't emitted yet because it comes from PCH.
3604 CGM.EmitTopLevelDecl(const_cast<ObjCMethodDecl*>(MD));
3605 assert(MethodDefinitions[MD] && "EmitTopLevelDecl didn't emit the method!");
3606 return MethodDefinitions[MD];
3607 }
3608
3609 return NULL;
3610}
3611
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003612/// GetIvarLayoutName - Returns a unique constant for the given
3613/// ivar layout bitmap.
3614llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003615 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003616 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003617}
3618
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003619void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003620 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003621 bool ForStrongLayout,
3622 bool &HasUnion) {
3623 const RecordDecl *RD = RT->getDecl();
3624 // FIXME - Use iterator.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003625 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003626 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003627 const llvm::StructLayout *RecLayout =
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003628 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003629
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003630 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3631 ForStrongLayout, HasUnion);
3632}
3633
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003634void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003635 const llvm::StructLayout *Layout,
3636 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00003637 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003638 unsigned int BytePos, bool ForStrongLayout,
3639 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003640 bool IsUnion = (RD && RD->isUnion());
3641 uint64_t MaxUnionIvarSize = 0;
3642 uint64_t MaxSkippedUnionIvarSize = 0;
3643 FieldDecl *MaxField = 0;
3644 FieldDecl *MaxSkippedField = 0;
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003645 FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003646 uint64_t MaxFieldOffset = 0;
3647 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003648 uint64_t LastBitfieldOrUnnamedOffset = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003649
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003650 if (RecFields.empty())
3651 return;
Chris Lattnerf1690852009-03-31 08:48:01 +00003652 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3653 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3654
Chris Lattnerf1690852009-03-31 08:48:01 +00003655 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003656 FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003657 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003658 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00003659 // Note that 'i' here is actually the field index inside RD of Field,
3660 // although this dependency is hidden.
3661 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
3662 FieldOffset = RL.getFieldOffset(i) / 8;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003663 } else
Daniel Dunbare05cc982009-05-03 23:35:23 +00003664 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003665
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003666 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003667 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003668 LastFieldBitfieldOrUnnamed = Field;
3669 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003670 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003671 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003672
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003673 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003674 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003675 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003676 if (FQT->isUnionType())
3677 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003678
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003679 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003680 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003681 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003682 continue;
3683 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003684
Chris Lattnerf1690852009-03-31 08:48:01 +00003685 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003686 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003687 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003688 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003689 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003690 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003691 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3692 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003693 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003694 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003695 FQT = CArray->getElementType();
3696 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003697
3698 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003699 "layout for array of unions not supported");
Fariborz Jahanianf96bdf42011-01-03 19:23:18 +00003700 if (FQT->isRecordType() && ElCount) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003701 int OldIndex = IvarsInfo.size() - 1;
3702 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003703
Ted Kremenek6217b802009-07-29 21:53:49 +00003704 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003705 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003706 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003707
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003708 // Replicate layout information for each array element. Note that
3709 // one element is already done.
3710 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003711 for (int FirstIndex = IvarsInfo.size() - 1,
3712 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003713 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003714 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3715 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3716 IvarsInfo[i].ivar_size));
3717 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3718 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3719 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003720 }
3721 continue;
3722 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003723 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003724 // At this point, we are done with Record/Union and array there of.
3725 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00003726 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003727
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003728 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00003729 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3730 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003731 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003732 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003733 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003734 MaxUnionIvarSize = UnionIvarSize;
3735 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003736 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003737 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003738 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003739 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003740 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003741 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003742 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00003743 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3744 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003745 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003746 // FIXME: Why the asymmetry? We divide by word size in bits on other
3747 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003748 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003749 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003750 MaxSkippedUnionIvarSize = UnionIvarSize;
3751 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003752 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003753 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003754 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003755 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003756 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003757 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003758 }
3759 }
3760 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003761
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003762 if (LastFieldBitfieldOrUnnamed) {
3763 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3764 // Last field was a bitfield. Must update skip info.
3765 Expr *BitWidth = LastFieldBitfieldOrUnnamed->getBitWidth();
3766 uint64_t BitFieldSize =
3767 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
3768 GC_IVAR skivar;
3769 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3770 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3771 + ((BitFieldSize % ByteSizeInBits) != 0);
3772 SkipIvars.push_back(skivar);
3773 } else {
3774 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3775 // Last field was unnamed. Must update skip info.
3776 unsigned FieldSize
3777 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3778 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3779 FieldSize / ByteSizeInBits));
3780 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003781 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003782
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003783 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003784 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003785 MaxUnionIvarSize));
3786 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003787 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003788 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003789}
3790
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003791/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3792/// the computations and returning the layout bitmap (for ivar or blocks) in
3793/// the given argument BitMap string container. Routine reads
3794/// two containers, IvarsInfo and SkipIvars which are assumed to be
3795/// filled already by the caller.
3796llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003797 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00003798 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003799
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003800 // Build the string of skip/scan nibbles
Fariborz Jahanian8c2f2d12009-04-24 17:15:27 +00003801 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003802 unsigned int WordSize =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003803 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003804 if (IvarsInfo[0].ivar_bytepos == 0) {
3805 WordsToSkip = 0;
3806 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003807 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003808 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3809 WordsToScan = IvarsInfo[0].ivar_size;
3810 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003811 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003812 unsigned int TailPrevGCObjC =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003813 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003814 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003815 // consecutive 'scanned' object pointers.
3816 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003817 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003818 // Skip over 'gc'able object pointer which lay over each other.
3819 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3820 continue;
3821 // Must skip over 1 or more words. We save current skip/scan values
3822 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003823 SKIP_SCAN SkScan;
3824 SkScan.skip = WordsToSkip;
3825 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003826 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003827
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003828 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003829 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3830 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003831 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003832 WordsToSkip = 0;
3833 WordsToScan = IvarsInfo[i].ivar_size;
3834 }
3835 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003836 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003837 SKIP_SCAN SkScan;
3838 SkScan.skip = WordsToSkip;
3839 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003840 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003841 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003842
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003843 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003844 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003845 int LastByteSkipped =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003846 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003847 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003848 int LastByteScanned =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003849 IvarsInfo[LastIndex].ivar_bytepos +
3850 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003851 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00003852 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003853 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003854 SKIP_SCAN SkScan;
3855 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3856 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003857 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003858 }
3859 }
3860 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3861 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003862 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003863 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003864 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3865 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3866 // 0xM0 followed by 0x0N detected.
3867 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3868 for (int j = i+1; j < SkipScan; j++)
3869 SkipScanIvars[j] = SkipScanIvars[j+1];
3870 --SkipScan;
3871 }
3872 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003873
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003874 // Generate the string.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003875 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003876 unsigned char byte;
3877 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3878 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3879 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3880 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003881
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003882 // first skip big.
3883 for (unsigned int ix = 0; ix < skip_big; ix++)
3884 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003885
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003886 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003887 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003888 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003889 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003890 byte |= 0xf;
3891 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003892 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003893 byte |= scan_small;
3894 scan_small = 0;
3895 }
3896 BitMap += byte;
3897 }
3898 // next scan big
3899 for (unsigned int ix = 0; ix < scan_big; ix++)
3900 BitMap += (unsigned char)(0x0f);
3901 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003902 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003903 byte = scan_small;
3904 BitMap += byte;
3905 }
3906 }
3907 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003908 unsigned char zero = 0;
3909 BitMap += zero;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003910
3911 llvm::GlobalVariable * Entry =
3912 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3913 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
3914 "__TEXT,__cstring,cstring_literals",
3915 1, true);
3916 return getConstantGEP(VMContext, Entry, 0, 0);
3917}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003918
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003919/// BuildIvarLayout - Builds ivar layout bitmap for the class
3920/// implementation for the __strong or __weak case.
3921/// The layout map displays which words in ivar list must be skipped
3922/// and which must be scanned by GC (see below). String is built of bytes.
3923/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3924/// of words to skip and right nibble is count of words to scan. So, each
3925/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3926/// represented by a 0x00 byte which also ends the string.
3927/// 1. when ForStrongLayout is true, following ivars are scanned:
3928/// - id, Class
3929/// - object *
3930/// - __strong anything
3931///
3932/// 2. When ForStrongLayout is false, following ivars are scanned:
3933/// - __weak anything
3934///
3935llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3936 const ObjCImplementationDecl *OMD,
3937 bool ForStrongLayout) {
3938 bool hasUnion = false;
3939
3940 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
3941 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
3942 return llvm::Constant::getNullValue(PtrTy);
3943
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003944 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003945 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003946 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003947
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003948 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003949 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3950 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3951
3952 if (RecFields.empty())
3953 return llvm::Constant::getNullValue(PtrTy);
3954
3955 SkipIvars.clear();
3956 IvarsInfo.clear();
3957
3958 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
3959 if (IvarsInfo.empty())
3960 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003961 // Sort on byte position in case we encounterred a union nested in
3962 // the ivar list.
3963 if (hasUnion && !IvarsInfo.empty())
3964 std::sort(IvarsInfo.begin(), IvarsInfo.end());
3965 if (hasUnion && !SkipIvars.empty())
3966 std::sort(SkipIvars.begin(), SkipIvars.end());
3967
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003968 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003969 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003970
3971 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003972 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003973 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar4087f272010-08-17 22:39:59 +00003974 OMD->getClassInterface()->getName().data());
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003975 const unsigned char *s = (unsigned char*)BitMap.c_str();
3976 for (unsigned i = 0; i < BitMap.size(); i++)
3977 if (!(s[i] & 0xf0))
3978 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3979 else
3980 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3981 printf("\n");
3982 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003983 return C;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003984}
3985
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003986llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003987 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3988
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003989 // FIXME: Avoid std::string copying.
3990 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003991 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson0032b272009-08-13 21:57:51 +00003992 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003993 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003994 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003995
Owen Andersona1cf15f2009-07-14 23:10:40 +00003996 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003997}
3998
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003999// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004000llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004001 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4002}
4003
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004004llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00004005 std::string TypeStr;
4006 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4007
4008 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004009
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004010 if (!Entry)
4011 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00004012 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00004013 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004014 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004015
Owen Andersona1cf15f2009-07-14 23:10:40 +00004016 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004017}
4018
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004019llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004020 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00004021 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
4022 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00004023
4024 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4025
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004026 if (!Entry)
4027 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00004028 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00004029 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004030 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00004031
Owen Andersona1cf15f2009-07-14 23:10:40 +00004032 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004033}
4034
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004035// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004036llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004037 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004038
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004039 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004040 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004041 llvm::ConstantArray::get(VMContext,
4042 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004043 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004044 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004045
Owen Andersona1cf15f2009-07-14 23:10:40 +00004046 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004047}
4048
4049// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004050// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004051llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004052CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4053 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004054 std::string TypeStr;
4055 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004056 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4057}
4058
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004059void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004060 const ObjCContainerDecl *CD,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004061 llvm::SmallVectorImpl<char> &Name) {
4062 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00004063 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004064 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4065 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004066 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004067 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramer900fc632010-04-17 09:33:03 +00004068 OS << '(' << CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004069 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00004070}
4071
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004072void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004073 EmitModuleInfo();
4074
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004075 // Emit the dummy bodies for any protocols which were referenced but
4076 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004077 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00004078 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4079 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004080 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004081
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004082 std::vector<llvm::Constant*> Values(5);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004083 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004084 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004085 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004086 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00004087 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004088 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00004089 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004090 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00004091 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004092 }
4093
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004094 // Add assembler directives to add lazy undefined symbol references
4095 // for classes which are referenced but not defined. This is
4096 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00004097 //
4098 // FIXME: It would be nice if we had an LLVM construct for this.
4099 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4100 llvm::SmallString<256> Asm;
4101 Asm += CGM.getModule().getModuleInlineAsm();
4102 if (!Asm.empty() && Asm.back() != '\n')
4103 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004104
Daniel Dunbar33063492009-09-07 00:20:42 +00004105 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00004106 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4107 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00004108 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4109 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004110 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004111 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004112 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004113 }
4114
4115 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4116 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4117 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4118 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004119
Daniel Dunbar33063492009-09-07 00:20:42 +00004120 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004121 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004122}
4123
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004124CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004125 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00004126 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004127 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004128 ObjCABI = 2;
4129}
4130
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004131/* *** */
4132
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004133ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004134 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004135 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4136 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004137
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004138 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004139 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004140 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004141 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004142 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004143
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004144 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004145 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004146 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004147
Mike Stumpf5408fe2009-05-16 07:57:57 +00004148 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4149 // comes out a bit cleaner.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004150 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004151 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004152
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004153 // I'm not sure I like this. The implicit coordination is a bit
4154 // gross. We should solve this in a reasonable fashion because this
4155 // is a pretty common task (match some runtime data structure with
4156 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004157
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004158 // FIXME: This is leaked.
4159 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004160
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004161 // struct _objc_super {
4162 // id self;
4163 // Class cls;
4164 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004165 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004166 Ctx.getTranslationUnitDecl(),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004167 SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004168 &Ctx.Idents.get("_objc_super"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004169 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004170 Ctx.getObjCIdType(), 0, 0, false));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004171 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004172 Ctx.getObjCClassType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004173 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004174
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004175 SuperCTy = Ctx.getTagDeclType(RD);
4176 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004177
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004178 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004179 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4180
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004181 // struct _prop_t {
4182 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004183 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004184 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004185 PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004186 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004187 PropertyTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004188
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004189 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004190 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004191 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004192 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004193 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004194 PropertyListTy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004195 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004196 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004197 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004198 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004199 PropertyListTy);
4200 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004201 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004202
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004203 // struct _objc_method {
4204 // SEL _cmd;
4205 // char *method_type;
4206 // char *_imp;
4207 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004208 MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004209 Int8PtrTy,
4210 Int8PtrTy,
4211 NULL);
4212 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004213
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004214 // struct _objc_cache *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004215 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004216 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004217 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004218}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004219
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004220ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004221 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004222 // struct _objc_method_description {
4223 // SEL name;
4224 // char *types;
4225 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004226 MethodDescriptionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004227 llvm::StructType::get(VMContext, SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004228 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004229 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004230 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004231 MethodDescriptionTy);
4232
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004233 // struct _objc_method_description_list {
4234 // int count;
4235 // struct _objc_method_description[1];
4236 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004237 MethodDescriptionListTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004238 llvm::StructType::get(VMContext, IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004239 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004240 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004241 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004242 MethodDescriptionListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004243
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004244 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004245 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004246 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004247
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004248 // Protocol description structures
4249
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004250 // struct _objc_protocol_extension {
4251 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4252 // struct _objc_method_description_list *optional_instance_methods;
4253 // struct _objc_method_description_list *optional_class_methods;
4254 // struct _objc_property_list *instance_properties;
4255 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004256 ProtocolExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004257 llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004258 MethodDescriptionListPtrTy,
4259 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004260 PropertyListPtrTy,
4261 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004262 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004263 ProtocolExtensionTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004264
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004265 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004266 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004267
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004268 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004269
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004270 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
4271 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004272
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004273 const llvm::Type *T =
Mike Stump1eb44332009-09-09 15:08:12 +00004274 llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00004275 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004276 LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004277 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004278 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004279 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
4280
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004281 // struct _objc_protocol {
4282 // struct _objc_protocol_extension *isa;
4283 // char *protocol_name;
4284 // struct _objc_protocol **_objc_protocol_list;
4285 // struct _objc_method_description_list *instance_methods;
4286 // struct _objc_method_description_list *class_methods;
4287 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004288 T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004289 Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004290 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004291 MethodDescriptionListPtrTy,
4292 MethodDescriptionListPtrTy,
4293 NULL);
4294 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
4295
4296 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004297 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004298 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004299 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004300 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004301
4302 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004303 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004304 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004305
4306 // Class description structures
4307
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004308 // struct _objc_ivar {
4309 // char *ivar_name;
4310 // char *ivar_type;
4311 // int ivar_offset;
4312 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004313 IvarTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004314 Int8PtrTy,
4315 IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004316 NULL);
4317 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
4318
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004319 // struct _objc_ivar_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004320 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004321 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004322 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004323
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004324 // struct _objc_method_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004325 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004326 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004327 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004328
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004329 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004330 ClassExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004331 llvm::StructType::get(VMContext, IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004332 Int8PtrTy,
4333 PropertyListPtrTy,
4334 NULL);
4335 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004336 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004337
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004338 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004339
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004340 // struct _objc_class {
4341 // Class isa;
4342 // Class super_class;
4343 // char *name;
4344 // long version;
4345 // long info;
4346 // long instance_size;
4347 // struct _objc_ivar_list *ivars;
4348 // struct _objc_method_list *methods;
4349 // struct _objc_cache *cache;
4350 // struct _objc_protocol_list *protocols;
4351 // char *ivar_layout;
4352 // struct _objc_class_ext *ext;
4353 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004354 T = llvm::StructType::get(VMContext,
4355 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson96e0fc72009-07-29 22:16:19 +00004356 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004357 Int8PtrTy,
4358 LongTy,
4359 LongTy,
4360 LongTy,
4361 IvarListPtrTy,
4362 MethodListPtrTy,
4363 CachePtrTy,
4364 ProtocolListPtrTy,
4365 Int8PtrTy,
4366 ClassExtensionPtrTy,
4367 NULL);
4368 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004369
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004370 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
4371 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004372 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004373
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004374 // struct _objc_category {
4375 // char *category_name;
4376 // char *class_name;
4377 // struct _objc_method_list *instance_method;
4378 // struct _objc_method_list *class_method;
4379 // uint32_t size; // sizeof(struct _objc_category)
4380 // struct _objc_property_list *instance_properties;// category's @property
4381 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004382 CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004383 Int8PtrTy,
4384 MethodListPtrTy,
4385 MethodListPtrTy,
4386 ProtocolListPtrTy,
4387 IntTy,
4388 PropertyListPtrTy,
4389 NULL);
4390 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
4391
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004392 // Global metadata structures
4393
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004394 // struct _objc_symtab {
4395 // long sel_ref_cnt;
4396 // SEL *refs;
4397 // short cls_def_cnt;
4398 // short cat_def_cnt;
4399 // char *defs[cls_def_cnt + cat_def_cnt];
4400 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004401 SymtabTy = llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004402 SelectorPtrTy,
4403 ShortTy,
4404 ShortTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004405 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004406 NULL);
4407 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004408 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004409
Fariborz Jahaniandb286862009-01-22 00:37:21 +00004410 // struct _objc_module {
4411 // long version;
4412 // long size; // sizeof(struct _objc_module)
4413 // char *name;
4414 // struct _objc_symtab* symtab;
4415 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004416 ModuleTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004417 llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004418 LongTy,
4419 Int8PtrTy,
4420 SymtabPtrTy,
4421 NULL);
4422 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00004423
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004424
Mike Stumpf5408fe2009-05-16 07:57:57 +00004425 // FIXME: This is the size of the setjmp buffer and should be target
4426 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00004427 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004428
Anders Carlsson124526b2008-09-09 10:10:21 +00004429 // Exceptions
Owen Anderson96e0fc72009-07-29 22:16:19 +00004430 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004431 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004432
4433 ExceptionDataTy =
Chris Lattner77b89b82010-06-27 07:15:29 +00004434 llvm::StructType::get(VMContext,
4435 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4436 SetJmpBufferSize),
Anders Carlsson124526b2008-09-09 10:10:21 +00004437 StackPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004438 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson124526b2008-09-09 10:10:21 +00004439 ExceptionDataTy);
4440
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004441}
4442
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004443ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004444 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004445 // struct _method_list_t {
4446 // uint32_t entsize; // sizeof(struct _objc_method)
4447 // uint32_t method_count;
4448 // struct _objc_method method_list[method_count];
4449 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004450 MethodListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004451 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004452 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004453 NULL);
4454 CGM.getModule().addTypeName("struct.__method_list_t",
4455 MethodListnfABITy);
4456 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004457 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004458
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004459 // struct _protocol_t {
4460 // id isa; // NULL
4461 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004462 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004463 // const struct method_list_t * const instance_methods;
4464 // const struct method_list_t * const class_methods;
4465 // const struct method_list_t *optionalInstanceMethods;
4466 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004467 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004468 // const uint32_t size; // sizeof(struct _protocol_t)
4469 // const uint32_t flags; // = 0
4470 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004471
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004472 // Holder for struct _protocol_list_t *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004473 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004474
Owen Anderson47a434f2009-08-05 23:18:46 +00004475 ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004476 Int8PtrTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004477 llvm::PointerType::getUnqual(
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004478 ProtocolListTyHolder),
4479 MethodListnfABIPtrTy,
4480 MethodListnfABIPtrTy,
4481 MethodListnfABIPtrTy,
4482 MethodListnfABIPtrTy,
4483 PropertyListPtrTy,
4484 IntTy,
4485 IntTy,
4486 NULL);
4487 CGM.getModule().addTypeName("struct._protocol_t",
4488 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004489
4490 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004491 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004492
Fariborz Jahanianda320092009-01-29 19:24:30 +00004493 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004494 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00004495 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004496 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004497 ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004498 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004499 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004500 NULL);
4501 CGM.getModule().addTypeName("struct._objc_protocol_list",
4502 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004503 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004504 ProtocolListnfABITy);
4505
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004506 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004507 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004508
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004509 // struct _ivar_t {
4510 // unsigned long int *offset; // pointer to ivar offset location
4511 // char *name;
4512 // char *type;
4513 // uint32_t alignment;
4514 // uint32_t size;
4515 // }
Mike Stump1eb44332009-09-09 15:08:12 +00004516 IvarnfABITy = llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00004517 llvm::PointerType::getUnqual(LongTy),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004518 Int8PtrTy,
4519 Int8PtrTy,
4520 IntTy,
4521 IntTy,
4522 NULL);
4523 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004524
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004525 // struct _ivar_list_t {
4526 // uint32 entsize; // sizeof(struct _ivar_t)
4527 // uint32 count;
4528 // struct _iver_t list[count];
4529 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004530 IvarListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004531 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004532 llvm::ArrayType::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004533 IvarnfABITy, 0),
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004534 NULL);
4535 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004536
Owen Anderson96e0fc72009-07-29 22:16:19 +00004537 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004538
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004539 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004540 // uint32_t const flags;
4541 // uint32_t const instanceStart;
4542 // uint32_t const instanceSize;
4543 // uint32_t const reserved; // only when building for 64bit targets
4544 // const uint8_t * const ivarLayout;
4545 // const char *const name;
4546 // const struct _method_list_t * const baseMethods;
4547 // const struct _objc_protocol_list *const baseProtocols;
4548 // const struct _ivar_list_t *const ivars;
4549 // const uint8_t * const weakIvarLayout;
4550 // const struct _prop_list_t * const properties;
4551 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004552
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004553 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson47a434f2009-08-05 23:18:46 +00004554 ClassRonfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004555 IntTy,
4556 IntTy,
4557 Int8PtrTy,
4558 Int8PtrTy,
4559 MethodListnfABIPtrTy,
4560 ProtocolListnfABIPtrTy,
4561 IvarListnfABIPtrTy,
4562 Int8PtrTy,
4563 PropertyListPtrTy,
4564 NULL);
4565 CGM.getModule().addTypeName("struct._class_ro_t",
4566 ClassRonfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004567
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004568 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
4569 std::vector<const llvm::Type*> Params;
4570 Params.push_back(ObjectPtrTy);
4571 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004572 ImpnfABITy = llvm::PointerType::getUnqual(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004573 llvm::FunctionType::get(ObjectPtrTy, Params, false));
4574
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004575 // struct _class_t {
4576 // struct _class_t *isa;
4577 // struct _class_t * const superclass;
4578 // void *cache;
4579 // IMP *vtable;
4580 // struct class_ro_t *ro;
4581 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004582
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004583 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004584 ClassnfABITy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004585 llvm::StructType::get(VMContext,
4586 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004587 llvm::PointerType::getUnqual(ClassTyHolder),
4588 CachePtrTy,
4589 llvm::PointerType::getUnqual(ImpnfABITy),
4590 llvm::PointerType::getUnqual(ClassRonfABITy),
4591 NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004592 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4593
4594 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004595 ClassnfABITy);
4596
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004597 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004598 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004599
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004600 // struct _category_t {
4601 // const char * const name;
4602 // struct _class_t *const cls;
4603 // const struct _method_list_t * const instance_methods;
4604 // const struct _method_list_t * const class_methods;
4605 // const struct _protocol_list_t * const protocols;
4606 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004607 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004608 CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004609 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004610 MethodListnfABIPtrTy,
4611 MethodListnfABIPtrTy,
4612 ProtocolListnfABIPtrTy,
4613 PropertyListPtrTy,
4614 NULL);
4615 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004616
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004617 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004618 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4619 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004620
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004621 // MessageRefTy - LLVM for:
4622 // struct _message_ref_t {
4623 // IMP messenger;
4624 // SEL name;
4625 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004626
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004627 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004628 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004629 Ctx.getTranslationUnitDecl(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004630 SourceLocation(),
4631 &Ctx.Idents.get("_message_ref_t"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004632 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004633 Ctx.VoidPtrTy, 0, 0, false));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004634 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004635 Ctx.getObjCSelType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004636 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004637
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004638 MessageRefCTy = Ctx.getTagDeclType(RD);
4639 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4640 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004641
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004642 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004643 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004644
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004645 // SuperMessageRefTy - LLVM for:
4646 // struct _super_message_ref_t {
4647 // SUPER_IMP messenger;
4648 // SEL name;
4649 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004650 SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004651 SelectorPtrTy,
4652 NULL);
4653 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004654
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004655 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004656 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4657
Daniel Dunbare588b992009-03-01 04:46:24 +00004658
4659 // struct objc_typeinfo {
4660 // const void** vtable; // objc_ehtype_vtable + 2
4661 // const char* name; // c++ typeinfo string
4662 // Class cls;
4663 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004664 EHTypeTy = llvm::StructType::get(VMContext,
4665 llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbare588b992009-03-01 04:46:24 +00004666 Int8PtrTy,
4667 ClassnfABIPtrTy,
4668 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004669 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004670 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004671}
4672
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004673llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004674 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004675
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004676 return NULL;
4677}
4678
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004679void CGObjCNonFragileABIMac::AddModuleClassList(const
4680 std::vector<llvm::GlobalValue*>
4681 &Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004682 const char *SymbolName,
4683 const char *SectionName) {
4684 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004685
Daniel Dunbar463b8762009-05-15 21:48:48 +00004686 if (!NumClasses)
4687 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004688
Daniel Dunbar463b8762009-05-15 21:48:48 +00004689 std::vector<llvm::Constant*> Symbols(NumClasses);
4690 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004691 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004692 ObjCTypes.Int8PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004693 llvm::Constant* Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004694 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004695 NumClasses),
4696 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004697
Daniel Dunbar463b8762009-05-15 21:48:48 +00004698 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004699 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004700 llvm::GlobalValue::InternalLinkage,
4701 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004702 SymbolName);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004703 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00004704 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004705 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004706}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004707
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004708void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4709 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004710
Daniel Dunbar463b8762009-05-15 21:48:48 +00004711 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004712 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004713 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004714 "\01L_OBJC_LABEL_CLASS_$",
4715 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004716
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004717 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4718 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4719 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4720 continue;
4721 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004722 }
4723
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004724 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4725 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4726 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4727 continue;
4728 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4729 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004730
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004731 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004732 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4733 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004734
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004735 // Build list of all implemented category addresses in array
4736 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004737 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004738 "\01L_OBJC_LABEL_CATEGORY_$",
4739 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004740 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004741 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4742 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004743
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004744 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004745}
4746
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004747/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004748/// NonLegacyDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004749/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004750/// message dispatch call for all the rest.
4751///
4752bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004753 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4754 default:
4755 assert(0 && "Invalid dispatch method!");
4756 case CodeGenOptions::Legacy:
Daniel Dunbar2feefe82010-02-01 21:07:33 +00004757 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004758 case CodeGenOptions::NonLegacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00004759 return false;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004760 case CodeGenOptions::Mixed:
4761 break;
4762 }
4763
4764 // If so, see whether this selector is in the white-list of things which must
4765 // use the new dispatch convention. We lazily build a dense set for this.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004766 if (NonLegacyDispatchMethods.empty()) {
4767 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4768 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4769 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4770 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4771 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4772 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4773 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4774 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4775 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4776 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004777
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004778 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4779 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4780 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4781 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4782 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4783 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4784 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4785 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004786 // "countByEnumeratingWithState:objects:count"
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004787 IdentifierInfo *KeyIdents[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004788 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4789 &CGM.getContext().Idents.get("objects"),
4790 &CGM.getContext().Idents.get("count")
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004791 };
4792 NonLegacyDispatchMethods.insert(
4793 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004794 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004795
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004796 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004797}
4798
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004799// Metadata flags
4800enum MetaDataDlags {
4801 CLS = 0x0,
4802 CLS_META = 0x1,
4803 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004804 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004805 CLS_EXCEPTION = 0x20
4806};
4807/// BuildClassRoTInitializer - generate meta-data for:
4808/// struct _class_ro_t {
4809/// uint32_t const flags;
4810/// uint32_t const instanceStart;
4811/// uint32_t const instanceSize;
4812/// uint32_t const reserved; // only when building for 64bit targets
4813/// const uint8_t * const ivarLayout;
4814/// const char *const name;
4815/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004816/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004817/// const struct _ivar_list_t *const ivars;
4818/// const uint8_t * const weakIvarLayout;
4819/// const struct _prop_list_t * const properties;
4820/// }
4821///
4822llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004823 unsigned flags,
4824 unsigned InstanceStart,
4825 unsigned InstanceSize,
4826 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004827 std::string ClassName = ID->getNameAsString();
4828 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004829 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4830 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4831 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004832 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004833 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4834 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004835 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004836 // const struct _method_list_t * const baseMethods;
4837 std::vector<llvm::Constant*> Methods;
4838 std::string MethodListName("\01l_OBJC_$_");
4839 if (flags & CLS_META) {
4840 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004841 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004842 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004843 // Class methods should always be defined.
4844 Methods.push_back(GetMethodConstant(*i));
4845 }
4846 } else {
4847 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004848 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004849 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004850 // Instance methods should always be defined.
4851 Methods.push_back(GetMethodConstant(*i));
4852 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004853 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004854 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004855 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004856
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004857 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4858 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004859
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004860 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4861 if (llvm::Constant *C = GetMethodConstant(MD))
4862 Methods.push_back(C);
4863 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4864 if (llvm::Constant *C = GetMethodConstant(MD))
4865 Methods.push_back(C);
4866 }
4867 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004868 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004869 Values[ 5] = EmitMethodList(MethodListName,
4870 "__DATA, __objc_const", Methods);
4871
Fariborz Jahanianda320092009-01-29 19:24:30 +00004872 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4873 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004874 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004875 + OID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00004876 OID->all_referenced_protocol_begin(),
4877 OID->all_referenced_protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004878
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004879 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004880 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004881 else
4882 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004883 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4884 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004885 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004886 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004887 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004888 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4889 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00004890 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004891 Values);
4892 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004893 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4894 llvm::GlobalValue::InternalLinkage,
4895 Init,
4896 (flags & CLS_META) ?
4897 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4898 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004899 CLASS_RO_GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004900 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004901 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004902 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004903
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004904}
4905
4906/// BuildClassMetaData - This routine defines that to-level meta-data
4907/// for the given ClassName for:
4908/// struct _class_t {
4909/// struct _class_t *isa;
4910/// struct _class_t * const superclass;
4911/// void *cache;
4912/// IMP *vtable;
4913/// struct class_ro_t *ro;
4914/// }
4915///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004916llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004917 std::string &ClassName,
4918 llvm::Constant *IsAGV,
4919 llvm::Constant *SuperClassGV,
4920 llvm::Constant *ClassRoGV,
4921 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004922 std::vector<llvm::Constant*> Values(5);
4923 Values[0] = IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004924 Values[1] = SuperClassGV;
4925 if (!Values[1])
4926 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004927 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4928 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4929 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004930 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004931 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004932 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4933 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004934 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004935 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004936 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004937 if (HiddenVisibility)
4938 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004939 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004940}
4941
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004942bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00004943CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004944 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004945}
4946
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004947void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004948 uint32_t &InstanceStart,
4949 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004950 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004951 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004952
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004953 // InstanceSize is really instance end.
Anders Carlsson243a6852009-07-18 21:26:44 +00004954 InstanceSize = llvm::RoundUpToAlignment(RL.getDataSize(), 8) / 8;
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004955
4956 // If there are no fields, the start is the same as the end.
4957 if (!RL.getFieldCount())
4958 InstanceStart = InstanceSize;
4959 else
4960 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004961}
4962
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004963void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4964 std::string ClassName = ID->getNameAsString();
4965 if (!ObjCEmptyCacheVar) {
4966 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004967 CGM.getModule(),
4968 ObjCTypes.CacheTy,
4969 false,
4970 llvm::GlobalValue::ExternalLinkage,
4971 0,
4972 "_objc_empty_cache");
4973
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004974 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004975 CGM.getModule(),
4976 ObjCTypes.ImpnfABITy,
4977 false,
4978 llvm::GlobalValue::ExternalLinkage,
4979 0,
4980 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004981 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004982 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004983 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004984 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004985 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00004986 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004987 uint32_t InstanceSize = InstanceStart;
4988 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004989 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4990 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004991
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004992 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004993
4994 bool classIsHidden =
John McCall1fb0caa2010-10-22 21:05:15 +00004995 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004996 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004997 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004998 if (ID->getNumIvarInitializers())
4999 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005000 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005001 // class is root
5002 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005003 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005004 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005005 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005006 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005007 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5008 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5009 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005010 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005011 if (Root->hasAttr<WeakImportAttr>())
5012 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005013 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005014 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005015 ObjCMetaClassName +
5016 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005017 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005018 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
5019 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005020 }
5021 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5022 InstanceStart,
5023 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005024 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005025 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005026 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5027 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005028 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005029
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005030 // Metadata for the class
5031 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005032 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005033 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00005034 if (ID->getNumIvarInitializers())
5035 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005036
Douglas Gregor68584ed2009-06-18 16:11:24 +00005037 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005038 flags |= CLS_EXCEPTION;
5039
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005040 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005041 flags |= CLS_ROOT;
5042 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00005043 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005044 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005045 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005046 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005047 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005048 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
5049 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005050 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005051 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005052 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005053 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005054 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005055 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005056
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005057 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005058 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005059 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5060 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005061 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005062
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005063 // Determine if this class is also "non-lazy".
5064 if (ImplementationIsNonLazy(ID))
5065 DefinedNonLazyClasses.push_back(ClassMD);
5066
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005067 // Force the definition of the EHType if necessary.
5068 if (flags & CLS_EXCEPTION)
5069 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005070}
5071
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005072/// GenerateProtocolRef - This routine is called to generate code for
5073/// a protocol reference expression; as in:
5074/// @code
5075/// @protocol(Proto1);
5076/// @endcode
5077/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5078/// which will hold address of the protocol meta-data.
5079///
5080llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005081 const ObjCProtocolDecl *PD) {
5082
Fariborz Jahanian960cd062009-04-10 18:47:34 +00005083 // This routine is called for @protocol only. So, we must build definition
5084 // of protocol's meta-data (not a reference to it!)
5085 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005086 llvm::Constant *Init =
5087 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
5088 ObjCTypes.ExternalProtocolPtrTy);
5089
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005090 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar4087f272010-08-17 22:39:59 +00005091 ProtocolName += PD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005092
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005093 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5094 if (PTGV)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005095 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005096 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005097 CGM.getModule(),
5098 Init->getType(), false,
5099 llvm::GlobalValue::WeakAnyLinkage,
5100 Init,
5101 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005102 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5103 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005104 CGM.AddUsedGlobal(PTGV);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005105 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005106}
5107
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005108/// GenerateCategory - Build metadata for a category implementation.
5109/// struct _category_t {
5110/// const char * const name;
5111/// struct _class_t *const cls;
5112/// const struct _method_list_t * const instance_methods;
5113/// const struct _method_list_t * const class_methods;
5114/// const struct _protocol_list_t * const protocols;
5115/// const struct _prop_list_t * const properties;
5116/// }
5117///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005118void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005119 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005120 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005121 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5122 "_$_" + OCD->getNameAsString());
5123 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005124 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005125
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005126 std::vector<llvm::Constant*> Values(6);
5127 Values[0] = GetClassName(OCD->getIdentifier());
5128 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005129 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00005130 if (Interface->hasAttr<WeakImportAttr>())
5131 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5132
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005133 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005134 std::vector<llvm::Constant*> Methods;
5135 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005136 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005137 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005138
5139 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005140 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005141 // Instance methods should always be defined.
5142 Methods.push_back(GetMethodConstant(*i));
5143 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005144
5145 Values[2] = EmitMethodList(MethodListName,
5146 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005147 Methods);
5148
5149 MethodListName = Prefix;
5150 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5151 OCD->getNameAsString();
5152 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005153 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005154 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005155 // Class methods should always be defined.
5156 Methods.push_back(GetMethodConstant(*i));
5157 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005158
5159 Values[3] = EmitMethodList(MethodListName,
5160 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005161 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005162 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005163 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005164 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005165 llvm::SmallString<256> ExtName;
5166 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5167 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005168 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005169 + Interface->getName() + "_$_"
5170 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005171 Category->protocol_begin(),
5172 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005173 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5174 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00005175 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005176 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5177 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005178 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005179
5180 llvm::Constant *Init =
5181 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005182 Values);
5183 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005184 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005185 false,
5186 llvm::GlobalValue::InternalLinkage,
5187 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005188 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005189 GCATV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005190 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005191 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00005192 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005193 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005194
5195 // Determine if this category is also "non-lazy".
5196 if (ImplementationIsNonLazy(OCD))
5197 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005198}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005199
5200/// GetMethodConstant - Return a struct objc_method constant for the
5201/// given method if it has been defined. The result is null if the
5202/// method has not been defined. The return value has type MethodPtrTy.
5203llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005204 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00005205 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005206 if (!Fn)
5207 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005208
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005209 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005210 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00005211 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005212 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005213 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005214 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005215 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005216}
5217
5218/// EmitMethodList - Build meta-data for method declarations
5219/// struct _method_list_t {
5220/// uint32_t entsize; // sizeof(struct _objc_method)
5221/// uint32_t method_count;
5222/// struct _objc_method method_list[method_count];
5223/// }
5224///
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005225llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
5226 const char *Section,
5227 const ConstantVector &Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005228 // Return null for empty list.
5229 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005230 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005231
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005232 std::vector<llvm::Constant*> Values(3);
5233 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00005234 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005235 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005236 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005237 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005238 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005239 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005240 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005241 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005242
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005243 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005244 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005245 llvm::GlobalValue::InternalLinkage,
5246 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005247 Name);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005248 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005249 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005250 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00005251 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005252 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005253 ObjCTypes.MethodListnfABIPtrTy);
5254}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005255
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005256/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5257/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00005258llvm::GlobalVariable *
5259CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5260 const ObjCIvarDecl *Ivar) {
5261 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00005262 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00005263 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005264 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005265 CGM.getModule().getGlobalVariable(Name);
5266 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005267 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005268 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005269 false,
5270 llvm::GlobalValue::ExternalLinkage,
5271 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00005272 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005273 return IvarOffsetGV;
5274}
5275
Daniel Dunbare83be122010-04-02 21:14:02 +00005276llvm::Constant *
5277CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5278 const ObjCIvarDecl *Ivar,
5279 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00005280 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005281 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00005282 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005283 IvarOffsetGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005284 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00005285
Mike Stumpf5408fe2009-05-16 07:57:57 +00005286 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5287 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00005288 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5289 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall1fb0caa2010-10-22 21:05:15 +00005290 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00005291 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00005292 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00005293 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005294 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005295 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005296}
5297
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005298/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00005299/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005300/// IvarListnfABIPtrTy.
5301/// struct _ivar_t {
5302/// unsigned long int *offset; // pointer to ivar offset location
5303/// char *name;
5304/// char *type;
5305/// uint32_t alignment;
5306/// uint32_t size;
5307/// }
5308/// struct _ivar_list_t {
5309/// uint32 entsize; // sizeof(struct _ivar_t)
5310/// uint32 count;
5311/// struct _iver_t list[count];
5312/// }
5313///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005314
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005315llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005316 const ObjCImplementationDecl *ID) {
5317
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005318 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005319
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005320 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5321 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005322
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005323 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005324
Daniel Dunbar91636d62009-04-20 00:33:43 +00005325 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00005326 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005327 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005328
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005329 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
5330 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005331 // Ignore unnamed bit-fields.
5332 if (!IVD->getDeclName())
5333 continue;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005334 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005335 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005336 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5337 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005338 const llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005339 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00005340 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005341 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005342 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005343 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005344 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00005345 // NOTE. Size of a bitfield does not match gcc's, because of the
5346 // way bitfields are treated special in each. But I am told that
5347 // 'size' for bitfield ivars is ignored by the runtime so it does
5348 // not matter. If it matters, there is enough info to get the
5349 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005350 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00005351 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005352 }
5353 // Return null for empty list.
5354 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005355 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005356 std::vector<llvm::Constant*> Values(3);
Duncan Sands9408c452009-05-09 07:08:47 +00005357 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005358 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5359 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005360 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005361 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005362 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005363 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005364 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5365 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005366 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005367 llvm::GlobalValue::InternalLinkage,
5368 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005369 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005370 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005371 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005372 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005373
Chris Lattnerad64e022009-07-17 23:57:13 +00005374 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005375 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005376}
5377
5378llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005379 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005380 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005381
Fariborz Jahanianda320092009-01-29 19:24:30 +00005382 if (!Entry) {
5383 // We use the initializer as a marker of whether this is a forward
5384 // reference or not. At module finalization we add the empty
5385 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005386 Entry =
5387 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5388 llvm::GlobalValue::ExternalLinkage,
5389 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005390 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00005391 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005392 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005393
Fariborz Jahanianda320092009-01-29 19:24:30 +00005394 return Entry;
5395}
5396
5397/// GetOrEmitProtocol - Generate the protocol meta-data:
5398/// @code
5399/// struct _protocol_t {
5400/// id isa; // NULL
5401/// const char * const protocol_name;
5402/// const struct _protocol_list_t * protocol_list; // super protocols
5403/// const struct method_list_t * const instance_methods;
5404/// const struct method_list_t * const class_methods;
5405/// const struct method_list_t *optionalInstanceMethods;
5406/// const struct method_list_t *optionalClassMethods;
5407/// const struct _prop_list_t * properties;
5408/// const uint32_t size; // sizeof(struct _protocol_t)
5409/// const uint32_t flags; // = 0
5410/// }
5411/// @endcode
5412///
5413
5414llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005415 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005416 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005417
Fariborz Jahanianda320092009-01-29 19:24:30 +00005418 // Early exit if a defining object has already been generated.
5419 if (Entry && Entry->hasInitializer())
5420 return Entry;
5421
Fariborz Jahanianda320092009-01-29 19:24:30 +00005422 // Construct method lists.
5423 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5424 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005425 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005426 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005427 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005428 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005429 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5430 OptInstanceMethods.push_back(C);
5431 } else {
5432 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005433 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005434 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005435
5436 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005437 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005438 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005439 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005440 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5441 OptClassMethods.push_back(C);
5442 } else {
5443 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005444 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005445 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005446
Fariborz Jahanianda320092009-01-29 19:24:30 +00005447 std::vector<llvm::Constant*> Values(10);
5448 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00005449 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005450 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005451 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5452 PD->protocol_begin(),
5453 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005454
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005455 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005456 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005457 "__DATA, __objc_const",
5458 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005459 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005460 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005461 "__DATA, __objc_const",
5462 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005463 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005464 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005465 "__DATA, __objc_const",
5466 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005467 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005468 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005469 "__DATA, __objc_const",
5470 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005471 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005472 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005473 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00005474 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005475 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005476 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005477 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005478 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005479
Fariborz Jahanianda320092009-01-29 19:24:30 +00005480 if (Entry) {
5481 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00005482 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005483 Entry->setInitializer(Init);
5484 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005485 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005486 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5487 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5488 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005489 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005490 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005491 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005492 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005493 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005494 CGM.AddUsedGlobal(Entry);
5495
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005496 // Use this protocol meta-data to build protocol list table in section
5497 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005498 llvm::GlobalVariable *PTGV =
5499 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5500 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5501 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005502 PTGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005503 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00005504 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005505 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005506 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005507 return Entry;
5508}
5509
5510/// EmitProtocolList - Generate protocol list meta-data:
5511/// @code
5512/// struct _protocol_list_t {
5513/// long protocol_count; // Note, this is 32/64 bit
5514/// struct _protocol_t[protocol_count];
5515/// }
5516/// @endcode
5517///
5518llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005519CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5520 ObjCProtocolDecl::protocol_iterator begin,
5521 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005522 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005523
Fariborz Jahanianda320092009-01-29 19:24:30 +00005524 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005525 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005526 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005527
Daniel Dunbar948e2582009-02-15 07:36:20 +00005528 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005529 llvm::SmallString<256> TmpName;
5530 Name.toVector(TmpName);
5531 llvm::GlobalVariable *GV =
5532 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005533 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005534 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005535
Daniel Dunbar948e2582009-02-15 07:36:20 +00005536 for (; begin != end; ++begin)
5537 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5538
Fariborz Jahanianda320092009-01-29 19:24:30 +00005539 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005540 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005541 ObjCTypes.ProtocolnfABIPtrTy));
5542
Fariborz Jahanianda320092009-01-29 19:24:30 +00005543 std::vector<llvm::Constant*> Values(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005544 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005545 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005546 Values[1] =
Owen Anderson7db6d832009-07-28 18:33:04 +00005547 llvm::ConstantArray::get(
Owen Anderson96e0fc72009-07-29 22:16:19 +00005548 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005549 ProtocolRefs.size()),
5550 ProtocolRefs);
5551
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005552 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005553 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005554 llvm::GlobalValue::InternalLinkage,
5555 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005556 Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005557 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005558 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005559 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00005560 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005561 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00005562 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005563}
5564
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005565/// GetMethodDescriptionConstant - This routine build following meta-data:
5566/// struct _objc_method {
5567/// SEL _cmd;
5568/// char *method_type;
5569/// char *_imp;
5570/// }
5571
5572llvm::Constant *
5573CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5574 std::vector<llvm::Constant*> Desc(3);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005575 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005576 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5577 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005578 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005579 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005580 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005581 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005582}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005583
5584/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5585/// This code gen. amounts to generating code for:
5586/// @code
5587/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5588/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005589///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00005590LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00005591 CodeGen::CodeGenFunction &CGF,
5592 QualType ObjectTy,
5593 llvm::Value *BaseValue,
5594 const ObjCIvarDecl *Ivar,
5595 unsigned CVRQualifiers) {
5596 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00005597 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5598 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005599}
5600
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005601llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005602 CodeGen::CodeGenFunction &CGF,
5603 const ObjCInterfaceDecl *Interface,
5604 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005605 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005606}
5607
Fariborz Jahanian46551122009-02-04 00:22:57 +00005608CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005609 CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005610 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005611 QualType ResultType,
5612 Selector Sel,
5613 llvm::Value *Receiver,
5614 QualType Arg0Ty,
5615 bool IsSuper,
5616 const CallArgList &CallArgs) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00005617 // FIXME. Even though IsSuper is passes. This function doese not handle calls
5618 // to 'super' receivers.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005619 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005620 llvm::Value *Arg0 = Receiver;
5621 if (!IsSuper)
5622 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005623
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005624 // Find the message function name.
Mike Stumpf5408fe2009-05-16 07:57:57 +00005625 // FIXME. This is too much work to get the ABI-specific result type needed to
5626 // find the message name.
John McCallead608a2010-02-26 00:48:12 +00005627 const CGFunctionInfo &FnInfo
Rafael Espindola264ba482010-03-30 20:24:48 +00005628 = Types.getFunctionInfo(ResultType, CallArgList(),
5629 FunctionType::ExtInfo());
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005630 llvm::Constant *Fn = 0;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005631 std::string Name("\01l_");
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005632 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Chris Lattner9b7d6702010-08-18 16:09:06 +00005633 if (IsSuper) {
5634 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5635 Name += "objc_msgSendSuper2_stret_fixup";
5636 } else {
5637 Fn = ObjCTypes.getMessageSendStretFixupFn();
5638 Name += "objc_msgSend_stret_fixup";
5639 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005640 } else if (!IsSuper && CGM.ReturnTypeUsesFPRet(ResultType)) {
5641 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5642 Name += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005643 } else {
Chris Lattner9b7d6702010-08-18 16:09:06 +00005644 if (IsSuper) {
5645 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
5646 Name += "objc_msgSendSuper2_fixup";
5647 } else {
5648 Fn = ObjCTypes.getMessageSendFixupFn();
5649 Name += "objc_msgSend_fixup";
5650 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005651 }
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005652 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005653 Name += '_';
5654 std::string SelName(Sel.getAsString());
5655 // Replace all ':' in selector name with '_' ouch!
Mike Stump1eb44332009-09-09 15:08:12 +00005656 for (unsigned i = 0; i < SelName.size(); i++)
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005657 if (SelName[i] == ':')
5658 SelName[i] = '_';
5659 Name += SelName;
5660 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5661 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005662 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005663 std::vector<llvm::Constant*> Values(2);
5664 Values[0] = Fn;
5665 Values[1] = GetMethodVarName(Sel);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005666 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005667 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005668 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005669 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005670 Name);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005671 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005672 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005673 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005674 }
5675 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005676
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005677 CallArgList ActualArgs;
5678 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005679 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005680 ObjCTypes.MessageRefCPtrTy));
5681 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
John McCall04a67a62010-02-05 21:31:56 +00005682 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00005683 FunctionType::ExtInfo());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005684 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5685 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00005686 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005687 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Anderson96e0fc72009-07-29 22:16:19 +00005688 llvm::PointerType::getUnqual(FTy));
John McCallef072fd2010-05-22 01:48:05 +00005689 return CGF.EmitCall(FnInfo1, Callee, Return, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005690}
5691
5692/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005693CodeGen::RValue
5694CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005695 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005696 QualType ResultType,
5697 Selector Sel,
5698 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005699 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00005700 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005701 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005702 return LegacyDispatchedSelector(Sel)
John McCallef072fd2010-05-22 01:48:05 +00005703 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5704 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005705 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005706 false, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005707 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005708 Receiver, CGF.getContext().getObjCIdType(),
5709 false, CallArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005710}
5711
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005712llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005713CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005714 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5715
Daniel Dunbardfff2302009-03-02 05:18:14 +00005716 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005717 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005718 false, llvm::GlobalValue::ExternalLinkage,
5719 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005720 }
5721
5722 return GV;
5723}
5724
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005725llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5726 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005727 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005728
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005729 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005730 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005731 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005732 Entry =
5733 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005734 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005735 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005736 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005737 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005738 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005739 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005740 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005741 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005742 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005743
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005744 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar11394522009-04-18 08:51:00 +00005745}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005746
Daniel Dunbar11394522009-04-18 08:51:00 +00005747llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005748CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005749 const ObjCInterfaceDecl *ID) {
5750 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005751
Daniel Dunbar11394522009-04-18 08:51:00 +00005752 if (!Entry) {
5753 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5754 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005755 Entry =
5756 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005757 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005758 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005759 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005760 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005761 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005762 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005763 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005764 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005765 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005766
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005767 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005768}
5769
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005770/// EmitMetaClassRef - Return a Value * of the address of _class_t
5771/// meta-data
5772///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005773llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5774 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005775 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5776 if (Entry)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005777 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005778
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005779 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005780 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005781 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005782 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005783 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005784 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005785 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005786 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005787 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005788 ObjCTypes.ClassnfABIPtrTy));
5789
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005790 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005791 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005792
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005793 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005794}
5795
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005796/// GetClass - Return a reference to the class for the given interface
5797/// decl.
5798llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5799 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00005800 if (ID->hasAttr<WeakImportAttr>()) {
5801 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5802 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5803 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5804 }
5805
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005806 return EmitClassRef(Builder, ID);
5807}
5808
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005809/// Generates a message send where the super is the receiver. This is
5810/// a message send to self with special delivery semantics indicating
5811/// which class's method should be called.
5812CodeGen::RValue
5813CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005814 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005815 QualType ResultType,
5816 Selector Sel,
5817 const ObjCInterfaceDecl *Class,
5818 bool isCategoryImpl,
5819 llvm::Value *Receiver,
5820 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005821 const CodeGen::CallArgList &CallArgs,
5822 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005823 // ...
5824 // Create and init a super structure; this is a (receiver, class)
5825 // pair we will pass to objc_msgSendSuper.
5826 llvm::Value *ObjCSuper =
5827 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005828
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005829 llvm::Value *ReceiverAsObject =
5830 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5831 CGF.Builder.CreateStore(ReceiverAsObject,
5832 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005833
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005834 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005835 llvm::Value *Target;
5836 if (IsClassMessage) {
5837 if (isCategoryImpl) {
5838 // Message sent to "super' in a class method defined in
5839 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005840 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005841 Target = CGF.Builder.CreateStructGEP(Target, 0);
5842 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00005843 } else
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005844 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stumpb3589f42009-07-30 22:28:39 +00005845 } else
Daniel Dunbar11394522009-04-18 08:51:00 +00005846 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005847
Mike Stumpf5408fe2009-05-16 07:57:57 +00005848 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5849 // ObjCTypes types.
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005850 const llvm::Type *ClassTy =
5851 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5852 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5853 CGF.Builder.CreateStore(Target,
5854 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005855
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005856 return (LegacyDispatchedSelector(Sel))
John McCallef072fd2010-05-22 01:48:05 +00005857 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5858 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005859 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005860 true, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005861 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005862 ObjCSuper, ObjCTypes.SuperPtrCTy,
5863 true, CallArgs);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005864}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005865
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005866llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005867 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005868 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005869
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005870 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005871 llvm::Constant *Casted =
5872 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5873 ObjCTypes.SelectorPtrTy);
5874 Entry =
5875 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5876 llvm::GlobalValue::InternalLinkage,
5877 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005878 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005879 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005880 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005881
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005882 if (lval)
5883 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005884 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005885}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005886/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005887/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005888///
5889void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005890 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005891 llvm::Value *dst,
5892 llvm::Value *ivarOffset) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005893 const llvm::Type * SrcTy = src->getType();
5894 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005895 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005896 assert(Size <= 8 && "does not support size > 8");
5897 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5898 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005899 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5900 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005901 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5902 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005903 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5904 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005905 return;
5906}
5907
5908/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5909/// objc_assign_strongCast (id src, id *dst)
5910///
5911void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005912 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005913 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005914 const llvm::Type * SrcTy = src->getType();
5915 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005916 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005917 assert(Size <= 8 && "does not support size > 8");
5918 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005919 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005920 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5921 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005922 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5923 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005924 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005925 src, dst, "weakassign");
5926 return;
5927}
5928
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005929void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005930 CodeGen::CodeGenFunction &CGF,
5931 llvm::Value *DestPtr,
5932 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005933 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005934 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5935 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005936 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005937 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005938 return;
5939}
5940
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005941/// EmitObjCWeakRead - Code gen for loading value of a __weak
5942/// object: objc_read_weak (id *src)
5943///
5944llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005945 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005946 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00005947 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005948 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5949 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005950 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005951 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005952 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005953 return read_weak;
5954}
5955
5956/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5957/// objc_assign_weak (id src, id *dst)
5958///
5959void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005960 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005961 const llvm::Type * SrcTy = src->getType();
5962 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005963 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005964 assert(Size <= 8 && "does not support size > 8");
5965 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5966 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005967 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5968 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005969 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5970 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005971 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005972 src, dst, "weakassign");
5973 return;
5974}
5975
5976/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5977/// objc_assign_global (id src, id *dst)
5978///
5979void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005980 llvm::Value *src, llvm::Value *dst,
5981 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005982 const llvm::Type * SrcTy = src->getType();
5983 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005984 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005985 assert(Size <= 8 && "does not support size > 8");
5986 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5987 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005988 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5989 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005990 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5991 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005992 if (!threadlocal)
5993 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
5994 src, dst, "globalassign");
5995 else
5996 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
5997 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005998 return;
5999}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006000
John McCall740e8072010-07-21 00:41:47 +00006001namespace {
John McCall1f0fca52010-07-21 07:22:38 +00006002 struct CallSyncExit : EHScopeStack::Cleanup {
John McCall740e8072010-07-21 00:41:47 +00006003 llvm::Value *SyncExitFn;
6004 llvm::Value *SyncArg;
6005 CallSyncExit(llvm::Value *SyncExitFn, llvm::Value *SyncArg)
6006 : SyncExitFn(SyncExitFn), SyncArg(SyncArg) {}
6007
6008 void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
6009 CGF.Builder.CreateCall(SyncExitFn, SyncArg)->setDoesNotThrow();
6010 }
6011 };
6012}
6013
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006014void
John McCallf1549f62010-07-06 01:34:17 +00006015CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6016 const ObjCAtSynchronizedStmt &S) {
6017 // Evaluate the lock operand. This should dominate the cleanup.
6018 llvm::Value *SyncArg = CGF.EmitScalarExpr(S.getSynchExpr());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006019
John McCallf1549f62010-07-06 01:34:17 +00006020 // Acquire the lock.
6021 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
6022 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
6023 ->setDoesNotThrow();
6024
6025 // Register an all-paths cleanup to release the lock.
John McCall1f0fca52010-07-21 07:22:38 +00006026 CGF.EHStack.pushCleanup<CallSyncExit>(NormalAndEHCleanup,
6027 ObjCTypes.getSyncExitFn(),
6028 SyncArg);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006029
John McCallf1549f62010-07-06 01:34:17 +00006030 // Emit the body of the statement.
6031 CGF.EmitStmt(S.getSynchBody());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006032
John McCallf1549f62010-07-06 01:34:17 +00006033 // Pop the lock-release cleanup.
6034 CGF.PopCleanupBlock();
6035}
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006036
John McCallf1549f62010-07-06 01:34:17 +00006037namespace {
6038 struct CatchHandler {
6039 const VarDecl *Variable;
6040 const Stmt *Body;
6041 llvm::BasicBlock *Block;
6042 llvm::Value *TypeInfo;
6043 };
John McCall8e3f8612010-07-13 22:12:14 +00006044
John McCall1f0fca52010-07-21 07:22:38 +00006045 struct CallObjCEndCatch : EHScopeStack::Cleanup {
John McCall8e3f8612010-07-13 22:12:14 +00006046 CallObjCEndCatch(bool MightThrow, llvm::Value *Fn) :
6047 MightThrow(MightThrow), Fn(Fn) {}
6048 bool MightThrow;
6049 llvm::Value *Fn;
6050
6051 void Emit(CodeGenFunction &CGF, bool IsForEH) {
6052 if (!MightThrow) {
6053 CGF.Builder.CreateCall(Fn)->setDoesNotThrow();
6054 return;
6055 }
6056
6057 CGF.EmitCallOrInvoke(Fn, 0, 0);
6058 }
6059 };
John McCallf1549f62010-07-06 01:34:17 +00006060}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006061
John McCall5a180392010-07-24 00:37:23 +00006062llvm::Constant *
6063CGObjCNonFragileABIMac::GetEHType(QualType T) {
6064 // There's a particular fixed type info for 'id'.
6065 if (T->isObjCIdType() ||
6066 T->isObjCQualifiedIdType()) {
6067 llvm::Constant *IDEHType =
6068 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6069 if (!IDEHType)
6070 IDEHType =
6071 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6072 false,
6073 llvm::GlobalValue::ExternalLinkage,
6074 0, "OBJC_EHTYPE_id");
6075 return IDEHType;
6076 }
6077
6078 // All other types should be Objective-C interface pointer types.
6079 const ObjCObjectPointerType *PT =
6080 T->getAs<ObjCObjectPointerType>();
6081 assert(PT && "Invalid @catch type.");
6082 const ObjCInterfaceType *IT = PT->getInterfaceType();
6083 assert(IT && "Invalid @catch type.");
6084 return GetInterfaceEHType(IT->getDecl(), false);
6085}
6086
John McCallf1549f62010-07-06 01:34:17 +00006087void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6088 const ObjCAtTryStmt &S) {
6089 // Jump destination for falling out of catch bodies.
6090 CodeGenFunction::JumpDest Cont;
6091 if (S.getNumCatchStmts())
6092 Cont = CGF.getJumpDestInCurrentScope("eh.cont");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006093
John McCallf1549f62010-07-06 01:34:17 +00006094 CodeGenFunction::FinallyInfo FinallyInfo;
6095 if (const ObjCAtFinallyStmt *Finally = S.getFinallyStmt())
6096 FinallyInfo = CGF.EnterFinallyBlock(Finally->getFinallyBody(),
6097 ObjCTypes.getObjCBeginCatchFn(),
6098 ObjCTypes.getObjCEndCatchFn(),
6099 ObjCTypes.getExceptionRethrowFn());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006100
John McCallf1549f62010-07-06 01:34:17 +00006101 llvm::SmallVector<CatchHandler, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006102
John McCallf1549f62010-07-06 01:34:17 +00006103 // Enter the catch, if there is one.
6104 if (S.getNumCatchStmts()) {
6105 for (unsigned I = 0, N = S.getNumCatchStmts(); I != N; ++I) {
6106 const ObjCAtCatchStmt *CatchStmt = S.getCatchStmt(I);
Douglas Gregorc00d8e12010-04-26 16:46:50 +00006107 const VarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006108
John McCallf1549f62010-07-06 01:34:17 +00006109 Handlers.push_back(CatchHandler());
6110 CatchHandler &Handler = Handlers.back();
6111 Handler.Variable = CatchDecl;
6112 Handler.Body = CatchStmt->getCatchBody();
6113 Handler.Block = CGF.createBasicBlock("catch");
6114
6115 // @catch(...) always matches.
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00006116 if (!CatchDecl) {
John McCallf1549f62010-07-06 01:34:17 +00006117 Handler.TypeInfo = 0; // catch-all
6118 // Don't consider any other catches.
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00006119 break;
6120 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006121
John McCall5a180392010-07-24 00:37:23 +00006122 Handler.TypeInfo = GetEHType(CatchDecl->getType());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006123 }
John McCallf1549f62010-07-06 01:34:17 +00006124
6125 EHCatchScope *Catch = CGF.EHStack.pushCatch(Handlers.size());
6126 for (unsigned I = 0, E = Handlers.size(); I != E; ++I)
6127 Catch->setHandler(I, Handlers[I].TypeInfo, Handlers[I].Block);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006128 }
John McCallf1549f62010-07-06 01:34:17 +00006129
6130 // Emit the try body.
6131 CGF.EmitStmt(S.getTryBody());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006132
John McCallf1549f62010-07-06 01:34:17 +00006133 // Leave the try.
6134 if (S.getNumCatchStmts())
6135 CGF.EHStack.popCatch();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006136
John McCallf1549f62010-07-06 01:34:17 +00006137 // Remember where we were.
6138 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006139
John McCallf1549f62010-07-06 01:34:17 +00006140 // Emit the handlers.
6141 for (unsigned I = 0, E = Handlers.size(); I != E; ++I) {
6142 CatchHandler &Handler = Handlers[I];
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006143
John McCallf1549f62010-07-06 01:34:17 +00006144 CGF.EmitBlock(Handler.Block);
6145 llvm::Value *RawExn = CGF.Builder.CreateLoad(CGF.getExceptionSlot());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006146
John McCallf1549f62010-07-06 01:34:17 +00006147 // Enter the catch.
6148 llvm::CallInst *Exn =
6149 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), RawExn,
6150 "exn.adjusted");
6151 Exn->setDoesNotThrow();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006152
John McCallf1549f62010-07-06 01:34:17 +00006153 // Add a cleanup to leave the catch.
John McCall8e3f8612010-07-13 22:12:14 +00006154 bool EndCatchMightThrow = (Handler.Variable == 0);
John McCall1f0fca52010-07-21 07:22:38 +00006155 CGF.EHStack.pushCleanup<CallObjCEndCatch>(NormalAndEHCleanup,
6156 EndCatchMightThrow,
6157 ObjCTypes.getObjCEndCatchFn());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006158
John McCallf1549f62010-07-06 01:34:17 +00006159 // Bind the catch parameter if it exists.
6160 if (const VarDecl *CatchParam = Handler.Variable) {
6161 const llvm::Type *CatchType = CGF.ConvertType(CatchParam->getType());
6162 llvm::Value *CastExn = CGF.Builder.CreateBitCast(Exn, CatchType);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006163
John McCallb6bbcc92010-10-15 04:57:14 +00006164 CGF.EmitAutoVarDecl(*CatchParam);
John McCallf1549f62010-07-06 01:34:17 +00006165 CGF.Builder.CreateStore(CastExn, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006166 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006167
John McCallf1549f62010-07-06 01:34:17 +00006168 CGF.ObjCEHValueStack.push_back(Exn);
6169 CGF.EmitStmt(Handler.Body);
6170 CGF.ObjCEHValueStack.pop_back();
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006171
John McCallf1549f62010-07-06 01:34:17 +00006172 // Leave the earlier cleanup.
6173 CGF.PopCleanupBlock();
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006174
John McCallf1549f62010-07-06 01:34:17 +00006175 CGF.EmitBranchThroughCleanup(Cont);
6176 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006177
John McCallf1549f62010-07-06 01:34:17 +00006178 // Go back to the try-statement fallthrough.
6179 CGF.Builder.restoreIP(SavedIP);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006180
John McCallf1549f62010-07-06 01:34:17 +00006181 // Pop out of the normal cleanup on the finally.
6182 if (S.getFinallyStmt())
6183 CGF.ExitFinallyBlock(FinallyInfo);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006184
John McCallff8e1152010-07-23 21:56:41 +00006185 if (Cont.isValid())
6186 CGF.EmitBlock(Cont.getBlock());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006187}
6188
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006189/// EmitThrowStmt - Generate code for a throw statement.
6190void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6191 const ObjCAtThrowStmt &S) {
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006192 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall7ec404c2010-10-16 08:21:07 +00006193 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
John McCallfd186ac2010-10-16 16:34:08 +00006194 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy,
6195 "tmp");
John McCall7ec404c2010-10-16 08:21:07 +00006196 llvm::Value *Args[] = { Exception };
6197 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(),
6198 Args, Args+1)
6199 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006200 } else {
John McCall7ec404c2010-10-16 08:21:07 +00006201 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn(), 0, 0)
6202 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006203 }
6204
John McCall7ec404c2010-10-16 08:21:07 +00006205 CGF.Builder.CreateUnreachable();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006206 CGF.Builder.ClearInsertionPoint();
6207}
Daniel Dunbare588b992009-03-01 04:46:24 +00006208
John McCall5a180392010-07-24 00:37:23 +00006209llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006210CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006211 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00006212 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00006213
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006214 // If we don't need a definition, return the entry if found or check
6215 // if we use an external reference.
6216 if (!ForDefinition) {
6217 if (Entry)
6218 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00006219
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006220 // If this type (or a super class) has the __objc_exception__
6221 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00006222 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006223 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006224 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006225 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006226 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006227 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006228 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006229 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006230
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006231 // Otherwise we need to either make a new entry or fill in the
6232 // initializer.
6233 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006234 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00006235 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006236 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00006237 CGM.getModule().getGlobalVariable(VTableName);
6238 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006239 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6240 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00006241 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00006242 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00006243
Chris Lattner77b89b82010-06-27 07:15:29 +00006244 llvm::Value *VTableIdx =
6245 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00006246
6247 std::vector<llvm::Constant*> Values(3);
Owen Anderson3c4972d2009-07-29 18:54:39 +00006248 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbare588b992009-03-01 04:46:24 +00006249 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006250 Values[2] = GetClassGlobal(ClassName);
Owen Andersona1cf15f2009-07-14 23:10:40 +00006251 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00006252 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00006253
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006254 if (Entry) {
6255 Entry->setInitializer(Init);
6256 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00006257 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006258 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006259 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006260 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006261 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006262 }
6263
John McCall1fb0caa2010-10-22 21:05:15 +00006264 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006265 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00006266 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6267 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006268
6269 if (ForDefinition) {
6270 Entry->setSection("__DATA,__objc_const");
6271 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6272 } else {
6273 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6274 }
Daniel Dunbare588b992009-03-01 04:46:24 +00006275
6276 return Entry;
6277}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006278
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00006279/* *** */
6280
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00006281CodeGen::CGObjCRuntime *
6282CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006283 return new CGObjCMac(CGM);
6284}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00006285
6286CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00006287CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00006288 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00006289}