blob: 6d9f4c03e5e586b837f110f57e7511e72e6db928 [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
16#include "CodeGenModule.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000017#include "CodeGenFunction.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000019#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000020#include "clang/AST/DeclObjC.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Chris Lattner16f00492009-04-26 01:32:48 +000022#include "clang/AST/StmtObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000023#include "clang/Basic/LangOptions.h"
24
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +000025#include "llvm/Intrinsics.h"
Owen Anderson69243822009-07-13 04:10:07 +000026#include "llvm/LLVMContext.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000027#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000028#include "llvm/ADT/DenseSet.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000029#include "llvm/Target/TargetData.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000030#include <sstream>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000031
32using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000033using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000034
Daniel Dunbar97776872009-04-22 07:32:20 +000035// Common CGObjCRuntime functions, these don't belong here, but they
36// don't belong in CGObjCRuntime either so we will live with it for
37// now.
38
Daniel Dunbar532d4da2009-05-03 13:15:50 +000039/// FindIvarInterface - Find the interface containing the ivar.
Daniel Dunbara2435782009-04-22 12:00:04 +000040///
Daniel Dunbar532d4da2009-05-03 13:15:50 +000041/// FIXME: We shouldn't need to do this, the containing context should
42/// be fixed.
43static const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context,
44 const ObjCInterfaceDecl *OID,
45 const ObjCIvarDecl *OIVD,
46 unsigned &Index) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +000047 // FIXME: The index here is closely tied to how
48 // ASTContext::getObjCLayout is implemented. This should be fixed to
49 // get the information from the layout directly.
50 Index = 0;
Fariborz Jahanian98200742009-05-12 18:14:29 +000051 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +000052 Context.ShallowCollectObjCIvars(OID, Ivars);
Fariborz Jahanian98200742009-05-12 18:14:29 +000053 for (unsigned k = 0, e = Ivars.size(); k != e; ++k) {
54 if (OIVD == Ivars[k])
55 return OID;
56 ++Index;
Daniel Dunbara80a0f62009-04-22 17:43:55 +000057 }
Fariborz Jahanian98200742009-05-12 18:14:29 +000058
Daniel Dunbar532d4da2009-05-03 13:15:50 +000059 // Otherwise check in the super class.
Daniel Dunbara81419d2009-05-05 00:36:57 +000060 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Daniel Dunbar532d4da2009-05-03 13:15:50 +000061 return FindIvarInterface(Context, Super, OIVD, Index);
62
63 return 0;
Daniel Dunbara2435782009-04-22 12:00:04 +000064}
65
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000066static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM,
67 const ObjCInterfaceDecl *OID,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000068 const ObjCImplementationDecl *ID,
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000069 const ObjCIvarDecl *Ivar) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +000070 unsigned Index;
71 const ObjCInterfaceDecl *Container =
72 FindIvarInterface(CGM.getContext(), OID, Ivar, Index);
73 assert(Container && "Unable to find ivar container");
74
75 // If we know have an implementation (and the ivar is in it) then
76 // look up in the implementation layout.
77 const ASTRecordLayout *RL;
78 if (ID && ID->getClassInterface() == Container)
79 RL = &CGM.getContext().getASTObjCImplementationLayout(ID);
80 else
81 RL = &CGM.getContext().getASTObjCInterfaceLayout(Container);
82 return RL->getFieldOffset(Index);
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000083}
84
85uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
86 const ObjCInterfaceDecl *OID,
87 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000088 return LookupFieldBitOffset(CGM, OID, 0, Ivar) / 8;
89}
90
91uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
92 const ObjCImplementationDecl *OID,
93 const ObjCIvarDecl *Ivar) {
94 return LookupFieldBitOffset(CGM, OID->getClassInterface(), OID, Ivar) / 8;
Daniel Dunbar97776872009-04-22 07:32:20 +000095}
96
97LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
98 const ObjCInterfaceDecl *OID,
99 llvm::Value *BaseValue,
100 const ObjCIvarDecl *Ivar,
101 unsigned CVRQualifiers,
102 llvm::Value *Offset) {
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000103 // Compute (type*) ( (char *) BaseValue + Offset)
Owen Andersona1cf15f2009-07-14 23:10:40 +0000104 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
105 llvm::Type *I8Ptr = VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty);
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 Andersona1cf15f2009-07-14 23:10:40 +0000110 V = CGF.Builder.CreateBitCast(V, VMContext.getPointerTypeUnqual(LTy));
Daniel Dunbar97776872009-04-22 07:32:20 +0000111
112 if (Ivar->isBitField()) {
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +0000113 // We need to compute the bit offset for the bit-field, the offset
114 // is to the byte. Note, there is a subtle invariant here: we can
115 // only call this routine on non-sythesized ivars but we may be
116 // called for synthesized ivars. However, a synthesized ivar can
117 // never be a bit-field so this is safe.
118 uint64_t BitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar) % 8;
119
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000120 uint64_t BitFieldSize =
121 Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue();
122 return LValue::MakeBitfield(V, BitOffset, BitFieldSize,
Daniel Dunbare38df862009-05-03 07:52:00 +0000123 IvarTy->isSignedIntegerType(),
124 IvarTy.getCVRQualifiers()|CVRQualifiers);
Daniel Dunbar97776872009-04-22 07:32:20 +0000125 }
126
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000127 LValue LV = LValue::MakeAddr(V, IvarTy.getCVRQualifiers()|CVRQualifiers,
128 CGF.CGM.getContext().getObjCGCAttrKind(IvarTy));
Daniel Dunbar97776872009-04-22 07:32:20 +0000129 LValue::SetObjCIvar(LV, true);
130 return LV;
131}
132
133///
134
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000135namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000136
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000137 typedef std::vector<llvm::Constant*> ConstantVector;
138
Mike Stumpf5408fe2009-05-16 07:57:57 +0000139 // FIXME: We should find a nicer way to make the labels for metadata, string
140 // concatenation is lame.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000141
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000142class ObjCCommonTypesHelper {
Owen Andersona1cf15f2009-07-14 23:10:40 +0000143protected:
144 llvm::LLVMContext &VMContext;
145
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000146private:
147 llvm::Constant *getMessageSendFn() const {
148 // id objc_msgSend (id, SEL, ...)
149 std::vector<const llvm::Type*> Params;
150 Params.push_back(ObjectPtrTy);
151 Params.push_back(SelectorPtrTy);
152 return
Owen Andersona1cf15f2009-07-14 23:10:40 +0000153 CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000154 Params, true),
155 "objc_msgSend");
156 }
157
158 llvm::Constant *getMessageSendStretFn() const {
159 // id objc_msgSend_stret (id, SEL, ...)
160 std::vector<const llvm::Type*> Params;
161 Params.push_back(ObjectPtrTy);
162 Params.push_back(SelectorPtrTy);
163 return
Owen Andersona1cf15f2009-07-14 23:10:40 +0000164 CGM.CreateRuntimeFunction(VMContext.getFunctionType(llvm::Type::VoidTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000165 Params, true),
166 "objc_msgSend_stret");
167
168 }
169
170 llvm::Constant *getMessageSendFpretFn() const {
171 // FIXME: This should be long double on x86_64?
172 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
173 std::vector<const llvm::Type*> Params;
174 Params.push_back(ObjectPtrTy);
175 Params.push_back(SelectorPtrTy);
176 return
Owen Andersona1cf15f2009-07-14 23:10:40 +0000177 CGM.CreateRuntimeFunction(VMContext.getFunctionType(llvm::Type::DoubleTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000178 Params,
179 true),
180 "objc_msgSend_fpret");
181
182 }
183
184 llvm::Constant *getMessageSendSuperFn() const {
185 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
186 const char *SuperName = "objc_msgSendSuper";
187 std::vector<const llvm::Type*> Params;
188 Params.push_back(SuperPtrTy);
189 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000190 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000191 Params, true),
192 SuperName);
193 }
194
195 llvm::Constant *getMessageSendSuperFn2() const {
196 // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
197 const char *SuperName = "objc_msgSendSuper2";
198 std::vector<const llvm::Type*> Params;
199 Params.push_back(SuperPtrTy);
200 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000201 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000202 Params, true),
203 SuperName);
204 }
205
206 llvm::Constant *getMessageSendSuperStretFn() const {
207 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
208 // SEL op, ...)
209 std::vector<const llvm::Type*> Params;
210 Params.push_back(Int8PtrTy);
211 Params.push_back(SuperPtrTy);
212 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000213 return CGM.CreateRuntimeFunction(
214 VMContext.getFunctionType(llvm::Type::VoidTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000215 Params, true),
216 "objc_msgSendSuper_stret");
217 }
218
219 llvm::Constant *getMessageSendSuperStretFn2() const {
220 // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
221 // SEL op, ...)
222 std::vector<const llvm::Type*> Params;
223 Params.push_back(Int8PtrTy);
224 Params.push_back(SuperPtrTy);
225 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000226 return CGM.CreateRuntimeFunction(
227 VMContext.getFunctionType(llvm::Type::VoidTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000228 Params, true),
229 "objc_msgSendSuper2_stret");
230 }
231
232 llvm::Constant *getMessageSendSuperFpretFn() const {
233 // There is no objc_msgSendSuper_fpret? How can that work?
234 return getMessageSendSuperFn();
235 }
236
237 llvm::Constant *getMessageSendSuperFpretFn2() const {
238 // There is no objc_msgSendSuper_fpret? How can that work?
239 return getMessageSendSuperFn2();
240 }
241
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000242protected:
243 CodeGen::CodeGenModule &CGM;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000244
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000245public:
Fariborz Jahanian0a855d02009-03-23 19:10:40 +0000246 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000247 const llvm::Type *Int8PtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000248
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000249 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
250 const llvm::Type *ObjectPtrTy;
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000251
252 /// PtrObjectPtrTy - LLVM type for id *
253 const llvm::Type *PtrObjectPtrTy;
254
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000255 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000256 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000257 /// ProtocolPtrTy - LLVM type for external protocol handles
258 /// (typeof(Protocol))
259 const llvm::Type *ExternalProtocolPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000260
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000261 // SuperCTy - clang type for struct objc_super.
262 QualType SuperCTy;
263 // SuperPtrCTy - clang type for struct objc_super *.
264 QualType SuperPtrCTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000265
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000266 /// SuperTy - LLVM type for struct objc_super.
267 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000268 /// SuperPtrTy - LLVM type for struct objc_super *.
269 const llvm::Type *SuperPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000270
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000271 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
272 /// in GCC parlance).
273 const llvm::StructType *PropertyTy;
274
275 /// PropertyListTy - LLVM type for struct objc_property_list
276 /// (_prop_list_t in GCC parlance).
277 const llvm::StructType *PropertyListTy;
278 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
279 const llvm::Type *PropertyListPtrTy;
280
281 // MethodTy - LLVM type for struct objc_method.
282 const llvm::StructType *MethodTy;
283
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000284 /// CacheTy - LLVM type for struct objc_cache.
285 const llvm::Type *CacheTy;
286 /// CachePtrTy - LLVM type for struct objc_cache *.
287 const llvm::Type *CachePtrTy;
288
Chris Lattner72db6c32009-04-22 02:44:54 +0000289 llvm::Constant *getGetPropertyFn() {
290 CodeGen::CodeGenTypes &Types = CGM.getTypes();
291 ASTContext &Ctx = CGM.getContext();
292 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
293 llvm::SmallVector<QualType,16> Params;
294 QualType IdType = Ctx.getObjCIdType();
295 QualType SelType = Ctx.getObjCSelType();
296 Params.push_back(IdType);
297 Params.push_back(SelType);
298 Params.push_back(Ctx.LongTy);
299 Params.push_back(Ctx.BoolTy);
300 const llvm::FunctionType *FTy =
301 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
302 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
303 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000304
Chris Lattner72db6c32009-04-22 02:44:54 +0000305 llvm::Constant *getSetPropertyFn() {
306 CodeGen::CodeGenTypes &Types = CGM.getTypes();
307 ASTContext &Ctx = CGM.getContext();
308 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
309 llvm::SmallVector<QualType,16> Params;
310 QualType IdType = Ctx.getObjCIdType();
311 QualType SelType = Ctx.getObjCSelType();
312 Params.push_back(IdType);
313 Params.push_back(SelType);
314 Params.push_back(Ctx.LongTy);
315 Params.push_back(IdType);
316 Params.push_back(Ctx.BoolTy);
317 Params.push_back(Ctx.BoolTy);
318 const llvm::FunctionType *FTy =
319 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
320 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
321 }
322
323 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000324 CodeGen::CodeGenTypes &Types = CGM.getTypes();
325 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000326 // void objc_enumerationMutation (id)
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000327 llvm::SmallVector<QualType,16> Params;
Daniel Dunbar309a4362009-07-24 07:40:24 +0000328 Params.push_back(Ctx.getObjCIdType());
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000329 const llvm::FunctionType *FTy =
330 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000331 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
332 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000333
334 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000335 llvm::Constant *getGcReadWeakFn() {
336 // id objc_read_weak (id *)
337 std::vector<const llvm::Type*> Args;
338 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000339 llvm::FunctionType *FTy =
340 VMContext.getFunctionType(ObjectPtrTy, Args, false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000341 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
342 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000343
344 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000345 llvm::Constant *getGcAssignWeakFn() {
346 // id objc_assign_weak (id, id *)
347 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
348 Args.push_back(ObjectPtrTy->getPointerTo());
349 llvm::FunctionType *FTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +0000350 VMContext.getFunctionType(ObjectPtrTy, Args, false);
Chris Lattner96508e12009-04-17 22:12:36 +0000351 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
352 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000353
354 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000355 llvm::Constant *getGcAssignGlobalFn() {
356 // id objc_assign_global(id, id *)
357 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
358 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000359 llvm::FunctionType *FTy =
360 VMContext.getFunctionType(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000361 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
362 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000363
364 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000365 llvm::Constant *getGcAssignIvarFn() {
366 // id objc_assign_ivar(id, id *)
367 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
368 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000369 llvm::FunctionType *FTy =
370 VMContext.getFunctionType(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000371 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
372 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000373
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000374 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
375 llvm::Constant *GcMemmoveCollectableFn() {
376 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
377 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
378 Args.push_back(Int8PtrTy);
379 Args.push_back(LongTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000380 llvm::FunctionType *FTy = VMContext.getFunctionType(Int8PtrTy, Args, false);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000381 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
382 }
383
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000384 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000385 llvm::Constant *getGcAssignStrongCastFn() {
386 // id objc_assign_global(id, id *)
387 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
388 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000389 llvm::FunctionType *FTy =
390 VMContext.getFunctionType(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000391 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
392 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000393
394 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000395 llvm::Constant *getExceptionThrowFn() {
396 // void objc_exception_throw(id)
397 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
398 llvm::FunctionType *FTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +0000399 VMContext.getFunctionType(llvm::Type::VoidTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000400 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
401 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000402
Daniel Dunbar1c566672009-02-24 01:43:46 +0000403 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000404 llvm::Constant *getSyncEnterFn() {
405 // void objc_sync_enter (id)
406 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
407 llvm::FunctionType *FTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +0000408 VMContext.getFunctionType(llvm::Type::VoidTy, Args, false);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000409 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
410 }
Daniel Dunbar1c566672009-02-24 01:43:46 +0000411
412 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000413 llvm::Constant *getSyncExitFn() {
414 // void objc_sync_exit (id)
415 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
416 llvm::FunctionType *FTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +0000417 VMContext.getFunctionType(llvm::Type::VoidTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000418 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
419 }
Daniel Dunbar1c566672009-02-24 01:43:46 +0000420
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000421 llvm::Constant *getSendFn(bool IsSuper) const {
422 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
423 }
424
425 llvm::Constant *getSendFn2(bool IsSuper) const {
426 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
427 }
428
429 llvm::Constant *getSendStretFn(bool IsSuper) const {
430 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
431 }
432
433 llvm::Constant *getSendStretFn2(bool IsSuper) const {
434 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
435 }
436
437 llvm::Constant *getSendFpretFn(bool IsSuper) const {
438 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
439 }
440
441 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
442 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
443 }
444
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000445 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
446 ~ObjCCommonTypesHelper(){}
447};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000448
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000449/// ObjCTypesHelper - Helper class that encapsulates lazy
450/// construction of varies types used during ObjC generation.
451class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000452public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000453 /// SymtabTy - LLVM type for struct objc_symtab.
454 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000455 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
456 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000457 /// ModuleTy - LLVM type for struct objc_module.
458 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000459
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000460 /// ProtocolTy - LLVM type for struct objc_protocol.
461 const llvm::StructType *ProtocolTy;
462 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
463 const llvm::Type *ProtocolPtrTy;
464 /// ProtocolExtensionTy - LLVM type for struct
465 /// objc_protocol_extension.
466 const llvm::StructType *ProtocolExtensionTy;
467 /// ProtocolExtensionTy - LLVM type for struct
468 /// objc_protocol_extension *.
469 const llvm::Type *ProtocolExtensionPtrTy;
470 /// MethodDescriptionTy - LLVM type for struct
471 /// objc_method_description.
472 const llvm::StructType *MethodDescriptionTy;
473 /// MethodDescriptionListTy - LLVM type for struct
474 /// objc_method_description_list.
475 const llvm::StructType *MethodDescriptionListTy;
476 /// MethodDescriptionListPtrTy - LLVM type for struct
477 /// objc_method_description_list *.
478 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000479 /// ProtocolListTy - LLVM type for struct objc_property_list.
480 const llvm::Type *ProtocolListTy;
481 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
482 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000483 /// CategoryTy - LLVM type for struct objc_category.
484 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000485 /// ClassTy - LLVM type for struct objc_class.
486 const llvm::StructType *ClassTy;
487 /// ClassPtrTy - LLVM type for struct objc_class *.
488 const llvm::Type *ClassPtrTy;
489 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
490 const llvm::StructType *ClassExtensionTy;
491 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
492 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000493 // IvarTy - LLVM type for struct objc_ivar.
494 const llvm::StructType *IvarTy;
495 /// IvarListTy - LLVM type for struct objc_ivar_list.
496 const llvm::Type *IvarListTy;
497 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
498 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000499 /// MethodListTy - LLVM type for struct objc_method_list.
500 const llvm::Type *MethodListTy;
501 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
502 const llvm::Type *MethodListPtrTy;
Anders Carlsson124526b2008-09-09 10:10:21 +0000503
504 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
505 const llvm::Type *ExceptionDataTy;
506
Anders Carlsson124526b2008-09-09 10:10:21 +0000507 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000508 llvm::Constant *getExceptionTryEnterFn() {
509 std::vector<const llvm::Type*> Params;
Owen Andersona1cf15f2009-07-14 23:10:40 +0000510 Params.push_back(VMContext.getPointerTypeUnqual(ExceptionDataTy));
511 return CGM.CreateRuntimeFunction(
512 VMContext.getFunctionType(llvm::Type::VoidTy,
Chris Lattner34b02a12009-04-22 02:26:14 +0000513 Params, false),
514 "objc_exception_try_enter");
515 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000516
517 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000518 llvm::Constant *getExceptionTryExitFn() {
519 std::vector<const llvm::Type*> Params;
Owen Andersona1cf15f2009-07-14 23:10:40 +0000520 Params.push_back(VMContext.getPointerTypeUnqual(ExceptionDataTy));
521 return CGM.CreateRuntimeFunction(
522 VMContext.getFunctionType(llvm::Type::VoidTy,
Chris Lattner34b02a12009-04-22 02:26:14 +0000523 Params, false),
524 "objc_exception_try_exit");
525 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000526
527 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000528 llvm::Constant *getExceptionExtractFn() {
529 std::vector<const llvm::Type*> Params;
Owen Andersona1cf15f2009-07-14 23:10:40 +0000530 Params.push_back(VMContext.getPointerTypeUnqual(ExceptionDataTy));
531 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Chris Lattner34b02a12009-04-22 02:26:14 +0000532 Params, false),
533 "objc_exception_extract");
534
535 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000536
537 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000538 llvm::Constant *getExceptionMatchFn() {
539 std::vector<const llvm::Type*> Params;
540 Params.push_back(ClassPtrTy);
541 Params.push_back(ObjectPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000542 return CGM.CreateRuntimeFunction(
543 VMContext.getFunctionType(llvm::Type::Int32Ty,
Chris Lattner34b02a12009-04-22 02:26:14 +0000544 Params, false),
545 "objc_exception_match");
546
547 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000548
549 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000550 llvm::Constant *getSetJmpFn() {
551 std::vector<const llvm::Type*> Params;
Owen Andersona1cf15f2009-07-14 23:10:40 +0000552 Params.push_back(VMContext.getPointerTypeUnqual(llvm::Type::Int32Ty));
Chris Lattner34b02a12009-04-22 02:26:14 +0000553 return
Owen Andersona1cf15f2009-07-14 23:10:40 +0000554 CGM.CreateRuntimeFunction(VMContext.getFunctionType(llvm::Type::Int32Ty,
Chris Lattner34b02a12009-04-22 02:26:14 +0000555 Params, false),
556 "_setjmp");
557
558 }
Chris Lattner10cac6f2008-11-15 21:26:17 +0000559
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000560public:
561 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000562 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000563};
564
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000565/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000566/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000567class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000568public:
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000569
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000570 // MethodListnfABITy - LLVM for struct _method_list_t
571 const llvm::StructType *MethodListnfABITy;
572
573 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
574 const llvm::Type *MethodListnfABIPtrTy;
575
576 // ProtocolnfABITy = LLVM for struct _protocol_t
577 const llvm::StructType *ProtocolnfABITy;
578
Daniel Dunbar948e2582009-02-15 07:36:20 +0000579 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
580 const llvm::Type *ProtocolnfABIPtrTy;
581
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000582 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
583 const llvm::StructType *ProtocolListnfABITy;
584
585 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
586 const llvm::Type *ProtocolListnfABIPtrTy;
587
588 // ClassnfABITy - LLVM for struct _class_t
589 const llvm::StructType *ClassnfABITy;
590
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000591 // ClassnfABIPtrTy - LLVM for struct _class_t*
592 const llvm::Type *ClassnfABIPtrTy;
593
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000594 // IvarnfABITy - LLVM for struct _ivar_t
595 const llvm::StructType *IvarnfABITy;
596
597 // IvarListnfABITy - LLVM for struct _ivar_list_t
598 const llvm::StructType *IvarListnfABITy;
599
600 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
601 const llvm::Type *IvarListnfABIPtrTy;
602
603 // ClassRonfABITy - LLVM for struct _class_ro_t
604 const llvm::StructType *ClassRonfABITy;
605
606 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
607 const llvm::Type *ImpnfABITy;
608
609 // CategorynfABITy - LLVM for struct _category_t
610 const llvm::StructType *CategorynfABITy;
611
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000612 // New types for nonfragile abi messaging.
613
614 // MessageRefTy - LLVM for:
615 // struct _message_ref_t {
616 // IMP messenger;
617 // SEL name;
618 // };
619 const llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000620 // MessageRefCTy - clang type for struct _message_ref_t
621 QualType MessageRefCTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000622
623 // MessageRefPtrTy - LLVM for struct _message_ref_t*
624 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000625 // MessageRefCPtrTy - clang type for struct _message_ref_t*
626 QualType MessageRefCPtrTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000627
Fariborz Jahanianef163782009-02-05 01:13:09 +0000628 // MessengerTy - Type of the messenger (shown as IMP above)
629 const llvm::FunctionType *MessengerTy;
630
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000631 // SuperMessageRefTy - LLVM for:
632 // struct _super_message_ref_t {
633 // SUPER_IMP messenger;
634 // SEL name;
635 // };
636 const llvm::StructType *SuperMessageRefTy;
637
638 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
639 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000640
Chris Lattner1c02f862009-04-22 02:53:24 +0000641 llvm::Constant *getMessageSendFixupFn() {
642 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
643 std::vector<const llvm::Type*> Params;
644 Params.push_back(ObjectPtrTy);
645 Params.push_back(MessageRefPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000646 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000647 Params, true),
648 "objc_msgSend_fixup");
649 }
650
651 llvm::Constant *getMessageSendFpretFixupFn() {
652 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
653 std::vector<const llvm::Type*> Params;
654 Params.push_back(ObjectPtrTy);
655 Params.push_back(MessageRefPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000656 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000657 Params, true),
658 "objc_msgSend_fpret_fixup");
659 }
660
661 llvm::Constant *getMessageSendStretFixupFn() {
662 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
663 std::vector<const llvm::Type*> Params;
664 Params.push_back(ObjectPtrTy);
665 Params.push_back(MessageRefPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000666 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000667 Params, true),
668 "objc_msgSend_stret_fixup");
669 }
670
671 llvm::Constant *getMessageSendIdFixupFn() {
672 // id objc_msgSendId_fixup(id, struct message_ref_t*, ...)
673 std::vector<const llvm::Type*> Params;
674 Params.push_back(ObjectPtrTy);
675 Params.push_back(MessageRefPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000676 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000677 Params, true),
678 "objc_msgSendId_fixup");
679 }
680
681 llvm::Constant *getMessageSendIdStretFixupFn() {
682 // id objc_msgSendId_stret_fixup(id, struct message_ref_t*, ...)
683 std::vector<const llvm::Type*> Params;
684 Params.push_back(ObjectPtrTy);
685 Params.push_back(MessageRefPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000686 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000687 Params, true),
688 "objc_msgSendId_stret_fixup");
689 }
690 llvm::Constant *getMessageSendSuper2FixupFn() {
691 // id objc_msgSendSuper2_fixup (struct objc_super *,
692 // struct _super_message_ref_t*, ...)
693 std::vector<const llvm::Type*> Params;
694 Params.push_back(SuperPtrTy);
695 Params.push_back(SuperMessageRefPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000696 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000697 Params, true),
698 "objc_msgSendSuper2_fixup");
699 }
700
701 llvm::Constant *getMessageSendSuper2StretFixupFn() {
702 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
703 // struct _super_message_ref_t*, ...)
704 std::vector<const llvm::Type*> Params;
705 Params.push_back(SuperPtrTy);
706 Params.push_back(SuperMessageRefPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000707 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000708 Params, true),
709 "objc_msgSendSuper2_stret_fixup");
710 }
711
712
713
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000714 /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C
715 /// exception personality function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000716 llvm::Value *getEHPersonalityPtr() {
717 llvm::Constant *Personality =
Owen Andersona1cf15f2009-07-14 23:10:40 +0000718 CGM.CreateRuntimeFunction(VMContext.getFunctionType(llvm::Type::Int32Ty,
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000719 true),
720 "__objc_personality_v0");
Owen Andersona1cf15f2009-07-14 23:10:40 +0000721 return VMContext.getConstantExprBitCast(Personality, Int8PtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000722 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000723
Chris Lattner8a569112009-04-22 02:15:23 +0000724 llvm::Constant *getUnwindResumeOrRethrowFn() {
725 std::vector<const llvm::Type*> Params;
726 Params.push_back(Int8PtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000727 return CGM.CreateRuntimeFunction(
728 VMContext.getFunctionType(llvm::Type::VoidTy,
Chris Lattner8a569112009-04-22 02:15:23 +0000729 Params, false),
730 "_Unwind_Resume_or_Rethrow");
731 }
732
733 llvm::Constant *getObjCEndCatchFn() {
Owen Andersona1cf15f2009-07-14 23:10:40 +0000734 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(llvm::Type::VoidTy,
Chris Lattnerb59761b2009-07-01 04:13:52 +0000735 false),
Chris Lattner8a569112009-04-22 02:15:23 +0000736 "objc_end_catch");
737
738 }
739
740 llvm::Constant *getObjCBeginCatchFn() {
741 std::vector<const llvm::Type*> Params;
742 Params.push_back(Int8PtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000743 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(Int8PtrTy,
Chris Lattner8a569112009-04-22 02:15:23 +0000744 Params, false),
745 "objc_begin_catch");
746 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000747
748 const llvm::StructType *EHTypeTy;
749 const llvm::Type *EHTypePtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000750
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000751 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
752 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000753};
754
755class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000756public:
757 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000758 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000759 public:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000760 unsigned ivar_bytepos;
761 unsigned ivar_size;
762 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
763 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000764
765 // Allow sorting based on byte pos.
766 bool operator<(const GC_IVAR &b) const {
767 return ivar_bytepos < b.ivar_bytepos;
768 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000769 };
770
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000771 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000772 public:
773 unsigned skip;
774 unsigned scan;
775 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
776 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000777 };
778
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000779protected:
780 CodeGen::CodeGenModule &CGM;
Owen Anderson69243822009-07-13 04:10:07 +0000781 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000782 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000783 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000784
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000785 // gc ivar layout bitmap calculation helper caches.
786 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
787 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000788
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000789 /// LazySymbols - Symbols to generate a lazy reference for. See
790 /// DefinedSymbols and FinishModule().
791 std::set<IdentifierInfo*> LazySymbols;
792
793 /// DefinedSymbols - External symbols which are defined by this
794 /// module. The symbols in this list and LazySymbols are used to add
795 /// special linker symbols which ensure that Objective-C modules are
796 /// linked properly.
797 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000798
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000799 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000800 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000801
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000802 /// MethodVarNames - uniqued method variable names.
803 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000804
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000805 /// MethodVarTypes - uniqued method type signatures. We have to use
806 /// a StringMap here because have no other unique reference.
807 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000808
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000809 /// MethodDefinitions - map of methods which have been defined in
810 /// this translation unit.
811 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000812
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000813 /// PropertyNames - uniqued method variable names.
814 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000815
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000816 /// ClassReferences - uniqued class references.
817 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000818
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000819 /// SelectorReferences - uniqued selector references.
820 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000821
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000822 /// Protocols - Protocols for which an objc_protocol structure has
823 /// been emitted. Forward declarations are handled by creating an
824 /// empty structure whose initializer is filled in when/if defined.
825 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000826
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000827 /// DefinedProtocols - Protocols which have actually been
828 /// defined. We should not need this, see FIXME in GenerateProtocol.
829 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000830
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000831 /// DefinedClasses - List of defined classes.
832 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000833
834 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
835 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000836
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000837 /// DefinedCategories - List of defined categories.
838 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000839
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000840 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
841 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
842
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000843 /// GetNameForMethod - Return a name for the given method.
844 /// \param[out] NameOut - The return value.
845 void GetNameForMethod(const ObjCMethodDecl *OMD,
846 const ObjCContainerDecl *CD,
847 std::string &NameOut);
848
849 /// GetMethodVarName - Return a unique constant for the given
850 /// selector's name. The return value has type char *.
851 llvm::Constant *GetMethodVarName(Selector Sel);
852 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
853 llvm::Constant *GetMethodVarName(const std::string &Name);
854
855 /// GetMethodVarType - Return a unique constant for the given
856 /// selector's name. The return value has type char *.
857
858 // FIXME: This is a horrible name.
859 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000860 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000861
862 /// GetPropertyName - Return a unique constant for the given
863 /// name. The return value has type char *.
864 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
865
866 // FIXME: This can be dropped once string functions are unified.
867 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
868 const Decl *Container);
869
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000870 /// GetClassName - Return a unique constant for the given selector's
871 /// name. The return value has type char *.
872 llvm::Constant *GetClassName(IdentifierInfo *Ident);
873
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000874 /// BuildIvarLayout - Builds ivar layout bitmap for the class
875 /// implementation for the __strong or __weak case.
876 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000877 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
878 bool ForStrongLayout);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000879
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000880 void BuildAggrIvarRecordLayout(const RecordType *RT,
881 unsigned int BytePos, bool ForStrongLayout,
882 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000883 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000884 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000885 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +0000886 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000887 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000888 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000889
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000890 /// GetIvarLayoutName - Returns a unique constant for the given
891 /// ivar layout bitmap.
892 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
893 const ObjCCommonTypesHelper &ObjCTypes);
894
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000895 /// EmitPropertyList - Emit the given property list. The return
896 /// value has type PropertyListPtrTy.
897 llvm::Constant *EmitPropertyList(const std::string &Name,
898 const Decl *Container,
899 const ObjCContainerDecl *OCD,
900 const ObjCCommonTypesHelper &ObjCTypes);
901
Fariborz Jahanianda320092009-01-29 19:24:30 +0000902 /// GetProtocolRef - Return a reference to the internal protocol
903 /// description, creating an empty one if it has not been
904 /// defined. The return value has type ProtocolPtrTy.
905 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000906
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000907 /// CreateMetadataVar - Create a global variable with internal
908 /// linkage for use by the Objective-C runtime.
909 ///
910 /// This is a convenience wrapper which not only creates the
911 /// variable, but also sets the section and alignment and adds the
Chris Lattnerad64e022009-07-17 23:57:13 +0000912 /// global to the "llvm.used" list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000913 ///
914 /// \param Name - The variable name.
915 /// \param Init - The variable initializer; this is also used to
916 /// define the type of the variable.
917 /// \param Section - The section the variable should go into, or 0.
918 /// \param Align - The alignment for the variable, or 0.
919 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000920 /// "llvm.used".
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000921 llvm::GlobalVariable *CreateMetadataVar(const std::string &Name,
922 llvm::Constant *Init,
923 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000924 unsigned Align,
925 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000926
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000927 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
928 QualType ResultType,
929 llvm::Value *Sel,
930 llvm::Value *Arg0,
931 QualType Arg0Ty,
932 bool IsSuper,
933 const CallArgList &CallArgs,
934 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000935
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000936public:
Owen Anderson69243822009-07-13 04:10:07 +0000937 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
938 CGM(cgm), VMContext(cgm.getLLVMContext())
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000939 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000940
Steve Naroff33fdb732009-03-31 16:53:37 +0000941 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000942
943 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
944 const ObjCContainerDecl *CD=0);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000945
946 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
947
948 /// GetOrEmitProtocol - Get the protocol object for the given
949 /// declaration, emitting it if necessary. The return value has type
950 /// ProtocolPtrTy.
951 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
952
953 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
954 /// object for the given declaration, emitting it if needed. These
955 /// forward references will be filled in with empty bodies if no
956 /// definition is seen. The return value has type ProtocolPtrTy.
957 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000958};
959
960class CGObjCMac : public CGObjCCommonMac {
961private:
962 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000963 /// EmitImageInfo - Emit the image info marker used to encode some module
964 /// level information.
965 void EmitImageInfo();
966
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000967 /// EmitModuleInfo - Another marker encoding module level
968 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000969 void EmitModuleInfo();
970
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000971 /// EmitModuleSymols - Emit module symbols, the list of defined
972 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000973 llvm::Constant *EmitModuleSymbols();
974
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000975 /// FinishModule - Write out global data structures at the end of
976 /// processing a translation unit.
977 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000978
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000979 /// EmitClassExtension - Generate the class extension structure used
980 /// to store the weak ivar layout and properties. The return value
981 /// has type ClassExtensionPtrTy.
982 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
983
984 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
985 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000986 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000987 const ObjCInterfaceDecl *ID);
988
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000989 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000990 QualType ResultType,
991 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000992 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000993 QualType Arg0Ty,
994 bool IsSuper,
995 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000996
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000997 /// EmitIvarList - Emit the ivar list for the given
998 /// implementation. If ForClass is true the list of class ivars
999 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1000 /// interface ivars will be emitted. The return value has type
1001 /// IvarListPtrTy.
1002 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001003 bool ForClass);
1004
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001005 /// EmitMetaClass - Emit a forward reference to the class structure
1006 /// for the metaclass of the given interface. The return value has
1007 /// type ClassPtrTy.
1008 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1009
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001010 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001011 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001012 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1013 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001014 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001015
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001016 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001017
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001018 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001019
1020 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001021 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001022 llvm::Constant *EmitMethodList(const std::string &Name,
1023 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001024 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001025
1026 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001027 /// method declarations.
1028 /// - TypeName: The name for the type containing the methods.
1029 /// - IsProtocol: True iff these methods are for a protocol.
1030 /// - ClassMethds: True iff these are class methods.
1031 /// - Required: When true, only "required" methods are
1032 /// listed. Similarly, when false only "optional" methods are
1033 /// listed. For classes this should always be true.
1034 /// - begin, end: The method list to output.
1035 ///
1036 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001037 llvm::Constant *EmitMethodDescList(const std::string &Name,
1038 const char *Section,
1039 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001040
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001041 /// GetOrEmitProtocol - Get the protocol object for the given
1042 /// declaration, emitting it if necessary. The return value has type
1043 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001044 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001045
1046 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1047 /// object for the given declaration, emitting it if needed. These
1048 /// forward references will be filled in with empty bodies if no
1049 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001050 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001051
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001052 /// EmitProtocolExtension - Generate the protocol extension
1053 /// structure used to store optional instance and class methods, and
1054 /// protocol properties. The return value has type
1055 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001056 llvm::Constant *
1057 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1058 const ConstantVector &OptInstanceMethods,
1059 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001060
1061 /// EmitProtocolList - Generate the list of referenced
1062 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +00001063 llvm::Constant *EmitProtocolList(const std::string &Name,
1064 ObjCProtocolDecl::protocol_iterator begin,
1065 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001066
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001067 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1068 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001069 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001070
Fariborz Jahanianda320092009-01-29 19:24:30 +00001071 public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001072 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001073
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001074 virtual llvm::Function *ModuleInitFunction();
1075
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001076 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001077 QualType ResultType,
1078 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001079 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001080 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001081 const CallArgList &CallArgs,
1082 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001083
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001084 virtual CodeGen::RValue
1085 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001086 QualType ResultType,
1087 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001088 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001089 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001090 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001091 bool IsClassMessage,
1092 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001093
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001094 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001095 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001096
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001097 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001098
1099 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1100 /// untyped one.
1101 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1102 const ObjCMethodDecl *Method);
1103
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001104 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001105
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001106 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001107
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001108 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001109 const ObjCProtocolDecl *PD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00001110
Chris Lattner74391b42009-03-22 21:03:39 +00001111 virtual llvm::Constant *GetPropertyGetFunction();
1112 virtual llvm::Constant *GetPropertySetFunction();
1113 virtual llvm::Constant *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001114
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001115 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1116 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001117 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1118 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001119 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001120 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001121 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1122 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001123 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1124 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001125 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1126 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001127 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1128 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001129 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1130 llvm::Value *dest, llvm::Value *src,
1131 unsigned long size);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001132
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001133 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1134 QualType ObjectTy,
1135 llvm::Value *BaseValue,
1136 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001137 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001138 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001139 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001140 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001141};
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001142
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001143class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001144private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001145 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001146 llvm::GlobalVariable* ObjCEmptyCacheVar;
1147 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001148
Daniel Dunbar11394522009-04-18 08:51:00 +00001149 /// SuperClassReferences - uniqued super class references.
1150 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
1151
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001152 /// MetaClassReferences - uniqued meta class references.
1153 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001154
1155 /// EHTypeReferences - uniqued class ehtype references.
1156 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001157
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001158 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001159 /// legacy messaging dispatch.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001160 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001161
1162 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001163 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001164 bool LegacyDispatchedSelector(Selector Sel);
1165
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001166 /// FinishNonFragileABIModule - Write out global data structures at the end of
1167 /// processing a translation unit.
1168 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001169
Daniel Dunbar463b8762009-05-15 21:48:48 +00001170 /// AddModuleClassList - Add the given list of class pointers to the
1171 /// module with the provided symbol and section names.
1172 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1173 const char *SymbolName,
1174 const char *SectionName);
1175
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00001176 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1177 unsigned InstanceStart,
1178 unsigned InstanceSize,
1179 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001180 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
1181 llvm::Constant *IsAGV,
1182 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001183 llvm::Constant *ClassRoGV,
1184 bool HiddenVisibility);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001185
1186 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
1187
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001188 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
1189
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001190 /// EmitMethodList - Emit the method list for the given
1191 /// implementation. The return value has type MethodListnfABITy.
1192 llvm::Constant *EmitMethodList(const std::string &Name,
1193 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001194 const ConstantVector &Methods);
1195 /// EmitIvarList - Emit the ivar list for the given
1196 /// implementation. If ForClass is true the list of class ivars
1197 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1198 /// interface ivars will be emitted. The return value has type
1199 /// IvarListnfABIPtrTy.
1200 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00001201
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001202 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001203 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001204 unsigned long int offset);
1205
Fariborz Jahanianda320092009-01-29 19:24:30 +00001206 /// GetOrEmitProtocol - Get the protocol object for the given
1207 /// declaration, emitting it if necessary. The return value has type
1208 /// ProtocolPtrTy.
1209 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
1210
1211 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1212 /// object for the given declaration, emitting it if needed. These
1213 /// forward references will be filled in with empty bodies if no
1214 /// definition is seen. The return value has type ProtocolPtrTy.
1215 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
1216
1217 /// EmitProtocolList - Generate the list of referenced
1218 /// protocols. The return value has type ProtocolListPtrTy.
1219 llvm::Constant *EmitProtocolList(const std::string &Name,
1220 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001221 ObjCProtocolDecl::protocol_iterator end);
1222
1223 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1224 QualType ResultType,
1225 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00001226 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001227 QualType Arg0Ty,
1228 bool IsSuper,
1229 const CallArgList &CallArgs);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001230
1231 /// GetClassGlobal - Return the global variable for the Objective-C
1232 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001233 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
1234
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001235 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001236 /// for the given class reference.
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001237 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001238 const ObjCInterfaceDecl *ID);
1239
1240 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1241 /// for the given super class reference.
1242 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1243 const ObjCInterfaceDecl *ID);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001244
1245 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1246 /// meta-data
1247 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
1248 const ObjCInterfaceDecl *ID);
1249
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001250 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1251 /// the given ivar.
1252 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001253 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00001254 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001255 const ObjCIvarDecl *Ivar);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001256
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001257 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1258 /// for the given selector.
1259 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbare588b992009-03-01 04:46:24 +00001260
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001261 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001262 /// interface. The return value has type EHTypePtrTy.
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001263 llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
1264 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001265
1266 const char *getMetaclassSymbolPrefix() const {
1267 return "OBJC_METACLASS_$_";
1268 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001269
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001270 const char *getClassSymbolPrefix() const {
1271 return "OBJC_CLASS_$_";
1272 }
1273
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001274 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001275 uint32_t &InstanceStart,
1276 uint32_t &InstanceSize);
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001277
1278 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001279 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001280 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1281 return CGM.getContext().Selectors.getSelector(0, &II);
1282 }
1283
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001284 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001285 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1286 return CGM.getContext().Selectors.getSelector(1, &II);
1287 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001288
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001289 /// ImplementationIsNonLazy - Check whether the given category or
1290 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001291 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001292
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001293public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001294 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001295 // FIXME. All stubs for now!
1296 virtual llvm::Function *ModuleInitFunction();
1297
1298 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
1299 QualType ResultType,
1300 Selector Sel,
1301 llvm::Value *Receiver,
1302 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001303 const CallArgList &CallArgs,
1304 const ObjCMethodDecl *Method);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001305
1306 virtual CodeGen::RValue
1307 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
1308 QualType ResultType,
1309 Selector Sel,
1310 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001311 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001312 llvm::Value *Receiver,
1313 bool IsClassMessage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001314 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001315
1316 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001317 const ObjCInterfaceDecl *ID);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001318
1319 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001320 { return EmitSelector(Builder, Sel); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001321
1322 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1323 /// untyped one.
1324 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1325 const ObjCMethodDecl *Method)
1326 { return EmitSelector(Builder, Method->getSelector()); }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001327
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001328 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001329
1330 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001331 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001332 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001333
Chris Lattner74391b42009-03-22 21:03:39 +00001334 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001335 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001336 }
Chris Lattner74391b42009-03-22 21:03:39 +00001337 virtual llvm::Constant *GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001338 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001339 }
Chris Lattner74391b42009-03-22 21:03:39 +00001340 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001341 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001342 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001343
1344 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00001345 const Stmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001346 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001347 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001348 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001349 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001350 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001351 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001352 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001353 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001354 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001355 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001356 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001357 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001358 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1359 llvm::Value *dest, llvm::Value *src,
1360 unsigned long size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001361 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1362 QualType ObjectTy,
1363 llvm::Value *BaseValue,
1364 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001365 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001366 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001367 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001368 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001369};
1370
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001371} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001372
1373/* *** Helper Functions *** */
1374
1375/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001376static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
1377 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001378 unsigned idx0,
1379 unsigned idx1) {
1380 llvm::Value *Idxs[] = {
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001381 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
1382 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001383 };
Owen Andersona1cf15f2009-07-14 23:10:40 +00001384 return VMContext.getConstantExprGetElementPtr(C, Idxs, 2);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001385}
1386
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001387/// hasObjCExceptionAttribute - Return true if this class or any super
1388/// class has the __objc_exception__ attribute.
Douglas Gregor68584ed2009-06-18 16:11:24 +00001389static bool hasObjCExceptionAttribute(ASTContext &Context,
1390 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001391 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001392 return true;
1393 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001394 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001395 return false;
1396}
1397
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001398/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001399
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001400CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
1401 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001402{
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001403 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001404 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001405}
1406
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001407/// GetClass - Return a reference to the class for the given interface
1408/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001409llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001410 const ObjCInterfaceDecl *ID) {
1411 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001412}
1413
1414/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001415llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001416 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001417}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001418llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
1419 *Method) {
1420 return EmitSelector(Builder, Method->getSelector());
1421}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001422
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001423/// Generate a constant CFString object.
1424/*
1425 struct __builtin_CFString {
1426 const int *isa; // point to __CFConstantStringClassReference
1427 int flags;
1428 const char *str;
1429 long length;
1430 };
1431*/
1432
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001433llvm::Constant *CGObjCCommonMac::GenerateConstantString(
Steve Naroff33fdb732009-03-31 16:53:37 +00001434 const ObjCStringLiteral *SL) {
Steve Naroff8d4141f2009-04-01 13:55:36 +00001435 return CGM.GetAddrOfConstantCFString(SL->getString());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001436}
1437
1438/// Generates a message send where the super is the receiver. This is
1439/// a message send to self with special delivery semantics indicating
1440/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001441CodeGen::RValue
1442CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001443 QualType ResultType,
1444 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001445 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001446 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001447 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001448 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001449 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001450 // Create and init a super structure; this is a (receiver, class)
1451 // pair we will pass to objc_msgSendSuper.
1452 llvm::Value *ObjCSuper =
1453 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
1454 llvm::Value *ReceiverAsObject =
1455 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
1456 CGF.Builder.CreateStore(ReceiverAsObject,
1457 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001458
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001459 // If this is a class message the metaclass is passed as the target.
1460 llvm::Value *Target;
1461 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001462 if (isCategoryImpl) {
1463 // Message sent to 'super' in a class method defined in a category
1464 // implementation requires an odd treatment.
1465 // If we are in a class method, we must retrieve the
1466 // _metaclass_ for the current class, pointed at by
1467 // the class's "isa" pointer. The following assumes that
1468 // isa" is the first ivar in a class (which it must be).
1469 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1470 Target = CGF.Builder.CreateStructGEP(Target, 0);
1471 Target = CGF.Builder.CreateLoad(Target);
1472 }
1473 else {
1474 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1475 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1476 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1477 Target = Super;
1478 }
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001479 } else {
1480 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1481 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001482 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1483 // ObjCTypes types.
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001484 const llvm::Type *ClassTy =
1485 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001486 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001487 CGF.Builder.CreateStore(Target,
1488 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001489 return EmitLegacyMessageSend(CGF, ResultType,
1490 EmitSelector(CGF.Builder, Sel),
1491 ObjCSuper, ObjCTypes.SuperPtrCTy,
1492 true, CallArgs, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001493}
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001494
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001495/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001496CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001497 QualType ResultType,
1498 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001499 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001500 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001501 const CallArgList &CallArgs,
1502 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001503 return EmitLegacyMessageSend(CGF, ResultType,
1504 EmitSelector(CGF.Builder, Sel),
1505 Receiver, CGF.getContext().getObjCIdType(),
1506 false, CallArgs, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001507}
1508
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001509CodeGen::RValue CGObjCCommonMac::EmitLegacyMessageSend(
1510 CodeGen::CodeGenFunction &CGF,
1511 QualType ResultType,
1512 llvm::Value *Sel,
1513 llvm::Value *Arg0,
1514 QualType Arg0Ty,
1515 bool IsSuper,
1516 const CallArgList &CallArgs,
1517 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001518 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001519 if (!IsSuper)
1520 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001521 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001522 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001523 CGF.getContext().getObjCSelType()));
1524 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001525
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001526 CodeGenTypes &Types = CGM.getTypes();
1527 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001528 // In 64bit ABI, type must be assumed VARARG. In 32bit abi,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001529 // it seems not to matter.
1530 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, (ObjCABI == 2));
1531
1532 llvm::Constant *Fn = NULL;
Daniel Dunbar88b53962009-02-02 22:03:45 +00001533 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001534 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
1535 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001536 } else if (ResultType->isFloatingType()) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001537 if (ObjCABI == 2) {
1538 if (const BuiltinType *BT = ResultType->getAsBuiltinType()) {
1539 BuiltinType::Kind k = BT->getKind();
1540 Fn = (k == BuiltinType::LongDouble) ? ObjCTypes.getSendFpretFn2(IsSuper)
1541 : ObjCTypes.getSendFn2(IsSuper);
Daniel Dunbar42f963d2009-06-10 04:38:50 +00001542 } else {
1543 Fn = ObjCTypes.getSendFn2(IsSuper);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001544 }
1545 }
1546 else
Mike Stumpf5408fe2009-05-16 07:57:57 +00001547 // FIXME. This currently matches gcc's API for x86-32. May need to change
1548 // for others if we have their API.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001549 Fn = ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001550 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001551 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
1552 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001553 }
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001554 assert(Fn && "EmitLegacyMessageSend - unknown API");
Owen Andersona1cf15f2009-07-14 23:10:40 +00001555 Fn = VMContext.getConstantExprBitCast(Fn,
1556 VMContext.getPointerTypeUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +00001557 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001558}
1559
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001560llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001561 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001562 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001563 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001564 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1565
Owen Andersona1cf15f2009-07-14 23:10:40 +00001566 return VMContext.getConstantExprBitCast(GetProtocolRef(PD),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001567 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001568}
1569
Fariborz Jahanianda320092009-01-29 19:24:30 +00001570void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001571 // FIXME: We shouldn't need this, the protocol decl should contain enough
1572 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001573 DefinedProtocols.insert(PD->getIdentifier());
1574
1575 // If we have generated a forward reference to this protocol, emit
1576 // it now. Otherwise do nothing, the protocol objects are lazily
1577 // emitted.
1578 if (Protocols.count(PD->getIdentifier()))
1579 GetOrEmitProtocol(PD);
1580}
1581
Fariborz Jahanianda320092009-01-29 19:24:30 +00001582llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001583 if (DefinedProtocols.count(PD->getIdentifier()))
1584 return GetOrEmitProtocol(PD);
1585 return GetOrEmitProtocolRef(PD);
1586}
1587
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001588/*
1589 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1590 struct _objc_protocol {
1591 struct _objc_protocol_extension *isa;
1592 char *protocol_name;
1593 struct _objc_protocol_list *protocol_list;
1594 struct _objc__method_prototype_list *instance_methods;
1595 struct _objc__method_prototype_list *class_methods
1596 };
1597
1598 See EmitProtocolExtension().
1599*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001600llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1601 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1602
1603 // Early exit if a defining object has already been generated.
1604 if (Entry && Entry->hasInitializer())
1605 return Entry;
1606
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001607 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001608 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001609 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1610
Chris Lattner8ec03f52008-11-24 03:54:41 +00001611 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001612
1613 // Construct method lists.
1614 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1615 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00001616 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001617 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001618 ObjCMethodDecl *MD = *i;
1619 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1620 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1621 OptInstanceMethods.push_back(C);
1622 } else {
1623 InstanceMethods.push_back(C);
1624 }
1625 }
1626
Douglas Gregor6ab35242009-04-09 21:40:53 +00001627 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001628 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001629 ObjCMethodDecl *MD = *i;
1630 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1631 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1632 OptClassMethods.push_back(C);
1633 } else {
1634 ClassMethods.push_back(C);
1635 }
1636 }
1637
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001638 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001639 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001640 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001641 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001642 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001643 PD->protocol_begin(),
1644 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001645 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001646 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
1647 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001648 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1649 InstanceMethods);
1650 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001651 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
1652 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001653 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1654 ClassMethods);
Owen Anderson08e25242009-07-27 22:29:56 +00001655 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001656 Values);
1657
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001658 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001659 // Already created, fix the linkage and update the initializer.
1660 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001661 Entry->setInitializer(Init);
1662 } else {
1663 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001664 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001665 llvm::GlobalValue::InternalLinkage,
1666 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00001667 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001668 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001669 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001670 // FIXME: Is this necessary? Why only for protocol?
1671 Entry->setAlignment(4);
1672 }
Chris Lattnerad64e022009-07-17 23:57:13 +00001673 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001674
1675 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001676}
1677
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001678llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001679 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1680
1681 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001682 // We use the initializer as a marker of whether this is a forward
1683 // reference or not. At module finalization we add the empty
1684 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001685 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001686 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001687 llvm::GlobalValue::ExternalLinkage,
1688 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00001689 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001690 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001691 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001692 // FIXME: Is this necessary? Why only for protocol?
1693 Entry->setAlignment(4);
1694 }
1695
1696 return Entry;
1697}
1698
1699/*
1700 struct _objc_protocol_extension {
1701 uint32_t size;
1702 struct objc_method_description_list *optional_instance_methods;
1703 struct objc_method_description_list *optional_class_methods;
1704 struct objc_property_list *instance_properties;
1705 };
1706*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001707llvm::Constant *
1708CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1709 const ConstantVector &OptInstanceMethods,
1710 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001711 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00001712 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001713 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001714 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001715 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001716 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1717 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001718 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1719 OptInstanceMethods);
1720 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001721 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1722 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001723 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1724 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001725 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1726 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001727 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001728
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001729 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001730 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1731 Values[3]->isNullValue())
Owen Anderson69243822009-07-13 04:10:07 +00001732 return VMContext.getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001733
1734 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00001735 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001736
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001737 // No special section, but goes in llvm.used
1738 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
1739 Init,
1740 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001741}
1742
1743/*
1744 struct objc_protocol_list {
1745 struct objc_protocol_list *next;
1746 long count;
1747 Protocol *list[];
1748 };
1749*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001750llvm::Constant *
1751CGObjCMac::EmitProtocolList(const std::string &Name,
1752 ObjCProtocolDecl::protocol_iterator begin,
1753 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001754 std::vector<llvm::Constant*> ProtocolRefs;
1755
Daniel Dunbardbc933702008-08-21 21:57:41 +00001756 for (; begin != end; ++begin)
1757 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001758
1759 // Just return null for empty protocol lists
1760 if (ProtocolRefs.empty())
Owen Anderson69243822009-07-13 04:10:07 +00001761 return VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001762
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001763 // This list is null terminated.
Owen Anderson69243822009-07-13 04:10:07 +00001764 ProtocolRefs.push_back(VMContext.getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001765
1766 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001767 // This field is only used by the runtime.
Owen Anderson69243822009-07-13 04:10:07 +00001768 Values[0] = VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001769 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001770 ProtocolRefs.size() - 1);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001771 Values[2] =
Owen Anderson7db6d832009-07-28 18:33:04 +00001772 llvm::ConstantArray::get(VMContext.getArrayType(ObjCTypes.ProtocolPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001773 ProtocolRefs.size()),
1774 ProtocolRefs);
1775
Owen Anderson08e25242009-07-27 22:29:56 +00001776 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001777 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001778 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001779 4, false);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001780 return VMContext.getConstantExprBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001781}
1782
1783/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001784 struct _objc_property {
1785 const char * const name;
1786 const char * const attributes;
1787 };
1788
1789 struct _objc_property_list {
1790 uint32_t entsize; // sizeof (struct _objc_property)
1791 uint32_t prop_count;
1792 struct _objc_property[prop_count];
1793 };
1794*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001795llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1796 const Decl *Container,
1797 const ObjCContainerDecl *OCD,
1798 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001799 std::vector<llvm::Constant*> Properties, Prop(2);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001800 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1801 E = OCD->prop_end(); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00001802 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001803 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001804 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson08e25242009-07-27 22:29:56 +00001805 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001806 Prop));
1807 }
1808
1809 // Return null for empty list.
1810 if (Properties.empty())
Owen Anderson69243822009-07-13 04:10:07 +00001811 return VMContext.getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001812
1813 unsigned PropertySize =
Duncan Sands9408c452009-05-09 07:08:47 +00001814 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001815 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001816 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1817 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Owen Andersona1cf15f2009-07-14 23:10:40 +00001818 llvm::ArrayType *AT = VMContext.getArrayType(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001819 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00001820 Values[2] = llvm::ConstantArray::get(AT, Properties);
Owen Anderson08e25242009-07-27 22:29:56 +00001821 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001822
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001823 llvm::GlobalVariable *GV =
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001824 CreateMetadataVar(Name, Init,
1825 (ObjCABI == 2) ? "__DATA, __objc_const" :
1826 "__OBJC,__property,regular,no_dead_strip",
1827 (ObjCABI == 2) ? 8 : 4,
1828 true);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001829 return VMContext.getConstantExprBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001830}
1831
1832/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001833 struct objc_method_description_list {
1834 int count;
1835 struct objc_method_description list[];
1836 };
1837*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001838llvm::Constant *
1839CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1840 std::vector<llvm::Constant*> Desc(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001841 Desc[0] =
1842 VMContext.getConstantExprBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001843 ObjCTypes.SelectorPtrTy);
1844 Desc[1] = GetMethodVarType(MD);
Owen Anderson08e25242009-07-27 22:29:56 +00001845 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001846 Desc);
1847}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001848
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001849llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1850 const char *Section,
1851 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001852 // Return null for empty list.
1853 if (Methods.empty())
Owen Anderson69243822009-07-13 04:10:07 +00001854 return VMContext.getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001855
1856 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001857 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Andersona1cf15f2009-07-14 23:10:40 +00001858 llvm::ArrayType *AT = VMContext.getArrayType(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001859 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00001860 Values[1] = llvm::ConstantArray::get(AT, Methods);
Owen Anderson08e25242009-07-27 22:29:56 +00001861 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001862
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001863 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001864 return VMContext.getConstantExprBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001865 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001866}
1867
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001868/*
1869 struct _objc_category {
1870 char *category_name;
1871 char *class_name;
1872 struct _objc_method_list *instance_methods;
1873 struct _objc_method_list *class_methods;
1874 struct _objc_protocol_list *protocols;
1875 uint32_t size; // <rdar://4585769>
1876 struct _objc_property_list *instance_properties;
1877 };
1878 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001879void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sands9408c452009-05-09 07:08:47 +00001880 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001881
Mike Stumpf5408fe2009-05-16 07:57:57 +00001882 // FIXME: This is poor design, the OCD should have a pointer to the category
1883 // decl. Additionally, note that Category can be null for the @implementation
1884 // w/o an @interface case. Sema should just create one for us as it does for
1885 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001886 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001887 const ObjCCategoryDecl *Category =
1888 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001889 std::string ExtName(Interface->getNameAsString() + "_" +
1890 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001891
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001892 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001893 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001894 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001895 // Instance methods should always be defined.
1896 InstanceMethods.push_back(GetMethodConstant(*i));
1897 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001898 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001899 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001900 // Class methods should always be defined.
1901 ClassMethods.push_back(GetMethodConstant(*i));
1902 }
1903
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001904 std::vector<llvm::Constant*> Values(7);
1905 Values[0] = GetClassName(OCD->getIdentifier());
1906 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00001907 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001908 Values[2] =
1909 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1910 ExtName,
1911 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001912 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001913 Values[3] =
1914 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001915 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001916 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001917 if (Category) {
1918 Values[4] =
1919 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1920 Category->protocol_begin(),
1921 Category->protocol_end());
1922 } else {
Owen Anderson69243822009-07-13 04:10:07 +00001923 Values[4] = VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001924 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001925 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001926
1927 // If there is no category @interface then there can be no properties.
1928 if (Category) {
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001929 Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001930 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001931 } else {
Owen Anderson69243822009-07-13 04:10:07 +00001932 Values[6] = VMContext.getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001933 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001934
Owen Anderson08e25242009-07-27 22:29:56 +00001935 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001936 Values);
1937
1938 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001939 CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init,
1940 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001941 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001942 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001943}
1944
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001945// FIXME: Get from somewhere?
1946enum ClassFlags {
1947 eClassFlags_Factory = 0x00001,
1948 eClassFlags_Meta = 0x00002,
1949 // <rdr://5142207>
1950 eClassFlags_HasCXXStructors = 0x02000,
1951 eClassFlags_Hidden = 0x20000,
1952 eClassFlags_ABI2_Hidden = 0x00010,
1953 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1954};
1955
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001956/*
1957 struct _objc_class {
1958 Class isa;
1959 Class super_class;
1960 const char *name;
1961 long version;
1962 long info;
1963 long instance_size;
1964 struct _objc_ivar_list *ivars;
1965 struct _objc_method_list *methods;
1966 struct _objc_cache *cache;
1967 struct _objc_protocol_list *protocols;
1968 // Objective-C 1.0 extensions (<rdr://4585769>)
1969 const char *ivar_layout;
1970 struct _objc_class_ext *ext;
1971 };
1972
1973 See EmitClassExtension();
1974 */
1975void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001976 DefinedSymbols.insert(ID->getIdentifier());
1977
Chris Lattner8ec03f52008-11-24 03:54:41 +00001978 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001979 // FIXME: Gross
1980 ObjCInterfaceDecl *Interface =
1981 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001982 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001983 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001984 Interface->protocol_begin(),
1985 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001986 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar2bebbf02009-05-03 10:46:44 +00001987 unsigned Size =
1988 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001989
1990 // FIXME: Set CXX-structors flag.
Daniel Dunbar04d40782009-04-14 06:00:08 +00001991 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001992 Flags |= eClassFlags_Hidden;
1993
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001994 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001995 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001996 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001997 // Instance methods should always be defined.
1998 InstanceMethods.push_back(GetMethodConstant(*i));
1999 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00002000 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002001 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002002 // Class methods should always be defined.
2003 ClassMethods.push_back(GetMethodConstant(*i));
2004 }
2005
Douglas Gregor653f1b12009-04-23 01:02:12 +00002006 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002007 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002008 ObjCPropertyImplDecl *PID = *i;
2009
2010 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2011 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2012
2013 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2014 if (llvm::Constant *C = GetMethodConstant(MD))
2015 InstanceMethods.push_back(C);
2016 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2017 if (llvm::Constant *C = GetMethodConstant(MD))
2018 InstanceMethods.push_back(C);
2019 }
2020 }
2021
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002022 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar5384b092009-05-03 08:56:52 +00002023 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002024 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002025 // Record a reference to the super class.
2026 LazySymbols.insert(Super->getIdentifier());
2027
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002028 Values[ 1] =
Owen Andersona1cf15f2009-07-14 23:10:40 +00002029 VMContext.getConstantExprBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002030 ObjCTypes.ClassPtrTy);
2031 } else {
Owen Anderson69243822009-07-13 04:10:07 +00002032 Values[ 1] = VMContext.getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002033 }
2034 Values[ 2] = GetClassName(ID->getIdentifier());
2035 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002036 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2037 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2038 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002039 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002040 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002041 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002042 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002043 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002044 // cache is always NULL.
Owen Anderson69243822009-07-13 04:10:07 +00002045 Values[ 8] = VMContext.getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002046 Values[ 9] = Protocols;
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002047 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002048 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00002049 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002050 Values);
2051
2052 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002053 CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init,
2054 "__OBJC,__class,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002055 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002056 DefinedClasses.push_back(GV);
2057}
2058
2059llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2060 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002061 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002062 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002063 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002064
Daniel Dunbar04d40782009-04-14 06:00:08 +00002065 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002066 Flags |= eClassFlags_Hidden;
2067
2068 std::vector<llvm::Constant*> Values(12);
2069 // The isa for the metaclass is the root of the hierarchy.
2070 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2071 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2072 Root = Super;
2073 Values[ 0] =
Owen Andersona1cf15f2009-07-14 23:10:40 +00002074 VMContext.getConstantExprBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002075 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002076 // The super class for the metaclass is emitted as the name of the
2077 // super class. The runtime fixes this up to point to the
2078 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002079 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
2080 Values[ 1] =
Owen Andersona1cf15f2009-07-14 23:10:40 +00002081 VMContext.getConstantExprBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002082 ObjCTypes.ClassPtrTy);
2083 } else {
Owen Anderson69243822009-07-13 04:10:07 +00002084 Values[ 1] = VMContext.getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002085 }
2086 Values[ 2] = GetClassName(ID->getIdentifier());
2087 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002088 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2089 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2090 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002091 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002092 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002093 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002094 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002095 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002096 // cache is always NULL.
Owen Anderson69243822009-07-13 04:10:07 +00002097 Values[ 8] = VMContext.getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002098 Values[ 9] = Protocols;
2099 // ivar_layout for metaclass is always NULL.
Owen Anderson69243822009-07-13 04:10:07 +00002100 Values[10] = VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002101 // The class extension is always unused for metaclasses.
Owen Anderson69243822009-07-13 04:10:07 +00002102 Values[11] = VMContext.getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002103 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002104 Values);
2105
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002106 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002107 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002108
2109 // Check for a forward reference.
2110 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2111 if (GV) {
2112 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2113 "Forward metaclass reference has incorrect type.");
2114 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2115 GV->setInitializer(Init);
2116 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002117 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002118 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002119 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002120 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002121 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002122 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002123 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002124
2125 return GV;
2126}
2127
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002128llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002129 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002130
Mike Stumpf5408fe2009-05-16 07:57:57 +00002131 // FIXME: Should we look these up somewhere other than the module. Its a bit
2132 // silly since we only generate these while processing an implementation, so
2133 // exactly one pointer would work if know when we entered/exitted an
2134 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002135
2136 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002137 // Previously, metaclass with internal linkage may have been defined.
2138 // pass 'true' as 2nd argument so it is returned.
2139 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002140 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2141 "Forward metaclass reference has incorrect type.");
2142 return GV;
2143 } else {
2144 // Generate as an external reference to keep a consistent
2145 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002146 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002147 llvm::GlobalValue::ExternalLinkage,
2148 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002149 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002150 }
2151}
2152
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002153/*
2154 struct objc_class_ext {
2155 uint32_t size;
2156 const char *weak_ivar_layout;
2157 struct _objc_property_list *properties;
2158 };
2159*/
2160llvm::Constant *
2161CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
2162 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002163 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002164
2165 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002166 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002167 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002168 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002169 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002170
2171 // Return null if no extension bits are used.
2172 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson69243822009-07-13 04:10:07 +00002173 return VMContext.getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002174
2175 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002176 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002177 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002178 Init, "__OBJC,__class_ext,regular,no_dead_strip",
2179 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002180}
2181
2182/*
2183 struct objc_ivar {
2184 char *ivar_name;
2185 char *ivar_type;
2186 int ivar_offset;
2187 };
2188
2189 struct objc_ivar_list {
2190 int ivar_count;
2191 struct objc_ivar list[count];
2192 };
2193 */
2194llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002195 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002196 std::vector<llvm::Constant*> Ivars, Ivar(3);
2197
2198 // When emitting the root class GCC emits ivar entries for the
2199 // actual class structure. It is not clear if we need to follow this
2200 // behavior; for now lets try and get away with not doing it. If so,
2201 // the cleanest solution would be to make up an ObjCInterfaceDecl
2202 // for the class.
2203 if (ForClass)
Owen Anderson69243822009-07-13 04:10:07 +00002204 return VMContext.getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002205
2206 ObjCInterfaceDecl *OID =
2207 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002208
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002209 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002210 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002211
2212 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2213 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002214 // Ignore unnamed bit-fields.
2215 if (!IVD->getDeclName())
2216 continue;
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002217 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2218 Ivar[1] = GetMethodVarType(IVD);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002219 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002220 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson08e25242009-07-27 22:29:56 +00002221 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002222 }
2223
2224 // Return null for empty list.
2225 if (Ivars.empty())
Owen Anderson69243822009-07-13 04:10:07 +00002226 return VMContext.getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002227
2228 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002229 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Andersona1cf15f2009-07-14 23:10:40 +00002230 llvm::ArrayType *AT = VMContext.getArrayType(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002231 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002232 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Owen Anderson08e25242009-07-27 22:29:56 +00002233 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002234
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002235 llvm::GlobalVariable *GV;
2236 if (ForClass)
2237 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(),
Daniel Dunbar58a29122009-03-09 22:18:41 +00002238 Init, "__OBJC,__class_vars,regular,no_dead_strip",
2239 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002240 else
2241 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_"
2242 + ID->getNameAsString(),
2243 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002244 4, true);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002245 return VMContext.getConstantExprBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002246}
2247
2248/*
2249 struct objc_method {
2250 SEL method_name;
2251 char *method_types;
2252 void *method;
2253 };
2254
2255 struct objc_method_list {
2256 struct objc_method_list *obsolete;
2257 int count;
2258 struct objc_method methods_list[count];
2259 };
2260*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002261
2262/// GetMethodConstant - Return a struct objc_method constant for the
2263/// given method if it has been defined. The result is null if the
2264/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002265llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002266 // FIXME: Use DenseMap::lookup
2267 llvm::Function *Fn = MethodDefinitions[MD];
2268 if (!Fn)
2269 return 0;
2270
2271 std::vector<llvm::Constant*> Method(3);
2272 Method[0] =
Owen Andersona1cf15f2009-07-14 23:10:40 +00002273 VMContext.getConstantExprBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002274 ObjCTypes.SelectorPtrTy);
2275 Method[1] = GetMethodVarType(MD);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002276 Method[2] = VMContext.getConstantExprBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002277 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002278}
2279
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002280llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
2281 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002282 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002283 // Return null for empty list.
2284 if (Methods.empty())
Owen Anderson69243822009-07-13 04:10:07 +00002285 return VMContext.getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002286
2287 std::vector<llvm::Constant*> Values(3);
Owen Anderson69243822009-07-13 04:10:07 +00002288 Values[0] = VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002289 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Andersona1cf15f2009-07-14 23:10:40 +00002290 llvm::ArrayType *AT = VMContext.getArrayType(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002291 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002292 Values[2] = llvm::ConstantArray::get(AT, Methods);
Owen Anderson08e25242009-07-27 22:29:56 +00002293 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002294
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002295 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002296 return VMContext.getConstantExprBitCast(GV,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002297 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002298}
2299
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002300llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00002301 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002302 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002303 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002304
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002305 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002306 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002307 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002308 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002309 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002310 llvm::GlobalValue::InternalLinkage,
2311 Name,
2312 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002313 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002314
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002315 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002316}
2317
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002318llvm::GlobalVariable *
2319CGObjCCommonMac::CreateMetadataVar(const std::string &Name,
2320 llvm::Constant *Init,
2321 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002322 unsigned Align,
2323 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002324 const llvm::Type *Ty = Init->getType();
2325 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002326 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002327 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002328 if (Section)
2329 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002330 if (Align)
2331 GV->setAlignment(Align);
2332 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002333 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002334 return GV;
2335}
2336
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002337llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002338 // Abuse this interface function as a place to finalize.
2339 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002340 return NULL;
2341}
2342
Chris Lattner74391b42009-03-22 21:03:39 +00002343llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002344 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002345}
2346
Chris Lattner74391b42009-03-22 21:03:39 +00002347llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002348 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002349}
2350
Chris Lattner74391b42009-03-22 21:03:39 +00002351llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002352 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002353}
2354
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002355/*
2356
2357Objective-C setjmp-longjmp (sjlj) Exception Handling
2358--
2359
2360The basic framework for a @try-catch-finally is as follows:
2361{
2362 objc_exception_data d;
2363 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002364 bool _call_try_exit = true;
2365
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002366 objc_exception_try_enter(&d);
2367 if (!setjmp(d.jmp_buf)) {
2368 ... try body ...
2369 } else {
2370 // exception path
2371 id _caught = objc_exception_extract(&d);
2372
2373 // enter new try scope for handlers
2374 if (!setjmp(d.jmp_buf)) {
2375 ... match exception and execute catch blocks ...
2376
2377 // fell off end, rethrow.
2378 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00002379 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002380 } else {
2381 // exception in catch block
2382 _rethrow = objc_exception_extract(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002383 _call_try_exit = false;
2384 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002385 }
2386 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002387 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002388
2389finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002390 if (_call_try_exit)
2391 objc_exception_try_exit(&d);
2392
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002393 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002394 ... dispatch to finally destination ...
2395
2396finally_rethrow:
2397 objc_exception_throw(_rethrow);
2398
2399finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002400}
2401
2402This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00002403uses _rethrow to determine if objc_exception_try_exit should be called
2404and if the object should be rethrown. This breaks in the face of
2405throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002406
2407We specialize this framework for a few particular circumstances:
2408
2409 - If there are no catch blocks, then we avoid emitting the second
2410 exception handling context.
2411
2412 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2413 e)) we avoid emitting the code to rethrow an uncaught exception.
2414
2415 - FIXME: If there is no @finally block we can do a few more
2416 simplifications.
2417
2418Rethrows and Jumps-Through-Finally
2419--
2420
2421Support for implicit rethrows and jumping through the finally block is
2422handled by storing the current exception-handling context in
2423ObjCEHStack.
2424
Daniel Dunbar898d5082008-09-30 01:06:03 +00002425In order to implement proper @finally semantics, we support one basic
2426mechanism for jumping through the finally block to an arbitrary
2427destination. Constructs which generate exits from a @try or @catch
2428block use this mechanism to implement the proper semantics by chaining
2429jumps, as necessary.
2430
2431This mechanism works like the one used for indirect goto: we
2432arbitrarily assign an ID to each destination and store the ID for the
2433destination in a variable prior to entering the finally block. At the
2434end of the finally block we simply create a switch to the proper
2435destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002436
2437Code gen for @synchronized(expr) stmt;
2438Effectively generating code for:
2439objc_sync_enter(expr);
2440@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002441*/
2442
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002443void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2444 const Stmt &S) {
2445 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002446 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00002447 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002448 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar55e87422008-11-11 02:29:29 +00002449 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
2450 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
2451 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar1c566672009-02-24 01:43:46 +00002452
2453 // For @synchronized, call objc_sync_enter(sync.expr). The
2454 // evaluation of the expression must occur before we enter the
2455 // @synchronized. We can safely avoid a temp here because jumps into
2456 // @synchronized are illegal & this will dominate uses.
2457 llvm::Value *SyncArg = 0;
2458 if (!isTry) {
2459 SyncArg =
2460 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2461 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00002462 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002463 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002464
2465 // Push an EH context entry, used for handling rethrows and jumps
2466 // through finally.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002467 CGF.PushCleanupBlock(FinallyBlock);
2468
Anders Carlsson273558f2009-02-07 21:37:21 +00002469 CGF.ObjCEHValueStack.push_back(0);
2470
Daniel Dunbar898d5082008-09-30 01:06:03 +00002471 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00002472 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2473 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002474 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
2475 "_rethrow");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002476 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
2477 "_call_try_exit");
Owen Anderson4cd16082009-07-21 18:06:41 +00002478 CGF.Builder.CreateStore(VMContext.getTrue(), CallTryExitPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002479
Anders Carlsson80f25672008-09-09 17:59:25 +00002480 // Enter a new try block and call setjmp.
Chris Lattner34b02a12009-04-22 02:26:14 +00002481 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Anders Carlsson80f25672008-09-09 17:59:25 +00002482 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
2483 "jmpbufarray");
2484 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
Chris Lattner34b02a12009-04-22 02:26:14 +00002485 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlsson80f25672008-09-09 17:59:25 +00002486 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00002487
Daniel Dunbar55e87422008-11-11 02:29:29 +00002488 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2489 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002490 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002491 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002492
2493 // Emit the @try block.
2494 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002495 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
2496 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002497 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002498
2499 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002500 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002501
2502 // Retrieve the exception object. We may emit multiple blocks but
2503 // nothing can cross this so the value is already in SSA form.
Chris Lattner34b02a12009-04-22 02:26:14 +00002504 llvm::Value *Caught =
2505 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2506 ExceptionData, "caught");
Anders Carlsson273558f2009-02-07 21:37:21 +00002507 CGF.ObjCEHValueStack.back() = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002508 if (!isTry)
2509 {
2510 CGF.Builder.CreateStore(Caught, RethrowPtr);
Owen Anderson4cd16082009-07-21 18:06:41 +00002511 CGF.Builder.CreateStore(VMContext.getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002512 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002513 }
2514 else if (const ObjCAtCatchStmt* CatchStmt =
2515 cast<ObjCAtTryStmt>(S).getCatchStmts())
2516 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00002517 // Enter a new exception try block (in case a @catch block throws
2518 // an exception).
Chris Lattner34b02a12009-04-22 02:26:14 +00002519 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002520
Chris Lattner34b02a12009-04-22 02:26:14 +00002521 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlsson80f25672008-09-09 17:59:25 +00002522 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002523 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00002524
Daniel Dunbar55e87422008-11-11 02:29:29 +00002525 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
2526 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002527 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002528
2529 CGF.EmitBlock(CatchBlock);
2530
Daniel Dunbar55e40722008-09-27 07:03:52 +00002531 // Handle catch list. As a special case we check if everything is
2532 // matched and avoid generating code for falling off the end if
2533 // so.
2534 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00002535 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00002536 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00002537
Steve Naroff7ba138a2009-03-03 19:52:17 +00002538 const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00002539 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00002540
Anders Carlsson80f25672008-09-09 17:59:25 +00002541 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00002542 if (!CatchParam) {
2543 AllMatched = true;
2544 } else {
Steve Naroff14108da2009-07-10 23:34:53 +00002545 OPT = CatchParam->getType()->getAsObjCObjectPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002546
Daniel Dunbar97f61d12008-09-27 22:21:14 +00002547 // catch(id e) always matches.
2548 // FIXME: For the time being we also match id<X>; this should
2549 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00002550 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00002551 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00002552 }
2553
Daniel Dunbar55e40722008-09-27 07:03:52 +00002554 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00002555 if (CatchParam) {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002556 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002557 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002558 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002559 }
Anders Carlsson1452f552008-09-11 08:21:54 +00002560
Anders Carlssondde0a942008-09-11 09:15:33 +00002561 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002562 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002563 break;
2564 }
2565
Steve Naroff14108da2009-07-10 23:34:53 +00002566 assert(OPT && "Unexpected non-object pointer type in @catch");
2567 QualType T = OPT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00002568 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002569 assert(ObjCType && "Catch parameter must have Objective-C type!");
2570
2571 // Check if the @catch block matches the exception object.
2572 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
2573
Chris Lattner34b02a12009-04-22 02:26:14 +00002574 llvm::Value *Match =
2575 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
2576 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00002577
Daniel Dunbar55e87422008-11-11 02:29:29 +00002578 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00002579
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002580 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002581 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002582
2583 // Emit the @catch block.
2584 CGF.EmitBlock(MatchedBlock);
Steve Naroff7ba138a2009-03-03 19:52:17 +00002585 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002586 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002587
2588 llvm::Value *Tmp =
Steve Naroff7ba138a2009-03-03 19:52:17 +00002589 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002590 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002591 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002592
2593 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002594 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002595
2596 CGF.EmitBlock(NextCatchBlock);
2597 }
2598
Daniel Dunbar55e40722008-09-27 07:03:52 +00002599 if (!AllMatched) {
2600 // None of the handlers caught the exception, so store it to be
2601 // rethrown at the end of the @finally block.
2602 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002603 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002604 }
2605
2606 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002607 CGF.EmitBlock(CatchHandler);
Chris Lattner34b02a12009-04-22 02:26:14 +00002608 CGF.Builder.CreateStore(
2609 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2610 ExceptionData),
Daniel Dunbar55e40722008-09-27 07:03:52 +00002611 RethrowPtr);
Owen Anderson4cd16082009-07-21 18:06:41 +00002612 CGF.Builder.CreateStore(VMContext.getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002613 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002614 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00002615 CGF.Builder.CreateStore(Caught, RethrowPtr);
Owen Anderson4cd16082009-07-21 18:06:41 +00002616 CGF.Builder.CreateStore(VMContext.getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002617 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson80f25672008-09-09 17:59:25 +00002618 }
2619
Daniel Dunbar898d5082008-09-30 01:06:03 +00002620 // Pop the exception-handling stack entry. It is important to do
2621 // this now, because the code in the @finally block is not in this
2622 // context.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002623 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2624
Anders Carlsson273558f2009-02-07 21:37:21 +00002625 CGF.ObjCEHValueStack.pop_back();
2626
Anders Carlsson80f25672008-09-09 17:59:25 +00002627 // Emit the @finally block.
2628 CGF.EmitBlock(FinallyBlock);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002629 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2630
2631 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2632
2633 CGF.EmitBlock(FinallyExit);
Chris Lattner34b02a12009-04-22 02:26:14 +00002634 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002635
2636 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002637 if (isTry) {
2638 if (const ObjCAtFinallyStmt* FinallyStmt =
2639 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2640 CGF.EmitStmt(FinallyStmt->getFinallyBody());
Daniel Dunbar1c566672009-02-24 01:43:46 +00002641 } else {
2642 // Emit objc_sync_exit(expr); as finally's sole statement for
2643 // @synchronized.
Chris Lattnerbbccd612009-04-22 02:38:11 +00002644 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002645 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002646
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002647 // Emit the switch block
2648 if (Info.SwitchBlock)
2649 CGF.EmitBlock(Info.SwitchBlock);
2650 if (Info.EndBlock)
2651 CGF.EmitBlock(Info.EndBlock);
2652
Daniel Dunbar898d5082008-09-30 01:06:03 +00002653 CGF.EmitBlock(FinallyRethrow);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002654 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbar898d5082008-09-30 01:06:03 +00002655 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002656 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00002657
2658 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002659}
2660
2661void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002662 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002663 llvm::Value *ExceptionAsObject;
2664
2665 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2666 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2667 ExceptionAsObject =
2668 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2669 } else {
Anders Carlsson273558f2009-02-07 21:37:21 +00002670 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002671 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00002672 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002673 }
2674
Chris Lattnerbbccd612009-04-22 02:38:11 +00002675 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002676 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002677
2678 // Clear the insertion point to indicate we are in unreachable code.
2679 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002680}
2681
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002682/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002683/// object: objc_read_weak (id *src)
2684///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002685llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002686 llvm::Value *AddrWeakObj)
2687{
Eli Friedman8339b352009-03-07 03:57:15 +00002688 const llvm::Type* DestTy =
2689 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002690 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00002691 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002692 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00002693 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002694 return read_weak;
2695}
2696
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002697/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2698/// objc_assign_weak (id src, id *dst)
2699///
2700void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2701 llvm::Value *src, llvm::Value *dst)
2702{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002703 const llvm::Type * SrcTy = src->getType();
2704 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002705 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002706 assert(Size <= 8 && "does not support size > 8");
2707 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2708 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002709 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2710 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002711 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2712 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00002713 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002714 src, dst, "weakassign");
2715 return;
2716}
2717
Fariborz Jahanian58626502008-11-19 00:59:10 +00002718/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2719/// objc_assign_global (id src, id *dst)
2720///
2721void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2722 llvm::Value *src, llvm::Value *dst)
2723{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002724 const llvm::Type * SrcTy = src->getType();
2725 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002726 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002727 assert(Size <= 8 && "does not support size > 8");
2728 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2729 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002730 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2731 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002732 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2733 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002734 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00002735 src, dst, "globalassign");
2736 return;
2737}
2738
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002739/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2740/// objc_assign_ivar (id src, id *dst)
2741///
2742void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2743 llvm::Value *src, llvm::Value *dst)
2744{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002745 const llvm::Type * SrcTy = src->getType();
2746 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002747 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002748 assert(Size <= 8 && "does not support size > 8");
2749 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2750 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002751 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2752 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002753 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2754 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002755 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(),
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002756 src, dst, "assignivar");
2757 return;
2758}
2759
Fariborz Jahanian58626502008-11-19 00:59:10 +00002760/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2761/// objc_assign_strongCast (id src, id *dst)
2762///
2763void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2764 llvm::Value *src, llvm::Value *dst)
2765{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002766 const llvm::Type * SrcTy = src->getType();
2767 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002768 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002769 assert(Size <= 8 && "does not support size > 8");
2770 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2771 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002772 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2773 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002774 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2775 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002776 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00002777 src, dst, "weakassign");
2778 return;
2779}
2780
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002781void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
2782 llvm::Value *DestPtr,
2783 llvm::Value *SrcPtr,
2784 unsigned long size) {
2785 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
2786 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002787 llvm::Value *N = llvm::ConstantInt::get(ObjCTypes.LongTy, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002788 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
2789 DestPtr, SrcPtr, N);
2790 return;
2791}
2792
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002793/// EmitObjCValueForIvar - Code Gen for ivar reference.
2794///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002795LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2796 QualType ObjectTy,
2797 llvm::Value *BaseValue,
2798 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002799 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00002800 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00002801 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
2802 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002803}
2804
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002805llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00002806 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002807 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00002808 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002809 return llvm::ConstantInt::get(
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002810 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2811 Offset);
2812}
2813
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002814/* *** Private Interface *** */
2815
2816/// EmitImageInfo - Emit the image info marker used to encode some module
2817/// level information.
2818///
2819/// See: <rdr://4810609&4810587&4810587>
2820/// struct IMAGE_INFO {
2821/// unsigned version;
2822/// unsigned flags;
2823/// };
2824enum ImageInfoFlags {
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002825 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what
2826 // this implies.
2827 eImageInfo_GarbageCollected = (1 << 1),
2828 eImageInfo_GCOnly = (1 << 2),
2829 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
2830
2831 // A flag indicating that the module has no instances of an
2832 // @synthesize of a superclass variable. <rdar://problem/6803242>
2833 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002834};
2835
2836void CGObjCMac::EmitImageInfo() {
2837 unsigned version = 0; // Version is unused?
2838 unsigned flags = 0;
2839
2840 // FIXME: Fix and continue?
2841 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2842 flags |= eImageInfo_GarbageCollected;
2843 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2844 flags |= eImageInfo_GCOnly;
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002845
2846 // We never allow @synthesize of a superclass property.
2847 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002848
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002849 // Emitted as int[2];
2850 llvm::Constant *values[2] = {
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002851 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2852 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002853 };
Owen Andersona1cf15f2009-07-14 23:10:40 +00002854 llvm::ArrayType *AT = VMContext.getArrayType(llvm::Type::Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002855
2856 const char *Section;
2857 if (ObjCABI == 1)
2858 Section = "__OBJC, __image_info,regular";
2859 else
2860 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002861 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002862 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson7db6d832009-07-28 18:33:04 +00002863 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002864 Section,
2865 0,
2866 true);
2867 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002868}
2869
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002870
2871// struct objc_module {
2872// unsigned long version;
2873// unsigned long size;
2874// const char *name;
2875// Symtab symtab;
2876// };
2877
2878// FIXME: Get from somewhere
2879static const int ModuleVersion = 7;
2880
2881void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00002882 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002883
2884 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002885 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2886 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002887 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002888 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002889 Values[3] = EmitModuleSymbols();
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002890 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00002891 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002892 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002893 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002894}
2895
2896llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002897 unsigned NumClasses = DefinedClasses.size();
2898 unsigned NumCategories = DefinedCategories.size();
2899
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002900 // Return null if no symbols were defined.
2901 if (!NumClasses && !NumCategories)
Owen Anderson69243822009-07-13 04:10:07 +00002902 return VMContext.getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002903
2904 std::vector<llvm::Constant*> Values(5);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002905 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson69243822009-07-13 04:10:07 +00002906 Values[1] = VMContext.getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002907 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2908 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002909
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002910 // The runtime expects exactly the list of defined classes followed
2911 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002912 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002913 for (unsigned i=0; i<NumClasses; i++)
Owen Andersona1cf15f2009-07-14 23:10:40 +00002914 Symbols[i] = VMContext.getConstantExprBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002915 ObjCTypes.Int8PtrTy);
2916 for (unsigned i=0; i<NumCategories; i++)
2917 Symbols[NumClasses + i] =
Owen Andersona1cf15f2009-07-14 23:10:40 +00002918 VMContext.getConstantExprBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002919 ObjCTypes.Int8PtrTy);
2920
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002921 Values[4] =
Owen Anderson7db6d832009-07-28 18:33:04 +00002922 llvm::ConstantArray::get(VMContext.getArrayType(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002923 NumClasses + NumCategories),
2924 Symbols);
2925
Owen Anderson08e25242009-07-27 22:29:56 +00002926 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002927
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002928 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002929 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
2930 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002931 4, true);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002932 return VMContext.getConstantExprBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002933}
2934
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002935llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002936 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002937 LazySymbols.insert(ID->getIdentifier());
2938
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002939 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2940
2941 if (!Entry) {
2942 llvm::Constant *Casted =
Owen Andersona1cf15f2009-07-14 23:10:40 +00002943 VMContext.getConstantExprBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002944 ObjCTypes.ClassPtrTy);
2945 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002946 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
2947 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002948 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002949 }
2950
2951 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002952}
2953
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002954llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002955 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2956
2957 if (!Entry) {
2958 llvm::Constant *Casted =
Owen Andersona1cf15f2009-07-14 23:10:40 +00002959 VMContext.getConstantExprBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002960 ObjCTypes.SelectorPtrTy);
2961 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002962 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
2963 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002964 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002965 }
2966
2967 return Builder.CreateLoad(Entry, false, "tmp");
2968}
2969
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002970llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002971 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002972
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002973 if (!Entry)
2974 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Owen Anderson7db6d832009-07-28 18:33:04 +00002975 llvm::ConstantArray::get(Ident->getName()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002976 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00002977 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002978
Owen Andersona1cf15f2009-07-14 23:10:40 +00002979 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002980}
2981
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00002982/// GetIvarLayoutName - Returns a unique constant for the given
2983/// ivar layout bitmap.
2984llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
2985 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson69243822009-07-13 04:10:07 +00002986 return VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00002987}
2988
Daniel Dunbard58edcb2009-05-03 14:10:34 +00002989static QualType::GCAttrTypes GetGCAttrTypeForType(ASTContext &Ctx,
2990 QualType FQT) {
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00002991 if (FQT.isObjCGCStrong())
2992 return QualType::Strong;
2993
2994 if (FQT.isObjCGCWeak())
2995 return QualType::Weak;
2996
Steve Narofff4954562009-07-16 15:41:00 +00002997 if (FQT->isObjCObjectPointerType())
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00002998 return QualType::Strong;
2999
Ted Kremenek35366a62009-07-17 17:50:17 +00003000 if (const PointerType *PT = FQT->getAsPointerType())
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003001 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003002
3003 return QualType::GCNone;
3004}
3005
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003006void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
3007 unsigned int BytePos,
3008 bool ForStrongLayout,
3009 bool &HasUnion) {
3010 const RecordDecl *RD = RT->getDecl();
3011 // FIXME - Use iterator.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003012 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003013 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
3014 const llvm::StructLayout *RecLayout =
3015 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
3016
3017 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3018 ForStrongLayout, HasUnion);
3019}
3020
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003021void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003022 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003023 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00003024 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003025 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003026 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003027 bool IsUnion = (RD && RD->isUnion());
3028 uint64_t MaxUnionIvarSize = 0;
3029 uint64_t MaxSkippedUnionIvarSize = 0;
3030 FieldDecl *MaxField = 0;
3031 FieldDecl *MaxSkippedField = 0;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003032 FieldDecl *LastFieldBitfield = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003033 uint64_t MaxFieldOffset = 0;
3034 uint64_t MaxSkippedFieldOffset = 0;
3035 uint64_t LastBitfieldOffset = 0;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003036
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003037 if (RecFields.empty())
3038 return;
Chris Lattnerf1690852009-03-31 08:48:01 +00003039 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3040 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3041
Chris Lattnerf1690852009-03-31 08:48:01 +00003042 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003043 FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003044 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003045 if (RD) {
3046 if (Field->isBitField()) {
3047 CodeGenTypes::BitFieldInfo Info = CGM.getTypes().getBitFieldInfo(Field);
3048 FieldOffset = Layout->getElementOffset(Info.FieldNo);
3049 } else
3050 FieldOffset =
3051 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
3052 } else
Daniel Dunbare05cc982009-05-03 23:35:23 +00003053 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003054
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003055 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003056 if (!Field->getIdentifier() || Field->isBitField()) {
3057 LastFieldBitfield = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003058 LastBitfieldOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003059 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003060 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003061
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003062 LastFieldBitfield = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003063 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003064 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003065 if (FQT->isUnionType())
3066 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003067
Ted Kremenek35366a62009-07-17 17:50:17 +00003068 BuildAggrIvarRecordLayout(FQT->getAsRecordType(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003069 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003070 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003071 continue;
3072 }
Chris Lattnerf1690852009-03-31 08:48:01 +00003073
3074 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003075 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003076 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003077 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003078 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003079 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003080 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3081 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003082 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003083 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003084 FQT = CArray->getElementType();
3085 }
3086
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003087 assert(!FQT->isUnionType() &&
3088 "layout for array of unions not supported");
3089 if (FQT->isRecordType()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003090 int OldIndex = IvarsInfo.size() - 1;
3091 int OldSkIndex = SkipIvars.size() -1;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003092
Ted Kremenek35366a62009-07-17 17:50:17 +00003093 const RecordType *RT = FQT->getAsRecordType();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003094 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003095 ForStrongLayout, HasUnion);
3096
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003097 // Replicate layout information for each array element. Note that
3098 // one element is already done.
3099 uint64_t ElIx = 1;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003100 for (int FirstIndex = IvarsInfo.size() - 1,
3101 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003102 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003103 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3104 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3105 IvarsInfo[i].ivar_size));
3106 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3107 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3108 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003109 }
3110 continue;
3111 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003112 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003113 // At this point, we are done with Record/Union and array there of.
3114 // For other arrays we are down to its element type.
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003115 QualType::GCAttrTypes GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003116
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003117 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003118 if ((ForStrongLayout && GCAttr == QualType::Strong)
3119 || (!ForStrongLayout && GCAttr == QualType::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003120 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003121 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003122 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003123 MaxUnionIvarSize = UnionIvarSize;
3124 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003125 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003126 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003127 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003128 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003129 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003130 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003131 } else if ((ForStrongLayout &&
3132 (GCAttr == QualType::GCNone || GCAttr == QualType::Weak))
3133 || (!ForStrongLayout && GCAttr != QualType::Weak)) {
3134 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003135 // FIXME: Why the asymmetry? We divide by word size in bits on other
3136 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003137 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003138 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003139 MaxSkippedUnionIvarSize = UnionIvarSize;
3140 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003141 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003142 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003143 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003144 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003145 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003146 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003147 }
3148 }
3149 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003150
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003151 if (LastFieldBitfield) {
3152 // Last field was a bitfield. Must update skip info.
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003153 Expr *BitWidth = LastFieldBitfield->getBitWidth();
3154 uint64_t BitFieldSize =
Eli Friedman9a901bb2009-04-26 19:19:15 +00003155 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Daniel Dunbar487993b2009-05-03 13:32:01 +00003156 GC_IVAR skivar;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003157 skivar.ivar_bytepos = BytePos + LastBitfieldOffset;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003158 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3159 + ((BitFieldSize % ByteSizeInBits) != 0);
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003160 SkipIvars.push_back(skivar);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003161 }
3162
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003163 if (MaxField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003164 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003165 MaxUnionIvarSize));
3166 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003167 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003168 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003169}
3170
3171/// BuildIvarLayout - Builds ivar layout bitmap for the class
3172/// implementation for the __strong or __weak case.
3173/// The layout map displays which words in ivar list must be skipped
3174/// and which must be scanned by GC (see below). String is built of bytes.
3175/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3176/// of words to skip and right nibble is count of words to scan. So, each
3177/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3178/// represented by a 0x00 byte which also ends the string.
3179/// 1. when ForStrongLayout is true, following ivars are scanned:
3180/// - id, Class
3181/// - object *
3182/// - __strong anything
3183///
3184/// 2. When ForStrongLayout is false, following ivars are scanned:
3185/// - __weak anything
3186///
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003187llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003188 const ObjCImplementationDecl *OMD,
3189 bool ForStrongLayout) {
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003190 bool hasUnion = false;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003191
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003192 unsigned int WordsToScan, WordsToSkip;
Owen Andersona1cf15f2009-07-14 23:10:40 +00003193 const llvm::Type *PtrTy = VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003194 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
Owen Anderson69243822009-07-13 04:10:07 +00003195 return VMContext.getNullValue(PtrTy);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003196
Chris Lattnerf1690852009-03-31 08:48:01 +00003197 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003198 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003199 CGM.getContext().CollectObjCIvars(OI, RecFields);
Fariborz Jahanian98200742009-05-12 18:14:29 +00003200
Daniel Dunbar37153282009-05-04 04:10:48 +00003201 // Add this implementations synthesized ivars.
Fariborz Jahanian98200742009-05-12 18:14:29 +00003202 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
3203 CGM.getContext().CollectSynthesizedIvars(OI, Ivars);
3204 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3205 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3206
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003207 if (RecFields.empty())
Owen Anderson69243822009-07-13 04:10:07 +00003208 return VMContext.getNullValue(PtrTy);
Chris Lattnerf1690852009-03-31 08:48:01 +00003209
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003210 SkipIvars.clear();
3211 IvarsInfo.clear();
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00003212
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003213 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003214 if (IvarsInfo.empty())
Owen Anderson69243822009-07-13 04:10:07 +00003215 return VMContext.getNullValue(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003216
3217 // Sort on byte position in case we encounterred a union nested in
3218 // the ivar list.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003219 if (hasUnion && !IvarsInfo.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003220 std::sort(IvarsInfo.begin(), IvarsInfo.end());
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003221 if (hasUnion && !SkipIvars.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003222 std::sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003223
3224 // Build the string of skip/scan nibbles
Fariborz Jahanian8c2f2d12009-04-24 17:15:27 +00003225 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003226 unsigned int WordSize =
Duncan Sands9408c452009-05-09 07:08:47 +00003227 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003228 if (IvarsInfo[0].ivar_bytepos == 0) {
3229 WordsToSkip = 0;
3230 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003231 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003232 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3233 WordsToScan = IvarsInfo[0].ivar_size;
3234 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003235 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003236 unsigned int TailPrevGCObjC =
3237 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003238 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003239 // consecutive 'scanned' object pointers.
3240 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003241 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003242 // Skip over 'gc'able object pointer which lay over each other.
3243 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3244 continue;
3245 // Must skip over 1 or more words. We save current skip/scan values
3246 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003247 SKIP_SCAN SkScan;
3248 SkScan.skip = WordsToSkip;
3249 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003250 SkipScanIvars.push_back(SkScan);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003251
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003252 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003253 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3254 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003255 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003256 WordsToSkip = 0;
3257 WordsToScan = IvarsInfo[i].ivar_size;
3258 }
3259 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003260 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003261 SKIP_SCAN SkScan;
3262 SkScan.skip = WordsToSkip;
3263 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003264 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003265 }
3266
3267 bool BytesSkipped = false;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003268 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003269 unsigned int LastIndex = SkipIvars.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003270 int LastByteSkipped =
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003271 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
3272 LastIndex = IvarsInfo.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003273 int LastByteScanned =
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003274 IvarsInfo[LastIndex].ivar_bytepos +
3275 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003276 BytesSkipped = (LastByteSkipped > LastByteScanned);
3277 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003278 if (BytesSkipped) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003279 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003280 SKIP_SCAN SkScan;
3281 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3282 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003283 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003284 }
3285 }
3286 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3287 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003288 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003289 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003290 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3291 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3292 // 0xM0 followed by 0x0N detected.
3293 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3294 for (int j = i+1; j < SkipScan; j++)
3295 SkipScanIvars[j] = SkipScanIvars[j+1];
3296 --SkipScan;
3297 }
3298 }
3299
3300 // Generate the string.
3301 std::string BitMap;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003302 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003303 unsigned char byte;
3304 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3305 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3306 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3307 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
3308
3309 if (skip_small > 0 || skip_big > 0)
3310 BytesSkipped = true;
3311 // first skip big.
3312 for (unsigned int ix = 0; ix < skip_big; ix++)
3313 BitMap += (unsigned char)(0xf0);
3314
3315 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003316 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003317 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003318 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003319 byte |= 0xf;
3320 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003321 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003322 byte |= scan_small;
3323 scan_small = 0;
3324 }
3325 BitMap += byte;
3326 }
3327 // next scan big
3328 for (unsigned int ix = 0; ix < scan_big; ix++)
3329 BitMap += (unsigned char)(0x0f);
3330 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003331 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003332 byte = scan_small;
3333 BitMap += byte;
3334 }
3335 }
3336 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003337 unsigned char zero = 0;
3338 BitMap += zero;
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003339
3340 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
3341 printf("\n%s ivar layout for class '%s': ",
3342 ForStrongLayout ? "strong" : "weak",
3343 OMD->getClassInterface()->getNameAsCString());
3344 const unsigned char *s = (unsigned char*)BitMap.c_str();
3345 for (unsigned i = 0; i < BitMap.size(); i++)
3346 if (!(s[i] & 0xf0))
3347 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3348 else
3349 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3350 printf("\n");
3351 }
3352
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003353 // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as
3354 // final layout.
3355 if (ForStrongLayout && !BytesSkipped)
Owen Anderson69243822009-07-13 04:10:07 +00003356 return VMContext.getNullValue(PtrTy);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003357 llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Owen Anderson7db6d832009-07-28 18:33:04 +00003358 llvm::ConstantArray::get(BitMap.c_str()),
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003359 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003360 1, true);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003361 return getConstantGEP(VMContext, Entry, 0, 0);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003362}
3363
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003364llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003365 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3366
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003367 // FIXME: Avoid std::string copying.
3368 if (!Entry)
3369 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson7db6d832009-07-28 18:33:04 +00003370 llvm::ConstantArray::get(Sel.getAsString()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003371 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003372 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003373
Owen Andersona1cf15f2009-07-14 23:10:40 +00003374 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003375}
3376
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003377// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003378llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003379 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3380}
3381
3382// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003383llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003384 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
3385}
3386
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003387llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003388 std::string TypeStr;
3389 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3390
3391 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003392
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003393 if (!Entry)
3394 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson7db6d832009-07-28 18:33:04 +00003395 llvm::ConstantArray::get(TypeStr),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003396 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003397 1, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003398
Owen Andersona1cf15f2009-07-14 23:10:40 +00003399 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003400}
3401
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003402llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003403 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003404 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3405 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00003406
3407 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3408
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003409 if (!Entry)
3410 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson7db6d832009-07-28 18:33:04 +00003411 llvm::ConstantArray::get(TypeStr),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003412 "__TEXT,__cstring,cstring_literals",
3413 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00003414
Owen Andersona1cf15f2009-07-14 23:10:40 +00003415 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003416}
3417
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003418// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003419llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003420 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
3421
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003422 if (!Entry)
3423 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Owen Anderson7db6d832009-07-28 18:33:04 +00003424 llvm::ConstantArray::get(Ident->getName()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003425 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003426 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003427
Owen Andersona1cf15f2009-07-14 23:10:40 +00003428 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003429}
3430
3431// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003432// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003433llvm::Constant *
3434 CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3435 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003436 std::string TypeStr;
3437 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003438 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3439}
3440
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003441void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
3442 const ObjCContainerDecl *CD,
3443 std::string &NameOut) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00003444 NameOut = '\01';
3445 NameOut += (D->isInstanceMethod() ? '-' : '+');
Chris Lattner077bf5e2008-11-24 03:33:13 +00003446 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003447 assert (CD && "Missing container decl in GetNameForMethod");
3448 NameOut += CD->getNameAsString();
Fariborz Jahanian1e9aef32009-04-16 18:34:20 +00003449 if (const ObjCCategoryImplDecl *CID =
3450 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) {
3451 NameOut += '(';
3452 NameOut += CID->getNameAsString();
3453 NameOut+= ')';
3454 }
Chris Lattner077bf5e2008-11-24 03:33:13 +00003455 NameOut += ' ';
3456 NameOut += D->getSelector().getAsString();
3457 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003458}
3459
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003460void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003461 EmitModuleInfo();
3462
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003463 // Emit the dummy bodies for any protocols which were referenced but
3464 // never defined.
3465 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00003466 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
3467 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003468 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003469
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003470 std::vector<llvm::Constant*> Values(5);
Owen Anderson69243822009-07-13 04:10:07 +00003471 Values[0] = VMContext.getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00003472 Values[1] = GetClassName(I->first);
Owen Anderson69243822009-07-13 04:10:07 +00003473 Values[2] = VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003474 Values[3] = Values[4] =
Owen Anderson69243822009-07-13 04:10:07 +00003475 VMContext.getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00003476 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00003477 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003478 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00003479 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003480 }
3481
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003482 // Add assembler directives to add lazy undefined symbol references
3483 // for classes which are referenced but not defined. This is
3484 // important for correct linker interaction.
3485
3486 // FIXME: Uh, this isn't particularly portable.
3487 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00003488
3489 if (!CGM.getModule().getModuleInlineAsm().empty())
3490 s << "\n";
3491
Chris Lattner41f55d32009-07-28 18:25:06 +00003492 // FIXME: This produces non-determinstic output.
Chris Lattnerad64e022009-07-17 23:57:13 +00003493 for (std::set<IdentifierInfo*>::iterator I = LazySymbols.begin(),
3494 e = LazySymbols.end(); I != e; ++I) {
3495 s << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003496 }
Chris Lattnerad64e022009-07-17 23:57:13 +00003497 for (std::set<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
3498 e = DefinedSymbols.end(); I != e; ++I) {
3499 s << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
3500 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003501 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00003502
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003503 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003504}
3505
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003506CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003507 : CGObjCCommonMac(cgm),
3508 ObjCTypes(cgm)
3509{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003510 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003511 ObjCABI = 2;
3512}
3513
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003514/* *** */
3515
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003516ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Owen Andersona1cf15f2009-07-14 23:10:40 +00003517: VMContext(cgm.getLLVMContext()), CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003518{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003519 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3520 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003521
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003522 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003523 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003524 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003525 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003526 Int8PtrTy = VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003527
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003528 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Andersona1cf15f2009-07-14 23:10:40 +00003529 PtrObjectPtrTy = VMContext.getPointerTypeUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003530 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003531
Mike Stumpf5408fe2009-05-16 07:57:57 +00003532 // FIXME: It would be nice to unify this with the opaque type, so that the IR
3533 // comes out a bit cleaner.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003534 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Andersona1cf15f2009-07-14 23:10:40 +00003535 ExternalProtocolPtrTy = VMContext.getPointerTypeUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003536
3537 // I'm not sure I like this. The implicit coordination is a bit
3538 // gross. We should solve this in a reasonable fashion because this
3539 // is a pretty common task (match some runtime data structure with
3540 // an LLVM data structure).
3541
3542 // FIXME: This is leaked.
3543 // FIXME: Merge with rewriter code?
3544
3545 // struct _objc_super {
3546 // id self;
3547 // Class cls;
3548 // }
3549 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3550 SourceLocation(),
3551 &Ctx.Idents.get("_objc_super"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003552 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Douglas Gregor6ab35242009-04-09 21:40:53 +00003553 Ctx.getObjCIdType(), 0, false));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003554 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Douglas Gregor6ab35242009-04-09 21:40:53 +00003555 Ctx.getObjCClassType(), 0, false));
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003556 RD->completeDefinition(Ctx);
3557
3558 SuperCTy = Ctx.getTagDeclType(RD);
3559 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
3560
3561 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +00003562 SuperPtrTy = VMContext.getPointerTypeUnqual(SuperTy);
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003563
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003564 // struct _prop_t {
3565 // char *name;
3566 // char *attributes;
3567 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003568 PropertyTy = VMContext.getStructType(Int8PtrTy, Int8PtrTy, NULL);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003569 CGM.getModule().addTypeName("struct._prop_t",
3570 PropertyTy);
3571
3572 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003573 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003574 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003575 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003576 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003577 PropertyListTy = VMContext.getStructType(IntTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003578 IntTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00003579 VMContext.getArrayType(PropertyTy, 0),
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003580 NULL);
3581 CGM.getModule().addTypeName("struct._prop_list_t",
3582 PropertyListTy);
3583 // struct _prop_list_t *
Owen Andersona1cf15f2009-07-14 23:10:40 +00003584 PropertyListPtrTy = VMContext.getPointerTypeUnqual(PropertyListTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003585
3586 // struct _objc_method {
3587 // SEL _cmd;
3588 // char *method_type;
3589 // char *_imp;
3590 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003591 MethodTy = VMContext.getStructType(SelectorPtrTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003592 Int8PtrTy,
3593 Int8PtrTy,
3594 NULL);
3595 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003596
3597 // struct _objc_cache *
Owen Andersona1cf15f2009-07-14 23:10:40 +00003598 CacheTy = VMContext.getOpaqueType();
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003599 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003600 CachePtrTy = VMContext.getPointerTypeUnqual(CacheTy);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003601}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003602
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003603ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
3604 : ObjCCommonTypesHelper(cgm)
3605{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003606 // struct _objc_method_description {
3607 // SEL name;
3608 // char *types;
3609 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003610 MethodDescriptionTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +00003611 VMContext.getStructType(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003612 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003613 NULL);
3614 CGM.getModule().addTypeName("struct._objc_method_description",
3615 MethodDescriptionTy);
3616
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003617 // struct _objc_method_description_list {
3618 // int count;
3619 // struct _objc_method_description[1];
3620 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003621 MethodDescriptionListTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +00003622 VMContext.getStructType(IntTy,
3623 VMContext.getArrayType(MethodDescriptionTy, 0),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003624 NULL);
3625 CGM.getModule().addTypeName("struct._objc_method_description_list",
3626 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003627
3628 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003629 MethodDescriptionListPtrTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +00003630 VMContext.getPointerTypeUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003631
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003632 // Protocol description structures
3633
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003634 // struct _objc_protocol_extension {
3635 // uint32_t size; // sizeof(struct _objc_protocol_extension)
3636 // struct _objc_method_description_list *optional_instance_methods;
3637 // struct _objc_method_description_list *optional_class_methods;
3638 // struct _objc_property_list *instance_properties;
3639 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003640 ProtocolExtensionTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +00003641 VMContext.getStructType(IntTy,
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003642 MethodDescriptionListPtrTy,
3643 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003644 PropertyListPtrTy,
3645 NULL);
3646 CGM.getModule().addTypeName("struct._objc_protocol_extension",
3647 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003648
3649 // struct _objc_protocol_extension *
Owen Andersona1cf15f2009-07-14 23:10:40 +00003650 ProtocolExtensionPtrTy = VMContext.getPointerTypeUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003651
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003652 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003653
Owen Andersona1cf15f2009-07-14 23:10:40 +00003654 llvm::PATypeHolder ProtocolTyHolder = VMContext.getOpaqueType();
3655 llvm::PATypeHolder ProtocolListTyHolder = VMContext.getOpaqueType();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003656
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003657 const llvm::Type *T =
Owen Andersona1cf15f2009-07-14 23:10:40 +00003658 VMContext.getStructType(VMContext.getPointerTypeUnqual(ProtocolListTyHolder),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003659 LongTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00003660 VMContext.getArrayType(ProtocolTyHolder, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003661 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003662 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
3663
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003664 // struct _objc_protocol {
3665 // struct _objc_protocol_extension *isa;
3666 // char *protocol_name;
3667 // struct _objc_protocol **_objc_protocol_list;
3668 // struct _objc_method_description_list *instance_methods;
3669 // struct _objc_method_description_list *class_methods;
3670 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003671 T = VMContext.getStructType(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003672 Int8PtrTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00003673 VMContext.getPointerTypeUnqual(ProtocolListTyHolder),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003674 MethodDescriptionListPtrTy,
3675 MethodDescriptionListPtrTy,
3676 NULL);
3677 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
3678
3679 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
3680 CGM.getModule().addTypeName("struct._objc_protocol_list",
3681 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003682 // struct _objc_protocol_list *
Owen Andersona1cf15f2009-07-14 23:10:40 +00003683 ProtocolListPtrTy = VMContext.getPointerTypeUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003684
3685 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003686 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003687 ProtocolPtrTy = VMContext.getPointerTypeUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003688
3689 // Class description structures
3690
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003691 // struct _objc_ivar {
3692 // char *ivar_name;
3693 // char *ivar_type;
3694 // int ivar_offset;
3695 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003696 IvarTy = VMContext.getStructType(Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003697 Int8PtrTy,
3698 IntTy,
3699 NULL);
3700 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
3701
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003702 // struct _objc_ivar_list *
Owen Andersona1cf15f2009-07-14 23:10:40 +00003703 IvarListTy = VMContext.getOpaqueType();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003704 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003705 IvarListPtrTy = VMContext.getPointerTypeUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003706
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003707 // struct _objc_method_list *
Owen Andersona1cf15f2009-07-14 23:10:40 +00003708 MethodListTy = VMContext.getOpaqueType();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003709 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003710 MethodListPtrTy = VMContext.getPointerTypeUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003711
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003712 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003713 ClassExtensionTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +00003714 VMContext.getStructType(IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003715 Int8PtrTy,
3716 PropertyListPtrTy,
3717 NULL);
3718 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003719 ClassExtensionPtrTy = VMContext.getPointerTypeUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003720
Owen Andersona1cf15f2009-07-14 23:10:40 +00003721 llvm::PATypeHolder ClassTyHolder = VMContext.getOpaqueType();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003722
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003723 // struct _objc_class {
3724 // Class isa;
3725 // Class super_class;
3726 // char *name;
3727 // long version;
3728 // long info;
3729 // long instance_size;
3730 // struct _objc_ivar_list *ivars;
3731 // struct _objc_method_list *methods;
3732 // struct _objc_cache *cache;
3733 // struct _objc_protocol_list *protocols;
3734 // char *ivar_layout;
3735 // struct _objc_class_ext *ext;
3736 // };
Owen Andersona1cf15f2009-07-14 23:10:40 +00003737 T = VMContext.getStructType(VMContext.getPointerTypeUnqual(ClassTyHolder),
3738 VMContext.getPointerTypeUnqual(ClassTyHolder),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003739 Int8PtrTy,
3740 LongTy,
3741 LongTy,
3742 LongTy,
3743 IvarListPtrTy,
3744 MethodListPtrTy,
3745 CachePtrTy,
3746 ProtocolListPtrTy,
3747 Int8PtrTy,
3748 ClassExtensionPtrTy,
3749 NULL);
3750 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
3751
3752 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
3753 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003754 ClassPtrTy = VMContext.getPointerTypeUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003755
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003756 // struct _objc_category {
3757 // char *category_name;
3758 // char *class_name;
3759 // struct _objc_method_list *instance_method;
3760 // struct _objc_method_list *class_method;
3761 // uint32_t size; // sizeof(struct _objc_category)
3762 // struct _objc_property_list *instance_properties;// category's @property
3763 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003764 CategoryTy = VMContext.getStructType(Int8PtrTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003765 Int8PtrTy,
3766 MethodListPtrTy,
3767 MethodListPtrTy,
3768 ProtocolListPtrTy,
3769 IntTy,
3770 PropertyListPtrTy,
3771 NULL);
3772 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
3773
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003774 // Global metadata structures
3775
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003776 // struct _objc_symtab {
3777 // long sel_ref_cnt;
3778 // SEL *refs;
3779 // short cls_def_cnt;
3780 // short cat_def_cnt;
3781 // char *defs[cls_def_cnt + cat_def_cnt];
3782 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003783 SymtabTy = VMContext.getStructType(LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003784 SelectorPtrTy,
3785 ShortTy,
3786 ShortTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00003787 VMContext.getArrayType(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003788 NULL);
3789 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003790 SymtabPtrTy = VMContext.getPointerTypeUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003791
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003792 // struct _objc_module {
3793 // long version;
3794 // long size; // sizeof(struct _objc_module)
3795 // char *name;
3796 // struct _objc_symtab* symtab;
3797 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003798 ModuleTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +00003799 VMContext.getStructType(LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003800 LongTy,
3801 Int8PtrTy,
3802 SymtabPtrTy,
3803 NULL);
3804 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003805
Anders Carlsson2abd89c2008-08-31 04:05:03 +00003806
Mike Stumpf5408fe2009-05-16 07:57:57 +00003807 // FIXME: This is the size of the setjmp buffer and should be target
3808 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00003809 uint64_t SetJmpBufferSize = 18;
3810
3811 // Exceptions
Owen Andersona1cf15f2009-07-14 23:10:40 +00003812 const llvm::Type *StackPtrTy = VMContext.getArrayType(
3813 VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00003814
3815 ExceptionDataTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +00003816 VMContext.getStructType(VMContext.getArrayType(llvm::Type::Int32Ty,
Anders Carlsson124526b2008-09-09 10:10:21 +00003817 SetJmpBufferSize),
3818 StackPtrTy, NULL);
3819 CGM.getModule().addTypeName("struct._objc_exception_data",
3820 ExceptionDataTy);
3821
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003822}
3823
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003824ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003825: ObjCCommonTypesHelper(cgm)
3826{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003827 // struct _method_list_t {
3828 // uint32_t entsize; // sizeof(struct _objc_method)
3829 // uint32_t method_count;
3830 // struct _objc_method method_list[method_count];
3831 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003832 MethodListnfABITy = VMContext.getStructType(IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003833 IntTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00003834 VMContext.getArrayType(MethodTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003835 NULL);
3836 CGM.getModule().addTypeName("struct.__method_list_t",
3837 MethodListnfABITy);
3838 // struct method_list_t *
Owen Andersona1cf15f2009-07-14 23:10:40 +00003839 MethodListnfABIPtrTy = VMContext.getPointerTypeUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003840
3841 // struct _protocol_t {
3842 // id isa; // NULL
3843 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003844 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003845 // const struct method_list_t * const instance_methods;
3846 // const struct method_list_t * const class_methods;
3847 // const struct method_list_t *optionalInstanceMethods;
3848 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003849 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003850 // const uint32_t size; // sizeof(struct _protocol_t)
3851 // const uint32_t flags; // = 0
3852 // }
3853
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003854 // Holder for struct _protocol_list_t *
Owen Andersona1cf15f2009-07-14 23:10:40 +00003855 llvm::PATypeHolder ProtocolListTyHolder = VMContext.getOpaqueType();
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003856
Owen Andersona1cf15f2009-07-14 23:10:40 +00003857 ProtocolnfABITy = VMContext.getStructType(ObjectPtrTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003858 Int8PtrTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00003859 VMContext.getPointerTypeUnqual(
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003860 ProtocolListTyHolder),
3861 MethodListnfABIPtrTy,
3862 MethodListnfABIPtrTy,
3863 MethodListnfABIPtrTy,
3864 MethodListnfABIPtrTy,
3865 PropertyListPtrTy,
3866 IntTy,
3867 IntTy,
3868 NULL);
3869 CGM.getModule().addTypeName("struct._protocol_t",
3870 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003871
3872 // struct _protocol_t*
Owen Andersona1cf15f2009-07-14 23:10:40 +00003873 ProtocolnfABIPtrTy = VMContext.getPointerTypeUnqual(ProtocolnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003874
Fariborz Jahanianda320092009-01-29 19:24:30 +00003875 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003876 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00003877 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003878 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003879 ProtocolListnfABITy = VMContext.getStructType(LongTy,
3880 VMContext.getArrayType(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003881 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003882 NULL);
3883 CGM.getModule().addTypeName("struct._objc_protocol_list",
3884 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003885 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3886 ProtocolListnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003887
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003888 // struct _objc_protocol_list*
Owen Andersona1cf15f2009-07-14 23:10:40 +00003889 ProtocolListnfABIPtrTy = VMContext.getPointerTypeUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003890
3891 // struct _ivar_t {
3892 // unsigned long int *offset; // pointer to ivar offset location
3893 // char *name;
3894 // char *type;
3895 // uint32_t alignment;
3896 // uint32_t size;
3897 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003898 IvarnfABITy = VMContext.getStructType(VMContext.getPointerTypeUnqual(LongTy),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003899 Int8PtrTy,
3900 Int8PtrTy,
3901 IntTy,
3902 IntTy,
3903 NULL);
3904 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3905
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003906 // struct _ivar_list_t {
3907 // uint32 entsize; // sizeof(struct _ivar_t)
3908 // uint32 count;
3909 // struct _iver_t list[count];
3910 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003911 IvarListnfABITy = VMContext.getStructType(IntTy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003912 IntTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00003913 VMContext.getArrayType(
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003914 IvarnfABITy, 0),
3915 NULL);
3916 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3917
Owen Andersona1cf15f2009-07-14 23:10:40 +00003918 IvarListnfABIPtrTy = VMContext.getPointerTypeUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003919
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003920 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003921 // uint32_t const flags;
3922 // uint32_t const instanceStart;
3923 // uint32_t const instanceSize;
3924 // uint32_t const reserved; // only when building for 64bit targets
3925 // const uint8_t * const ivarLayout;
3926 // const char *const name;
3927 // const struct _method_list_t * const baseMethods;
3928 // const struct _objc_protocol_list *const baseProtocols;
3929 // const struct _ivar_list_t *const ivars;
3930 // const uint8_t * const weakIvarLayout;
3931 // const struct _prop_list_t * const properties;
3932 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003933
3934 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Andersona1cf15f2009-07-14 23:10:40 +00003935 ClassRonfABITy = VMContext.getStructType(IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003936 IntTy,
3937 IntTy,
3938 Int8PtrTy,
3939 Int8PtrTy,
3940 MethodListnfABIPtrTy,
3941 ProtocolListnfABIPtrTy,
3942 IvarListnfABIPtrTy,
3943 Int8PtrTy,
3944 PropertyListPtrTy,
3945 NULL);
3946 CGM.getModule().addTypeName("struct._class_ro_t",
3947 ClassRonfABITy);
3948
3949 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3950 std::vector<const llvm::Type*> Params;
3951 Params.push_back(ObjectPtrTy);
3952 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003953 ImpnfABITy = VMContext.getPointerTypeUnqual(
3954 VMContext.getFunctionType(ObjectPtrTy, Params, false));
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003955
3956 // struct _class_t {
3957 // struct _class_t *isa;
3958 // struct _class_t * const superclass;
3959 // void *cache;
3960 // IMP *vtable;
3961 // struct class_ro_t *ro;
3962 // }
3963
Owen Andersona1cf15f2009-07-14 23:10:40 +00003964 llvm::PATypeHolder ClassTyHolder = VMContext.getOpaqueType();
3965 ClassnfABITy =
3966 VMContext.getStructType(VMContext.getPointerTypeUnqual(ClassTyHolder),
3967 VMContext.getPointerTypeUnqual(ClassTyHolder),
3968 CachePtrTy,
3969 VMContext.getPointerTypeUnqual(ImpnfABITy),
3970 VMContext.getPointerTypeUnqual(ClassRonfABITy),
3971 NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003972 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3973
3974 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3975 ClassnfABITy);
3976
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003977 // LLVM for struct _class_t *
Owen Andersona1cf15f2009-07-14 23:10:40 +00003978 ClassnfABIPtrTy = VMContext.getPointerTypeUnqual(ClassnfABITy);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003979
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003980 // struct _category_t {
3981 // const char * const name;
3982 // struct _class_t *const cls;
3983 // const struct _method_list_t * const instance_methods;
3984 // const struct _method_list_t * const class_methods;
3985 // const struct _protocol_list_t * const protocols;
3986 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003987 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003988 CategorynfABITy = VMContext.getStructType(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003989 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003990 MethodListnfABIPtrTy,
3991 MethodListnfABIPtrTy,
3992 ProtocolListnfABIPtrTy,
3993 PropertyListPtrTy,
3994 NULL);
3995 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003996
3997 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003998 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3999 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004000
4001 // MessageRefTy - LLVM for:
4002 // struct _message_ref_t {
4003 // IMP messenger;
4004 // SEL name;
4005 // };
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004006
4007 // First the clang type for struct _message_ref_t
4008 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
4009 SourceLocation(),
4010 &Ctx.Idents.get("_message_ref_t"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004011 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Douglas Gregor6ab35242009-04-09 21:40:53 +00004012 Ctx.VoidPtrTy, 0, false));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004013 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Douglas Gregor6ab35242009-04-09 21:40:53 +00004014 Ctx.getObjCSelType(), 0, false));
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004015 RD->completeDefinition(Ctx);
4016
4017 MessageRefCTy = Ctx.getTagDeclType(RD);
4018 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4019 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004020
4021 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Andersona1cf15f2009-07-14 23:10:40 +00004022 MessageRefPtrTy = VMContext.getPointerTypeUnqual(MessageRefTy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004023
4024 // SuperMessageRefTy - LLVM for:
4025 // struct _super_message_ref_t {
4026 // SUPER_IMP messenger;
4027 // SEL name;
4028 // };
Owen Andersona1cf15f2009-07-14 23:10:40 +00004029 SuperMessageRefTy = VMContext.getStructType(ImpnfABITy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004030 SelectorPtrTy,
4031 NULL);
4032 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
4033
4034 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Owen Andersona1cf15f2009-07-14 23:10:40 +00004035 SuperMessageRefPtrTy = VMContext.getPointerTypeUnqual(SuperMessageRefTy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004036
Daniel Dunbare588b992009-03-01 04:46:24 +00004037
4038 // struct objc_typeinfo {
4039 // const void** vtable; // objc_ehtype_vtable + 2
4040 // const char* name; // c++ typeinfo string
4041 // Class cls;
4042 // };
Owen Andersona1cf15f2009-07-14 23:10:40 +00004043 EHTypeTy = VMContext.getStructType(VMContext.getPointerTypeUnqual(Int8PtrTy),
Daniel Dunbare588b992009-03-01 04:46:24 +00004044 Int8PtrTy,
4045 ClassnfABIPtrTy,
4046 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004047 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004048 EHTypePtrTy = VMContext.getPointerTypeUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004049}
4050
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004051llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
4052 FinishNonFragileABIModule();
4053
4054 return NULL;
4055}
4056
Daniel Dunbar463b8762009-05-15 21:48:48 +00004057void CGObjCNonFragileABIMac::AddModuleClassList(const
4058 std::vector<llvm::GlobalValue*>
4059 &Container,
4060 const char *SymbolName,
4061 const char *SectionName) {
4062 unsigned NumClasses = Container.size();
4063
4064 if (!NumClasses)
4065 return;
4066
4067 std::vector<llvm::Constant*> Symbols(NumClasses);
4068 for (unsigned i=0; i<NumClasses; i++)
Owen Andersona1cf15f2009-07-14 23:10:40 +00004069 Symbols[i] = VMContext.getConstantExprBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004070 ObjCTypes.Int8PtrTy);
4071 llvm::Constant* Init =
Owen Anderson7db6d832009-07-28 18:33:04 +00004072 llvm::ConstantArray::get(VMContext.getArrayType(ObjCTypes.Int8PtrTy,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004073 NumClasses),
4074 Symbols);
4075
4076 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004077 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004078 llvm::GlobalValue::InternalLinkage,
4079 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004080 SymbolName);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004081 GV->setAlignment(8);
4082 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004083 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004084}
4085
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004086void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4087 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004088
Daniel Dunbar463b8762009-05-15 21:48:48 +00004089 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004090 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar463b8762009-05-15 21:48:48 +00004091 AddModuleClassList(DefinedClasses,
4092 "\01L_OBJC_LABEL_CLASS_$",
4093 "__DATA, __objc_classlist, regular, no_dead_strip");
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004094 AddModuleClassList(DefinedNonLazyClasses,
4095 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4096 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004097
4098 // Build list of all implemented category addresses in array
4099 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar463b8762009-05-15 21:48:48 +00004100 AddModuleClassList(DefinedCategories,
4101 "\01L_OBJC_LABEL_CATEGORY_$",
4102 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004103 AddModuleClassList(DefinedNonLazyCategories,
4104 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4105 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004106
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004107 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
4108 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
4109 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004110 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
Fariborz Jahanian067986e2009-02-24 21:08:09 +00004111 unsigned int flags = 0;
Fariborz Jahanian66a5c2c2009-02-24 23:34:44 +00004112 // FIXME: Fix and continue?
4113 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
4114 flags |= eImageInfo_GarbageCollected;
4115 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
4116 flags |= eImageInfo_GCOnly;
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004117 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
Owen Anderson7db6d832009-07-28 18:33:04 +00004118 llvm::Constant* Init = llvm::ConstantArray::get(
Owen Andersona1cf15f2009-07-14 23:10:40 +00004119 VMContext.getArrayType(ObjCTypes.IntTy, 2),
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004120 Values);
4121 llvm::GlobalVariable *IMGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004122 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004123 llvm::GlobalValue::InternalLinkage,
4124 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004125 "\01L_OBJC_IMAGE_INFO");
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004126 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
Daniel Dunbar325f7582009-04-23 08:03:21 +00004127 IMGV->setConstant(true);
Chris Lattnerad64e022009-07-17 23:57:13 +00004128 CGM.AddUsedGlobal(IMGV);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004129}
4130
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004131/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004132/// NonLegacyDispatchMethods; false otherwise. What this means is that
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004133/// except for the 19 selectors in the list, we generate 32bit-style
4134/// message dispatch call for all the rest.
4135///
4136bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004137 if (NonLegacyDispatchMethods.empty()) {
4138 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4139 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4140 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4141 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4142 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4143 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4144 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4145 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4146 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4147 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
4148
4149 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4150 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4151 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4152 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4153 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4154 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4155 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4156 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004157 // "countByEnumeratingWithState:objects:count"
4158 IdentifierInfo *KeyIdents[] = {
4159 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4160 &CGM.getContext().Idents.get("objects"),
4161 &CGM.getContext().Idents.get("count")
4162 };
4163 NonLegacyDispatchMethods.insert(
4164 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004165 }
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004166 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004167}
4168
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004169// Metadata flags
4170enum MetaDataDlags {
4171 CLS = 0x0,
4172 CLS_META = 0x1,
4173 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004174 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004175 CLS_EXCEPTION = 0x20
4176};
4177/// BuildClassRoTInitializer - generate meta-data for:
4178/// struct _class_ro_t {
4179/// uint32_t const flags;
4180/// uint32_t const instanceStart;
4181/// uint32_t const instanceSize;
4182/// uint32_t const reserved; // only when building for 64bit targets
4183/// const uint8_t * const ivarLayout;
4184/// const char *const name;
4185/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004186/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004187/// const struct _ivar_list_t *const ivars;
4188/// const uint8_t * const weakIvarLayout;
4189/// const struct _prop_list_t * const properties;
4190/// }
4191///
4192llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
4193 unsigned flags,
4194 unsigned InstanceStart,
4195 unsigned InstanceSize,
4196 const ObjCImplementationDecl *ID) {
4197 std::string ClassName = ID->getNameAsString();
4198 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004199 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4200 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4201 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004202 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004203 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4204 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004205 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004206 // const struct _method_list_t * const baseMethods;
4207 std::vector<llvm::Constant*> Methods;
4208 std::string MethodListName("\01l_OBJC_$_");
4209 if (flags & CLS_META) {
4210 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004211 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004212 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004213 // Class methods should always be defined.
4214 Methods.push_back(GetMethodConstant(*i));
4215 }
4216 } else {
4217 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004218 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004219 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004220 // Instance methods should always be defined.
4221 Methods.push_back(GetMethodConstant(*i));
4222 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00004223 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004224 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004225 ObjCPropertyImplDecl *PID = *i;
4226
4227 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4228 ObjCPropertyDecl *PD = PID->getPropertyDecl();
4229
4230 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4231 if (llvm::Constant *C = GetMethodConstant(MD))
4232 Methods.push_back(C);
4233 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4234 if (llvm::Constant *C = GetMethodConstant(MD))
4235 Methods.push_back(C);
4236 }
4237 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004238 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004239 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004240 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004241
4242 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4243 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
4244 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
4245 + OID->getNameAsString(),
4246 OID->protocol_begin(),
4247 OID->protocol_end());
4248
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004249 if (flags & CLS_META)
Owen Anderson69243822009-07-13 04:10:07 +00004250 Values[ 7] = VMContext.getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004251 else
4252 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004253 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4254 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004255 if (flags & CLS_META)
Owen Anderson69243822009-07-13 04:10:07 +00004256 Values[ 9] = VMContext.getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004257 else
4258 Values[ 9] =
Chris Lattner41f55d32009-07-28 18:25:06 +00004259 EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004260 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00004261 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004262 Values);
4263 llvm::GlobalVariable *CLASS_RO_GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004264 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004265 llvm::GlobalValue::InternalLinkage,
4266 Init,
4267 (flags & CLS_META) ?
4268 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
Owen Anderson1c431b32009-07-08 19:05:04 +00004269 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004270 CLASS_RO_GV->setAlignment(
4271 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004272 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004273 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004274
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004275}
4276
4277/// BuildClassMetaData - This routine defines that to-level meta-data
4278/// for the given ClassName for:
4279/// struct _class_t {
4280/// struct _class_t *isa;
4281/// struct _class_t * const superclass;
4282/// void *cache;
4283/// IMP *vtable;
4284/// struct class_ro_t *ro;
4285/// }
4286///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004287llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
4288 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004289 llvm::Constant *IsAGV,
4290 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004291 llvm::Constant *ClassRoGV,
4292 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004293 std::vector<llvm::Constant*> Values(5);
4294 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004295 Values[1] = SuperClassGV
4296 ? SuperClassGV
Owen Anderson69243822009-07-13 04:10:07 +00004297 : VMContext.getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004298 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4299 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4300 Values[4] = ClassRoGV; // &CLASS_RO_GV
Owen Anderson08e25242009-07-27 22:29:56 +00004301 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004302 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004303 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4304 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004305 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004306 GV->setAlignment(
4307 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004308 if (HiddenVisibility)
4309 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004310 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004311}
4312
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004313bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00004314CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004315 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004316}
4317
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004318void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004319 uint32_t &InstanceStart,
4320 uint32_t &InstanceSize) {
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004321 const ASTRecordLayout &RL =
4322 CGM.getContext().getASTObjCImplementationLayout(OID);
4323
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004324 // InstanceSize is really instance end.
Anders Carlsson243a6852009-07-18 21:26:44 +00004325 InstanceSize = llvm::RoundUpToAlignment(RL.getDataSize(), 8) / 8;
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004326
4327 // If there are no fields, the start is the same as the end.
4328 if (!RL.getFieldCount())
4329 InstanceStart = InstanceSize;
4330 else
4331 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004332}
4333
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004334void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4335 std::string ClassName = ID->getNameAsString();
4336 if (!ObjCEmptyCacheVar) {
4337 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Owen Anderson1c431b32009-07-08 19:05:04 +00004338 CGM.getModule(),
Daniel Dunbar948e2582009-02-15 07:36:20 +00004339 ObjCTypes.CacheTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004340 false,
4341 llvm::GlobalValue::ExternalLinkage,
4342 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00004343 "_objc_empty_cache");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004344
4345 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Owen Anderson1c431b32009-07-08 19:05:04 +00004346 CGM.getModule(),
Daniel Dunbar948e2582009-02-15 07:36:20 +00004347 ObjCTypes.ImpnfABITy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004348 false,
4349 llvm::GlobalValue::ExternalLinkage,
4350 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00004351 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004352 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004353 assert(ID->getClassInterface() &&
4354 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004355 // FIXME: Is this correct (that meta class size is never computed)?
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004356 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00004357 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004358 uint32_t InstanceSize = InstanceStart;
4359 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004360 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4361 std::string ObjCClassName(getClassSymbolPrefix());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004362
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004363 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004364
Daniel Dunbar04d40782009-04-14 06:00:08 +00004365 bool classIsHidden =
4366 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004367 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004368 flags |= OBJC2_CLS_HIDDEN;
4369 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004370 // class is root
4371 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004372 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004373 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004374 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004375 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004376 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4377 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4378 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004379 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004380 // work on super class metadata symbol.
4381 std::string SuperClassName =
4382 ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004383 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004384 }
4385 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4386 InstanceStart,
4387 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004388 std::string TClassName = ObjCMetaClassName + ClassName;
4389 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004390 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4391 classIsHidden);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004392
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004393 // Metadata for the class
4394 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004395 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004396 flags |= OBJC2_CLS_HIDDEN;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004397
Douglas Gregor68584ed2009-06-18 16:11:24 +00004398 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004399 flags |= CLS_EXCEPTION;
4400
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004401 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004402 flags |= CLS_ROOT;
4403 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00004404 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004405 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004406 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004407 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004408 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004409 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004410 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004411 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004412 InstanceStart,
4413 InstanceSize,
4414 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004415
4416 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004417 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004418 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4419 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004420 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004421
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004422 // Determine if this class is also "non-lazy".
4423 if (ImplementationIsNonLazy(ID))
4424 DefinedNonLazyClasses.push_back(ClassMD);
4425
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004426 // Force the definition of the EHType if necessary.
4427 if (flags & CLS_EXCEPTION)
4428 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004429}
4430
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004431/// GenerateProtocolRef - This routine is called to generate code for
4432/// a protocol reference expression; as in:
4433/// @code
4434/// @protocol(Proto1);
4435/// @endcode
4436/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4437/// which will hold address of the protocol meta-data.
4438///
4439llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
4440 const ObjCProtocolDecl *PD) {
4441
Fariborz Jahanian960cd062009-04-10 18:47:34 +00004442 // This routine is called for @protocol only. So, we must build definition
4443 // of protocol's meta-data (not a reference to it!)
4444 //
Owen Andersona1cf15f2009-07-14 23:10:40 +00004445 llvm::Constant *Init =
4446 VMContext.getConstantExprBitCast(GetOrEmitProtocol(PD),
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004447 ObjCTypes.ExternalProtocolPtrTy);
4448
4449 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
4450 ProtocolName += PD->getNameAsCString();
4451
4452 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
4453 if (PTGV)
4454 return Builder.CreateLoad(PTGV, false, "tmp");
4455 PTGV = new llvm::GlobalVariable(
Owen Anderson1c431b32009-07-08 19:05:04 +00004456 CGM.getModule(),
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004457 Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00004458 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004459 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004460 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004461 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
4462 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00004463 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004464 return Builder.CreateLoad(PTGV, false, "tmp");
4465}
4466
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004467/// GenerateCategory - Build metadata for a category implementation.
4468/// struct _category_t {
4469/// const char * const name;
4470/// struct _class_t *const cls;
4471/// const struct _method_list_t * const instance_methods;
4472/// const struct _method_list_t * const class_methods;
4473/// const struct _protocol_list_t * const protocols;
4474/// const struct _prop_list_t * const properties;
4475/// }
4476///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004477void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004478 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004479 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
4480 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004481 "_$_" + OCD->getNameAsString());
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004482 std::string ExtClassName(getClassSymbolPrefix() +
4483 Interface->getNameAsString());
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004484
4485 std::vector<llvm::Constant*> Values(6);
4486 Values[0] = GetClassName(OCD->getIdentifier());
4487 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004488 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004489 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004490 std::vector<llvm::Constant*> Methods;
4491 std::string MethodListName(Prefix);
4492 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
4493 "_$_" + OCD->getNameAsString();
4494
Douglas Gregor653f1b12009-04-23 01:02:12 +00004495 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004496 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004497 // Instance methods should always be defined.
4498 Methods.push_back(GetMethodConstant(*i));
4499 }
4500
4501 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004502 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004503 Methods);
4504
4505 MethodListName = Prefix;
4506 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
4507 OCD->getNameAsString();
4508 Methods.clear();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004509 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004510 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004511 // Class methods should always be defined.
4512 Methods.push_back(GetMethodConstant(*i));
4513 }
4514
4515 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004516 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004517 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004518 const ObjCCategoryDecl *Category =
4519 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004520 if (Category) {
4521 std::string ExtName(Interface->getNameAsString() + "_$_" +
4522 OCD->getNameAsString());
4523 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
4524 + Interface->getNameAsString() + "_$_"
4525 + Category->getNameAsString(),
4526 Category->protocol_begin(),
4527 Category->protocol_end());
4528 Values[5] =
4529 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
4530 OCD, Category, ObjCTypes);
4531 }
4532 else {
Owen Anderson69243822009-07-13 04:10:07 +00004533 Values[4] = VMContext.getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4534 Values[5] = VMContext.getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004535 }
4536
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004537 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00004538 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004539 Values);
4540 llvm::GlobalVariable *GCATV
Owen Anderson1c431b32009-07-08 19:05:04 +00004541 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004542 false,
4543 llvm::GlobalValue::InternalLinkage,
4544 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004545 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004546 GCATV->setAlignment(
4547 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004548 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00004549 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004550 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004551
4552 // Determine if this category is also "non-lazy".
4553 if (ImplementationIsNonLazy(OCD))
4554 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004555}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004556
4557/// GetMethodConstant - Return a struct objc_method constant for the
4558/// given method if it has been defined. The result is null if the
4559/// method has not been defined. The return value has type MethodPtrTy.
4560llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
4561 const ObjCMethodDecl *MD) {
4562 // FIXME: Use DenseMap::lookup
4563 llvm::Function *Fn = MethodDefinitions[MD];
4564 if (!Fn)
4565 return 0;
4566
4567 std::vector<llvm::Constant*> Method(3);
4568 Method[0] =
Owen Andersona1cf15f2009-07-14 23:10:40 +00004569 VMContext.getConstantExprBitCast(GetMethodVarName(MD->getSelector()),
4570 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004571 Method[1] = GetMethodVarType(MD);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004572 Method[2] = VMContext.getConstantExprBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00004573 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004574}
4575
4576/// EmitMethodList - Build meta-data for method declarations
4577/// struct _method_list_t {
4578/// uint32_t entsize; // sizeof(struct _objc_method)
4579/// uint32_t method_count;
4580/// struct _objc_method method_list[method_count];
4581/// }
4582///
4583llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
4584 const std::string &Name,
4585 const char *Section,
4586 const ConstantVector &Methods) {
4587 // Return null for empty list.
4588 if (Methods.empty())
Owen Anderson69243822009-07-13 04:10:07 +00004589 return VMContext.getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004590
4591 std::vector<llvm::Constant*> Values(3);
4592 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00004593 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004594 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004595 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004596 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Andersona1cf15f2009-07-14 23:10:40 +00004597 llvm::ArrayType *AT = VMContext.getArrayType(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004598 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00004599 Values[2] = llvm::ConstantArray::get(AT, Methods);
Owen Anderson08e25242009-07-27 22:29:56 +00004600 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004601
4602 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004603 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004604 llvm::GlobalValue::InternalLinkage,
4605 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004606 Name);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004607 GV->setAlignment(
4608 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004609 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00004610 CGM.AddUsedGlobal(GV);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004611 return VMContext.getConstantExprBitCast(GV,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004612 ObjCTypes.MethodListnfABIPtrTy);
4613}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004614
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004615/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
4616/// the given ivar.
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004617llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004618 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004619 const ObjCIvarDecl *Ivar) {
Daniel Dunbara81419d2009-05-05 00:36:57 +00004620 // FIXME: We shouldn't need to do this lookup.
4621 unsigned Index;
4622 const ObjCInterfaceDecl *Container =
4623 FindIvarInterface(CGM.getContext(), ID, Ivar, Index);
4624 assert(Container && "Unable to find ivar container!");
4625 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00004626 '.' + Ivar->getNameAsString();
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004627 llvm::GlobalVariable *IvarOffsetGV =
4628 CGM.getModule().getGlobalVariable(Name);
4629 if (!IvarOffsetGV)
4630 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004631 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004632 false,
4633 llvm::GlobalValue::ExternalLinkage,
4634 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00004635 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004636 return IvarOffsetGV;
4637}
4638
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004639llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004640 const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004641 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004642 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00004643 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004644 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00004645 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004646 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004647 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00004648
Mike Stumpf5408fe2009-05-16 07:57:57 +00004649 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
4650 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00004651 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
4652 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
4653 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004654 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00004655 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00004656 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004657 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004658 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004659}
4660
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004661/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00004662/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004663/// IvarListnfABIPtrTy.
4664/// struct _ivar_t {
4665/// unsigned long int *offset; // pointer to ivar offset location
4666/// char *name;
4667/// char *type;
4668/// uint32_t alignment;
4669/// uint32_t size;
4670/// }
4671/// struct _ivar_list_t {
4672/// uint32 entsize; // sizeof(struct _ivar_t)
4673/// uint32 count;
4674/// struct _iver_t list[count];
4675/// }
4676///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004677
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004678llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
4679 const ObjCImplementationDecl *ID) {
4680
4681 std::vector<llvm::Constant*> Ivars, Ivar(5);
4682
4683 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4684 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
4685
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004686 // FIXME. Consolidate this with similar code in GenerateClass.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00004687
Daniel Dunbar91636d62009-04-20 00:33:43 +00004688 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00004689 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00004690 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Fariborz Jahanian99eee362009-04-01 19:37:34 +00004691
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004692 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
4693 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00004694 // Ignore unnamed bit-fields.
4695 if (!IVD->getDeclName())
4696 continue;
Daniel Dunbar3eec8aa2009-04-20 05:53:40 +00004697 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004698 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004699 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
4700 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004701 const llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004702 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00004703 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004704 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004705 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004706 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004707 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00004708 // NOTE. Size of a bitfield does not match gcc's, because of the
4709 // way bitfields are treated special in each. But I am told that
4710 // 'size' for bitfield ivars is ignored by the runtime so it does
4711 // not matter. If it matters, there is enough info to get the
4712 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004713 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00004714 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004715 }
4716 // Return null for empty list.
4717 if (Ivars.empty())
Owen Anderson69243822009-07-13 04:10:07 +00004718 return VMContext.getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004719 std::vector<llvm::Constant*> Values(3);
Duncan Sands9408c452009-05-09 07:08:47 +00004720 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004721 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4722 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Andersona1cf15f2009-07-14 23:10:40 +00004723 llvm::ArrayType *AT = VMContext.getArrayType(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004724 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00004725 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Owen Anderson08e25242009-07-27 22:29:56 +00004726 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004727 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4728 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004729 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004730 llvm::GlobalValue::InternalLinkage,
4731 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004732 Prefix + OID->getNameAsString());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004733 GV->setAlignment(
4734 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004735 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004736
Chris Lattnerad64e022009-07-17 23:57:13 +00004737 CGM.AddUsedGlobal(GV);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004738 return VMContext.getConstantExprBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004739}
4740
4741llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4742 const ObjCProtocolDecl *PD) {
4743 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4744
4745 if (!Entry) {
4746 // We use the initializer as a marker of whether this is a forward
4747 // reference or not. At module finalization we add the empty
4748 // contents for protocols which were referenced but never defined.
4749 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00004750 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004751 llvm::GlobalValue::ExternalLinkage,
4752 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00004753 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString());
Fariborz Jahanianda320092009-01-29 19:24:30 +00004754 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004755 }
4756
4757 return Entry;
4758}
4759
4760/// GetOrEmitProtocol - Generate the protocol meta-data:
4761/// @code
4762/// struct _protocol_t {
4763/// id isa; // NULL
4764/// const char * const protocol_name;
4765/// const struct _protocol_list_t * protocol_list; // super protocols
4766/// const struct method_list_t * const instance_methods;
4767/// const struct method_list_t * const class_methods;
4768/// const struct method_list_t *optionalInstanceMethods;
4769/// const struct method_list_t *optionalClassMethods;
4770/// const struct _prop_list_t * properties;
4771/// const uint32_t size; // sizeof(struct _protocol_t)
4772/// const uint32_t flags; // = 0
4773/// }
4774/// @endcode
4775///
4776
4777llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4778 const ObjCProtocolDecl *PD) {
4779 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4780
4781 // Early exit if a defining object has already been generated.
4782 if (Entry && Entry->hasInitializer())
4783 return Entry;
4784
4785 const char *ProtocolName = PD->getNameAsCString();
4786
4787 // Construct method lists.
4788 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4789 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00004790 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004791 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004792 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004793 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004794 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4795 OptInstanceMethods.push_back(C);
4796 } else {
4797 InstanceMethods.push_back(C);
4798 }
4799 }
4800
Douglas Gregor6ab35242009-04-09 21:40:53 +00004801 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004802 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004803 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004804 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004805 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4806 OptClassMethods.push_back(C);
4807 } else {
4808 ClassMethods.push_back(C);
4809 }
4810 }
4811
4812 std::vector<llvm::Constant*> Values(10);
4813 // isa is NULL
Owen Anderson69243822009-07-13 04:10:07 +00004814 Values[0] = VMContext.getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004815 Values[1] = GetClassName(PD->getIdentifier());
4816 Values[2] = EmitProtocolList(
4817 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4818 PD->protocol_begin(),
4819 PD->protocol_end());
4820
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004821 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004822 + PD->getNameAsString(),
4823 "__DATA, __objc_const",
4824 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004825 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004826 + PD->getNameAsString(),
4827 "__DATA, __objc_const",
4828 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004829 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004830 + PD->getNameAsString(),
4831 "__DATA, __objc_const",
4832 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004833 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004834 + PD->getNameAsString(),
4835 "__DATA, __objc_const",
4836 OptClassMethods);
4837 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4838 0, PD, ObjCTypes);
4839 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00004840 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004841 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson69243822009-07-13 04:10:07 +00004842 Values[9] = VMContext.getNullValue(ObjCTypes.IntTy);
Owen Anderson08e25242009-07-27 22:29:56 +00004843 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004844 Values);
4845
4846 if (Entry) {
4847 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00004848 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004849 Entry->setInitializer(Init);
4850 } else {
4851 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00004852 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004853 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004854 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004855 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004856 Entry->setAlignment(
4857 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004858 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004859 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004860 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00004861 CGM.AddUsedGlobal(Entry);
4862
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004863 // Use this protocol meta-data to build protocol list table in section
4864 // __DATA, __objc_protolist
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004865 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
Owen Anderson1c431b32009-07-08 19:05:04 +00004866 CGM.getModule(),
Daniel Dunbar948e2582009-02-15 07:36:20 +00004867 ObjCTypes.ProtocolnfABIPtrTy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004868 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004869 Entry,
4870 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
Owen Anderson1c431b32009-07-08 19:05:04 +00004871 +ProtocolName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004872 PTGV->setAlignment(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004873 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00004874 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004875 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00004876 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004877 return Entry;
4878}
4879
4880/// EmitProtocolList - Generate protocol list meta-data:
4881/// @code
4882/// struct _protocol_list_t {
4883/// long protocol_count; // Note, this is 32/64 bit
4884/// struct _protocol_t[protocol_count];
4885/// }
4886/// @endcode
4887///
4888llvm::Constant *
4889CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4890 ObjCProtocolDecl::protocol_iterator begin,
4891 ObjCProtocolDecl::protocol_iterator end) {
4892 std::vector<llvm::Constant*> ProtocolRefs;
4893
Fariborz Jahanianda320092009-01-29 19:24:30 +00004894 // Just return null for empty protocol lists
Daniel Dunbar948e2582009-02-15 07:36:20 +00004895 if (begin == end)
Owen Anderson69243822009-07-13 04:10:07 +00004896 return VMContext.getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004897
Daniel Dunbar948e2582009-02-15 07:36:20 +00004898 // FIXME: We shouldn't need to do this lookup here, should we?
Fariborz Jahanianda320092009-01-29 19:24:30 +00004899 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4900 if (GV)
Owen Andersona1cf15f2009-07-14 23:10:40 +00004901 return VMContext.getConstantExprBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00004902 ObjCTypes.ProtocolListnfABIPtrTy);
4903
4904 for (; begin != end; ++begin)
4905 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4906
Fariborz Jahanianda320092009-01-29 19:24:30 +00004907 // This list is null terminated.
Owen Anderson69243822009-07-13 04:10:07 +00004908 ProtocolRefs.push_back(VMContext.getNullValue(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004909 ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004910
4911 std::vector<llvm::Constant*> Values(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004912 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004913 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004914 Values[1] =
Owen Anderson7db6d832009-07-28 18:33:04 +00004915 llvm::ConstantArray::get(
Owen Andersona1cf15f2009-07-14 23:10:40 +00004916 VMContext.getArrayType(ObjCTypes.ProtocolnfABIPtrTy,
4917 ProtocolRefs.size()),
4918 ProtocolRefs);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004919
Owen Anderson08e25242009-07-27 22:29:56 +00004920 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Owen Anderson1c431b32009-07-08 19:05:04 +00004921 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004922 llvm::GlobalValue::InternalLinkage,
4923 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004924 Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004925 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004926 GV->setAlignment(
4927 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00004928 CGM.AddUsedGlobal(GV);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004929 return VMContext.getConstantExprBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00004930 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004931}
4932
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004933/// GetMethodDescriptionConstant - This routine build following meta-data:
4934/// struct _objc_method {
4935/// SEL _cmd;
4936/// char *method_type;
4937/// char *_imp;
4938/// }
4939
4940llvm::Constant *
4941CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4942 std::vector<llvm::Constant*> Desc(3);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004943 Desc[0] =
4944 VMContext.getConstantExprBitCast(GetMethodVarName(MD->getSelector()),
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004945 ObjCTypes.SelectorPtrTy);
4946 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004947 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson69243822009-07-13 04:10:07 +00004948 Desc[2] = VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00004949 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004950}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004951
4952/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4953/// This code gen. amounts to generating code for:
4954/// @code
4955/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4956/// @encode
4957///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004958LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004959 CodeGen::CodeGenFunction &CGF,
4960 QualType ObjectTy,
4961 llvm::Value *BaseValue,
4962 const ObjCIvarDecl *Ivar,
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004963 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00004964 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00004965 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
4966 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004967}
4968
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004969llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
4970 CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00004971 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004972 const ObjCIvarDecl *Ivar) {
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00004973 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),
4974 false, "ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004975}
4976
Fariborz Jahanian46551122009-02-04 00:22:57 +00004977CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4978 CodeGen::CodeGenFunction &CGF,
4979 QualType ResultType,
4980 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004981 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00004982 QualType Arg0Ty,
4983 bool IsSuper,
4984 const CallArgList &CallArgs) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00004985 // FIXME. Even though IsSuper is passes. This function doese not handle calls
4986 // to 'super' receivers.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004987 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004988 llvm::Value *Arg0 = Receiver;
4989 if (!IsSuper)
4990 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004991
4992 // Find the message function name.
Mike Stumpf5408fe2009-05-16 07:57:57 +00004993 // FIXME. This is too much work to get the ABI-specific result type needed to
4994 // find the message name.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004995 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4996 llvm::SmallVector<QualType, 16>());
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00004997 llvm::Constant *Fn = 0;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004998 std::string Name("\01l_");
4999 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005000#if 0
5001 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005002 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005003 Fn = ObjCTypes.getMessageSendIdStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005004 // FIXME. Is there a better way of getting these names.
5005 // They are available in RuntimeFunctions vector pair.
5006 Name += "objc_msgSendId_stret_fixup";
5007 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005008 else
5009#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005010 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005011 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005012 Name += "objc_msgSendSuper2_stret_fixup";
5013 }
5014 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005015 {
Chris Lattner1c02f862009-04-22 02:53:24 +00005016 Fn = ObjCTypes.getMessageSendStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005017 Name += "objc_msgSend_stret_fixup";
5018 }
5019 }
Fariborz Jahanian5b2bad02009-04-30 16:31:11 +00005020 else if (!IsSuper && ResultType->isFloatingType()) {
Daniel Dunbarc0183e82009-06-26 18:32:06 +00005021 if (ResultType->isSpecificBuiltinType(BuiltinType::LongDouble)) {
5022 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5023 Name += "objc_msgSend_fpret_fixup";
5024 }
5025 else {
5026 Fn = ObjCTypes.getMessageSendFixupFn();
5027 Name += "objc_msgSend_fixup";
Fariborz Jahanian5b2bad02009-04-30 16:31:11 +00005028 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005029 }
5030 else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005031#if 0
5032// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005033 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005034 Fn = ObjCTypes.getMessageSendIdFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005035 Name += "objc_msgSendId_fixup";
5036 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005037 else
5038#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005039 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005040 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005041 Name += "objc_msgSendSuper2_fixup";
5042 }
5043 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005044 {
Chris Lattner1c02f862009-04-22 02:53:24 +00005045 Fn = ObjCTypes.getMessageSendFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005046 Name += "objc_msgSend_fixup";
5047 }
5048 }
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005049 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005050 Name += '_';
5051 std::string SelName(Sel.getAsString());
5052 // Replace all ':' in selector name with '_' ouch!
5053 for(unsigned i = 0; i < SelName.size(); i++)
5054 if (SelName[i] == ':')
5055 SelName[i] = '_';
5056 Name += SelName;
5057 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5058 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005059 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005060 std::vector<llvm::Constant*> Values(2);
5061 Values[0] = Fn;
5062 Values[1] = GetMethodVarName(Sel);
Owen Anderson08e25242009-07-27 22:29:56 +00005063 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Owen Anderson1c431b32009-07-08 19:05:04 +00005064 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005065 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005066 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005067 Name);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005068 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005069 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005070 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005071 }
5072 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005073
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005074 CallArgList ActualArgs;
5075 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
5076 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
5077 ObjCTypes.MessageRefCPtrTy));
5078 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005079 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
5080 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5081 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00005082 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005083 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Andersona1cf15f2009-07-14 23:10:40 +00005084 VMContext.getPointerTypeUnqual(FTy));
Fariborz Jahanianef163782009-02-05 01:13:09 +00005085 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005086}
5087
5088/// Generate code for a message send expression in the nonfragile abi.
5089CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
5090 CodeGen::CodeGenFunction &CGF,
5091 QualType ResultType,
5092 Selector Sel,
5093 llvm::Value *Receiver,
5094 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00005095 const CallArgList &CallArgs,
5096 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005097 return LegacyDispatchedSelector(Sel)
5098 ? EmitLegacyMessageSend(CGF, ResultType, EmitSelector(CGF.Builder, Sel),
5099 Receiver, CGF.getContext().getObjCIdType(),
5100 false, CallArgs, ObjCTypes)
5101 : EmitMessageSend(CGF, ResultType, Sel,
5102 Receiver, CGF.getContext().getObjCIdType(),
5103 false, CallArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005104}
5105
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005106llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005107CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005108 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5109
Daniel Dunbardfff2302009-03-02 05:18:14 +00005110 if (!GV) {
Owen Anderson1c431b32009-07-08 19:05:04 +00005111 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
5112 false, llvm::GlobalValue::ExternalLinkage,
5113 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005114 }
5115
5116 return GV;
5117}
5118
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005119llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005120 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005121 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
5122
5123 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005124 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005125 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005126 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005127 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5128 false, llvm::GlobalValue::InternalLinkage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005129 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005130 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005131 Entry->setAlignment(
5132 CGM.getTargetData().getPrefTypeAlignment(
5133 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005134 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005135 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005136 }
5137
5138 return Builder.CreateLoad(Entry, false, "tmp");
5139}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005140
Daniel Dunbar11394522009-04-18 08:51:00 +00005141llvm::Value *
5142CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
5143 const ObjCInterfaceDecl *ID) {
5144 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
5145
5146 if (!Entry) {
5147 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5148 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5149 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005150 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5151 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar11394522009-04-18 08:51:00 +00005152 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005153 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005154 Entry->setAlignment(
5155 CGM.getTargetData().getPrefTypeAlignment(
5156 ObjCTypes.ClassnfABIPtrTy));
5157 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005158 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005159 }
5160
5161 return Builder.CreateLoad(Entry, false, "tmp");
5162}
5163
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005164/// EmitMetaClassRef - Return a Value * of the address of _class_t
5165/// meta-data
5166///
5167llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5168 const ObjCInterfaceDecl *ID) {
5169 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5170 if (Entry)
5171 return Builder.CreateLoad(Entry, false, "tmp");
5172
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005173 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005174 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005175 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005176 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005177 llvm::GlobalValue::InternalLinkage,
5178 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005179 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005180 Entry->setAlignment(
5181 CGM.getTargetData().getPrefTypeAlignment(
5182 ObjCTypes.ClassnfABIPtrTy));
5183
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005184 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005185 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005186
5187 return Builder.CreateLoad(Entry, false, "tmp");
5188}
5189
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005190/// GetClass - Return a reference to the class for the given interface
5191/// decl.
5192llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5193 const ObjCInterfaceDecl *ID) {
5194 return EmitClassRef(Builder, ID);
5195}
5196
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005197/// Generates a message send where the super is the receiver. This is
5198/// a message send to self with special delivery semantics indicating
5199/// which class's method should be called.
5200CodeGen::RValue
5201CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
5202 QualType ResultType,
5203 Selector Sel,
5204 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005205 bool isCategoryImpl,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005206 llvm::Value *Receiver,
5207 bool IsClassMessage,
5208 const CodeGen::CallArgList &CallArgs) {
5209 // ...
5210 // Create and init a super structure; this is a (receiver, class)
5211 // pair we will pass to objc_msgSendSuper.
5212 llvm::Value *ObjCSuper =
5213 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
5214
5215 llvm::Value *ReceiverAsObject =
5216 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5217 CGF.Builder.CreateStore(ReceiverAsObject,
5218 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
5219
5220 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005221 llvm::Value *Target;
5222 if (IsClassMessage) {
5223 if (isCategoryImpl) {
5224 // Message sent to "super' in a class method defined in
5225 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005226 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005227 Target = CGF.Builder.CreateStructGEP(Target, 0);
5228 Target = CGF.Builder.CreateLoad(Target);
5229 }
5230 else
5231 Target = EmitMetaClassRef(CGF.Builder, Class);
5232 }
5233 else
Daniel Dunbar11394522009-04-18 08:51:00 +00005234 Target = EmitSuperClassRef(CGF.Builder, Class);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005235
Mike Stumpf5408fe2009-05-16 07:57:57 +00005236 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5237 // ObjCTypes types.
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005238 const llvm::Type *ClassTy =
5239 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5240 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5241 CGF.Builder.CreateStore(Target,
5242 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
5243
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005244 return (LegacyDispatchedSelector(Sel))
5245 ? EmitLegacyMessageSend(CGF, ResultType,EmitSelector(CGF.Builder, Sel),
5246 ObjCSuper, ObjCTypes.SuperPtrCTy,
5247 true, CallArgs,
5248 ObjCTypes)
5249 : EmitMessageSend(CGF, ResultType, Sel,
5250 ObjCSuper, ObjCTypes.SuperPtrCTy,
5251 true, CallArgs);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005252}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005253
5254llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
5255 Selector Sel) {
5256 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
5257
5258 if (!Entry) {
5259 llvm::Constant *Casted =
Owen Andersona1cf15f2009-07-14 23:10:40 +00005260 VMContext.getConstantExprBitCast(GetMethodVarName(Sel),
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005261 ObjCTypes.SelectorPtrTy);
5262 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005263 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005264 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00005265 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005266 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005267 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005268 }
5269
5270 return Builder.CreateLoad(Entry, false, "tmp");
5271}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005272/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
5273/// objc_assign_ivar (id src, id *dst)
5274///
5275void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
5276 llvm::Value *src, llvm::Value *dst)
5277{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005278 const llvm::Type * SrcTy = src->getType();
5279 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005280 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005281 assert(Size <= 8 && "does not support size > 8");
5282 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5283 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005284 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5285 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005286 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5287 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005288 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005289 src, dst, "assignivar");
5290 return;
5291}
5292
5293/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5294/// objc_assign_strongCast (id src, id *dst)
5295///
5296void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
5297 CodeGen::CodeGenFunction &CGF,
5298 llvm::Value *src, llvm::Value *dst)
5299{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005300 const llvm::Type * SrcTy = src->getType();
5301 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005302 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005303 assert(Size <= 8 && "does not support size > 8");
5304 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5305 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005306 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5307 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005308 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5309 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005310 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005311 src, dst, "weakassign");
5312 return;
5313}
5314
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005315void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
5316 CodeGen::CodeGenFunction &CGF,
5317 llvm::Value *DestPtr,
5318 llvm::Value *SrcPtr,
5319 unsigned long size) {
5320 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5321 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005322 llvm::Value *N = llvm::ConstantInt::get(ObjCTypes.LongTy, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005323 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
5324 DestPtr, SrcPtr, N);
5325 return;
5326}
5327
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005328/// EmitObjCWeakRead - Code gen for loading value of a __weak
5329/// object: objc_read_weak (id *src)
5330///
5331llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
5332 CodeGen::CodeGenFunction &CGF,
5333 llvm::Value *AddrWeakObj)
5334{
Eli Friedman8339b352009-03-07 03:57:15 +00005335 const llvm::Type* DestTy =
5336 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005337 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005338 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005339 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005340 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005341 return read_weak;
5342}
5343
5344/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5345/// objc_assign_weak (id src, id *dst)
5346///
5347void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
5348 llvm::Value *src, llvm::Value *dst)
5349{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005350 const llvm::Type * SrcTy = src->getType();
5351 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005352 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005353 assert(Size <= 8 && "does not support size > 8");
5354 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5355 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005356 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5357 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005358 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5359 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005360 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005361 src, dst, "weakassign");
5362 return;
5363}
5364
5365/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5366/// objc_assign_global (id src, id *dst)
5367///
5368void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
5369 llvm::Value *src, llvm::Value *dst)
5370{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005371 const llvm::Type * SrcTy = src->getType();
5372 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005373 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005374 assert(Size <= 8 && "does not support size > 8");
5375 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5376 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005377 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5378 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005379 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5380 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005381 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005382 src, dst, "globalassign");
5383 return;
5384}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005385
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005386void
5387CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5388 const Stmt &S) {
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005389 bool isTry = isa<ObjCAtTryStmt>(S);
5390 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
5391 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005392 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005393 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005394 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005395 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
5396
5397 // For @synchronized, call objc_sync_enter(sync.expr). The
5398 // evaluation of the expression must occur before we enter the
5399 // @synchronized. We can safely avoid a temp here because jumps into
5400 // @synchronized are illegal & this will dominate uses.
5401 llvm::Value *SyncArg = 0;
5402 if (!isTry) {
5403 SyncArg =
5404 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
5405 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005406 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005407 }
5408
5409 // Push an EH context entry, used for handling rethrows and jumps
5410 // through finally.
5411 CGF.PushCleanupBlock(FinallyBlock);
5412
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005413 CGF.setInvokeDest(TryHandler);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005414
5415 CGF.EmitBlock(TryBlock);
5416 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
5417 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
5418 CGF.EmitBranchThroughCleanup(FinallyEnd);
5419
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005420 // Emit the exception handler.
5421
5422 CGF.EmitBlock(TryHandler);
5423
5424 llvm::Value *llvm_eh_exception =
5425 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
5426 llvm::Value *llvm_eh_selector_i64 =
5427 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64);
5428 llvm::Value *llvm_eh_typeid_for_i64 =
5429 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64);
5430 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5431 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow");
5432
5433 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
5434 SelectorArgs.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005435 SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005436
5437 // Construct the lists of (type, catch body) to handle.
Daniel Dunbarede8de92009-03-06 00:01:21 +00005438 llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005439 bool HasCatchAll = false;
5440 if (isTry) {
5441 if (const ObjCAtCatchStmt* CatchStmt =
5442 cast<ObjCAtTryStmt>(S).getCatchStmts()) {
5443 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005444 const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Steve Naroff7ba138a2009-03-03 19:52:17 +00005445 Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody()));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005446
5447 // catch(...) always matches.
Steve Naroff7ba138a2009-03-03 19:52:17 +00005448 if (!CatchDecl) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005449 // Use i8* null here to signal this is a catch all, not a cleanup.
Owen Anderson69243822009-07-13 04:10:07 +00005450 llvm::Value *Null = VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005451 SelectorArgs.push_back(Null);
5452 HasCatchAll = true;
5453 break;
5454 }
5455
Steve Naroff14108da2009-07-10 23:34:53 +00005456 if (CatchDecl->getType()->isObjCIdType() ||
Daniel Dunbarede8de92009-03-06 00:01:21 +00005457 CatchDecl->getType()->isObjCQualifiedIdType()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005458 llvm::Value *IDEHType =
5459 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5460 if (!IDEHType)
5461 IDEHType =
Owen Anderson1c431b32009-07-08 19:05:04 +00005462 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
5463 false,
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005464 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00005465 0, "OBJC_EHTYPE_id");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005466 SelectorArgs.push_back(IDEHType);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005467 }
Fariborz Jahanian99438a72009-07-27 23:12:41 +00005468 else {
5469 // All other types should be Objective-C interface pointer types.
5470 const ObjCObjectPointerType *PT =
5471 CatchDecl->getType()->getAsObjCObjectPointerType();
5472 assert(PT && "Invalid @catch type.");
5473 const ObjCInterfaceType *IT = PT->getInterfaceType();
5474 assert(IT && "Invalid @catch type.");
5475 llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false);
5476 SelectorArgs.push_back(EHType);
5477 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005478 }
5479 }
5480 }
5481
5482 // We use a cleanup unless there was already a catch all.
5483 if (!HasCatchAll) {
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005484 SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
Daniel Dunbarede8de92009-03-06 00:01:21 +00005485 Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005486 }
5487
5488 llvm::Value *Selector =
5489 CGF.Builder.CreateCall(llvm_eh_selector_i64,
5490 SelectorArgs.begin(), SelectorArgs.end(),
5491 "selector");
5492 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005493 const ParmVarDecl *CatchParam = Handlers[i].first;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005494 const Stmt *CatchBody = Handlers[i].second;
5495
5496 llvm::BasicBlock *Next = 0;
5497
5498 // The last handler always matches.
5499 if (i + 1 != e) {
5500 assert(CatchParam && "Only last handler can be a catch all.");
5501
5502 llvm::BasicBlock *Match = CGF.createBasicBlock("match");
5503 Next = CGF.createBasicBlock("catch.next");
5504 llvm::Value *Id =
5505 CGF.Builder.CreateCall(llvm_eh_typeid_for_i64,
5506 CGF.Builder.CreateBitCast(SelectorArgs[i+2],
5507 ObjCTypes.Int8PtrTy));
5508 CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id),
5509 Match, Next);
5510
5511 CGF.EmitBlock(Match);
5512 }
5513
5514 if (CatchBody) {
5515 llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end");
5516 llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler");
5517
5518 // Cleanups must call objc_end_catch.
5519 //
Mike Stumpf5408fe2009-05-16 07:57:57 +00005520 // FIXME: It seems incorrect for objc_begin_catch to be inside this
5521 // context, but this matches gcc.
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005522 CGF.PushCleanupBlock(MatchEnd);
5523 CGF.setInvokeDest(MatchHandler);
5524
5525 llvm::Value *ExcObject =
Chris Lattner8a569112009-04-22 02:15:23 +00005526 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005527
5528 // Bind the catch parameter if it exists.
5529 if (CatchParam) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005530 ExcObject =
5531 CGF.Builder.CreateBitCast(ExcObject,
5532 CGF.ConvertType(CatchParam->getType()));
5533 // CatchParam is a ParmVarDecl because of the grammar
5534 // construction used to handle this, but for codegen purposes
5535 // we treat this as a local decl.
5536 CGF.EmitLocalBlockVarDecl(*CatchParam);
5537 CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005538 }
5539
5540 CGF.ObjCEHValueStack.push_back(ExcObject);
5541 CGF.EmitStmt(CatchBody);
5542 CGF.ObjCEHValueStack.pop_back();
5543
5544 CGF.EmitBranchThroughCleanup(FinallyEnd);
5545
5546 CGF.EmitBlock(MatchHandler);
5547
5548 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5549 // We are required to emit this call to satisfy LLVM, even
5550 // though we don't use the result.
5551 llvm::SmallVector<llvm::Value*, 8> Args;
5552 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005553 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005554 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005555 0));
5556 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5557 CGF.Builder.CreateStore(Exc, RethrowPtr);
5558 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5559
5560 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5561
5562 CGF.EmitBlock(MatchEnd);
5563
5564 // Unfortunately, we also have to generate another EH frame here
5565 // in case this throws.
5566 llvm::BasicBlock *MatchEndHandler =
5567 CGF.createBasicBlock("match.end.handler");
5568 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattner8a569112009-04-22 02:15:23 +00005569 CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005570 Cont, MatchEndHandler,
5571 Args.begin(), Args.begin());
5572
5573 CGF.EmitBlock(Cont);
5574 if (Info.SwitchBlock)
5575 CGF.EmitBlock(Info.SwitchBlock);
5576 if (Info.EndBlock)
5577 CGF.EmitBlock(Info.EndBlock);
5578
5579 CGF.EmitBlock(MatchEndHandler);
5580 Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5581 // We are required to emit this call to satisfy LLVM, even
5582 // though we don't use the result.
5583 Args.clear();
5584 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005585 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005586 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005587 0));
5588 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5589 CGF.Builder.CreateStore(Exc, RethrowPtr);
5590 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5591
5592 if (Next)
5593 CGF.EmitBlock(Next);
5594 } else {
5595 assert(!Next && "catchup should be last handler.");
5596
5597 CGF.Builder.CreateStore(Exc, RethrowPtr);
5598 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5599 }
5600 }
5601
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005602 // Pop the cleanup entry, the @finally is outside this cleanup
5603 // scope.
5604 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5605 CGF.setInvokeDest(PrevLandingPad);
5606
5607 CGF.EmitBlock(FinallyBlock);
5608
5609 if (isTry) {
5610 if (const ObjCAtFinallyStmt* FinallyStmt =
5611 cast<ObjCAtTryStmt>(S).getFinallyStmt())
5612 CGF.EmitStmt(FinallyStmt->getFinallyBody());
5613 } else {
5614 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
5615 // @synchronized.
Chris Lattnerbbccd612009-04-22 02:38:11 +00005616 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005617 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005618
5619 if (Info.SwitchBlock)
5620 CGF.EmitBlock(Info.SwitchBlock);
5621 if (Info.EndBlock)
5622 CGF.EmitBlock(Info.EndBlock);
5623
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005624 // Branch around the rethrow code.
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005625 CGF.EmitBranch(FinallyEnd);
5626
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005627 CGF.EmitBlock(FinallyRethrow);
Chris Lattner8a569112009-04-22 02:15:23 +00005628 CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005629 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005630 CGF.Builder.CreateUnreachable();
5631
5632 CGF.EmitBlock(FinallyEnd);
5633}
5634
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005635/// EmitThrowStmt - Generate code for a throw statement.
5636void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
5637 const ObjCAtThrowStmt &S) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005638 llvm::Value *Exception;
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005639 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005640 Exception = CGF.EmitScalarExpr(ThrowExpr);
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005641 } else {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005642 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
5643 "Unexpected rethrow outside @catch block.");
5644 Exception = CGF.ObjCEHValueStack.back();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005645 }
5646
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005647 llvm::Value *ExceptionAsObject =
5648 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
5649 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
5650 if (InvokeDest) {
5651 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattnerbbccd612009-04-22 02:38:11 +00005652 CGF.Builder.CreateInvoke(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005653 Cont, InvokeDest,
5654 &ExceptionAsObject, &ExceptionAsObject + 1);
5655 CGF.EmitBlock(Cont);
5656 } else
Chris Lattnerbbccd612009-04-22 02:38:11 +00005657 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005658 CGF.Builder.CreateUnreachable();
5659
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005660 // Clear the insertion point to indicate we are in unreachable code.
5661 CGF.Builder.ClearInsertionPoint();
5662}
Daniel Dunbare588b992009-03-01 04:46:24 +00005663
5664llvm::Value *
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005665CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
5666 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00005667 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00005668
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005669 // If we don't need a definition, return the entry if found or check
5670 // if we use an external reference.
5671 if (!ForDefinition) {
5672 if (Entry)
5673 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00005674
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005675 // If this type (or a super class) has the __objc_exception__
5676 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00005677 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005678 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005679 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005680 llvm::GlobalValue::ExternalLinkage,
5681 0,
5682 (std::string("OBJC_EHTYPE_$_") +
Owen Anderson1c431b32009-07-08 19:05:04 +00005683 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005684 }
5685
5686 // Otherwise we need to either make a new entry or fill in the
5687 // initializer.
5688 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005689 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00005690 std::string VTableName = "objc_ehtype_vtable";
5691 llvm::GlobalVariable *VTableGV =
5692 CGM.getModule().getGlobalVariable(VTableName);
5693 if (!VTableGV)
Owen Anderson1c431b32009-07-08 19:05:04 +00005694 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
5695 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00005696 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00005697 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00005698
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005699 llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00005700
5701 std::vector<llvm::Constant*> Values(3);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005702 Values[0] = VMContext.getConstantExprGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbare588b992009-03-01 04:46:24 +00005703 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005704 Values[2] = GetClassGlobal(ClassName);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005705 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00005706 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00005707
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005708 if (Entry) {
5709 Entry->setInitializer(Init);
5710 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00005711 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005712 llvm::GlobalValue::WeakAnyLinkage,
5713 Init,
5714 (std::string("OBJC_EHTYPE_$_") +
Owen Anderson1c431b32009-07-08 19:05:04 +00005715 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005716 }
5717
Daniel Dunbar04d40782009-04-14 06:00:08 +00005718 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005719 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005720 Entry->setAlignment(8);
5721
5722 if (ForDefinition) {
5723 Entry->setSection("__DATA,__objc_const");
5724 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
5725 } else {
5726 Entry->setSection("__DATA,__datacoal_nt,coalesced");
5727 }
Daniel Dunbare588b992009-03-01 04:46:24 +00005728
5729 return Entry;
5730}
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005731
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005732/* *** */
5733
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005734CodeGen::CGObjCRuntime *
5735CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00005736 return new CGObjCMac(CGM);
5737}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005738
5739CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005740CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005741 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005742}