blob: 70eb6a70e15f5863c767b0e4fd82cdf597947ac5 [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"
Anders Carlsson3d598a52009-07-14 17:29:11 +000019#include "clang/AST/ASTRecordLayout.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000021#include "clang/AST/DeclObjC.h"
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;
328 QualType IdType = Ctx.getObjCIdType();
329 Params.push_back(IdType);
330 const llvm::FunctionType *FTy =
331 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000332 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
333 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000334
335 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000336 llvm::Constant *getGcReadWeakFn() {
337 // id objc_read_weak (id *)
338 std::vector<const llvm::Type*> Args;
339 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000340 llvm::FunctionType *FTy =
341 VMContext.getFunctionType(ObjectPtrTy, Args, false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000342 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
343 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000344
345 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000346 llvm::Constant *getGcAssignWeakFn() {
347 // id objc_assign_weak (id, id *)
348 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
349 Args.push_back(ObjectPtrTy->getPointerTo());
350 llvm::FunctionType *FTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +0000351 VMContext.getFunctionType(ObjectPtrTy, Args, false);
Chris Lattner96508e12009-04-17 22:12:36 +0000352 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
353 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000354
355 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000356 llvm::Constant *getGcAssignGlobalFn() {
357 // id objc_assign_global(id, id *)
358 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
359 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000360 llvm::FunctionType *FTy =
361 VMContext.getFunctionType(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000362 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
363 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000364
365 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000366 llvm::Constant *getGcAssignIvarFn() {
367 // id objc_assign_ivar(id, id *)
368 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
369 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000370 llvm::FunctionType *FTy =
371 VMContext.getFunctionType(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000372 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
373 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000374
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000375 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
376 llvm::Constant *GcMemmoveCollectableFn() {
377 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
378 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
379 Args.push_back(Int8PtrTy);
380 Args.push_back(LongTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000381 llvm::FunctionType *FTy = VMContext.getFunctionType(Int8PtrTy, Args, false);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000382 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
383 }
384
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000385 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000386 llvm::Constant *getGcAssignStrongCastFn() {
387 // id objc_assign_global(id, id *)
388 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
389 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000390 llvm::FunctionType *FTy =
391 VMContext.getFunctionType(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000392 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
393 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000394
395 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000396 llvm::Constant *getExceptionThrowFn() {
397 // void objc_exception_throw(id)
398 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
399 llvm::FunctionType *FTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +0000400 VMContext.getFunctionType(llvm::Type::VoidTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000401 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
402 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000403
Daniel Dunbar1c566672009-02-24 01:43:46 +0000404 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000405 llvm::Constant *getSyncEnterFn() {
406 // void objc_sync_enter (id)
407 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
408 llvm::FunctionType *FTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +0000409 VMContext.getFunctionType(llvm::Type::VoidTy, Args, false);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000410 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
411 }
Daniel Dunbar1c566672009-02-24 01:43:46 +0000412
413 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000414 llvm::Constant *getSyncExitFn() {
415 // void objc_sync_exit (id)
416 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
417 llvm::FunctionType *FTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +0000418 VMContext.getFunctionType(llvm::Type::VoidTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000419 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
420 }
Daniel Dunbar1c566672009-02-24 01:43:46 +0000421
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000422 llvm::Constant *getSendFn(bool IsSuper) const {
423 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
424 }
425
426 llvm::Constant *getSendFn2(bool IsSuper) const {
427 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
428 }
429
430 llvm::Constant *getSendStretFn(bool IsSuper) const {
431 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
432 }
433
434 llvm::Constant *getSendStretFn2(bool IsSuper) const {
435 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
436 }
437
438 llvm::Constant *getSendFpretFn(bool IsSuper) const {
439 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
440 }
441
442 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
443 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
444 }
445
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000446 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
447 ~ObjCCommonTypesHelper(){}
448};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000449
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000450/// ObjCTypesHelper - Helper class that encapsulates lazy
451/// construction of varies types used during ObjC generation.
452class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000453public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000454 /// SymtabTy - LLVM type for struct objc_symtab.
455 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000456 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
457 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000458 /// ModuleTy - LLVM type for struct objc_module.
459 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000460
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000461 /// ProtocolTy - LLVM type for struct objc_protocol.
462 const llvm::StructType *ProtocolTy;
463 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
464 const llvm::Type *ProtocolPtrTy;
465 /// ProtocolExtensionTy - LLVM type for struct
466 /// objc_protocol_extension.
467 const llvm::StructType *ProtocolExtensionTy;
468 /// ProtocolExtensionTy - LLVM type for struct
469 /// objc_protocol_extension *.
470 const llvm::Type *ProtocolExtensionPtrTy;
471 /// MethodDescriptionTy - LLVM type for struct
472 /// objc_method_description.
473 const llvm::StructType *MethodDescriptionTy;
474 /// MethodDescriptionListTy - LLVM type for struct
475 /// objc_method_description_list.
476 const llvm::StructType *MethodDescriptionListTy;
477 /// MethodDescriptionListPtrTy - LLVM type for struct
478 /// objc_method_description_list *.
479 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000480 /// ProtocolListTy - LLVM type for struct objc_property_list.
481 const llvm::Type *ProtocolListTy;
482 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
483 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000484 /// CategoryTy - LLVM type for struct objc_category.
485 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000486 /// ClassTy - LLVM type for struct objc_class.
487 const llvm::StructType *ClassTy;
488 /// ClassPtrTy - LLVM type for struct objc_class *.
489 const llvm::Type *ClassPtrTy;
490 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
491 const llvm::StructType *ClassExtensionTy;
492 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
493 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000494 // IvarTy - LLVM type for struct objc_ivar.
495 const llvm::StructType *IvarTy;
496 /// IvarListTy - LLVM type for struct objc_ivar_list.
497 const llvm::Type *IvarListTy;
498 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
499 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000500 /// MethodListTy - LLVM type for struct objc_method_list.
501 const llvm::Type *MethodListTy;
502 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
503 const llvm::Type *MethodListPtrTy;
Anders Carlsson124526b2008-09-09 10:10:21 +0000504
505 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
506 const llvm::Type *ExceptionDataTy;
507
Anders Carlsson124526b2008-09-09 10:10:21 +0000508 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000509 llvm::Constant *getExceptionTryEnterFn() {
510 std::vector<const llvm::Type*> Params;
Owen Andersona1cf15f2009-07-14 23:10:40 +0000511 Params.push_back(VMContext.getPointerTypeUnqual(ExceptionDataTy));
512 return CGM.CreateRuntimeFunction(
513 VMContext.getFunctionType(llvm::Type::VoidTy,
Chris Lattner34b02a12009-04-22 02:26:14 +0000514 Params, false),
515 "objc_exception_try_enter");
516 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000517
518 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000519 llvm::Constant *getExceptionTryExitFn() {
520 std::vector<const llvm::Type*> Params;
Owen Andersona1cf15f2009-07-14 23:10:40 +0000521 Params.push_back(VMContext.getPointerTypeUnqual(ExceptionDataTy));
522 return CGM.CreateRuntimeFunction(
523 VMContext.getFunctionType(llvm::Type::VoidTy,
Chris Lattner34b02a12009-04-22 02:26:14 +0000524 Params, false),
525 "objc_exception_try_exit");
526 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000527
528 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000529 llvm::Constant *getExceptionExtractFn() {
530 std::vector<const llvm::Type*> Params;
Owen Andersona1cf15f2009-07-14 23:10:40 +0000531 Params.push_back(VMContext.getPointerTypeUnqual(ExceptionDataTy));
532 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Chris Lattner34b02a12009-04-22 02:26:14 +0000533 Params, false),
534 "objc_exception_extract");
535
536 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000537
538 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000539 llvm::Constant *getExceptionMatchFn() {
540 std::vector<const llvm::Type*> Params;
541 Params.push_back(ClassPtrTy);
542 Params.push_back(ObjectPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000543 return CGM.CreateRuntimeFunction(
544 VMContext.getFunctionType(llvm::Type::Int32Ty,
Chris Lattner34b02a12009-04-22 02:26:14 +0000545 Params, false),
546 "objc_exception_match");
547
548 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000549
550 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000551 llvm::Constant *getSetJmpFn() {
552 std::vector<const llvm::Type*> Params;
Owen Andersona1cf15f2009-07-14 23:10:40 +0000553 Params.push_back(VMContext.getPointerTypeUnqual(llvm::Type::Int32Ty));
Chris Lattner34b02a12009-04-22 02:26:14 +0000554 return
Owen Andersona1cf15f2009-07-14 23:10:40 +0000555 CGM.CreateRuntimeFunction(VMContext.getFunctionType(llvm::Type::Int32Ty,
Chris Lattner34b02a12009-04-22 02:26:14 +0000556 Params, false),
557 "_setjmp");
558
559 }
Chris Lattner10cac6f2008-11-15 21:26:17 +0000560
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000561public:
562 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000563 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000564};
565
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000566/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000567/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000568class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000569public:
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000570
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000571 // MethodListnfABITy - LLVM for struct _method_list_t
572 const llvm::StructType *MethodListnfABITy;
573
574 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
575 const llvm::Type *MethodListnfABIPtrTy;
576
577 // ProtocolnfABITy = LLVM for struct _protocol_t
578 const llvm::StructType *ProtocolnfABITy;
579
Daniel Dunbar948e2582009-02-15 07:36:20 +0000580 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
581 const llvm::Type *ProtocolnfABIPtrTy;
582
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000583 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
584 const llvm::StructType *ProtocolListnfABITy;
585
586 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
587 const llvm::Type *ProtocolListnfABIPtrTy;
588
589 // ClassnfABITy - LLVM for struct _class_t
590 const llvm::StructType *ClassnfABITy;
591
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000592 // ClassnfABIPtrTy - LLVM for struct _class_t*
593 const llvm::Type *ClassnfABIPtrTy;
594
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000595 // IvarnfABITy - LLVM for struct _ivar_t
596 const llvm::StructType *IvarnfABITy;
597
598 // IvarListnfABITy - LLVM for struct _ivar_list_t
599 const llvm::StructType *IvarListnfABITy;
600
601 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
602 const llvm::Type *IvarListnfABIPtrTy;
603
604 // ClassRonfABITy - LLVM for struct _class_ro_t
605 const llvm::StructType *ClassRonfABITy;
606
607 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
608 const llvm::Type *ImpnfABITy;
609
610 // CategorynfABITy - LLVM for struct _category_t
611 const llvm::StructType *CategorynfABITy;
612
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000613 // New types for nonfragile abi messaging.
614
615 // MessageRefTy - LLVM for:
616 // struct _message_ref_t {
617 // IMP messenger;
618 // SEL name;
619 // };
620 const llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000621 // MessageRefCTy - clang type for struct _message_ref_t
622 QualType MessageRefCTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000623
624 // MessageRefPtrTy - LLVM for struct _message_ref_t*
625 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000626 // MessageRefCPtrTy - clang type for struct _message_ref_t*
627 QualType MessageRefCPtrTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000628
Fariborz Jahanianef163782009-02-05 01:13:09 +0000629 // MessengerTy - Type of the messenger (shown as IMP above)
630 const llvm::FunctionType *MessengerTy;
631
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000632 // SuperMessageRefTy - LLVM for:
633 // struct _super_message_ref_t {
634 // SUPER_IMP messenger;
635 // SEL name;
636 // };
637 const llvm::StructType *SuperMessageRefTy;
638
639 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
640 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000641
Chris Lattner1c02f862009-04-22 02:53:24 +0000642 llvm::Constant *getMessageSendFixupFn() {
643 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
644 std::vector<const llvm::Type*> Params;
645 Params.push_back(ObjectPtrTy);
646 Params.push_back(MessageRefPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000647 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000648 Params, true),
649 "objc_msgSend_fixup");
650 }
651
652 llvm::Constant *getMessageSendFpretFixupFn() {
653 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
654 std::vector<const llvm::Type*> Params;
655 Params.push_back(ObjectPtrTy);
656 Params.push_back(MessageRefPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000657 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000658 Params, true),
659 "objc_msgSend_fpret_fixup");
660 }
661
662 llvm::Constant *getMessageSendStretFixupFn() {
663 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
664 std::vector<const llvm::Type*> Params;
665 Params.push_back(ObjectPtrTy);
666 Params.push_back(MessageRefPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000667 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000668 Params, true),
669 "objc_msgSend_stret_fixup");
670 }
671
672 llvm::Constant *getMessageSendIdFixupFn() {
673 // id objc_msgSendId_fixup(id, struct message_ref_t*, ...)
674 std::vector<const llvm::Type*> Params;
675 Params.push_back(ObjectPtrTy);
676 Params.push_back(MessageRefPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000677 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000678 Params, true),
679 "objc_msgSendId_fixup");
680 }
681
682 llvm::Constant *getMessageSendIdStretFixupFn() {
683 // id objc_msgSendId_stret_fixup(id, struct message_ref_t*, ...)
684 std::vector<const llvm::Type*> Params;
685 Params.push_back(ObjectPtrTy);
686 Params.push_back(MessageRefPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000687 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000688 Params, true),
689 "objc_msgSendId_stret_fixup");
690 }
691 llvm::Constant *getMessageSendSuper2FixupFn() {
692 // id objc_msgSendSuper2_fixup (struct objc_super *,
693 // struct _super_message_ref_t*, ...)
694 std::vector<const llvm::Type*> Params;
695 Params.push_back(SuperPtrTy);
696 Params.push_back(SuperMessageRefPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000697 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000698 Params, true),
699 "objc_msgSendSuper2_fixup");
700 }
701
702 llvm::Constant *getMessageSendSuper2StretFixupFn() {
703 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
704 // struct _super_message_ref_t*, ...)
705 std::vector<const llvm::Type*> Params;
706 Params.push_back(SuperPtrTy);
707 Params.push_back(SuperMessageRefPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000708 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000709 Params, true),
710 "objc_msgSendSuper2_stret_fixup");
711 }
712
713
714
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000715 /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C
716 /// exception personality function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000717 llvm::Value *getEHPersonalityPtr() {
718 llvm::Constant *Personality =
Owen Andersona1cf15f2009-07-14 23:10:40 +0000719 CGM.CreateRuntimeFunction(VMContext.getFunctionType(llvm::Type::Int32Ty,
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000720 true),
721 "__objc_personality_v0");
Owen Andersona1cf15f2009-07-14 23:10:40 +0000722 return VMContext.getConstantExprBitCast(Personality, Int8PtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000723 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000724
Chris Lattner8a569112009-04-22 02:15:23 +0000725 llvm::Constant *getUnwindResumeOrRethrowFn() {
726 std::vector<const llvm::Type*> Params;
727 Params.push_back(Int8PtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000728 return CGM.CreateRuntimeFunction(
729 VMContext.getFunctionType(llvm::Type::VoidTy,
Chris Lattner8a569112009-04-22 02:15:23 +0000730 Params, false),
731 "_Unwind_Resume_or_Rethrow");
732 }
733
734 llvm::Constant *getObjCEndCatchFn() {
Owen Andersona1cf15f2009-07-14 23:10:40 +0000735 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(llvm::Type::VoidTy,
Chris Lattnerb59761b2009-07-01 04:13:52 +0000736 false),
Chris Lattner8a569112009-04-22 02:15:23 +0000737 "objc_end_catch");
738
739 }
740
741 llvm::Constant *getObjCBeginCatchFn() {
742 std::vector<const llvm::Type*> Params;
743 Params.push_back(Int8PtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000744 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(Int8PtrTy,
Chris Lattner8a569112009-04-22 02:15:23 +0000745 Params, false),
746 "objc_begin_catch");
747 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000748
749 const llvm::StructType *EHTypeTy;
750 const llvm::Type *EHTypePtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000751
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000752 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
753 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000754};
755
756class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000757public:
758 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000759 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000760 public:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000761 unsigned ivar_bytepos;
762 unsigned ivar_size;
763 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
764 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000765
766 // Allow sorting based on byte pos.
767 bool operator<(const GC_IVAR &b) const {
768 return ivar_bytepos < b.ivar_bytepos;
769 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000770 };
771
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000772 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000773 public:
774 unsigned skip;
775 unsigned scan;
776 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
777 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000778 };
779
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000780protected:
781 CodeGen::CodeGenModule &CGM;
Owen Anderson69243822009-07-13 04:10:07 +0000782 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000783 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000784 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000785
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000786 // gc ivar layout bitmap calculation helper caches.
787 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
788 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000789
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000790 /// LazySymbols - Symbols to generate a lazy reference for. See
791 /// DefinedSymbols and FinishModule().
792 std::set<IdentifierInfo*> LazySymbols;
793
794 /// DefinedSymbols - External symbols which are defined by this
795 /// module. The symbols in this list and LazySymbols are used to add
796 /// special linker symbols which ensure that Objective-C modules are
797 /// linked properly.
798 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000799
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000800 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000801 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000802
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000803 /// MethodVarNames - uniqued method variable names.
804 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000805
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000806 /// MethodVarTypes - uniqued method type signatures. We have to use
807 /// a StringMap here because have no other unique reference.
808 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000809
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000810 /// MethodDefinitions - map of methods which have been defined in
811 /// this translation unit.
812 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000813
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000814 /// PropertyNames - uniqued method variable names.
815 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000816
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000817 /// ClassReferences - uniqued class references.
818 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000819
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000820 /// SelectorReferences - uniqued selector references.
821 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000822
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000823 /// Protocols - Protocols for which an objc_protocol structure has
824 /// been emitted. Forward declarations are handled by creating an
825 /// empty structure whose initializer is filled in when/if defined.
826 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000827
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000828 /// DefinedProtocols - Protocols which have actually been
829 /// defined. We should not need this, see FIXME in GenerateProtocol.
830 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000831
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000832 /// DefinedClasses - List of defined classes.
833 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000834
835 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
836 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000837
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000838 /// DefinedCategories - List of defined categories.
839 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000840
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000841 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
842 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
843
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000844 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000845 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000846 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000847
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000848 /// GetNameForMethod - Return a name for the given method.
849 /// \param[out] NameOut - The return value.
850 void GetNameForMethod(const ObjCMethodDecl *OMD,
851 const ObjCContainerDecl *CD,
852 std::string &NameOut);
853
854 /// GetMethodVarName - Return a unique constant for the given
855 /// selector's name. The return value has type char *.
856 llvm::Constant *GetMethodVarName(Selector Sel);
857 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
858 llvm::Constant *GetMethodVarName(const std::string &Name);
859
860 /// GetMethodVarType - Return a unique constant for the given
861 /// selector's name. The return value has type char *.
862
863 // FIXME: This is a horrible name.
864 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000865 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000866
867 /// GetPropertyName - Return a unique constant for the given
868 /// name. The return value has type char *.
869 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
870
871 // FIXME: This can be dropped once string functions are unified.
872 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
873 const Decl *Container);
874
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000875 /// GetClassName - Return a unique constant for the given selector's
876 /// name. The return value has type char *.
877 llvm::Constant *GetClassName(IdentifierInfo *Ident);
878
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000879 /// BuildIvarLayout - Builds ivar layout bitmap for the class
880 /// implementation for the __strong or __weak case.
881 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000882 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
883 bool ForStrongLayout);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000884
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000885 void BuildAggrIvarRecordLayout(const RecordType *RT,
886 unsigned int BytePos, bool ForStrongLayout,
887 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000888 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000889 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000890 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +0000891 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000892 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000893 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000894
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000895 /// GetIvarLayoutName - Returns a unique constant for the given
896 /// ivar layout bitmap.
897 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
898 const ObjCCommonTypesHelper &ObjCTypes);
899
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000900 /// EmitPropertyList - Emit the given property list. The return
901 /// value has type PropertyListPtrTy.
902 llvm::Constant *EmitPropertyList(const std::string &Name,
903 const Decl *Container,
904 const ObjCContainerDecl *OCD,
905 const ObjCCommonTypesHelper &ObjCTypes);
906
Fariborz Jahanianda320092009-01-29 19:24:30 +0000907 /// GetProtocolRef - Return a reference to the internal protocol
908 /// description, creating an empty one if it has not been
909 /// defined. The return value has type ProtocolPtrTy.
910 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000911
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000912 /// CreateMetadataVar - Create a global variable with internal
913 /// linkage for use by the Objective-C runtime.
914 ///
915 /// This is a convenience wrapper which not only creates the
916 /// variable, but also sets the section and alignment and adds the
917 /// global to the UsedGlobals list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000918 ///
919 /// \param Name - The variable name.
920 /// \param Init - The variable initializer; this is also used to
921 /// define the type of the variable.
922 /// \param Section - The section the variable should go into, or 0.
923 /// \param Align - The alignment for the variable, or 0.
924 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000925 /// "llvm.used".
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000926 llvm::GlobalVariable *CreateMetadataVar(const std::string &Name,
927 llvm::Constant *Init,
928 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000929 unsigned Align,
930 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000931
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000932 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
933 QualType ResultType,
934 llvm::Value *Sel,
935 llvm::Value *Arg0,
936 QualType Arg0Ty,
937 bool IsSuper,
938 const CallArgList &CallArgs,
939 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000940
Fariborz Jahanianc38e9af2009-06-23 21:47:46 +0000941 virtual void MergeMetadataGlobals(std::vector<llvm::Constant*> &UsedArray);
942
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000943public:
Owen Anderson69243822009-07-13 04:10:07 +0000944 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
945 CGM(cgm), VMContext(cgm.getLLVMContext())
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000946 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000947
Steve Naroff33fdb732009-03-31 16:53:37 +0000948 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000949
950 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
951 const ObjCContainerDecl *CD=0);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000952
953 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
954
955 /// GetOrEmitProtocol - Get the protocol object for the given
956 /// declaration, emitting it if necessary. The return value has type
957 /// ProtocolPtrTy.
958 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
959
960 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
961 /// object for the given declaration, emitting it if needed. These
962 /// forward references will be filled in with empty bodies if no
963 /// definition is seen. The return value has type ProtocolPtrTy.
964 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000965};
966
967class CGObjCMac : public CGObjCCommonMac {
968private:
969 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000970 /// EmitImageInfo - Emit the image info marker used to encode some module
971 /// level information.
972 void EmitImageInfo();
973
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000974 /// EmitModuleInfo - Another marker encoding module level
975 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000976 void EmitModuleInfo();
977
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000978 /// EmitModuleSymols - Emit module symbols, the list of defined
979 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000980 llvm::Constant *EmitModuleSymbols();
981
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000982 /// FinishModule - Write out global data structures at the end of
983 /// processing a translation unit.
984 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000985
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000986 /// EmitClassExtension - Generate the class extension structure used
987 /// to store the weak ivar layout and properties. The return value
988 /// has type ClassExtensionPtrTy.
989 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
990
991 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
992 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000993 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000994 const ObjCInterfaceDecl *ID);
995
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000996 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000997 QualType ResultType,
998 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000999 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001000 QualType Arg0Ty,
1001 bool IsSuper,
1002 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001003
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001004 /// EmitIvarList - Emit the ivar list for the given
1005 /// implementation. If ForClass is true the list of class ivars
1006 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1007 /// interface ivars will be emitted. The return value has type
1008 /// IvarListPtrTy.
1009 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001010 bool ForClass);
1011
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001012 /// EmitMetaClass - Emit a forward reference to the class structure
1013 /// for the metaclass of the given interface. The return value has
1014 /// type ClassPtrTy.
1015 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1016
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001017 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001018 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001019 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1020 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001021 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001022
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001023 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001024
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001025 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001026
1027 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001028 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001029 llvm::Constant *EmitMethodList(const std::string &Name,
1030 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001031 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001032
1033 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001034 /// method declarations.
1035 /// - TypeName: The name for the type containing the methods.
1036 /// - IsProtocol: True iff these methods are for a protocol.
1037 /// - ClassMethds: True iff these are class methods.
1038 /// - Required: When true, only "required" methods are
1039 /// listed. Similarly, when false only "optional" methods are
1040 /// listed. For classes this should always be true.
1041 /// - begin, end: The method list to output.
1042 ///
1043 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001044 llvm::Constant *EmitMethodDescList(const std::string &Name,
1045 const char *Section,
1046 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001047
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001048 /// GetOrEmitProtocol - Get the protocol object for the given
1049 /// declaration, emitting it if necessary. The return value has type
1050 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001051 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001052
1053 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1054 /// object for the given declaration, emitting it if needed. These
1055 /// forward references will be filled in with empty bodies if no
1056 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001057 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001058
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001059 /// EmitProtocolExtension - Generate the protocol extension
1060 /// structure used to store optional instance and class methods, and
1061 /// protocol properties. The return value has type
1062 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001063 llvm::Constant *
1064 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1065 const ConstantVector &OptInstanceMethods,
1066 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001067
1068 /// EmitProtocolList - Generate the list of referenced
1069 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc93372008-08-21 21:57:41 +00001070 llvm::Constant *EmitProtocolList(const std::string &Name,
1071 ObjCProtocolDecl::protocol_iterator begin,
1072 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001073
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001074 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1075 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001076 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001077
Fariborz Jahanianda320092009-01-29 19:24:30 +00001078 public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001079 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001080
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001081 virtual llvm::Function *ModuleInitFunction();
1082
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001083 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001084 QualType ResultType,
1085 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001086 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001087 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001088 const CallArgList &CallArgs,
1089 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001090
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001091 virtual CodeGen::RValue
1092 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001093 QualType ResultType,
1094 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001095 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001096 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001097 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001098 bool IsClassMessage,
1099 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001100
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001101 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001102 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001103
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001104 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001105
1106 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1107 /// untyped one.
1108 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1109 const ObjCMethodDecl *Method);
1110
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001111 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001112
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001113 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001114
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001115 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001116 const ObjCProtocolDecl *PD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00001117
Chris Lattner74391b42009-03-22 21:03:39 +00001118 virtual llvm::Constant *GetPropertyGetFunction();
1119 virtual llvm::Constant *GetPropertySetFunction();
1120 virtual llvm::Constant *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001121
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001122 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1123 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001124 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1125 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001126 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001127 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001128 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1129 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001130 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1131 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001132 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1133 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001134 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1135 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001136 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1137 llvm::Value *dest, llvm::Value *src,
1138 unsigned long size);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001139
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001140 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1141 QualType ObjectTy,
1142 llvm::Value *BaseValue,
1143 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001144 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001145 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001146 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001147 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001148};
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001149
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001150class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001151private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001152 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001153 llvm::GlobalVariable* ObjCEmptyCacheVar;
1154 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001155
Daniel Dunbar11394522009-04-18 08:51:00 +00001156 /// SuperClassReferences - uniqued super class references.
1157 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
1158
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001159 /// MetaClassReferences - uniqued meta class references.
1160 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001161
1162 /// EHTypeReferences - uniqued class ehtype references.
1163 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001164
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001165 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001166 /// legacy messaging dispatch.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001167 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001168
1169 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001170 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001171 bool LegacyDispatchedSelector(Selector Sel);
1172
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001173 /// FinishNonFragileABIModule - Write out global data structures at the end of
1174 /// processing a translation unit.
1175 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001176
Daniel Dunbar463b8762009-05-15 21:48:48 +00001177 /// AddModuleClassList - Add the given list of class pointers to the
1178 /// module with the provided symbol and section names.
1179 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1180 const char *SymbolName,
1181 const char *SectionName);
1182
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00001183 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1184 unsigned InstanceStart,
1185 unsigned InstanceSize,
1186 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001187 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
1188 llvm::Constant *IsAGV,
1189 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001190 llvm::Constant *ClassRoGV,
1191 bool HiddenVisibility);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001192
1193 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
1194
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001195 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
1196
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001197 /// EmitMethodList - Emit the method list for the given
1198 /// implementation. The return value has type MethodListnfABITy.
1199 llvm::Constant *EmitMethodList(const std::string &Name,
1200 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001201 const ConstantVector &Methods);
1202 /// EmitIvarList - Emit the ivar list for the given
1203 /// implementation. If ForClass is true the list of class ivars
1204 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1205 /// interface ivars will be emitted. The return value has type
1206 /// IvarListnfABIPtrTy.
1207 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00001208
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001209 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001210 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001211 unsigned long int offset);
1212
Fariborz Jahanianda320092009-01-29 19:24:30 +00001213 /// GetOrEmitProtocol - Get the protocol object for the given
1214 /// declaration, emitting it if necessary. The return value has type
1215 /// ProtocolPtrTy.
1216 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
1217
1218 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1219 /// object for the given declaration, emitting it if needed. These
1220 /// forward references will be filled in with empty bodies if no
1221 /// definition is seen. The return value has type ProtocolPtrTy.
1222 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
1223
1224 /// EmitProtocolList - Generate the list of referenced
1225 /// protocols. The return value has type ProtocolListPtrTy.
1226 llvm::Constant *EmitProtocolList(const std::string &Name,
1227 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001228 ObjCProtocolDecl::protocol_iterator end);
1229
1230 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1231 QualType ResultType,
1232 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00001233 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001234 QualType Arg0Ty,
1235 bool IsSuper,
1236 const CallArgList &CallArgs);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001237
1238 /// GetClassGlobal - Return the global variable for the Objective-C
1239 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001240 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
1241
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001242 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001243 /// for the given class reference.
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001244 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001245 const ObjCInterfaceDecl *ID);
1246
1247 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1248 /// for the given super class reference.
1249 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1250 const ObjCInterfaceDecl *ID);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001251
1252 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1253 /// meta-data
1254 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
1255 const ObjCInterfaceDecl *ID);
1256
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001257 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1258 /// the given ivar.
1259 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001260 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00001261 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001262 const ObjCIvarDecl *Ivar);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001263
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001264 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1265 /// for the given selector.
1266 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbare588b992009-03-01 04:46:24 +00001267
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001268 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001269 /// interface. The return value has type EHTypePtrTy.
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001270 llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
1271 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001272
1273 const char *getMetaclassSymbolPrefix() const {
1274 return "OBJC_METACLASS_$_";
1275 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001276
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001277 const char *getClassSymbolPrefix() const {
1278 return "OBJC_CLASS_$_";
1279 }
1280
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001281 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001282 uint32_t &InstanceStart,
1283 uint32_t &InstanceSize);
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001284
1285 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001286 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001287 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1288 return CGM.getContext().Selectors.getSelector(0, &II);
1289 }
1290
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001291 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001292 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1293 return CGM.getContext().Selectors.getSelector(1, &II);
1294 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001295
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001296 /// ImplementationIsNonLazy - Check whether the given category or
1297 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001298 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001299
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001300public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001301 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001302 // FIXME. All stubs for now!
1303 virtual llvm::Function *ModuleInitFunction();
1304
1305 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
1306 QualType ResultType,
1307 Selector Sel,
1308 llvm::Value *Receiver,
1309 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001310 const CallArgList &CallArgs,
1311 const ObjCMethodDecl *Method);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001312
1313 virtual CodeGen::RValue
1314 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
1315 QualType ResultType,
1316 Selector Sel,
1317 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001318 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001319 llvm::Value *Receiver,
1320 bool IsClassMessage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001321 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001322
1323 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001324 const ObjCInterfaceDecl *ID);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001325
1326 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001327 { return EmitSelector(Builder, Sel); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001328
1329 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1330 /// untyped one.
1331 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1332 const ObjCMethodDecl *Method)
1333 { return EmitSelector(Builder, Method->getSelector()); }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001334
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001335 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001336
1337 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001338 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001339 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001340
Chris Lattner74391b42009-03-22 21:03:39 +00001341 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001342 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001343 }
Chris Lattner74391b42009-03-22 21:03:39 +00001344 virtual llvm::Constant *GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001345 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001346 }
Chris Lattner74391b42009-03-22 21:03:39 +00001347 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001348 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001349 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001350
1351 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00001352 const Stmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001353 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001354 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001355 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001356 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001357 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001358 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001359 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001360 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001361 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001362 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001363 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001364 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001365 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1366 llvm::Value *dest, llvm::Value *src,
1367 unsigned long size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001368 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1369 QualType ObjectTy,
1370 llvm::Value *BaseValue,
1371 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001372 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001373 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001374 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001375 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001376};
1377
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001378} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001379
1380/* *** Helper Functions *** */
1381
1382/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001383static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
1384 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001385 unsigned idx0,
1386 unsigned idx1) {
1387 llvm::Value *Idxs[] = {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001388 VMContext.getConstantInt(llvm::Type::Int32Ty, idx0),
1389 VMContext.getConstantInt(llvm::Type::Int32Ty, idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001390 };
Owen Andersona1cf15f2009-07-14 23:10:40 +00001391 return VMContext.getConstantExprGetElementPtr(C, Idxs, 2);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001392}
1393
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001394/// hasObjCExceptionAttribute - Return true if this class or any super
1395/// class has the __objc_exception__ attribute.
Douglas Gregor68584ed2009-06-18 16:11:24 +00001396static bool hasObjCExceptionAttribute(ASTContext &Context,
1397 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001398 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001399 return true;
1400 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001401 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001402 return false;
1403}
1404
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001405/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001406
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001407CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
1408 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001409{
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001410 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001411 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001412}
1413
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001414/// GetClass - Return a reference to the class for the given interface
1415/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001416llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001417 const ObjCInterfaceDecl *ID) {
1418 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001419}
1420
1421/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001422llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001423 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001424}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001425llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
1426 *Method) {
1427 return EmitSelector(Builder, Method->getSelector());
1428}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001429
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001430/// Generate a constant CFString object.
1431/*
1432 struct __builtin_CFString {
1433 const int *isa; // point to __CFConstantStringClassReference
1434 int flags;
1435 const char *str;
1436 long length;
1437 };
1438*/
1439
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001440llvm::Constant *CGObjCCommonMac::GenerateConstantString(
Steve Naroff33fdb732009-03-31 16:53:37 +00001441 const ObjCStringLiteral *SL) {
Steve Naroff8d4141f2009-04-01 13:55:36 +00001442 return CGM.GetAddrOfConstantCFString(SL->getString());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001443}
1444
1445/// Generates a message send where the super is the receiver. This is
1446/// a message send to self with special delivery semantics indicating
1447/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001448CodeGen::RValue
1449CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001450 QualType ResultType,
1451 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001452 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001453 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001454 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001455 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001456 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001457 // Create and init a super structure; this is a (receiver, class)
1458 // pair we will pass to objc_msgSendSuper.
1459 llvm::Value *ObjCSuper =
1460 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
1461 llvm::Value *ReceiverAsObject =
1462 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
1463 CGF.Builder.CreateStore(ReceiverAsObject,
1464 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001465
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001466 // If this is a class message the metaclass is passed as the target.
1467 llvm::Value *Target;
1468 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001469 if (isCategoryImpl) {
1470 // Message sent to 'super' in a class method defined in a category
1471 // implementation requires an odd treatment.
1472 // If we are in a class method, we must retrieve the
1473 // _metaclass_ for the current class, pointed at by
1474 // the class's "isa" pointer. The following assumes that
1475 // isa" is the first ivar in a class (which it must be).
1476 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1477 Target = CGF.Builder.CreateStructGEP(Target, 0);
1478 Target = CGF.Builder.CreateLoad(Target);
1479 }
1480 else {
1481 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1482 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1483 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1484 Target = Super;
1485 }
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001486 } else {
1487 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1488 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001489 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1490 // ObjCTypes types.
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001491 const llvm::Type *ClassTy =
1492 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001493 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001494 CGF.Builder.CreateStore(Target,
1495 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001496 return EmitLegacyMessageSend(CGF, ResultType,
1497 EmitSelector(CGF.Builder, Sel),
1498 ObjCSuper, ObjCTypes.SuperPtrCTy,
1499 true, CallArgs, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001500}
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001501
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001502/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001503CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001504 QualType ResultType,
1505 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001506 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001507 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001508 const CallArgList &CallArgs,
1509 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001510 return EmitLegacyMessageSend(CGF, ResultType,
1511 EmitSelector(CGF.Builder, Sel),
1512 Receiver, CGF.getContext().getObjCIdType(),
1513 false, CallArgs, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001514}
1515
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001516CodeGen::RValue CGObjCCommonMac::EmitLegacyMessageSend(
1517 CodeGen::CodeGenFunction &CGF,
1518 QualType ResultType,
1519 llvm::Value *Sel,
1520 llvm::Value *Arg0,
1521 QualType Arg0Ty,
1522 bool IsSuper,
1523 const CallArgList &CallArgs,
1524 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001525 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001526 if (!IsSuper)
1527 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001528 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001529 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001530 CGF.getContext().getObjCSelType()));
1531 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001532
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001533 CodeGenTypes &Types = CGM.getTypes();
1534 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001535 // In 64bit ABI, type must be assumed VARARG. In 32bit abi,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001536 // it seems not to matter.
1537 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, (ObjCABI == 2));
1538
1539 llvm::Constant *Fn = NULL;
Daniel Dunbar88b53962009-02-02 22:03:45 +00001540 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001541 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
1542 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001543 } else if (ResultType->isFloatingType()) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001544 if (ObjCABI == 2) {
1545 if (const BuiltinType *BT = ResultType->getAsBuiltinType()) {
1546 BuiltinType::Kind k = BT->getKind();
1547 Fn = (k == BuiltinType::LongDouble) ? ObjCTypes.getSendFpretFn2(IsSuper)
1548 : ObjCTypes.getSendFn2(IsSuper);
Daniel Dunbar42f963d2009-06-10 04:38:50 +00001549 } else {
1550 Fn = ObjCTypes.getSendFn2(IsSuper);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001551 }
1552 }
1553 else
Mike Stumpf5408fe2009-05-16 07:57:57 +00001554 // FIXME. This currently matches gcc's API for x86-32. May need to change
1555 // for others if we have their API.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001556 Fn = ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001557 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001558 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
1559 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001560 }
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001561 assert(Fn && "EmitLegacyMessageSend - unknown API");
Owen Andersona1cf15f2009-07-14 23:10:40 +00001562 Fn = VMContext.getConstantExprBitCast(Fn,
1563 VMContext.getPointerTypeUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +00001564 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001565}
1566
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001567llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001568 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001569 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001570 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001571 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1572
Owen Andersona1cf15f2009-07-14 23:10:40 +00001573 return VMContext.getConstantExprBitCast(GetProtocolRef(PD),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001574 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001575}
1576
Fariborz Jahanianda320092009-01-29 19:24:30 +00001577void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001578 // FIXME: We shouldn't need this, the protocol decl should contain enough
1579 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001580 DefinedProtocols.insert(PD->getIdentifier());
1581
1582 // If we have generated a forward reference to this protocol, emit
1583 // it now. Otherwise do nothing, the protocol objects are lazily
1584 // emitted.
1585 if (Protocols.count(PD->getIdentifier()))
1586 GetOrEmitProtocol(PD);
1587}
1588
Fariborz Jahanianda320092009-01-29 19:24:30 +00001589llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001590 if (DefinedProtocols.count(PD->getIdentifier()))
1591 return GetOrEmitProtocol(PD);
1592 return GetOrEmitProtocolRef(PD);
1593}
1594
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001595/*
1596 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1597 struct _objc_protocol {
1598 struct _objc_protocol_extension *isa;
1599 char *protocol_name;
1600 struct _objc_protocol_list *protocol_list;
1601 struct _objc__method_prototype_list *instance_methods;
1602 struct _objc__method_prototype_list *class_methods
1603 };
1604
1605 See EmitProtocolExtension().
1606*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001607llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1608 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1609
1610 // Early exit if a defining object has already been generated.
1611 if (Entry && Entry->hasInitializer())
1612 return Entry;
1613
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001614 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001615 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001616 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1617
Chris Lattner8ec03f52008-11-24 03:54:41 +00001618 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001619
1620 // Construct method lists.
1621 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1622 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00001623 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001624 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001625 ObjCMethodDecl *MD = *i;
1626 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1627 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1628 OptInstanceMethods.push_back(C);
1629 } else {
1630 InstanceMethods.push_back(C);
1631 }
1632 }
1633
Douglas Gregor6ab35242009-04-09 21:40:53 +00001634 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001635 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001636 ObjCMethodDecl *MD = *i;
1637 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1638 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1639 OptClassMethods.push_back(C);
1640 } else {
1641 ClassMethods.push_back(C);
1642 }
1643 }
1644
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001645 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001646 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001647 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc93372008-08-21 21:57:41 +00001648 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001649 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00001650 PD->protocol_begin(),
1651 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001652 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001653 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
1654 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001655 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1656 InstanceMethods);
1657 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001658 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
1659 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001660 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1661 ClassMethods);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001662 llvm::Constant *Init = VMContext.getConstantStruct(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001663 Values);
1664
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001665 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001666 // Already created, fix the linkage and update the initializer.
1667 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001668 Entry->setInitializer(Init);
1669 } else {
1670 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001671 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001672 llvm::GlobalValue::InternalLinkage,
1673 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00001674 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001675 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001676 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001677 UsedGlobals.push_back(Entry);
1678 // FIXME: Is this necessary? Why only for protocol?
1679 Entry->setAlignment(4);
1680 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001681
1682 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001683}
1684
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001685llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001686 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1687
1688 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001689 // We use the initializer as a marker of whether this is a forward
1690 // reference or not. At module finalization we add the empty
1691 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001692 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001693 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001694 llvm::GlobalValue::ExternalLinkage,
1695 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00001696 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001697 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001698 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001699 UsedGlobals.push_back(Entry);
1700 // FIXME: Is this necessary? Why only for protocol?
1701 Entry->setAlignment(4);
1702 }
1703
1704 return Entry;
1705}
1706
1707/*
1708 struct _objc_protocol_extension {
1709 uint32_t size;
1710 struct objc_method_description_list *optional_instance_methods;
1711 struct objc_method_description_list *optional_class_methods;
1712 struct objc_property_list *instance_properties;
1713 };
1714*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001715llvm::Constant *
1716CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1717 const ConstantVector &OptInstanceMethods,
1718 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001719 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00001720 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001721 std::vector<llvm::Constant*> Values(4);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001722 Values[0] = VMContext.getConstantInt(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001723 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001724 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1725 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001726 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1727 OptInstanceMethods);
1728 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001729 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1730 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001731 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1732 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001733 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1734 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001735 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001736
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001737 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001738 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1739 Values[3]->isNullValue())
Owen Anderson69243822009-07-13 04:10:07 +00001740 return VMContext.getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001741
1742 llvm::Constant *Init =
Owen Andersona1cf15f2009-07-14 23:10:40 +00001743 VMContext.getConstantStruct(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001744
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001745 // No special section, but goes in llvm.used
1746 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
1747 Init,
1748 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001749}
1750
1751/*
1752 struct objc_protocol_list {
1753 struct objc_protocol_list *next;
1754 long count;
1755 Protocol *list[];
1756 };
1757*/
Daniel Dunbardbc93372008-08-21 21:57:41 +00001758llvm::Constant *
1759CGObjCMac::EmitProtocolList(const std::string &Name,
1760 ObjCProtocolDecl::protocol_iterator begin,
1761 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001762 std::vector<llvm::Constant*> ProtocolRefs;
1763
Daniel Dunbardbc93372008-08-21 21:57:41 +00001764 for (; begin != end; ++begin)
1765 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001766
1767 // Just return null for empty protocol lists
1768 if (ProtocolRefs.empty())
Owen Anderson69243822009-07-13 04:10:07 +00001769 return VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001770
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001771 // This list is null terminated.
Owen Anderson69243822009-07-13 04:10:07 +00001772 ProtocolRefs.push_back(VMContext.getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001773
1774 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001775 // This field is only used by the runtime.
Owen Anderson69243822009-07-13 04:10:07 +00001776 Values[0] = VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001777 Values[1] = VMContext.getConstantInt(ObjCTypes.LongTy,
1778 ProtocolRefs.size() - 1);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001779 Values[2] =
Owen Andersona1cf15f2009-07-14 23:10:40 +00001780 VMContext.getConstantArray(VMContext.getArrayType(ObjCTypes.ProtocolPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001781 ProtocolRefs.size()),
1782 ProtocolRefs);
1783
Owen Andersona1cf15f2009-07-14 23:10:40 +00001784 llvm::Constant *Init = VMContext.getConstantStruct(Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001785 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001786 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001787 4, false);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001788 return VMContext.getConstantExprBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001789}
1790
1791/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001792 struct _objc_property {
1793 const char * const name;
1794 const char * const attributes;
1795 };
1796
1797 struct _objc_property_list {
1798 uint32_t entsize; // sizeof (struct _objc_property)
1799 uint32_t prop_count;
1800 struct _objc_property[prop_count];
1801 };
1802*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001803llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1804 const Decl *Container,
1805 const ObjCContainerDecl *OCD,
1806 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001807 std::vector<llvm::Constant*> Properties, Prop(2);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001808 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1809 E = OCD->prop_end(); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00001810 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001811 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001812 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001813 Properties.push_back(VMContext.getConstantStruct(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001814 Prop));
1815 }
1816
1817 // Return null for empty list.
1818 if (Properties.empty())
Owen Anderson69243822009-07-13 04:10:07 +00001819 return VMContext.getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001820
1821 unsigned PropertySize =
Duncan Sands9408c452009-05-09 07:08:47 +00001822 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001823 std::vector<llvm::Constant*> Values(3);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001824 Values[0] = VMContext.getConstantInt(ObjCTypes.IntTy, PropertySize);
1825 Values[1] = VMContext.getConstantInt(ObjCTypes.IntTy, Properties.size());
1826 llvm::ArrayType *AT = VMContext.getArrayType(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001827 Properties.size());
Owen Andersona1cf15f2009-07-14 23:10:40 +00001828 Values[2] = VMContext.getConstantArray(AT, Properties);
1829 llvm::Constant *Init = VMContext.getConstantStruct(Values);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001830
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001831 llvm::GlobalVariable *GV =
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001832 CreateMetadataVar(Name, Init,
1833 (ObjCABI == 2) ? "__DATA, __objc_const" :
1834 "__OBJC,__property,regular,no_dead_strip",
1835 (ObjCABI == 2) ? 8 : 4,
1836 true);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001837 return VMContext.getConstantExprBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001838}
1839
1840/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001841 struct objc_method_description_list {
1842 int count;
1843 struct objc_method_description list[];
1844 };
1845*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001846llvm::Constant *
1847CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1848 std::vector<llvm::Constant*> Desc(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001849 Desc[0] =
1850 VMContext.getConstantExprBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001851 ObjCTypes.SelectorPtrTy);
1852 Desc[1] = GetMethodVarType(MD);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001853 return VMContext.getConstantStruct(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001854 Desc);
1855}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001856
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001857llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1858 const char *Section,
1859 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001860 // Return null for empty list.
1861 if (Methods.empty())
Owen Anderson69243822009-07-13 04:10:07 +00001862 return VMContext.getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001863
1864 std::vector<llvm::Constant*> Values(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001865 Values[0] = VMContext.getConstantInt(ObjCTypes.IntTy, Methods.size());
1866 llvm::ArrayType *AT = VMContext.getArrayType(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001867 Methods.size());
Owen Andersona1cf15f2009-07-14 23:10:40 +00001868 Values[1] = VMContext.getConstantArray(AT, Methods);
1869 llvm::Constant *Init = VMContext.getConstantStruct(Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001870
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001871 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001872 return VMContext.getConstantExprBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001873 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001874}
1875
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001876/*
1877 struct _objc_category {
1878 char *category_name;
1879 char *class_name;
1880 struct _objc_method_list *instance_methods;
1881 struct _objc_method_list *class_methods;
1882 struct _objc_protocol_list *protocols;
1883 uint32_t size; // <rdar://4585769>
1884 struct _objc_property_list *instance_properties;
1885 };
1886 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001887void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sands9408c452009-05-09 07:08:47 +00001888 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001889
Mike Stumpf5408fe2009-05-16 07:57:57 +00001890 // FIXME: This is poor design, the OCD should have a pointer to the category
1891 // decl. Additionally, note that Category can be null for the @implementation
1892 // w/o an @interface case. Sema should just create one for us as it does for
1893 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001894 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001895 const ObjCCategoryDecl *Category =
1896 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001897 std::string ExtName(Interface->getNameAsString() + "_" +
1898 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001899
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001900 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001901 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001902 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001903 // Instance methods should always be defined.
1904 InstanceMethods.push_back(GetMethodConstant(*i));
1905 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001906 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001907 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001908 // Class methods should always be defined.
1909 ClassMethods.push_back(GetMethodConstant(*i));
1910 }
1911
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001912 std::vector<llvm::Constant*> Values(7);
1913 Values[0] = GetClassName(OCD->getIdentifier());
1914 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00001915 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001916 Values[2] =
1917 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1918 ExtName,
1919 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001920 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001921 Values[3] =
1922 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001923 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001924 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001925 if (Category) {
1926 Values[4] =
1927 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1928 Category->protocol_begin(),
1929 Category->protocol_end());
1930 } else {
Owen Anderson69243822009-07-13 04:10:07 +00001931 Values[4] = VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001932 }
Owen Andersona1cf15f2009-07-14 23:10:40 +00001933 Values[5] = VMContext.getConstantInt(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001934
1935 // If there is no category @interface then there can be no properties.
1936 if (Category) {
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001937 Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001938 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001939 } else {
Owen Anderson69243822009-07-13 04:10:07 +00001940 Values[6] = VMContext.getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001941 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001942
Owen Andersona1cf15f2009-07-14 23:10:40 +00001943 llvm::Constant *Init = VMContext.getConstantStruct(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001944 Values);
1945
1946 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001947 CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init,
1948 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001949 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001950 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001951}
1952
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001953// FIXME: Get from somewhere?
1954enum ClassFlags {
1955 eClassFlags_Factory = 0x00001,
1956 eClassFlags_Meta = 0x00002,
1957 // <rdr://5142207>
1958 eClassFlags_HasCXXStructors = 0x02000,
1959 eClassFlags_Hidden = 0x20000,
1960 eClassFlags_ABI2_Hidden = 0x00010,
1961 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1962};
1963
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001964/*
1965 struct _objc_class {
1966 Class isa;
1967 Class super_class;
1968 const char *name;
1969 long version;
1970 long info;
1971 long instance_size;
1972 struct _objc_ivar_list *ivars;
1973 struct _objc_method_list *methods;
1974 struct _objc_cache *cache;
1975 struct _objc_protocol_list *protocols;
1976 // Objective-C 1.0 extensions (<rdr://4585769>)
1977 const char *ivar_layout;
1978 struct _objc_class_ext *ext;
1979 };
1980
1981 See EmitClassExtension();
1982 */
1983void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001984 DefinedSymbols.insert(ID->getIdentifier());
1985
Chris Lattner8ec03f52008-11-24 03:54:41 +00001986 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001987 // FIXME: Gross
1988 ObjCInterfaceDecl *Interface =
1989 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc93372008-08-21 21:57:41 +00001990 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001991 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00001992 Interface->protocol_begin(),
1993 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001994 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar2bebbf02009-05-03 10:46:44 +00001995 unsigned Size =
1996 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001997
1998 // FIXME: Set CXX-structors flag.
Daniel Dunbar04d40782009-04-14 06:00:08 +00001999 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002000 Flags |= eClassFlags_Hidden;
2001
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002002 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregor653f1b12009-04-23 01:02:12 +00002003 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002004 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002005 // Instance methods should always be defined.
2006 InstanceMethods.push_back(GetMethodConstant(*i));
2007 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00002008 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002009 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002010 // Class methods should always be defined.
2011 ClassMethods.push_back(GetMethodConstant(*i));
2012 }
2013
Douglas Gregor653f1b12009-04-23 01:02:12 +00002014 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002015 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002016 ObjCPropertyImplDecl *PID = *i;
2017
2018 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2019 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2020
2021 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2022 if (llvm::Constant *C = GetMethodConstant(MD))
2023 InstanceMethods.push_back(C);
2024 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2025 if (llvm::Constant *C = GetMethodConstant(MD))
2026 InstanceMethods.push_back(C);
2027 }
2028 }
2029
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002030 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar5384b092009-05-03 08:56:52 +00002031 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002032 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002033 // Record a reference to the super class.
2034 LazySymbols.insert(Super->getIdentifier());
2035
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002036 Values[ 1] =
Owen Andersona1cf15f2009-07-14 23:10:40 +00002037 VMContext.getConstantExprBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002038 ObjCTypes.ClassPtrTy);
2039 } else {
Owen Anderson69243822009-07-13 04:10:07 +00002040 Values[ 1] = VMContext.getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002041 }
2042 Values[ 2] = GetClassName(ID->getIdentifier());
2043 // Version is always 0.
Owen Andersona1cf15f2009-07-14 23:10:40 +00002044 Values[ 3] = VMContext.getConstantInt(ObjCTypes.LongTy, 0);
2045 Values[ 4] = VMContext.getConstantInt(ObjCTypes.LongTy, Flags);
2046 Values[ 5] = VMContext.getConstantInt(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002047 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002048 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002049 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002050 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002051 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002052 // cache is always NULL.
Owen Anderson69243822009-07-13 04:10:07 +00002053 Values[ 8] = VMContext.getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002054 Values[ 9] = Protocols;
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002055 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002056 Values[11] = EmitClassExtension(ID);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002057 llvm::Constant *Init = VMContext.getConstantStruct(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002058 Values);
2059
2060 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002061 CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init,
2062 "__OBJC,__class,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002063 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002064 DefinedClasses.push_back(GV);
2065}
2066
2067llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2068 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002069 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002070 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002071 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002072
Daniel Dunbar04d40782009-04-14 06:00:08 +00002073 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002074 Flags |= eClassFlags_Hidden;
2075
2076 std::vector<llvm::Constant*> Values(12);
2077 // The isa for the metaclass is the root of the hierarchy.
2078 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2079 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2080 Root = Super;
2081 Values[ 0] =
Owen Andersona1cf15f2009-07-14 23:10:40 +00002082 VMContext.getConstantExprBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002083 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002084 // The super class for the metaclass is emitted as the name of the
2085 // super class. The runtime fixes this up to point to the
2086 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002087 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
2088 Values[ 1] =
Owen Andersona1cf15f2009-07-14 23:10:40 +00002089 VMContext.getConstantExprBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002090 ObjCTypes.ClassPtrTy);
2091 } else {
Owen Anderson69243822009-07-13 04:10:07 +00002092 Values[ 1] = VMContext.getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002093 }
2094 Values[ 2] = GetClassName(ID->getIdentifier());
2095 // Version is always 0.
Owen Andersona1cf15f2009-07-14 23:10:40 +00002096 Values[ 3] = VMContext.getConstantInt(ObjCTypes.LongTy, 0);
2097 Values[ 4] = VMContext.getConstantInt(ObjCTypes.LongTy, Flags);
2098 Values[ 5] = VMContext.getConstantInt(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002099 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002100 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002101 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002102 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002103 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002104 // cache is always NULL.
Owen Anderson69243822009-07-13 04:10:07 +00002105 Values[ 8] = VMContext.getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002106 Values[ 9] = Protocols;
2107 // ivar_layout for metaclass is always NULL.
Owen Anderson69243822009-07-13 04:10:07 +00002108 Values[10] = VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002109 // The class extension is always unused for metaclasses.
Owen Anderson69243822009-07-13 04:10:07 +00002110 Values[11] = VMContext.getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002111 llvm::Constant *Init = VMContext.getConstantStruct(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002112 Values);
2113
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002114 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002115 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002116
2117 // Check for a forward reference.
2118 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2119 if (GV) {
2120 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2121 "Forward metaclass reference has incorrect type.");
2122 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2123 GV->setInitializer(Init);
2124 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002125 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002126 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002127 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002128 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002129 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002130 GV->setAlignment(4);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002131 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002132
2133 return GV;
2134}
2135
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002136llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002137 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002138
Mike Stumpf5408fe2009-05-16 07:57:57 +00002139 // FIXME: Should we look these up somewhere other than the module. Its a bit
2140 // silly since we only generate these while processing an implementation, so
2141 // exactly one pointer would work if know when we entered/exitted an
2142 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002143
2144 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002145 // Previously, metaclass with internal linkage may have been defined.
2146 // pass 'true' as 2nd argument so it is returned.
2147 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002148 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2149 "Forward metaclass reference has incorrect type.");
2150 return GV;
2151 } else {
2152 // Generate as an external reference to keep a consistent
2153 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002154 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002155 llvm::GlobalValue::ExternalLinkage,
2156 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002157 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002158 }
2159}
2160
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002161/*
2162 struct objc_class_ext {
2163 uint32_t size;
2164 const char *weak_ivar_layout;
2165 struct _objc_property_list *properties;
2166 };
2167*/
2168llvm::Constant *
2169CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
2170 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002171 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002172
2173 std::vector<llvm::Constant*> Values(3);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002174 Values[0] = VMContext.getConstantInt(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002175 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002176 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002177 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002178
2179 // Return null if no extension bits are used.
2180 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson69243822009-07-13 04:10:07 +00002181 return VMContext.getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002182
2183 llvm::Constant *Init =
Owen Andersona1cf15f2009-07-14 23:10:40 +00002184 VMContext.getConstantStruct(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002185 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002186 Init, "__OBJC,__class_ext,regular,no_dead_strip",
2187 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002188}
2189
2190/*
2191 struct objc_ivar {
2192 char *ivar_name;
2193 char *ivar_type;
2194 int ivar_offset;
2195 };
2196
2197 struct objc_ivar_list {
2198 int ivar_count;
2199 struct objc_ivar list[count];
2200 };
2201 */
2202llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002203 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002204 std::vector<llvm::Constant*> Ivars, Ivar(3);
2205
2206 // When emitting the root class GCC emits ivar entries for the
2207 // actual class structure. It is not clear if we need to follow this
2208 // behavior; for now lets try and get away with not doing it. If so,
2209 // the cleanest solution would be to make up an ObjCInterfaceDecl
2210 // for the class.
2211 if (ForClass)
Owen Anderson69243822009-07-13 04:10:07 +00002212 return VMContext.getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002213
2214 ObjCInterfaceDecl *OID =
2215 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002216
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002217 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002218 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002219
2220 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2221 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002222 // Ignore unnamed bit-fields.
2223 if (!IVD->getDeclName())
2224 continue;
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002225 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2226 Ivar[1] = GetMethodVarType(IVD);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002227 Ivar[2] = VMContext.getConstantInt(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002228 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Andersona1cf15f2009-07-14 23:10:40 +00002229 Ivars.push_back(VMContext.getConstantStruct(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002230 }
2231
2232 // Return null for empty list.
2233 if (Ivars.empty())
Owen Anderson69243822009-07-13 04:10:07 +00002234 return VMContext.getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002235
2236 std::vector<llvm::Constant*> Values(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002237 Values[0] = VMContext.getConstantInt(ObjCTypes.IntTy, Ivars.size());
2238 llvm::ArrayType *AT = VMContext.getArrayType(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002239 Ivars.size());
Owen Andersona1cf15f2009-07-14 23:10:40 +00002240 Values[1] = VMContext.getConstantArray(AT, Ivars);
2241 llvm::Constant *Init = VMContext.getConstantStruct(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002242
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002243 llvm::GlobalVariable *GV;
2244 if (ForClass)
2245 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(),
Daniel Dunbar58a29122009-03-09 22:18:41 +00002246 Init, "__OBJC,__class_vars,regular,no_dead_strip",
2247 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002248 else
2249 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_"
2250 + ID->getNameAsString(),
2251 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002252 4, true);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002253 return VMContext.getConstantExprBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002254}
2255
2256/*
2257 struct objc_method {
2258 SEL method_name;
2259 char *method_types;
2260 void *method;
2261 };
2262
2263 struct objc_method_list {
2264 struct objc_method_list *obsolete;
2265 int count;
2266 struct objc_method methods_list[count];
2267 };
2268*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002269
2270/// GetMethodConstant - Return a struct objc_method constant for the
2271/// given method if it has been defined. The result is null if the
2272/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002273llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002274 // FIXME: Use DenseMap::lookup
2275 llvm::Function *Fn = MethodDefinitions[MD];
2276 if (!Fn)
2277 return 0;
2278
2279 std::vector<llvm::Constant*> Method(3);
2280 Method[0] =
Owen Andersona1cf15f2009-07-14 23:10:40 +00002281 VMContext.getConstantExprBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002282 ObjCTypes.SelectorPtrTy);
2283 Method[1] = GetMethodVarType(MD);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002284 Method[2] = VMContext.getConstantExprBitCast(Fn, ObjCTypes.Int8PtrTy);
2285 return VMContext.getConstantStruct(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002286}
2287
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002288llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
2289 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002290 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002291 // Return null for empty list.
2292 if (Methods.empty())
Owen Anderson69243822009-07-13 04:10:07 +00002293 return VMContext.getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002294
2295 std::vector<llvm::Constant*> Values(3);
Owen Anderson69243822009-07-13 04:10:07 +00002296 Values[0] = VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002297 Values[1] = VMContext.getConstantInt(ObjCTypes.IntTy, Methods.size());
2298 llvm::ArrayType *AT = VMContext.getArrayType(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002299 Methods.size());
Owen Andersona1cf15f2009-07-14 23:10:40 +00002300 Values[2] = VMContext.getConstantArray(AT, Methods);
2301 llvm::Constant *Init = VMContext.getConstantStruct(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002302
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002303 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002304 return VMContext.getConstantExprBitCast(GV,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002305 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002306}
2307
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002308llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00002309 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002310 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002311 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002312
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002313 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002314 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002315 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002316 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002317 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002318 llvm::GlobalValue::InternalLinkage,
2319 Name,
2320 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002321 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002322
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002323 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002324}
2325
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002326llvm::GlobalVariable *
2327CGObjCCommonMac::CreateMetadataVar(const std::string &Name,
2328 llvm::Constant *Init,
2329 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002330 unsigned Align,
2331 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002332 const llvm::Type *Ty = Init->getType();
2333 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002334 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002335 llvm::GlobalValue::InternalLinkage,
2336 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00002337 Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002338 if (Section)
2339 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002340 if (Align)
2341 GV->setAlignment(Align);
2342 if (AddToUsed)
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002343 UsedGlobals.push_back(GV);
2344 return GV;
2345}
2346
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002347llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002348 // Abuse this interface function as a place to finalize.
2349 FinishModule();
2350
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002351 return NULL;
2352}
2353
Chris Lattner74391b42009-03-22 21:03:39 +00002354llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002355 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002356}
2357
Chris Lattner74391b42009-03-22 21:03:39 +00002358llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002359 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002360}
2361
Chris Lattner74391b42009-03-22 21:03:39 +00002362llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002363 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002364}
2365
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002366/*
2367
2368Objective-C setjmp-longjmp (sjlj) Exception Handling
2369--
2370
2371The basic framework for a @try-catch-finally is as follows:
2372{
2373 objc_exception_data d;
2374 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002375 bool _call_try_exit = true;
2376
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002377 objc_exception_try_enter(&d);
2378 if (!setjmp(d.jmp_buf)) {
2379 ... try body ...
2380 } else {
2381 // exception path
2382 id _caught = objc_exception_extract(&d);
2383
2384 // enter new try scope for handlers
2385 if (!setjmp(d.jmp_buf)) {
2386 ... match exception and execute catch blocks ...
2387
2388 // fell off end, rethrow.
2389 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00002390 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002391 } else {
2392 // exception in catch block
2393 _rethrow = objc_exception_extract(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002394 _call_try_exit = false;
2395 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002396 }
2397 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002398 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002399
2400finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002401 if (_call_try_exit)
2402 objc_exception_try_exit(&d);
2403
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002404 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002405 ... dispatch to finally destination ...
2406
2407finally_rethrow:
2408 objc_exception_throw(_rethrow);
2409
2410finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002411}
2412
2413This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00002414uses _rethrow to determine if objc_exception_try_exit should be called
2415and if the object should be rethrown. This breaks in the face of
2416throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002417
2418We specialize this framework for a few particular circumstances:
2419
2420 - If there are no catch blocks, then we avoid emitting the second
2421 exception handling context.
2422
2423 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2424 e)) we avoid emitting the code to rethrow an uncaught exception.
2425
2426 - FIXME: If there is no @finally block we can do a few more
2427 simplifications.
2428
2429Rethrows and Jumps-Through-Finally
2430--
2431
2432Support for implicit rethrows and jumping through the finally block is
2433handled by storing the current exception-handling context in
2434ObjCEHStack.
2435
Daniel Dunbar898d5082008-09-30 01:06:03 +00002436In order to implement proper @finally semantics, we support one basic
2437mechanism for jumping through the finally block to an arbitrary
2438destination. Constructs which generate exits from a @try or @catch
2439block use this mechanism to implement the proper semantics by chaining
2440jumps, as necessary.
2441
2442This mechanism works like the one used for indirect goto: we
2443arbitrarily assign an ID to each destination and store the ID for the
2444destination in a variable prior to entering the finally block. At the
2445end of the finally block we simply create a switch to the proper
2446destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002447
2448Code gen for @synchronized(expr) stmt;
2449Effectively generating code for:
2450objc_sync_enter(expr);
2451@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002452*/
2453
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002454void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2455 const Stmt &S) {
2456 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002457 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00002458 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002459 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar55e87422008-11-11 02:29:29 +00002460 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
2461 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
2462 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar1c566672009-02-24 01:43:46 +00002463
2464 // For @synchronized, call objc_sync_enter(sync.expr). The
2465 // evaluation of the expression must occur before we enter the
2466 // @synchronized. We can safely avoid a temp here because jumps into
2467 // @synchronized are illegal & this will dominate uses.
2468 llvm::Value *SyncArg = 0;
2469 if (!isTry) {
2470 SyncArg =
2471 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2472 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00002473 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002474 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002475
2476 // Push an EH context entry, used for handling rethrows and jumps
2477 // through finally.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002478 CGF.PushCleanupBlock(FinallyBlock);
2479
Anders Carlsson273558f2009-02-07 21:37:21 +00002480 CGF.ObjCEHValueStack.push_back(0);
2481
Daniel Dunbar898d5082008-09-30 01:06:03 +00002482 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00002483 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2484 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002485 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
2486 "_rethrow");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002487 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
2488 "_call_try_exit");
Owen Andersona1cf15f2009-07-14 23:10:40 +00002489 CGF.Builder.CreateStore(VMContext.getConstantIntTrue(), CallTryExitPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002490
Anders Carlsson80f25672008-09-09 17:59:25 +00002491 // Enter a new try block and call setjmp.
Chris Lattner34b02a12009-04-22 02:26:14 +00002492 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Anders Carlsson80f25672008-09-09 17:59:25 +00002493 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
2494 "jmpbufarray");
2495 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
Chris Lattner34b02a12009-04-22 02:26:14 +00002496 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlsson80f25672008-09-09 17:59:25 +00002497 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00002498
Daniel Dunbar55e87422008-11-11 02:29:29 +00002499 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2500 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002501 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002502 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002503
2504 // Emit the @try block.
2505 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002506 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
2507 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002508 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002509
2510 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002511 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002512
2513 // Retrieve the exception object. We may emit multiple blocks but
2514 // nothing can cross this so the value is already in SSA form.
Chris Lattner34b02a12009-04-22 02:26:14 +00002515 llvm::Value *Caught =
2516 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2517 ExceptionData, "caught");
Anders Carlsson273558f2009-02-07 21:37:21 +00002518 CGF.ObjCEHValueStack.back() = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002519 if (!isTry)
2520 {
2521 CGF.Builder.CreateStore(Caught, RethrowPtr);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002522 CGF.Builder.CreateStore(VMContext.getConstantIntFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002523 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002524 }
2525 else if (const ObjCAtCatchStmt* CatchStmt =
2526 cast<ObjCAtTryStmt>(S).getCatchStmts())
2527 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00002528 // Enter a new exception try block (in case a @catch block throws
2529 // an exception).
Chris Lattner34b02a12009-04-22 02:26:14 +00002530 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002531
Chris Lattner34b02a12009-04-22 02:26:14 +00002532 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlsson80f25672008-09-09 17:59:25 +00002533 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002534 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00002535
Daniel Dunbar55e87422008-11-11 02:29:29 +00002536 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
2537 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002538 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002539
2540 CGF.EmitBlock(CatchBlock);
2541
Daniel Dunbar55e40722008-09-27 07:03:52 +00002542 // Handle catch list. As a special case we check if everything is
2543 // matched and avoid generating code for falling off the end if
2544 // so.
2545 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00002546 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00002547 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00002548
Steve Naroff7ba138a2009-03-03 19:52:17 +00002549 const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00002550 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00002551
Anders Carlsson80f25672008-09-09 17:59:25 +00002552 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00002553 if (!CatchParam) {
2554 AllMatched = true;
2555 } else {
Steve Naroff14108da2009-07-10 23:34:53 +00002556 OPT = CatchParam->getType()->getAsObjCObjectPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002557
Daniel Dunbar97f61d12008-09-27 22:21:14 +00002558 // catch(id e) always matches.
2559 // FIXME: For the time being we also match id<X>; this should
2560 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00002561 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00002562 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00002563 }
2564
Daniel Dunbar55e40722008-09-27 07:03:52 +00002565 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00002566 if (CatchParam) {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002567 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002568 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002569 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002570 }
Anders Carlsson1452f552008-09-11 08:21:54 +00002571
Anders Carlssondde0a942008-09-11 09:15:33 +00002572 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002573 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002574 break;
2575 }
2576
Steve Naroff14108da2009-07-10 23:34:53 +00002577 assert(OPT && "Unexpected non-object pointer type in @catch");
2578 QualType T = OPT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00002579 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002580 assert(ObjCType && "Catch parameter must have Objective-C type!");
2581
2582 // Check if the @catch block matches the exception object.
2583 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
2584
Chris Lattner34b02a12009-04-22 02:26:14 +00002585 llvm::Value *Match =
2586 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
2587 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00002588
Daniel Dunbar55e87422008-11-11 02:29:29 +00002589 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00002590
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002591 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002592 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002593
2594 // Emit the @catch block.
2595 CGF.EmitBlock(MatchedBlock);
Steve Naroff7ba138a2009-03-03 19:52:17 +00002596 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002597 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002598
2599 llvm::Value *Tmp =
Steve Naroff7ba138a2009-03-03 19:52:17 +00002600 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002601 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002602 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002603
2604 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002605 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002606
2607 CGF.EmitBlock(NextCatchBlock);
2608 }
2609
Daniel Dunbar55e40722008-09-27 07:03:52 +00002610 if (!AllMatched) {
2611 // None of the handlers caught the exception, so store it to be
2612 // rethrown at the end of the @finally block.
2613 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002614 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002615 }
2616
2617 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002618 CGF.EmitBlock(CatchHandler);
Chris Lattner34b02a12009-04-22 02:26:14 +00002619 CGF.Builder.CreateStore(
2620 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2621 ExceptionData),
Daniel Dunbar55e40722008-09-27 07:03:52 +00002622 RethrowPtr);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002623 CGF.Builder.CreateStore(VMContext.getConstantIntFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002624 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002625 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00002626 CGF.Builder.CreateStore(Caught, RethrowPtr);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002627 CGF.Builder.CreateStore(VMContext.getConstantIntFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002628 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson80f25672008-09-09 17:59:25 +00002629 }
2630
Daniel Dunbar898d5082008-09-30 01:06:03 +00002631 // Pop the exception-handling stack entry. It is important to do
2632 // this now, because the code in the @finally block is not in this
2633 // context.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002634 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2635
Anders Carlsson273558f2009-02-07 21:37:21 +00002636 CGF.ObjCEHValueStack.pop_back();
2637
Anders Carlsson80f25672008-09-09 17:59:25 +00002638 // Emit the @finally block.
2639 CGF.EmitBlock(FinallyBlock);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002640 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2641
2642 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2643
2644 CGF.EmitBlock(FinallyExit);
Chris Lattner34b02a12009-04-22 02:26:14 +00002645 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002646
2647 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002648 if (isTry) {
2649 if (const ObjCAtFinallyStmt* FinallyStmt =
2650 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2651 CGF.EmitStmt(FinallyStmt->getFinallyBody());
Daniel Dunbar1c566672009-02-24 01:43:46 +00002652 } else {
2653 // Emit objc_sync_exit(expr); as finally's sole statement for
2654 // @synchronized.
Chris Lattnerbbccd612009-04-22 02:38:11 +00002655 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002656 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002657
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002658 // Emit the switch block
2659 if (Info.SwitchBlock)
2660 CGF.EmitBlock(Info.SwitchBlock);
2661 if (Info.EndBlock)
2662 CGF.EmitBlock(Info.EndBlock);
2663
Daniel Dunbar898d5082008-09-30 01:06:03 +00002664 CGF.EmitBlock(FinallyRethrow);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002665 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbar898d5082008-09-30 01:06:03 +00002666 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002667 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00002668
2669 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002670}
2671
2672void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002673 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002674 llvm::Value *ExceptionAsObject;
2675
2676 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2677 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2678 ExceptionAsObject =
2679 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2680 } else {
Anders Carlsson273558f2009-02-07 21:37:21 +00002681 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002682 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00002683 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002684 }
2685
Chris Lattnerbbccd612009-04-22 02:38:11 +00002686 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002687 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002688
2689 // Clear the insertion point to indicate we are in unreachable code.
2690 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002691}
2692
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002693/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002694/// object: objc_read_weak (id *src)
2695///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002696llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002697 llvm::Value *AddrWeakObj)
2698{
Eli Friedman8339b352009-03-07 03:57:15 +00002699 const llvm::Type* DestTy =
2700 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002701 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00002702 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002703 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00002704 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002705 return read_weak;
2706}
2707
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002708/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2709/// objc_assign_weak (id src, id *dst)
2710///
2711void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2712 llvm::Value *src, llvm::Value *dst)
2713{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002714 const llvm::Type * SrcTy = src->getType();
2715 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002716 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002717 assert(Size <= 8 && "does not support size > 8");
2718 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2719 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002720 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2721 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002722 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2723 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00002724 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002725 src, dst, "weakassign");
2726 return;
2727}
2728
Fariborz Jahanian58626502008-11-19 00:59:10 +00002729/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2730/// objc_assign_global (id src, id *dst)
2731///
2732void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2733 llvm::Value *src, llvm::Value *dst)
2734{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002735 const llvm::Type * SrcTy = src->getType();
2736 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002737 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002738 assert(Size <= 8 && "does not support size > 8");
2739 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2740 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002741 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2742 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002743 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2744 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002745 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00002746 src, dst, "globalassign");
2747 return;
2748}
2749
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002750/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2751/// objc_assign_ivar (id src, id *dst)
2752///
2753void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2754 llvm::Value *src, llvm::Value *dst)
2755{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002756 const llvm::Type * SrcTy = src->getType();
2757 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002758 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002759 assert(Size <= 8 && "does not support size > 8");
2760 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2761 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002762 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2763 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002764 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2765 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002766 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(),
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002767 src, dst, "assignivar");
2768 return;
2769}
2770
Fariborz Jahanian58626502008-11-19 00:59:10 +00002771/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2772/// objc_assign_strongCast (id src, id *dst)
2773///
2774void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2775 llvm::Value *src, llvm::Value *dst)
2776{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002777 const llvm::Type * SrcTy = src->getType();
2778 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002779 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002780 assert(Size <= 8 && "does not support size > 8");
2781 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2782 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002783 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2784 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002785 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2786 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002787 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00002788 src, dst, "weakassign");
2789 return;
2790}
2791
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002792void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
2793 llvm::Value *DestPtr,
2794 llvm::Value *SrcPtr,
2795 unsigned long size) {
2796 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
2797 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002798 llvm::Value *N = VMContext.getConstantInt(ObjCTypes.LongTy, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002799 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
2800 DestPtr, SrcPtr, N);
2801 return;
2802}
2803
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002804/// EmitObjCValueForIvar - Code Gen for ivar reference.
2805///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002806LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2807 QualType ObjectTy,
2808 llvm::Value *BaseValue,
2809 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002810 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00002811 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00002812 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
2813 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002814}
2815
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002816llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00002817 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002818 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00002819 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002820 return VMContext.getConstantInt(
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002821 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2822 Offset);
2823}
2824
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002825/* *** Private Interface *** */
2826
2827/// EmitImageInfo - Emit the image info marker used to encode some module
2828/// level information.
2829///
2830/// See: <rdr://4810609&4810587&4810587>
2831/// struct IMAGE_INFO {
2832/// unsigned version;
2833/// unsigned flags;
2834/// };
2835enum ImageInfoFlags {
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002836 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what
2837 // this implies.
2838 eImageInfo_GarbageCollected = (1 << 1),
2839 eImageInfo_GCOnly = (1 << 2),
2840 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
2841
2842 // A flag indicating that the module has no instances of an
2843 // @synthesize of a superclass variable. <rdar://problem/6803242>
2844 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002845};
2846
2847void CGObjCMac::EmitImageInfo() {
2848 unsigned version = 0; // Version is unused?
2849 unsigned flags = 0;
2850
2851 // FIXME: Fix and continue?
2852 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2853 flags |= eImageInfo_GarbageCollected;
2854 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2855 flags |= eImageInfo_GCOnly;
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002856
2857 // We never allow @synthesize of a superclass property.
2858 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002859
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002860 // Emitted as int[2];
2861 llvm::Constant *values[2] = {
Owen Andersona1cf15f2009-07-14 23:10:40 +00002862 VMContext.getConstantInt(llvm::Type::Int32Ty, version),
2863 VMContext.getConstantInt(llvm::Type::Int32Ty, flags)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002864 };
Owen Andersona1cf15f2009-07-14 23:10:40 +00002865 llvm::ArrayType *AT = VMContext.getArrayType(llvm::Type::Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002866
2867 const char *Section;
2868 if (ObjCABI == 1)
2869 Section = "__OBJC, __image_info,regular";
2870 else
2871 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002872 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002873 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Andersona1cf15f2009-07-14 23:10:40 +00002874 VMContext.getConstantArray(AT, values, 2),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002875 Section,
2876 0,
2877 true);
2878 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002879}
2880
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002881
2882// struct objc_module {
2883// unsigned long version;
2884// unsigned long size;
2885// const char *name;
2886// Symtab symtab;
2887// };
2888
2889// FIXME: Get from somewhere
2890static const int ModuleVersion = 7;
2891
2892void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00002893 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002894
2895 std::vector<llvm::Constant*> Values(4);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002896 Values[0] = VMContext.getConstantInt(ObjCTypes.LongTy, ModuleVersion);
2897 Values[1] = VMContext.getConstantInt(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002898 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002899 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002900 Values[3] = EmitModuleSymbols();
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002901 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Andersona1cf15f2009-07-14 23:10:40 +00002902 VMContext.getConstantStruct(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002903 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002904 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002905}
2906
2907llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002908 unsigned NumClasses = DefinedClasses.size();
2909 unsigned NumCategories = DefinedCategories.size();
2910
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002911 // Return null if no symbols were defined.
2912 if (!NumClasses && !NumCategories)
Owen Anderson69243822009-07-13 04:10:07 +00002913 return VMContext.getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002914
2915 std::vector<llvm::Constant*> Values(5);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002916 Values[0] = VMContext.getConstantInt(ObjCTypes.LongTy, 0);
Owen Anderson69243822009-07-13 04:10:07 +00002917 Values[1] = VMContext.getNullValue(ObjCTypes.SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002918 Values[2] = VMContext.getConstantInt(ObjCTypes.ShortTy, NumClasses);
2919 Values[3] = VMContext.getConstantInt(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002920
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002921 // The runtime expects exactly the list of defined classes followed
2922 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002923 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002924 for (unsigned i=0; i<NumClasses; i++)
Owen Andersona1cf15f2009-07-14 23:10:40 +00002925 Symbols[i] = VMContext.getConstantExprBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002926 ObjCTypes.Int8PtrTy);
2927 for (unsigned i=0; i<NumCategories; i++)
2928 Symbols[NumClasses + i] =
Owen Andersona1cf15f2009-07-14 23:10:40 +00002929 VMContext.getConstantExprBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002930 ObjCTypes.Int8PtrTy);
2931
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002932 Values[4] =
Owen Andersona1cf15f2009-07-14 23:10:40 +00002933 VMContext.getConstantArray(VMContext.getArrayType(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002934 NumClasses + NumCategories),
2935 Symbols);
2936
Owen Andersona1cf15f2009-07-14 23:10:40 +00002937 llvm::Constant *Init = VMContext.getConstantStruct(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002938
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002939 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002940 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
2941 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002942 4, true);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002943 return VMContext.getConstantExprBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002944}
2945
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002946llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002947 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002948 LazySymbols.insert(ID->getIdentifier());
2949
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002950 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2951
2952 if (!Entry) {
2953 llvm::Constant *Casted =
Owen Andersona1cf15f2009-07-14 23:10:40 +00002954 VMContext.getConstantExprBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002955 ObjCTypes.ClassPtrTy);
2956 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002957 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
2958 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002959 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002960 }
2961
2962 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002963}
2964
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002965llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002966 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2967
2968 if (!Entry) {
2969 llvm::Constant *Casted =
Owen Andersona1cf15f2009-07-14 23:10:40 +00002970 VMContext.getConstantExprBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002971 ObjCTypes.SelectorPtrTy);
2972 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002973 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
2974 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002975 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002976 }
2977
2978 return Builder.CreateLoad(Entry, false, "tmp");
2979}
2980
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002981llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002982 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002983
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002984 if (!Entry)
2985 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Owen Andersona1cf15f2009-07-14 23:10:40 +00002986 VMContext.getConstantArray(Ident->getName()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002987 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00002988 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002989
Owen Andersona1cf15f2009-07-14 23:10:40 +00002990 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002991}
2992
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00002993/// GetIvarLayoutName - Returns a unique constant for the given
2994/// ivar layout bitmap.
2995llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
2996 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson69243822009-07-13 04:10:07 +00002997 return VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00002998}
2999
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003000static QualType::GCAttrTypes GetGCAttrTypeForType(ASTContext &Ctx,
3001 QualType FQT) {
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003002 if (FQT.isObjCGCStrong())
3003 return QualType::Strong;
3004
3005 if (FQT.isObjCGCWeak())
3006 return QualType::Weak;
3007
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003008 if (Ctx.isObjCObjectPointerType(FQT))
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003009 return QualType::Strong;
3010
3011 if (const PointerType *PT = FQT->getAsPointerType())
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003012 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003013
3014 return QualType::GCNone;
3015}
3016
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003017void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
3018 unsigned int BytePos,
3019 bool ForStrongLayout,
3020 bool &HasUnion) {
3021 const RecordDecl *RD = RT->getDecl();
3022 // FIXME - Use iterator.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003023 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003024 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
3025 const llvm::StructLayout *RecLayout =
3026 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
3027
3028 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3029 ForStrongLayout, HasUnion);
3030}
3031
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003032void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003033 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003034 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00003035 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003036 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003037 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003038 bool IsUnion = (RD && RD->isUnion());
3039 uint64_t MaxUnionIvarSize = 0;
3040 uint64_t MaxSkippedUnionIvarSize = 0;
3041 FieldDecl *MaxField = 0;
3042 FieldDecl *MaxSkippedField = 0;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003043 FieldDecl *LastFieldBitfield = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003044 uint64_t MaxFieldOffset = 0;
3045 uint64_t MaxSkippedFieldOffset = 0;
3046 uint64_t LastBitfieldOffset = 0;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003047
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003048 if (RecFields.empty())
3049 return;
Chris Lattnerf1690852009-03-31 08:48:01 +00003050 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3051 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3052
Chris Lattnerf1690852009-03-31 08:48:01 +00003053 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003054 FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003055 uint64_t FieldOffset;
3056 if (RD)
3057 FieldOffset =
3058 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
3059 else
3060 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003061
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003062 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003063 if (!Field->getIdentifier() || Field->isBitField()) {
3064 LastFieldBitfield = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003065 LastBitfieldOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003066 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003067 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003068
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003069 LastFieldBitfield = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003070 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003071 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003072 if (FQT->isUnionType())
3073 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003074
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003075 BuildAggrIvarRecordLayout(FQT->getAsRecordType(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003076 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003077 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003078 continue;
3079 }
Chris Lattnerf1690852009-03-31 08:48:01 +00003080
3081 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003082 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003083 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003084 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003085 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003086 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003087 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3088 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003089 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003090 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003091 FQT = CArray->getElementType();
3092 }
3093
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003094 assert(!FQT->isUnionType() &&
3095 "layout for array of unions not supported");
3096 if (FQT->isRecordType()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003097 int OldIndex = IvarsInfo.size() - 1;
3098 int OldSkIndex = SkipIvars.size() -1;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003099
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003100 const RecordType *RT = FQT->getAsRecordType();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003101 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003102 ForStrongLayout, HasUnion);
3103
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003104 // Replicate layout information for each array element. Note that
3105 // one element is already done.
3106 uint64_t ElIx = 1;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003107 for (int FirstIndex = IvarsInfo.size() - 1,
3108 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003109 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003110 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3111 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3112 IvarsInfo[i].ivar_size));
3113 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3114 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3115 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003116 }
3117 continue;
3118 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003119 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003120 // At this point, we are done with Record/Union and array there of.
3121 // For other arrays we are down to its element type.
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003122 QualType::GCAttrTypes GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003123
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003124 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003125 if ((ForStrongLayout && GCAttr == QualType::Strong)
3126 || (!ForStrongLayout && GCAttr == QualType::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003127 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003128 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003129 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003130 MaxUnionIvarSize = UnionIvarSize;
3131 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003132 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003133 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003134 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003135 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003136 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003137 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003138 } else if ((ForStrongLayout &&
3139 (GCAttr == QualType::GCNone || GCAttr == QualType::Weak))
3140 || (!ForStrongLayout && GCAttr != QualType::Weak)) {
3141 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003142 // FIXME: Why the asymmetry? We divide by word size in bits on other
3143 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003144 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003145 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003146 MaxSkippedUnionIvarSize = UnionIvarSize;
3147 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003148 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003149 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003150 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003151 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003152 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003153 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003154 }
3155 }
3156 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003157
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003158 if (LastFieldBitfield) {
3159 // Last field was a bitfield. Must update skip info.
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003160 Expr *BitWidth = LastFieldBitfield->getBitWidth();
3161 uint64_t BitFieldSize =
Eli Friedman9a901bb2009-04-26 19:19:15 +00003162 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Daniel Dunbar487993b2009-05-03 13:32:01 +00003163 GC_IVAR skivar;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003164 skivar.ivar_bytepos = BytePos + LastBitfieldOffset;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003165 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3166 + ((BitFieldSize % ByteSizeInBits) != 0);
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003167 SkipIvars.push_back(skivar);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003168 }
3169
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003170 if (MaxField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003171 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003172 MaxUnionIvarSize));
3173 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003174 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003175 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003176}
3177
3178/// BuildIvarLayout - Builds ivar layout bitmap for the class
3179/// implementation for the __strong or __weak case.
3180/// The layout map displays which words in ivar list must be skipped
3181/// and which must be scanned by GC (see below). String is built of bytes.
3182/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3183/// of words to skip and right nibble is count of words to scan. So, each
3184/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3185/// represented by a 0x00 byte which also ends the string.
3186/// 1. when ForStrongLayout is true, following ivars are scanned:
3187/// - id, Class
3188/// - object *
3189/// - __strong anything
3190///
3191/// 2. When ForStrongLayout is false, following ivars are scanned:
3192/// - __weak anything
3193///
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003194llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003195 const ObjCImplementationDecl *OMD,
3196 bool ForStrongLayout) {
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003197 bool hasUnion = false;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003198
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003199 unsigned int WordsToScan, WordsToSkip;
Owen Andersona1cf15f2009-07-14 23:10:40 +00003200 const llvm::Type *PtrTy = VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003201 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
Owen Anderson69243822009-07-13 04:10:07 +00003202 return VMContext.getNullValue(PtrTy);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003203
Chris Lattnerf1690852009-03-31 08:48:01 +00003204 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003205 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003206 CGM.getContext().CollectObjCIvars(OI, RecFields);
Fariborz Jahanian98200742009-05-12 18:14:29 +00003207
Daniel Dunbar37153282009-05-04 04:10:48 +00003208 // Add this implementations synthesized ivars.
Fariborz Jahanian98200742009-05-12 18:14:29 +00003209 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
3210 CGM.getContext().CollectSynthesizedIvars(OI, Ivars);
3211 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3212 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3213
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003214 if (RecFields.empty())
Owen Anderson69243822009-07-13 04:10:07 +00003215 return VMContext.getNullValue(PtrTy);
Chris Lattnerf1690852009-03-31 08:48:01 +00003216
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003217 SkipIvars.clear();
3218 IvarsInfo.clear();
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00003219
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003220 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003221 if (IvarsInfo.empty())
Owen Anderson69243822009-07-13 04:10:07 +00003222 return VMContext.getNullValue(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003223
3224 // Sort on byte position in case we encounterred a union nested in
3225 // the ivar list.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003226 if (hasUnion && !IvarsInfo.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003227 std::sort(IvarsInfo.begin(), IvarsInfo.end());
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003228 if (hasUnion && !SkipIvars.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003229 std::sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003230
3231 // Build the string of skip/scan nibbles
Fariborz Jahanian8c2f2d12009-04-24 17:15:27 +00003232 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003233 unsigned int WordSize =
Duncan Sands9408c452009-05-09 07:08:47 +00003234 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003235 if (IvarsInfo[0].ivar_bytepos == 0) {
3236 WordsToSkip = 0;
3237 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003238 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003239 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3240 WordsToScan = IvarsInfo[0].ivar_size;
3241 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003242 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003243 unsigned int TailPrevGCObjC =
3244 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003245 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003246 // consecutive 'scanned' object pointers.
3247 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003248 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003249 // Skip over 'gc'able object pointer which lay over each other.
3250 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3251 continue;
3252 // Must skip over 1 or more words. We save current skip/scan values
3253 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003254 SKIP_SCAN SkScan;
3255 SkScan.skip = WordsToSkip;
3256 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003257 SkipScanIvars.push_back(SkScan);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003258
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003259 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003260 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3261 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003262 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003263 WordsToSkip = 0;
3264 WordsToScan = IvarsInfo[i].ivar_size;
3265 }
3266 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003267 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003268 SKIP_SCAN SkScan;
3269 SkScan.skip = WordsToSkip;
3270 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003271 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003272 }
3273
3274 bool BytesSkipped = false;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003275 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003276 unsigned int LastIndex = SkipIvars.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003277 int LastByteSkipped =
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003278 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
3279 LastIndex = IvarsInfo.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003280 int LastByteScanned =
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003281 IvarsInfo[LastIndex].ivar_bytepos +
3282 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003283 BytesSkipped = (LastByteSkipped > LastByteScanned);
3284 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003285 if (BytesSkipped) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003286 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003287 SKIP_SCAN SkScan;
3288 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3289 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003290 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003291 }
3292 }
3293 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3294 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003295 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003296 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003297 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3298 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3299 // 0xM0 followed by 0x0N detected.
3300 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3301 for (int j = i+1; j < SkipScan; j++)
3302 SkipScanIvars[j] = SkipScanIvars[j+1];
3303 --SkipScan;
3304 }
3305 }
3306
3307 // Generate the string.
3308 std::string BitMap;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003309 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003310 unsigned char byte;
3311 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3312 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3313 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3314 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
3315
3316 if (skip_small > 0 || skip_big > 0)
3317 BytesSkipped = true;
3318 // first skip big.
3319 for (unsigned int ix = 0; ix < skip_big; ix++)
3320 BitMap += (unsigned char)(0xf0);
3321
3322 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003323 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003324 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003325 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003326 byte |= 0xf;
3327 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003328 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003329 byte |= scan_small;
3330 scan_small = 0;
3331 }
3332 BitMap += byte;
3333 }
3334 // next scan big
3335 for (unsigned int ix = 0; ix < scan_big; ix++)
3336 BitMap += (unsigned char)(0x0f);
3337 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003338 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003339 byte = scan_small;
3340 BitMap += byte;
3341 }
3342 }
3343 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003344 unsigned char zero = 0;
3345 BitMap += zero;
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003346
3347 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
3348 printf("\n%s ivar layout for class '%s': ",
3349 ForStrongLayout ? "strong" : "weak",
3350 OMD->getClassInterface()->getNameAsCString());
3351 const unsigned char *s = (unsigned char*)BitMap.c_str();
3352 for (unsigned i = 0; i < BitMap.size(); i++)
3353 if (!(s[i] & 0xf0))
3354 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3355 else
3356 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3357 printf("\n");
3358 }
3359
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003360 // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as
3361 // final layout.
3362 if (ForStrongLayout && !BytesSkipped)
Owen Anderson69243822009-07-13 04:10:07 +00003363 return VMContext.getNullValue(PtrTy);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003364 llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Owen Andersona1cf15f2009-07-14 23:10:40 +00003365 VMContext.getConstantArray(BitMap.c_str()),
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003366 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003367 1, true);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003368 return getConstantGEP(VMContext, Entry, 0, 0);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003369}
3370
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003371llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003372 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3373
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003374 // FIXME: Avoid std::string copying.
3375 if (!Entry)
3376 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Andersona1cf15f2009-07-14 23:10:40 +00003377 VMContext.getConstantArray(Sel.getAsString()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003378 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003379 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003380
Owen Andersona1cf15f2009-07-14 23:10:40 +00003381 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003382}
3383
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003384// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003385llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003386 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3387}
3388
3389// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003390llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003391 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
3392}
3393
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003394llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003395 std::string TypeStr;
3396 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3397
3398 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003399
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003400 if (!Entry)
3401 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Andersona1cf15f2009-07-14 23:10:40 +00003402 VMContext.getConstantArray(TypeStr),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003403 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003404 1, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003405
Owen Andersona1cf15f2009-07-14 23:10:40 +00003406 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003407}
3408
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003409llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003410 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003411 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3412 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00003413
3414 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3415
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003416 if (!Entry)
3417 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Andersona1cf15f2009-07-14 23:10:40 +00003418 VMContext.getConstantArray(TypeStr),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003419 "__TEXT,__cstring,cstring_literals",
3420 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00003421
Owen Andersona1cf15f2009-07-14 23:10:40 +00003422 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003423}
3424
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003425// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003426llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003427 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
3428
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003429 if (!Entry)
3430 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Owen Andersona1cf15f2009-07-14 23:10:40 +00003431 VMContext.getConstantArray(Ident->getName()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003432 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003433 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003434
Owen Andersona1cf15f2009-07-14 23:10:40 +00003435 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003436}
3437
3438// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003439// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003440llvm::Constant *
3441 CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3442 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003443 std::string TypeStr;
3444 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003445 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3446}
3447
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003448void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
3449 const ObjCContainerDecl *CD,
3450 std::string &NameOut) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00003451 NameOut = '\01';
3452 NameOut += (D->isInstanceMethod() ? '-' : '+');
Chris Lattner077bf5e2008-11-24 03:33:13 +00003453 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003454 assert (CD && "Missing container decl in GetNameForMethod");
3455 NameOut += CD->getNameAsString();
Fariborz Jahanian1e9aef32009-04-16 18:34:20 +00003456 if (const ObjCCategoryImplDecl *CID =
3457 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) {
3458 NameOut += '(';
3459 NameOut += CID->getNameAsString();
3460 NameOut+= ')';
3461 }
Chris Lattner077bf5e2008-11-24 03:33:13 +00003462 NameOut += ' ';
3463 NameOut += D->getSelector().getAsString();
3464 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003465}
3466
Fariborz Jahanianc38e9af2009-06-23 21:47:46 +00003467void CGObjCCommonMac::MergeMetadataGlobals(
3468 std::vector<llvm::Constant*> &UsedArray) {
Owen Andersona1cf15f2009-07-14 23:10:40 +00003469 llvm::Type *i8PTy = VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty);
Fariborz Jahanianc38e9af2009-06-23 21:47:46 +00003470 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3471 e = UsedGlobals.end(); i != e; ++i) {
Owen Andersona1cf15f2009-07-14 23:10:40 +00003472 UsedArray.push_back(
3473 VMContext.getConstantExprBitCast(cast<llvm::Constant>(*i),
Fariborz Jahanianc38e9af2009-06-23 21:47:46 +00003474 i8PTy));
3475 }
3476}
3477
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003478void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003479 EmitModuleInfo();
3480
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003481 // Emit the dummy bodies for any protocols which were referenced but
3482 // never defined.
3483 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
3484 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
3485 if (i->second->hasInitializer())
3486 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003487
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003488 std::vector<llvm::Constant*> Values(5);
Owen Anderson69243822009-07-13 04:10:07 +00003489 Values[0] = VMContext.getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003490 Values[1] = GetClassName(i->first);
Owen Anderson69243822009-07-13 04:10:07 +00003491 Values[2] = VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003492 Values[3] = Values[4] =
Owen Anderson69243822009-07-13 04:10:07 +00003493 VMContext.getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003494 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003495 i->second->setInitializer(VMContext.getConstantStruct(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003496 Values));
3497 }
3498
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003499 // Add assembler directives to add lazy undefined symbol references
3500 // for classes which are referenced but not defined. This is
3501 // important for correct linker interaction.
3502
3503 // FIXME: Uh, this isn't particularly portable.
3504 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00003505
3506 if (!CGM.getModule().getModuleInlineAsm().empty())
3507 s << "\n";
3508
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003509 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
3510 e = LazySymbols.end(); i != e; ++i) {
3511 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
3512 }
3513 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
3514 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003515 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003516 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
3517 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00003518
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003519 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003520}
3521
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003522CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003523 : CGObjCCommonMac(cgm),
3524 ObjCTypes(cgm)
3525{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003526 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003527 ObjCABI = 2;
3528}
3529
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003530/* *** */
3531
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003532ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Owen Andersona1cf15f2009-07-14 23:10:40 +00003533: VMContext(cgm.getLLVMContext()), CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003534{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003535 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3536 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003537
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003538 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003539 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003540 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003541 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003542 Int8PtrTy = VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003543
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003544 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Andersona1cf15f2009-07-14 23:10:40 +00003545 PtrObjectPtrTy = VMContext.getPointerTypeUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003546 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003547
Mike Stumpf5408fe2009-05-16 07:57:57 +00003548 // FIXME: It would be nice to unify this with the opaque type, so that the IR
3549 // comes out a bit cleaner.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003550 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Andersona1cf15f2009-07-14 23:10:40 +00003551 ExternalProtocolPtrTy = VMContext.getPointerTypeUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003552
3553 // I'm not sure I like this. The implicit coordination is a bit
3554 // gross. We should solve this in a reasonable fashion because this
3555 // is a pretty common task (match some runtime data structure with
3556 // an LLVM data structure).
3557
3558 // FIXME: This is leaked.
3559 // FIXME: Merge with rewriter code?
3560
3561 // struct _objc_super {
3562 // id self;
3563 // Class cls;
3564 // }
3565 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3566 SourceLocation(),
3567 &Ctx.Idents.get("_objc_super"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003568 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Douglas Gregor6ab35242009-04-09 21:40:53 +00003569 Ctx.getObjCIdType(), 0, false));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003570 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Douglas Gregor6ab35242009-04-09 21:40:53 +00003571 Ctx.getObjCClassType(), 0, false));
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003572 RD->completeDefinition(Ctx);
3573
3574 SuperCTy = Ctx.getTagDeclType(RD);
3575 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
3576
3577 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +00003578 SuperPtrTy = VMContext.getPointerTypeUnqual(SuperTy);
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003579
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003580 // struct _prop_t {
3581 // char *name;
3582 // char *attributes;
3583 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003584 PropertyTy = VMContext.getStructType(Int8PtrTy, Int8PtrTy, NULL);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003585 CGM.getModule().addTypeName("struct._prop_t",
3586 PropertyTy);
3587
3588 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003589 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003590 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003591 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003592 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003593 PropertyListTy = VMContext.getStructType(IntTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003594 IntTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00003595 VMContext.getArrayType(PropertyTy, 0),
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003596 NULL);
3597 CGM.getModule().addTypeName("struct._prop_list_t",
3598 PropertyListTy);
3599 // struct _prop_list_t *
Owen Andersona1cf15f2009-07-14 23:10:40 +00003600 PropertyListPtrTy = VMContext.getPointerTypeUnqual(PropertyListTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003601
3602 // struct _objc_method {
3603 // SEL _cmd;
3604 // char *method_type;
3605 // char *_imp;
3606 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003607 MethodTy = VMContext.getStructType(SelectorPtrTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003608 Int8PtrTy,
3609 Int8PtrTy,
3610 NULL);
3611 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003612
3613 // struct _objc_cache *
Owen Andersona1cf15f2009-07-14 23:10:40 +00003614 CacheTy = VMContext.getOpaqueType();
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003615 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003616 CachePtrTy = VMContext.getPointerTypeUnqual(CacheTy);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003617}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003618
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003619ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
3620 : ObjCCommonTypesHelper(cgm)
3621{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003622 // struct _objc_method_description {
3623 // SEL name;
3624 // char *types;
3625 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003626 MethodDescriptionTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +00003627 VMContext.getStructType(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003628 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003629 NULL);
3630 CGM.getModule().addTypeName("struct._objc_method_description",
3631 MethodDescriptionTy);
3632
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003633 // struct _objc_method_description_list {
3634 // int count;
3635 // struct _objc_method_description[1];
3636 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003637 MethodDescriptionListTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +00003638 VMContext.getStructType(IntTy,
3639 VMContext.getArrayType(MethodDescriptionTy, 0),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003640 NULL);
3641 CGM.getModule().addTypeName("struct._objc_method_description_list",
3642 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003643
3644 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003645 MethodDescriptionListPtrTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +00003646 VMContext.getPointerTypeUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003647
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003648 // Protocol description structures
3649
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003650 // struct _objc_protocol_extension {
3651 // uint32_t size; // sizeof(struct _objc_protocol_extension)
3652 // struct _objc_method_description_list *optional_instance_methods;
3653 // struct _objc_method_description_list *optional_class_methods;
3654 // struct _objc_property_list *instance_properties;
3655 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003656 ProtocolExtensionTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +00003657 VMContext.getStructType(IntTy,
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003658 MethodDescriptionListPtrTy,
3659 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003660 PropertyListPtrTy,
3661 NULL);
3662 CGM.getModule().addTypeName("struct._objc_protocol_extension",
3663 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003664
3665 // struct _objc_protocol_extension *
Owen Andersona1cf15f2009-07-14 23:10:40 +00003666 ProtocolExtensionPtrTy = VMContext.getPointerTypeUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003667
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003668 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003669
Owen Andersona1cf15f2009-07-14 23:10:40 +00003670 llvm::PATypeHolder ProtocolTyHolder = VMContext.getOpaqueType();
3671 llvm::PATypeHolder ProtocolListTyHolder = VMContext.getOpaqueType();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003672
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003673 const llvm::Type *T =
Owen Andersona1cf15f2009-07-14 23:10:40 +00003674 VMContext.getStructType(VMContext.getPointerTypeUnqual(ProtocolListTyHolder),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003675 LongTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00003676 VMContext.getArrayType(ProtocolTyHolder, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003677 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003678 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
3679
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003680 // struct _objc_protocol {
3681 // struct _objc_protocol_extension *isa;
3682 // char *protocol_name;
3683 // struct _objc_protocol **_objc_protocol_list;
3684 // struct _objc_method_description_list *instance_methods;
3685 // struct _objc_method_description_list *class_methods;
3686 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003687 T = VMContext.getStructType(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003688 Int8PtrTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00003689 VMContext.getPointerTypeUnqual(ProtocolListTyHolder),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003690 MethodDescriptionListPtrTy,
3691 MethodDescriptionListPtrTy,
3692 NULL);
3693 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
3694
3695 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
3696 CGM.getModule().addTypeName("struct._objc_protocol_list",
3697 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003698 // struct _objc_protocol_list *
Owen Andersona1cf15f2009-07-14 23:10:40 +00003699 ProtocolListPtrTy = VMContext.getPointerTypeUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003700
3701 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003702 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003703 ProtocolPtrTy = VMContext.getPointerTypeUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003704
3705 // Class description structures
3706
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003707 // struct _objc_ivar {
3708 // char *ivar_name;
3709 // char *ivar_type;
3710 // int ivar_offset;
3711 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003712 IvarTy = VMContext.getStructType(Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003713 Int8PtrTy,
3714 IntTy,
3715 NULL);
3716 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
3717
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003718 // struct _objc_ivar_list *
Owen Andersona1cf15f2009-07-14 23:10:40 +00003719 IvarListTy = VMContext.getOpaqueType();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003720 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003721 IvarListPtrTy = VMContext.getPointerTypeUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003722
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003723 // struct _objc_method_list *
Owen Andersona1cf15f2009-07-14 23:10:40 +00003724 MethodListTy = VMContext.getOpaqueType();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003725 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003726 MethodListPtrTy = VMContext.getPointerTypeUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003727
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003728 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003729 ClassExtensionTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +00003730 VMContext.getStructType(IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003731 Int8PtrTy,
3732 PropertyListPtrTy,
3733 NULL);
3734 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003735 ClassExtensionPtrTy = VMContext.getPointerTypeUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003736
Owen Andersona1cf15f2009-07-14 23:10:40 +00003737 llvm::PATypeHolder ClassTyHolder = VMContext.getOpaqueType();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003738
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003739 // struct _objc_class {
3740 // Class isa;
3741 // Class super_class;
3742 // char *name;
3743 // long version;
3744 // long info;
3745 // long instance_size;
3746 // struct _objc_ivar_list *ivars;
3747 // struct _objc_method_list *methods;
3748 // struct _objc_cache *cache;
3749 // struct _objc_protocol_list *protocols;
3750 // char *ivar_layout;
3751 // struct _objc_class_ext *ext;
3752 // };
Owen Andersona1cf15f2009-07-14 23:10:40 +00003753 T = VMContext.getStructType(VMContext.getPointerTypeUnqual(ClassTyHolder),
3754 VMContext.getPointerTypeUnqual(ClassTyHolder),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003755 Int8PtrTy,
3756 LongTy,
3757 LongTy,
3758 LongTy,
3759 IvarListPtrTy,
3760 MethodListPtrTy,
3761 CachePtrTy,
3762 ProtocolListPtrTy,
3763 Int8PtrTy,
3764 ClassExtensionPtrTy,
3765 NULL);
3766 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
3767
3768 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
3769 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003770 ClassPtrTy = VMContext.getPointerTypeUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003771
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003772 // struct _objc_category {
3773 // char *category_name;
3774 // char *class_name;
3775 // struct _objc_method_list *instance_method;
3776 // struct _objc_method_list *class_method;
3777 // uint32_t size; // sizeof(struct _objc_category)
3778 // struct _objc_property_list *instance_properties;// category's @property
3779 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003780 CategoryTy = VMContext.getStructType(Int8PtrTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003781 Int8PtrTy,
3782 MethodListPtrTy,
3783 MethodListPtrTy,
3784 ProtocolListPtrTy,
3785 IntTy,
3786 PropertyListPtrTy,
3787 NULL);
3788 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
3789
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003790 // Global metadata structures
3791
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003792 // struct _objc_symtab {
3793 // long sel_ref_cnt;
3794 // SEL *refs;
3795 // short cls_def_cnt;
3796 // short cat_def_cnt;
3797 // char *defs[cls_def_cnt + cat_def_cnt];
3798 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003799 SymtabTy = VMContext.getStructType(LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003800 SelectorPtrTy,
3801 ShortTy,
3802 ShortTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00003803 VMContext.getArrayType(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003804 NULL);
3805 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003806 SymtabPtrTy = VMContext.getPointerTypeUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003807
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003808 // struct _objc_module {
3809 // long version;
3810 // long size; // sizeof(struct _objc_module)
3811 // char *name;
3812 // struct _objc_symtab* symtab;
3813 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003814 ModuleTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +00003815 VMContext.getStructType(LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003816 LongTy,
3817 Int8PtrTy,
3818 SymtabPtrTy,
3819 NULL);
3820 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003821
Anders Carlsson2abd89c2008-08-31 04:05:03 +00003822
Mike Stumpf5408fe2009-05-16 07:57:57 +00003823 // FIXME: This is the size of the setjmp buffer and should be target
3824 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00003825 uint64_t SetJmpBufferSize = 18;
3826
3827 // Exceptions
Owen Andersona1cf15f2009-07-14 23:10:40 +00003828 const llvm::Type *StackPtrTy = VMContext.getArrayType(
3829 VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00003830
3831 ExceptionDataTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +00003832 VMContext.getStructType(VMContext.getArrayType(llvm::Type::Int32Ty,
Anders Carlsson124526b2008-09-09 10:10:21 +00003833 SetJmpBufferSize),
3834 StackPtrTy, NULL);
3835 CGM.getModule().addTypeName("struct._objc_exception_data",
3836 ExceptionDataTy);
3837
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003838}
3839
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003840ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003841: ObjCCommonTypesHelper(cgm)
3842{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003843 // struct _method_list_t {
3844 // uint32_t entsize; // sizeof(struct _objc_method)
3845 // uint32_t method_count;
3846 // struct _objc_method method_list[method_count];
3847 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003848 MethodListnfABITy = VMContext.getStructType(IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003849 IntTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00003850 VMContext.getArrayType(MethodTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003851 NULL);
3852 CGM.getModule().addTypeName("struct.__method_list_t",
3853 MethodListnfABITy);
3854 // struct method_list_t *
Owen Andersona1cf15f2009-07-14 23:10:40 +00003855 MethodListnfABIPtrTy = VMContext.getPointerTypeUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003856
3857 // struct _protocol_t {
3858 // id isa; // NULL
3859 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003860 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003861 // const struct method_list_t * const instance_methods;
3862 // const struct method_list_t * const class_methods;
3863 // const struct method_list_t *optionalInstanceMethods;
3864 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003865 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003866 // const uint32_t size; // sizeof(struct _protocol_t)
3867 // const uint32_t flags; // = 0
3868 // }
3869
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003870 // Holder for struct _protocol_list_t *
Owen Andersona1cf15f2009-07-14 23:10:40 +00003871 llvm::PATypeHolder ProtocolListTyHolder = VMContext.getOpaqueType();
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003872
Owen Andersona1cf15f2009-07-14 23:10:40 +00003873 ProtocolnfABITy = VMContext.getStructType(ObjectPtrTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003874 Int8PtrTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00003875 VMContext.getPointerTypeUnqual(
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003876 ProtocolListTyHolder),
3877 MethodListnfABIPtrTy,
3878 MethodListnfABIPtrTy,
3879 MethodListnfABIPtrTy,
3880 MethodListnfABIPtrTy,
3881 PropertyListPtrTy,
3882 IntTy,
3883 IntTy,
3884 NULL);
3885 CGM.getModule().addTypeName("struct._protocol_t",
3886 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003887
3888 // struct _protocol_t*
Owen Andersona1cf15f2009-07-14 23:10:40 +00003889 ProtocolnfABIPtrTy = VMContext.getPointerTypeUnqual(ProtocolnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003890
Fariborz Jahanianda320092009-01-29 19:24:30 +00003891 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003892 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00003893 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003894 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003895 ProtocolListnfABITy = VMContext.getStructType(LongTy,
3896 VMContext.getArrayType(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003897 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003898 NULL);
3899 CGM.getModule().addTypeName("struct._objc_protocol_list",
3900 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003901 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3902 ProtocolListnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003903
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003904 // struct _objc_protocol_list*
Owen Andersona1cf15f2009-07-14 23:10:40 +00003905 ProtocolListnfABIPtrTy = VMContext.getPointerTypeUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003906
3907 // struct _ivar_t {
3908 // unsigned long int *offset; // pointer to ivar offset location
3909 // char *name;
3910 // char *type;
3911 // uint32_t alignment;
3912 // uint32_t size;
3913 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003914 IvarnfABITy = VMContext.getStructType(VMContext.getPointerTypeUnqual(LongTy),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003915 Int8PtrTy,
3916 Int8PtrTy,
3917 IntTy,
3918 IntTy,
3919 NULL);
3920 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3921
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003922 // struct _ivar_list_t {
3923 // uint32 entsize; // sizeof(struct _ivar_t)
3924 // uint32 count;
3925 // struct _iver_t list[count];
3926 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003927 IvarListnfABITy = VMContext.getStructType(IntTy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003928 IntTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00003929 VMContext.getArrayType(
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003930 IvarnfABITy, 0),
3931 NULL);
3932 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3933
Owen Andersona1cf15f2009-07-14 23:10:40 +00003934 IvarListnfABIPtrTy = VMContext.getPointerTypeUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003935
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003936 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003937 // uint32_t const flags;
3938 // uint32_t const instanceStart;
3939 // uint32_t const instanceSize;
3940 // uint32_t const reserved; // only when building for 64bit targets
3941 // const uint8_t * const ivarLayout;
3942 // const char *const name;
3943 // const struct _method_list_t * const baseMethods;
3944 // const struct _objc_protocol_list *const baseProtocols;
3945 // const struct _ivar_list_t *const ivars;
3946 // const uint8_t * const weakIvarLayout;
3947 // const struct _prop_list_t * const properties;
3948 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003949
3950 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Andersona1cf15f2009-07-14 23:10:40 +00003951 ClassRonfABITy = VMContext.getStructType(IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003952 IntTy,
3953 IntTy,
3954 Int8PtrTy,
3955 Int8PtrTy,
3956 MethodListnfABIPtrTy,
3957 ProtocolListnfABIPtrTy,
3958 IvarListnfABIPtrTy,
3959 Int8PtrTy,
3960 PropertyListPtrTy,
3961 NULL);
3962 CGM.getModule().addTypeName("struct._class_ro_t",
3963 ClassRonfABITy);
3964
3965 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3966 std::vector<const llvm::Type*> Params;
3967 Params.push_back(ObjectPtrTy);
3968 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003969 ImpnfABITy = VMContext.getPointerTypeUnqual(
3970 VMContext.getFunctionType(ObjectPtrTy, Params, false));
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003971
3972 // struct _class_t {
3973 // struct _class_t *isa;
3974 // struct _class_t * const superclass;
3975 // void *cache;
3976 // IMP *vtable;
3977 // struct class_ro_t *ro;
3978 // }
3979
Owen Andersona1cf15f2009-07-14 23:10:40 +00003980 llvm::PATypeHolder ClassTyHolder = VMContext.getOpaqueType();
3981 ClassnfABITy =
3982 VMContext.getStructType(VMContext.getPointerTypeUnqual(ClassTyHolder),
3983 VMContext.getPointerTypeUnqual(ClassTyHolder),
3984 CachePtrTy,
3985 VMContext.getPointerTypeUnqual(ImpnfABITy),
3986 VMContext.getPointerTypeUnqual(ClassRonfABITy),
3987 NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003988 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3989
3990 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3991 ClassnfABITy);
3992
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003993 // LLVM for struct _class_t *
Owen Andersona1cf15f2009-07-14 23:10:40 +00003994 ClassnfABIPtrTy = VMContext.getPointerTypeUnqual(ClassnfABITy);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003995
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003996 // struct _category_t {
3997 // const char * const name;
3998 // struct _class_t *const cls;
3999 // const struct _method_list_t * const instance_methods;
4000 // const struct _method_list_t * const class_methods;
4001 // const struct _protocol_list_t * const protocols;
4002 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004003 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00004004 CategorynfABITy = VMContext.getStructType(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004005 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004006 MethodListnfABIPtrTy,
4007 MethodListnfABIPtrTy,
4008 ProtocolListnfABIPtrTy,
4009 PropertyListPtrTy,
4010 NULL);
4011 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004012
4013 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004014 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4015 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004016
4017 // MessageRefTy - LLVM for:
4018 // struct _message_ref_t {
4019 // IMP messenger;
4020 // SEL name;
4021 // };
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004022
4023 // First the clang type for struct _message_ref_t
4024 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
4025 SourceLocation(),
4026 &Ctx.Idents.get("_message_ref_t"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004027 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Douglas Gregor6ab35242009-04-09 21:40:53 +00004028 Ctx.VoidPtrTy, 0, false));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004029 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Douglas Gregor6ab35242009-04-09 21:40:53 +00004030 Ctx.getObjCSelType(), 0, false));
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004031 RD->completeDefinition(Ctx);
4032
4033 MessageRefCTy = Ctx.getTagDeclType(RD);
4034 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4035 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004036
4037 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Andersona1cf15f2009-07-14 23:10:40 +00004038 MessageRefPtrTy = VMContext.getPointerTypeUnqual(MessageRefTy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004039
4040 // SuperMessageRefTy - LLVM for:
4041 // struct _super_message_ref_t {
4042 // SUPER_IMP messenger;
4043 // SEL name;
4044 // };
Owen Andersona1cf15f2009-07-14 23:10:40 +00004045 SuperMessageRefTy = VMContext.getStructType(ImpnfABITy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004046 SelectorPtrTy,
4047 NULL);
4048 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
4049
4050 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Owen Andersona1cf15f2009-07-14 23:10:40 +00004051 SuperMessageRefPtrTy = VMContext.getPointerTypeUnqual(SuperMessageRefTy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004052
Daniel Dunbare588b992009-03-01 04:46:24 +00004053
4054 // struct objc_typeinfo {
4055 // const void** vtable; // objc_ehtype_vtable + 2
4056 // const char* name; // c++ typeinfo string
4057 // Class cls;
4058 // };
Owen Andersona1cf15f2009-07-14 23:10:40 +00004059 EHTypeTy = VMContext.getStructType(VMContext.getPointerTypeUnqual(Int8PtrTy),
Daniel Dunbare588b992009-03-01 04:46:24 +00004060 Int8PtrTy,
4061 ClassnfABIPtrTy,
4062 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004063 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004064 EHTypePtrTy = VMContext.getPointerTypeUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004065}
4066
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004067llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
4068 FinishNonFragileABIModule();
4069
4070 return NULL;
4071}
4072
Daniel Dunbar463b8762009-05-15 21:48:48 +00004073void CGObjCNonFragileABIMac::AddModuleClassList(const
4074 std::vector<llvm::GlobalValue*>
4075 &Container,
4076 const char *SymbolName,
4077 const char *SectionName) {
4078 unsigned NumClasses = Container.size();
4079
4080 if (!NumClasses)
4081 return;
4082
4083 std::vector<llvm::Constant*> Symbols(NumClasses);
4084 for (unsigned i=0; i<NumClasses; i++)
Owen Andersona1cf15f2009-07-14 23:10:40 +00004085 Symbols[i] = VMContext.getConstantExprBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004086 ObjCTypes.Int8PtrTy);
4087 llvm::Constant* Init =
Owen Andersona1cf15f2009-07-14 23:10:40 +00004088 VMContext.getConstantArray(VMContext.getArrayType(ObjCTypes.Int8PtrTy,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004089 NumClasses),
4090 Symbols);
4091
4092 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004093 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004094 llvm::GlobalValue::InternalLinkage,
4095 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004096 SymbolName);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004097 GV->setAlignment(8);
4098 GV->setSection(SectionName);
4099 UsedGlobals.push_back(GV);
4100}
4101
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004102void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4103 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004104
Daniel Dunbar463b8762009-05-15 21:48:48 +00004105 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004106 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar463b8762009-05-15 21:48:48 +00004107 AddModuleClassList(DefinedClasses,
4108 "\01L_OBJC_LABEL_CLASS_$",
4109 "__DATA, __objc_classlist, regular, no_dead_strip");
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004110 AddModuleClassList(DefinedNonLazyClasses,
4111 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4112 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004113
4114 // Build list of all implemented category addresses in array
4115 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar463b8762009-05-15 21:48:48 +00004116 AddModuleClassList(DefinedCategories,
4117 "\01L_OBJC_LABEL_CATEGORY_$",
4118 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004119 AddModuleClassList(DefinedNonLazyCategories,
4120 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4121 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004122
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004123 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
4124 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
4125 std::vector<llvm::Constant*> Values(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004126 Values[0] = VMContext.getConstantInt(ObjCTypes.IntTy, 0);
Fariborz Jahanian067986e2009-02-24 21:08:09 +00004127 unsigned int flags = 0;
Fariborz Jahanian66a5c2c2009-02-24 23:34:44 +00004128 // FIXME: Fix and continue?
4129 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
4130 flags |= eImageInfo_GarbageCollected;
4131 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
4132 flags |= eImageInfo_GCOnly;
Owen Andersona1cf15f2009-07-14 23:10:40 +00004133 Values[1] = VMContext.getConstantInt(ObjCTypes.IntTy, flags);
4134 llvm::Constant* Init = VMContext.getConstantArray(
4135 VMContext.getArrayType(ObjCTypes.IntTy, 2),
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004136 Values);
4137 llvm::GlobalVariable *IMGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004138 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004139 llvm::GlobalValue::InternalLinkage,
4140 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004141 "\01L_OBJC_IMAGE_INFO");
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004142 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
Daniel Dunbar325f7582009-04-23 08:03:21 +00004143 IMGV->setConstant(true);
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004144 UsedGlobals.push_back(IMGV);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004145}
4146
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004147/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004148/// NonLegacyDispatchMethods; false otherwise. What this means is that
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004149/// except for the 19 selectors in the list, we generate 32bit-style
4150/// message dispatch call for all the rest.
4151///
4152bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004153 if (NonLegacyDispatchMethods.empty()) {
4154 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4155 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4156 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4157 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4158 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4159 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4160 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4161 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4162 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4163 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
4164
4165 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4166 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4167 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4168 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4169 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4170 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4171 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4172 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004173 // "countByEnumeratingWithState:objects:count"
4174 IdentifierInfo *KeyIdents[] = {
4175 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4176 &CGM.getContext().Idents.get("objects"),
4177 &CGM.getContext().Idents.get("count")
4178 };
4179 NonLegacyDispatchMethods.insert(
4180 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004181 }
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004182 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004183}
4184
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004185// Metadata flags
4186enum MetaDataDlags {
4187 CLS = 0x0,
4188 CLS_META = 0x1,
4189 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004190 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004191 CLS_EXCEPTION = 0x20
4192};
4193/// BuildClassRoTInitializer - generate meta-data for:
4194/// struct _class_ro_t {
4195/// uint32_t const flags;
4196/// uint32_t const instanceStart;
4197/// uint32_t const instanceSize;
4198/// uint32_t const reserved; // only when building for 64bit targets
4199/// const uint8_t * const ivarLayout;
4200/// const char *const name;
4201/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004202/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004203/// const struct _ivar_list_t *const ivars;
4204/// const uint8_t * const weakIvarLayout;
4205/// const struct _prop_list_t * const properties;
4206/// }
4207///
4208llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
4209 unsigned flags,
4210 unsigned InstanceStart,
4211 unsigned InstanceSize,
4212 const ObjCImplementationDecl *ID) {
4213 std::string ClassName = ID->getNameAsString();
4214 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Andersona1cf15f2009-07-14 23:10:40 +00004215 Values[ 0] = VMContext.getConstantInt(ObjCTypes.IntTy, flags);
4216 Values[ 1] = VMContext.getConstantInt(ObjCTypes.IntTy, InstanceStart);
4217 Values[ 2] = VMContext.getConstantInt(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004218 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004219 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4220 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004221 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004222 // const struct _method_list_t * const baseMethods;
4223 std::vector<llvm::Constant*> Methods;
4224 std::string MethodListName("\01l_OBJC_$_");
4225 if (flags & CLS_META) {
4226 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004227 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004228 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004229 // Class methods should always be defined.
4230 Methods.push_back(GetMethodConstant(*i));
4231 }
4232 } else {
4233 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004234 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004235 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004236 // Instance methods should always be defined.
4237 Methods.push_back(GetMethodConstant(*i));
4238 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00004239 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004240 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004241 ObjCPropertyImplDecl *PID = *i;
4242
4243 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4244 ObjCPropertyDecl *PD = PID->getPropertyDecl();
4245
4246 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4247 if (llvm::Constant *C = GetMethodConstant(MD))
4248 Methods.push_back(C);
4249 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4250 if (llvm::Constant *C = GetMethodConstant(MD))
4251 Methods.push_back(C);
4252 }
4253 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004254 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004255 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004256 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004257
4258 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4259 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
4260 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
4261 + OID->getNameAsString(),
4262 OID->protocol_begin(),
4263 OID->protocol_end());
4264
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004265 if (flags & CLS_META)
Owen Anderson69243822009-07-13 04:10:07 +00004266 Values[ 7] = VMContext.getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004267 else
4268 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004269 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4270 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004271 if (flags & CLS_META)
Owen Anderson69243822009-07-13 04:10:07 +00004272 Values[ 9] = VMContext.getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004273 else
4274 Values[ 9] =
4275 EmitPropertyList(
4276 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
4277 ID, ID->getClassInterface(), ObjCTypes);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004278 llvm::Constant *Init = VMContext.getConstantStruct(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004279 Values);
4280 llvm::GlobalVariable *CLASS_RO_GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004281 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004282 llvm::GlobalValue::InternalLinkage,
4283 Init,
4284 (flags & CLS_META) ?
4285 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
Owen Anderson1c431b32009-07-08 19:05:04 +00004286 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004287 CLASS_RO_GV->setAlignment(
4288 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004289 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004290 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004291
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004292}
4293
4294/// BuildClassMetaData - This routine defines that to-level meta-data
4295/// for the given ClassName for:
4296/// struct _class_t {
4297/// struct _class_t *isa;
4298/// struct _class_t * const superclass;
4299/// void *cache;
4300/// IMP *vtable;
4301/// struct class_ro_t *ro;
4302/// }
4303///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004304llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
4305 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004306 llvm::Constant *IsAGV,
4307 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004308 llvm::Constant *ClassRoGV,
4309 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004310 std::vector<llvm::Constant*> Values(5);
4311 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004312 Values[1] = SuperClassGV
4313 ? SuperClassGV
Owen Anderson69243822009-07-13 04:10:07 +00004314 : VMContext.getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004315 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4316 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4317 Values[4] = ClassRoGV; // &CLASS_RO_GV
Owen Andersona1cf15f2009-07-14 23:10:40 +00004318 llvm::Constant *Init = VMContext.getConstantStruct(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004319 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004320 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4321 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004322 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004323 GV->setAlignment(
4324 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004325 if (HiddenVisibility)
4326 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004327 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004328}
4329
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004330bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00004331CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004332 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004333}
4334
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004335void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004336 uint32_t &InstanceStart,
4337 uint32_t &InstanceSize) {
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004338 const ASTRecordLayout &RL =
4339 CGM.getContext().getASTObjCImplementationLayout(OID);
4340
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004341 // InstanceSize is really instance end.
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004342 InstanceSize = llvm::RoundUpToAlignment(RL.getNextOffset(), 8) / 8;
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004343
4344 // If there are no fields, the start is the same as the end.
4345 if (!RL.getFieldCount())
4346 InstanceStart = InstanceSize;
4347 else
4348 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004349}
4350
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004351void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4352 std::string ClassName = ID->getNameAsString();
4353 if (!ObjCEmptyCacheVar) {
4354 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Owen Anderson1c431b32009-07-08 19:05:04 +00004355 CGM.getModule(),
Daniel Dunbar948e2582009-02-15 07:36:20 +00004356 ObjCTypes.CacheTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004357 false,
4358 llvm::GlobalValue::ExternalLinkage,
4359 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00004360 "_objc_empty_cache");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004361
4362 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Owen Anderson1c431b32009-07-08 19:05:04 +00004363 CGM.getModule(),
Daniel Dunbar948e2582009-02-15 07:36:20 +00004364 ObjCTypes.ImpnfABITy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004365 false,
4366 llvm::GlobalValue::ExternalLinkage,
4367 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00004368 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004369 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004370 assert(ID->getClassInterface() &&
4371 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004372 // FIXME: Is this correct (that meta class size is never computed)?
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004373 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00004374 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004375 uint32_t InstanceSize = InstanceStart;
4376 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004377 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4378 std::string ObjCClassName(getClassSymbolPrefix());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004379
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004380 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004381
Daniel Dunbar04d40782009-04-14 06:00:08 +00004382 bool classIsHidden =
4383 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004384 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004385 flags |= OBJC2_CLS_HIDDEN;
4386 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004387 // class is root
4388 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004389 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004390 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004391 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004392 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004393 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4394 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4395 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004396 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004397 // work on super class metadata symbol.
4398 std::string SuperClassName =
4399 ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004400 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004401 }
4402 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4403 InstanceStart,
4404 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004405 std::string TClassName = ObjCMetaClassName + ClassName;
4406 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004407 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4408 classIsHidden);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004409
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004410 // Metadata for the class
4411 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004412 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004413 flags |= OBJC2_CLS_HIDDEN;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004414
Douglas Gregor68584ed2009-06-18 16:11:24 +00004415 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004416 flags |= CLS_EXCEPTION;
4417
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004418 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004419 flags |= CLS_ROOT;
4420 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00004421 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004422 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004423 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004424 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004425 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004426 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004427 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004428 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004429 InstanceStart,
4430 InstanceSize,
4431 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004432
4433 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004434 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004435 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4436 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004437 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004438
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004439 // Determine if this class is also "non-lazy".
4440 if (ImplementationIsNonLazy(ID))
4441 DefinedNonLazyClasses.push_back(ClassMD);
4442
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004443 // Force the definition of the EHType if necessary.
4444 if (flags & CLS_EXCEPTION)
4445 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004446}
4447
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004448/// GenerateProtocolRef - This routine is called to generate code for
4449/// a protocol reference expression; as in:
4450/// @code
4451/// @protocol(Proto1);
4452/// @endcode
4453/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4454/// which will hold address of the protocol meta-data.
4455///
4456llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
4457 const ObjCProtocolDecl *PD) {
4458
Fariborz Jahanian960cd062009-04-10 18:47:34 +00004459 // This routine is called for @protocol only. So, we must build definition
4460 // of protocol's meta-data (not a reference to it!)
4461 //
Owen Andersona1cf15f2009-07-14 23:10:40 +00004462 llvm::Constant *Init =
4463 VMContext.getConstantExprBitCast(GetOrEmitProtocol(PD),
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004464 ObjCTypes.ExternalProtocolPtrTy);
4465
4466 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
4467 ProtocolName += PD->getNameAsCString();
4468
4469 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
4470 if (PTGV)
4471 return Builder.CreateLoad(PTGV, false, "tmp");
4472 PTGV = new llvm::GlobalVariable(
Owen Anderson1c431b32009-07-08 19:05:04 +00004473 CGM.getModule(),
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004474 Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00004475 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004476 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004477 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004478 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
4479 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4480 UsedGlobals.push_back(PTGV);
4481 return Builder.CreateLoad(PTGV, false, "tmp");
4482}
4483
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004484/// GenerateCategory - Build metadata for a category implementation.
4485/// struct _category_t {
4486/// const char * const name;
4487/// struct _class_t *const cls;
4488/// const struct _method_list_t * const instance_methods;
4489/// const struct _method_list_t * const class_methods;
4490/// const struct _protocol_list_t * const protocols;
4491/// const struct _prop_list_t * const properties;
4492/// }
4493///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004494void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004495 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004496 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
4497 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004498 "_$_" + OCD->getNameAsString());
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004499 std::string ExtClassName(getClassSymbolPrefix() +
4500 Interface->getNameAsString());
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004501
4502 std::vector<llvm::Constant*> Values(6);
4503 Values[0] = GetClassName(OCD->getIdentifier());
4504 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004505 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004506 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004507 std::vector<llvm::Constant*> Methods;
4508 std::string MethodListName(Prefix);
4509 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
4510 "_$_" + OCD->getNameAsString();
4511
Douglas Gregor653f1b12009-04-23 01:02:12 +00004512 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004513 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004514 // Instance methods should always be defined.
4515 Methods.push_back(GetMethodConstant(*i));
4516 }
4517
4518 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004519 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004520 Methods);
4521
4522 MethodListName = Prefix;
4523 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
4524 OCD->getNameAsString();
4525 Methods.clear();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004526 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004527 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004528 // Class methods should always be defined.
4529 Methods.push_back(GetMethodConstant(*i));
4530 }
4531
4532 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004533 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004534 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004535 const ObjCCategoryDecl *Category =
4536 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004537 if (Category) {
4538 std::string ExtName(Interface->getNameAsString() + "_$_" +
4539 OCD->getNameAsString());
4540 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
4541 + Interface->getNameAsString() + "_$_"
4542 + Category->getNameAsString(),
4543 Category->protocol_begin(),
4544 Category->protocol_end());
4545 Values[5] =
4546 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
4547 OCD, Category, ObjCTypes);
4548 }
4549 else {
Owen Anderson69243822009-07-13 04:10:07 +00004550 Values[4] = VMContext.getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4551 Values[5] = VMContext.getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004552 }
4553
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004554 llvm::Constant *Init =
Owen Andersona1cf15f2009-07-14 23:10:40 +00004555 VMContext.getConstantStruct(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004556 Values);
4557 llvm::GlobalVariable *GCATV
Owen Anderson1c431b32009-07-08 19:05:04 +00004558 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004559 false,
4560 llvm::GlobalValue::InternalLinkage,
4561 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004562 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004563 GCATV->setAlignment(
4564 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004565 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004566 UsedGlobals.push_back(GCATV);
4567 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004568
4569 // Determine if this category is also "non-lazy".
4570 if (ImplementationIsNonLazy(OCD))
4571 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004572}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004573
4574/// GetMethodConstant - Return a struct objc_method constant for the
4575/// given method if it has been defined. The result is null if the
4576/// method has not been defined. The return value has type MethodPtrTy.
4577llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
4578 const ObjCMethodDecl *MD) {
4579 // FIXME: Use DenseMap::lookup
4580 llvm::Function *Fn = MethodDefinitions[MD];
4581 if (!Fn)
4582 return 0;
4583
4584 std::vector<llvm::Constant*> Method(3);
4585 Method[0] =
Owen Andersona1cf15f2009-07-14 23:10:40 +00004586 VMContext.getConstantExprBitCast(GetMethodVarName(MD->getSelector()),
4587 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004588 Method[1] = GetMethodVarType(MD);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004589 Method[2] = VMContext.getConstantExprBitCast(Fn, ObjCTypes.Int8PtrTy);
4590 return VMContext.getConstantStruct(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004591}
4592
4593/// EmitMethodList - Build meta-data for method declarations
4594/// struct _method_list_t {
4595/// uint32_t entsize; // sizeof(struct _objc_method)
4596/// uint32_t method_count;
4597/// struct _objc_method method_list[method_count];
4598/// }
4599///
4600llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
4601 const std::string &Name,
4602 const char *Section,
4603 const ConstantVector &Methods) {
4604 // Return null for empty list.
4605 if (Methods.empty())
Owen Anderson69243822009-07-13 04:10:07 +00004606 return VMContext.getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004607
4608 std::vector<llvm::Constant*> Values(3);
4609 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00004610 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004611 Values[0] = VMContext.getConstantInt(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004612 // method_count
Owen Andersona1cf15f2009-07-14 23:10:40 +00004613 Values[1] = VMContext.getConstantInt(ObjCTypes.IntTy, Methods.size());
4614 llvm::ArrayType *AT = VMContext.getArrayType(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004615 Methods.size());
Owen Andersona1cf15f2009-07-14 23:10:40 +00004616 Values[2] = VMContext.getConstantArray(AT, Methods);
4617 llvm::Constant *Init = VMContext.getConstantStruct(Values);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004618
4619 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004620 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004621 llvm::GlobalValue::InternalLinkage,
4622 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004623 Name);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004624 GV->setAlignment(
4625 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004626 GV->setSection(Section);
4627 UsedGlobals.push_back(GV);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004628 return VMContext.getConstantExprBitCast(GV,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004629 ObjCTypes.MethodListnfABIPtrTy);
4630}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004631
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004632/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
4633/// the given ivar.
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004634llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004635 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004636 const ObjCIvarDecl *Ivar) {
Daniel Dunbara81419d2009-05-05 00:36:57 +00004637 // FIXME: We shouldn't need to do this lookup.
4638 unsigned Index;
4639 const ObjCInterfaceDecl *Container =
4640 FindIvarInterface(CGM.getContext(), ID, Ivar, Index);
4641 assert(Container && "Unable to find ivar container!");
4642 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00004643 '.' + Ivar->getNameAsString();
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004644 llvm::GlobalVariable *IvarOffsetGV =
4645 CGM.getModule().getGlobalVariable(Name);
4646 if (!IvarOffsetGV)
4647 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004648 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004649 false,
4650 llvm::GlobalValue::ExternalLinkage,
4651 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00004652 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004653 return IvarOffsetGV;
4654}
4655
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004656llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004657 const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004658 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004659 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00004660 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004661 IvarOffsetGV->setInitializer(VMContext.getConstantInt(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00004662 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004663 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004664 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00004665
Mike Stumpf5408fe2009-05-16 07:57:57 +00004666 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
4667 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00004668 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
4669 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
4670 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004671 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00004672 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00004673 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004674 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004675 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004676}
4677
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004678/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00004679/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004680/// IvarListnfABIPtrTy.
4681/// struct _ivar_t {
4682/// unsigned long int *offset; // pointer to ivar offset location
4683/// char *name;
4684/// char *type;
4685/// uint32_t alignment;
4686/// uint32_t size;
4687/// }
4688/// struct _ivar_list_t {
4689/// uint32 entsize; // sizeof(struct _ivar_t)
4690/// uint32 count;
4691/// struct _iver_t list[count];
4692/// }
4693///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004694
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004695llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
4696 const ObjCImplementationDecl *ID) {
4697
4698 std::vector<llvm::Constant*> Ivars, Ivar(5);
4699
4700 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4701 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
4702
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004703 // FIXME. Consolidate this with similar code in GenerateClass.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00004704
Daniel Dunbar91636d62009-04-20 00:33:43 +00004705 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00004706 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00004707 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Fariborz Jahanian99eee362009-04-01 19:37:34 +00004708
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004709 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
4710 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00004711 // Ignore unnamed bit-fields.
4712 if (!IVD->getDeclName())
4713 continue;
Daniel Dunbar3eec8aa2009-04-20 05:53:40 +00004714 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004715 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004716 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
4717 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004718 const llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004719 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00004720 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004721 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004722 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004723 Align = llvm::Log2_32(Align);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004724 Ivar[3] = VMContext.getConstantInt(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00004725 // NOTE. Size of a bitfield does not match gcc's, because of the
4726 // way bitfields are treated special in each. But I am told that
4727 // 'size' for bitfield ivars is ignored by the runtime so it does
4728 // not matter. If it matters, there is enough info to get the
4729 // bitfield right!
Owen Andersona1cf15f2009-07-14 23:10:40 +00004730 Ivar[4] = VMContext.getConstantInt(ObjCTypes.IntTy, Size);
4731 Ivars.push_back(VMContext.getConstantStruct(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004732 }
4733 // Return null for empty list.
4734 if (Ivars.empty())
Owen Anderson69243822009-07-13 04:10:07 +00004735 return VMContext.getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004736 std::vector<llvm::Constant*> Values(3);
Duncan Sands9408c452009-05-09 07:08:47 +00004737 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004738 Values[0] = VMContext.getConstantInt(ObjCTypes.IntTy, Size);
4739 Values[1] = VMContext.getConstantInt(ObjCTypes.IntTy, Ivars.size());
4740 llvm::ArrayType *AT = VMContext.getArrayType(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004741 Ivars.size());
Owen Andersona1cf15f2009-07-14 23:10:40 +00004742 Values[2] = VMContext.getConstantArray(AT, Ivars);
4743 llvm::Constant *Init = VMContext.getConstantStruct(Values);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004744 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4745 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004746 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004747 llvm::GlobalValue::InternalLinkage,
4748 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004749 Prefix + OID->getNameAsString());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004750 GV->setAlignment(
4751 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004752 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004753
4754 UsedGlobals.push_back(GV);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004755 return VMContext.getConstantExprBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004756}
4757
4758llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4759 const ObjCProtocolDecl *PD) {
4760 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4761
4762 if (!Entry) {
4763 // We use the initializer as a marker of whether this is a forward
4764 // reference or not. At module finalization we add the empty
4765 // contents for protocols which were referenced but never defined.
4766 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00004767 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004768 llvm::GlobalValue::ExternalLinkage,
4769 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00004770 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString());
Fariborz Jahanianda320092009-01-29 19:24:30 +00004771 Entry->setSection("__DATA,__datacoal_nt,coalesced");
4772 UsedGlobals.push_back(Entry);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004773 }
4774
4775 return Entry;
4776}
4777
4778/// GetOrEmitProtocol - Generate the protocol meta-data:
4779/// @code
4780/// struct _protocol_t {
4781/// id isa; // NULL
4782/// const char * const protocol_name;
4783/// const struct _protocol_list_t * protocol_list; // super protocols
4784/// const struct method_list_t * const instance_methods;
4785/// const struct method_list_t * const class_methods;
4786/// const struct method_list_t *optionalInstanceMethods;
4787/// const struct method_list_t *optionalClassMethods;
4788/// const struct _prop_list_t * properties;
4789/// const uint32_t size; // sizeof(struct _protocol_t)
4790/// const uint32_t flags; // = 0
4791/// }
4792/// @endcode
4793///
4794
4795llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4796 const ObjCProtocolDecl *PD) {
4797 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4798
4799 // Early exit if a defining object has already been generated.
4800 if (Entry && Entry->hasInitializer())
4801 return Entry;
4802
4803 const char *ProtocolName = PD->getNameAsCString();
4804
4805 // Construct method lists.
4806 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4807 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00004808 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004809 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004810 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004811 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004812 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4813 OptInstanceMethods.push_back(C);
4814 } else {
4815 InstanceMethods.push_back(C);
4816 }
4817 }
4818
Douglas Gregor6ab35242009-04-09 21:40:53 +00004819 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004820 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004821 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004822 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004823 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4824 OptClassMethods.push_back(C);
4825 } else {
4826 ClassMethods.push_back(C);
4827 }
4828 }
4829
4830 std::vector<llvm::Constant*> Values(10);
4831 // isa is NULL
Owen Anderson69243822009-07-13 04:10:07 +00004832 Values[0] = VMContext.getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004833 Values[1] = GetClassName(PD->getIdentifier());
4834 Values[2] = EmitProtocolList(
4835 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4836 PD->protocol_begin(),
4837 PD->protocol_end());
4838
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004839 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004840 + PD->getNameAsString(),
4841 "__DATA, __objc_const",
4842 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004843 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004844 + PD->getNameAsString(),
4845 "__DATA, __objc_const",
4846 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004847 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004848 + PD->getNameAsString(),
4849 "__DATA, __objc_const",
4850 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004851 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004852 + PD->getNameAsString(),
4853 "__DATA, __objc_const",
4854 OptClassMethods);
4855 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4856 0, PD, ObjCTypes);
4857 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00004858 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004859 Values[8] = VMContext.getConstantInt(ObjCTypes.IntTy, Size);
Owen Anderson69243822009-07-13 04:10:07 +00004860 Values[9] = VMContext.getNullValue(ObjCTypes.IntTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004861 llvm::Constant *Init = VMContext.getConstantStruct(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004862 Values);
4863
4864 if (Entry) {
4865 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00004866 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004867 Entry->setInitializer(Init);
4868 } else {
4869 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00004870 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004871 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004872 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004873 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004874 Entry->setAlignment(
4875 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004876 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004877 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004878 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
4879
4880 // Use this protocol meta-data to build protocol list table in section
4881 // __DATA, __objc_protolist
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004882 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
Owen Anderson1c431b32009-07-08 19:05:04 +00004883 CGM.getModule(),
Daniel Dunbar948e2582009-02-15 07:36:20 +00004884 ObjCTypes.ProtocolnfABIPtrTy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004885 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004886 Entry,
4887 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
Owen Anderson1c431b32009-07-08 19:05:04 +00004888 +ProtocolName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004889 PTGV->setAlignment(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004890 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00004891 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004892 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4893 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004894 return Entry;
4895}
4896
4897/// EmitProtocolList - Generate protocol list meta-data:
4898/// @code
4899/// struct _protocol_list_t {
4900/// long protocol_count; // Note, this is 32/64 bit
4901/// struct _protocol_t[protocol_count];
4902/// }
4903/// @endcode
4904///
4905llvm::Constant *
4906CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4907 ObjCProtocolDecl::protocol_iterator begin,
4908 ObjCProtocolDecl::protocol_iterator end) {
4909 std::vector<llvm::Constant*> ProtocolRefs;
4910
Fariborz Jahanianda320092009-01-29 19:24:30 +00004911 // Just return null for empty protocol lists
Daniel Dunbar948e2582009-02-15 07:36:20 +00004912 if (begin == end)
Owen Anderson69243822009-07-13 04:10:07 +00004913 return VMContext.getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004914
Daniel Dunbar948e2582009-02-15 07:36:20 +00004915 // FIXME: We shouldn't need to do this lookup here, should we?
Fariborz Jahanianda320092009-01-29 19:24:30 +00004916 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4917 if (GV)
Owen Andersona1cf15f2009-07-14 23:10:40 +00004918 return VMContext.getConstantExprBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00004919 ObjCTypes.ProtocolListnfABIPtrTy);
4920
4921 for (; begin != end; ++begin)
4922 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4923
Fariborz Jahanianda320092009-01-29 19:24:30 +00004924 // This list is null terminated.
Owen Anderson69243822009-07-13 04:10:07 +00004925 ProtocolRefs.push_back(VMContext.getNullValue(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004926 ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004927
4928 std::vector<llvm::Constant*> Values(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004929 Values[0] =
4930 VMContext.getConstantInt(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004931 Values[1] =
Owen Andersona1cf15f2009-07-14 23:10:40 +00004932 VMContext.getConstantArray(
4933 VMContext.getArrayType(ObjCTypes.ProtocolnfABIPtrTy,
4934 ProtocolRefs.size()),
4935 ProtocolRefs);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004936
Owen Andersona1cf15f2009-07-14 23:10:40 +00004937 llvm::Constant *Init = VMContext.getConstantStruct(Values);
Owen Anderson1c431b32009-07-08 19:05:04 +00004938 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004939 llvm::GlobalValue::InternalLinkage,
4940 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004941 Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004942 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004943 GV->setAlignment(
4944 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004945 UsedGlobals.push_back(GV);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004946 return VMContext.getConstantExprBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00004947 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004948}
4949
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004950/// GetMethodDescriptionConstant - This routine build following meta-data:
4951/// struct _objc_method {
4952/// SEL _cmd;
4953/// char *method_type;
4954/// char *_imp;
4955/// }
4956
4957llvm::Constant *
4958CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4959 std::vector<llvm::Constant*> Desc(3);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004960 Desc[0] =
4961 VMContext.getConstantExprBitCast(GetMethodVarName(MD->getSelector()),
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004962 ObjCTypes.SelectorPtrTy);
4963 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004964 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson69243822009-07-13 04:10:07 +00004965 Desc[2] = VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004966 return VMContext.getConstantStruct(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004967}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004968
4969/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4970/// This code gen. amounts to generating code for:
4971/// @code
4972/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4973/// @encode
4974///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004975LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004976 CodeGen::CodeGenFunction &CGF,
4977 QualType ObjectTy,
4978 llvm::Value *BaseValue,
4979 const ObjCIvarDecl *Ivar,
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004980 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00004981 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00004982 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
4983 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004984}
4985
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004986llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
4987 CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00004988 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004989 const ObjCIvarDecl *Ivar) {
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00004990 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),
4991 false, "ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004992}
4993
Fariborz Jahanian46551122009-02-04 00:22:57 +00004994CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4995 CodeGen::CodeGenFunction &CGF,
4996 QualType ResultType,
4997 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004998 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00004999 QualType Arg0Ty,
5000 bool IsSuper,
5001 const CallArgList &CallArgs) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00005002 // FIXME. Even though IsSuper is passes. This function doese not handle calls
5003 // to 'super' receivers.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005004 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005005 llvm::Value *Arg0 = Receiver;
5006 if (!IsSuper)
5007 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005008
5009 // Find the message function name.
Mike Stumpf5408fe2009-05-16 07:57:57 +00005010 // FIXME. This is too much work to get the ABI-specific result type needed to
5011 // find the message name.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005012 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
5013 llvm::SmallVector<QualType, 16>());
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005014 llvm::Constant *Fn = 0;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005015 std::string Name("\01l_");
5016 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005017#if 0
5018 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005019 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005020 Fn = ObjCTypes.getMessageSendIdStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005021 // FIXME. Is there a better way of getting these names.
5022 // They are available in RuntimeFunctions vector pair.
5023 Name += "objc_msgSendId_stret_fixup";
5024 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005025 else
5026#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005027 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005028 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005029 Name += "objc_msgSendSuper2_stret_fixup";
5030 }
5031 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005032 {
Chris Lattner1c02f862009-04-22 02:53:24 +00005033 Fn = ObjCTypes.getMessageSendStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005034 Name += "objc_msgSend_stret_fixup";
5035 }
5036 }
Fariborz Jahanian5b2bad02009-04-30 16:31:11 +00005037 else if (!IsSuper && ResultType->isFloatingType()) {
Daniel Dunbarc0183e82009-06-26 18:32:06 +00005038 if (ResultType->isSpecificBuiltinType(BuiltinType::LongDouble)) {
5039 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5040 Name += "objc_msgSend_fpret_fixup";
5041 }
5042 else {
5043 Fn = ObjCTypes.getMessageSendFixupFn();
5044 Name += "objc_msgSend_fixup";
Fariborz Jahanian5b2bad02009-04-30 16:31:11 +00005045 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005046 }
5047 else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005048#if 0
5049// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005050 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005051 Fn = ObjCTypes.getMessageSendIdFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005052 Name += "objc_msgSendId_fixup";
5053 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005054 else
5055#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005056 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005057 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005058 Name += "objc_msgSendSuper2_fixup";
5059 }
5060 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005061 {
Chris Lattner1c02f862009-04-22 02:53:24 +00005062 Fn = ObjCTypes.getMessageSendFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005063 Name += "objc_msgSend_fixup";
5064 }
5065 }
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005066 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005067 Name += '_';
5068 std::string SelName(Sel.getAsString());
5069 // Replace all ':' in selector name with '_' ouch!
5070 for(unsigned i = 0; i < SelName.size(); i++)
5071 if (SelName[i] == ':')
5072 SelName[i] = '_';
5073 Name += SelName;
5074 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5075 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005076 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005077 std::vector<llvm::Constant*> Values(2);
5078 Values[0] = Fn;
5079 Values[1] = GetMethodVarName(Sel);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005080 llvm::Constant *Init = VMContext.getConstantStruct(Values);
Owen Anderson1c431b32009-07-08 19:05:04 +00005081 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005082 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005083 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005084 Name);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005085 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005086 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005087 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005088 }
5089 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005090
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005091 CallArgList ActualArgs;
5092 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
5093 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
5094 ObjCTypes.MessageRefCPtrTy));
5095 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005096 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
5097 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5098 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00005099 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005100 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Andersona1cf15f2009-07-14 23:10:40 +00005101 VMContext.getPointerTypeUnqual(FTy));
Fariborz Jahanianef163782009-02-05 01:13:09 +00005102 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005103}
5104
5105/// Generate code for a message send expression in the nonfragile abi.
5106CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
5107 CodeGen::CodeGenFunction &CGF,
5108 QualType ResultType,
5109 Selector Sel,
5110 llvm::Value *Receiver,
5111 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00005112 const CallArgList &CallArgs,
5113 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005114 return LegacyDispatchedSelector(Sel)
5115 ? EmitLegacyMessageSend(CGF, ResultType, EmitSelector(CGF.Builder, Sel),
5116 Receiver, CGF.getContext().getObjCIdType(),
5117 false, CallArgs, ObjCTypes)
5118 : EmitMessageSend(CGF, ResultType, Sel,
5119 Receiver, CGF.getContext().getObjCIdType(),
5120 false, CallArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005121}
5122
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005123llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005124CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005125 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5126
Daniel Dunbardfff2302009-03-02 05:18:14 +00005127 if (!GV) {
Owen Anderson1c431b32009-07-08 19:05:04 +00005128 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
5129 false, llvm::GlobalValue::ExternalLinkage,
5130 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005131 }
5132
5133 return GV;
5134}
5135
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005136llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005137 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005138 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
5139
5140 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005141 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005142 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005143 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005144 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5145 false, llvm::GlobalValue::InternalLinkage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005146 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005147 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005148 Entry->setAlignment(
5149 CGM.getTargetData().getPrefTypeAlignment(
5150 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005151 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
5152 UsedGlobals.push_back(Entry);
5153 }
5154
5155 return Builder.CreateLoad(Entry, false, "tmp");
5156}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005157
Daniel Dunbar11394522009-04-18 08:51:00 +00005158llvm::Value *
5159CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
5160 const ObjCInterfaceDecl *ID) {
5161 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
5162
5163 if (!Entry) {
5164 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5165 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5166 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005167 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5168 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar11394522009-04-18 08:51:00 +00005169 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005170 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005171 Entry->setAlignment(
5172 CGM.getTargetData().getPrefTypeAlignment(
5173 ObjCTypes.ClassnfABIPtrTy));
5174 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005175 UsedGlobals.push_back(Entry);
5176 }
5177
5178 return Builder.CreateLoad(Entry, false, "tmp");
5179}
5180
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005181/// EmitMetaClassRef - Return a Value * of the address of _class_t
5182/// meta-data
5183///
5184llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5185 const ObjCInterfaceDecl *ID) {
5186 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5187 if (Entry)
5188 return Builder.CreateLoad(Entry, false, "tmp");
5189
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005190 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005191 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005192 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005193 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005194 llvm::GlobalValue::InternalLinkage,
5195 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005196 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005197 Entry->setAlignment(
5198 CGM.getTargetData().getPrefTypeAlignment(
5199 ObjCTypes.ClassnfABIPtrTy));
5200
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005201 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005202 UsedGlobals.push_back(Entry);
5203
5204 return Builder.CreateLoad(Entry, false, "tmp");
5205}
5206
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005207/// GetClass - Return a reference to the class for the given interface
5208/// decl.
5209llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5210 const ObjCInterfaceDecl *ID) {
5211 return EmitClassRef(Builder, ID);
5212}
5213
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005214/// Generates a message send where the super is the receiver. This is
5215/// a message send to self with special delivery semantics indicating
5216/// which class's method should be called.
5217CodeGen::RValue
5218CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
5219 QualType ResultType,
5220 Selector Sel,
5221 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005222 bool isCategoryImpl,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005223 llvm::Value *Receiver,
5224 bool IsClassMessage,
5225 const CodeGen::CallArgList &CallArgs) {
5226 // ...
5227 // Create and init a super structure; this is a (receiver, class)
5228 // pair we will pass to objc_msgSendSuper.
5229 llvm::Value *ObjCSuper =
5230 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
5231
5232 llvm::Value *ReceiverAsObject =
5233 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5234 CGF.Builder.CreateStore(ReceiverAsObject,
5235 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
5236
5237 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005238 llvm::Value *Target;
5239 if (IsClassMessage) {
5240 if (isCategoryImpl) {
5241 // Message sent to "super' in a class method defined in
5242 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005243 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005244 Target = CGF.Builder.CreateStructGEP(Target, 0);
5245 Target = CGF.Builder.CreateLoad(Target);
5246 }
5247 else
5248 Target = EmitMetaClassRef(CGF.Builder, Class);
5249 }
5250 else
Daniel Dunbar11394522009-04-18 08:51:00 +00005251 Target = EmitSuperClassRef(CGF.Builder, Class);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005252
Mike Stumpf5408fe2009-05-16 07:57:57 +00005253 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5254 // ObjCTypes types.
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005255 const llvm::Type *ClassTy =
5256 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5257 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5258 CGF.Builder.CreateStore(Target,
5259 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
5260
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005261 return (LegacyDispatchedSelector(Sel))
5262 ? EmitLegacyMessageSend(CGF, ResultType,EmitSelector(CGF.Builder, Sel),
5263 ObjCSuper, ObjCTypes.SuperPtrCTy,
5264 true, CallArgs,
5265 ObjCTypes)
5266 : EmitMessageSend(CGF, ResultType, Sel,
5267 ObjCSuper, ObjCTypes.SuperPtrCTy,
5268 true, CallArgs);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005269}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005270
5271llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
5272 Selector Sel) {
5273 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
5274
5275 if (!Entry) {
5276 llvm::Constant *Casted =
Owen Andersona1cf15f2009-07-14 23:10:40 +00005277 VMContext.getConstantExprBitCast(GetMethodVarName(Sel),
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005278 ObjCTypes.SelectorPtrTy);
5279 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005280 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005281 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00005282 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005283 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005284 UsedGlobals.push_back(Entry);
5285 }
5286
5287 return Builder.CreateLoad(Entry, false, "tmp");
5288}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005289/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
5290/// objc_assign_ivar (id src, id *dst)
5291///
5292void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
5293 llvm::Value *src, llvm::Value *dst)
5294{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005295 const llvm::Type * SrcTy = src->getType();
5296 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005297 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005298 assert(Size <= 8 && "does not support size > 8");
5299 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5300 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005301 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5302 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005303 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5304 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005305 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005306 src, dst, "assignivar");
5307 return;
5308}
5309
5310/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5311/// objc_assign_strongCast (id src, id *dst)
5312///
5313void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
5314 CodeGen::CodeGenFunction &CGF,
5315 llvm::Value *src, llvm::Value *dst)
5316{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005317 const llvm::Type * SrcTy = src->getType();
5318 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005319 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005320 assert(Size <= 8 && "does not support size > 8");
5321 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5322 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005323 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5324 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005325 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5326 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005327 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005328 src, dst, "weakassign");
5329 return;
5330}
5331
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005332void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
5333 CodeGen::CodeGenFunction &CGF,
5334 llvm::Value *DestPtr,
5335 llvm::Value *SrcPtr,
5336 unsigned long size) {
5337 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5338 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005339 llvm::Value *N = VMContext.getConstantInt(ObjCTypes.LongTy, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005340 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
5341 DestPtr, SrcPtr, N);
5342 return;
5343}
5344
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005345/// EmitObjCWeakRead - Code gen for loading value of a __weak
5346/// object: objc_read_weak (id *src)
5347///
5348llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
5349 CodeGen::CodeGenFunction &CGF,
5350 llvm::Value *AddrWeakObj)
5351{
Eli Friedman8339b352009-03-07 03:57:15 +00005352 const llvm::Type* DestTy =
5353 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005354 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005355 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005356 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005357 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005358 return read_weak;
5359}
5360
5361/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5362/// objc_assign_weak (id src, id *dst)
5363///
5364void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
5365 llvm::Value *src, llvm::Value *dst)
5366{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005367 const llvm::Type * SrcTy = src->getType();
5368 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005369 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005370 assert(Size <= 8 && "does not support size > 8");
5371 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5372 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005373 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5374 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005375 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5376 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005377 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005378 src, dst, "weakassign");
5379 return;
5380}
5381
5382/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5383/// objc_assign_global (id src, id *dst)
5384///
5385void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
5386 llvm::Value *src, llvm::Value *dst)
5387{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005388 const llvm::Type * SrcTy = src->getType();
5389 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005390 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005391 assert(Size <= 8 && "does not support size > 8");
5392 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5393 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005394 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5395 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005396 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5397 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005398 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005399 src, dst, "globalassign");
5400 return;
5401}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005402
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005403void
5404CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5405 const Stmt &S) {
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005406 bool isTry = isa<ObjCAtTryStmt>(S);
5407 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
5408 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005409 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005410 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005411 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005412 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
5413
5414 // For @synchronized, call objc_sync_enter(sync.expr). The
5415 // evaluation of the expression must occur before we enter the
5416 // @synchronized. We can safely avoid a temp here because jumps into
5417 // @synchronized are illegal & this will dominate uses.
5418 llvm::Value *SyncArg = 0;
5419 if (!isTry) {
5420 SyncArg =
5421 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
5422 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005423 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005424 }
5425
5426 // Push an EH context entry, used for handling rethrows and jumps
5427 // through finally.
5428 CGF.PushCleanupBlock(FinallyBlock);
5429
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005430 CGF.setInvokeDest(TryHandler);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005431
5432 CGF.EmitBlock(TryBlock);
5433 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
5434 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
5435 CGF.EmitBranchThroughCleanup(FinallyEnd);
5436
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005437 // Emit the exception handler.
5438
5439 CGF.EmitBlock(TryHandler);
5440
5441 llvm::Value *llvm_eh_exception =
5442 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
5443 llvm::Value *llvm_eh_selector_i64 =
5444 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64);
5445 llvm::Value *llvm_eh_typeid_for_i64 =
5446 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64);
5447 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5448 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow");
5449
5450 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
5451 SelectorArgs.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005452 SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005453
5454 // Construct the lists of (type, catch body) to handle.
Daniel Dunbarede8de92009-03-06 00:01:21 +00005455 llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005456 bool HasCatchAll = false;
5457 if (isTry) {
5458 if (const ObjCAtCatchStmt* CatchStmt =
5459 cast<ObjCAtTryStmt>(S).getCatchStmts()) {
5460 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005461 const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Steve Naroff7ba138a2009-03-03 19:52:17 +00005462 Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody()));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005463
5464 // catch(...) always matches.
Steve Naroff7ba138a2009-03-03 19:52:17 +00005465 if (!CatchDecl) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005466 // Use i8* null here to signal this is a catch all, not a cleanup.
Owen Anderson69243822009-07-13 04:10:07 +00005467 llvm::Value *Null = VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005468 SelectorArgs.push_back(Null);
5469 HasCatchAll = true;
5470 break;
5471 }
5472
Steve Naroff14108da2009-07-10 23:34:53 +00005473 if (CatchDecl->getType()->isObjCIdType() ||
Daniel Dunbarede8de92009-03-06 00:01:21 +00005474 CatchDecl->getType()->isObjCQualifiedIdType()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005475 llvm::Value *IDEHType =
5476 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5477 if (!IDEHType)
5478 IDEHType =
Owen Anderson1c431b32009-07-08 19:05:04 +00005479 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
5480 false,
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005481 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00005482 0, "OBJC_EHTYPE_id");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005483 SelectorArgs.push_back(IDEHType);
5484 HasCatchAll = true;
5485 break;
5486 }
5487
5488 // All other types should be Objective-C interface pointer types.
Steve Naroff14108da2009-07-10 23:34:53 +00005489 const ObjCObjectPointerType *PT =
5490 CatchDecl->getType()->getAsObjCObjectPointerType();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005491 assert(PT && "Invalid @catch type.");
Steve Naroff14108da2009-07-10 23:34:53 +00005492 const ObjCInterfaceType *IT = PT->getInterfaceType();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005493 assert(IT && "Invalid @catch type.");
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005494 llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005495 SelectorArgs.push_back(EHType);
5496 }
5497 }
5498 }
5499
5500 // We use a cleanup unless there was already a catch all.
5501 if (!HasCatchAll) {
Owen Andersona1cf15f2009-07-14 23:10:40 +00005502 SelectorArgs.push_back(VMContext.getConstantInt(llvm::Type::Int32Ty, 0));
Daniel Dunbarede8de92009-03-06 00:01:21 +00005503 Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005504 }
5505
5506 llvm::Value *Selector =
5507 CGF.Builder.CreateCall(llvm_eh_selector_i64,
5508 SelectorArgs.begin(), SelectorArgs.end(),
5509 "selector");
5510 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005511 const ParmVarDecl *CatchParam = Handlers[i].first;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005512 const Stmt *CatchBody = Handlers[i].second;
5513
5514 llvm::BasicBlock *Next = 0;
5515
5516 // The last handler always matches.
5517 if (i + 1 != e) {
5518 assert(CatchParam && "Only last handler can be a catch all.");
5519
5520 llvm::BasicBlock *Match = CGF.createBasicBlock("match");
5521 Next = CGF.createBasicBlock("catch.next");
5522 llvm::Value *Id =
5523 CGF.Builder.CreateCall(llvm_eh_typeid_for_i64,
5524 CGF.Builder.CreateBitCast(SelectorArgs[i+2],
5525 ObjCTypes.Int8PtrTy));
5526 CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id),
5527 Match, Next);
5528
5529 CGF.EmitBlock(Match);
5530 }
5531
5532 if (CatchBody) {
5533 llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end");
5534 llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler");
5535
5536 // Cleanups must call objc_end_catch.
5537 //
Mike Stumpf5408fe2009-05-16 07:57:57 +00005538 // FIXME: It seems incorrect for objc_begin_catch to be inside this
5539 // context, but this matches gcc.
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005540 CGF.PushCleanupBlock(MatchEnd);
5541 CGF.setInvokeDest(MatchHandler);
5542
5543 llvm::Value *ExcObject =
Chris Lattner8a569112009-04-22 02:15:23 +00005544 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005545
5546 // Bind the catch parameter if it exists.
5547 if (CatchParam) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005548 ExcObject =
5549 CGF.Builder.CreateBitCast(ExcObject,
5550 CGF.ConvertType(CatchParam->getType()));
5551 // CatchParam is a ParmVarDecl because of the grammar
5552 // construction used to handle this, but for codegen purposes
5553 // we treat this as a local decl.
5554 CGF.EmitLocalBlockVarDecl(*CatchParam);
5555 CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005556 }
5557
5558 CGF.ObjCEHValueStack.push_back(ExcObject);
5559 CGF.EmitStmt(CatchBody);
5560 CGF.ObjCEHValueStack.pop_back();
5561
5562 CGF.EmitBranchThroughCleanup(FinallyEnd);
5563
5564 CGF.EmitBlock(MatchHandler);
5565
5566 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5567 // We are required to emit this call to satisfy LLVM, even
5568 // though we don't use the result.
5569 llvm::SmallVector<llvm::Value*, 8> Args;
5570 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005571 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Owen Andersona1cf15f2009-07-14 23:10:40 +00005572 Args.push_back(VMContext.getConstantInt(llvm::Type::Int32Ty,
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005573 0));
5574 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5575 CGF.Builder.CreateStore(Exc, RethrowPtr);
5576 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5577
5578 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5579
5580 CGF.EmitBlock(MatchEnd);
5581
5582 // Unfortunately, we also have to generate another EH frame here
5583 // in case this throws.
5584 llvm::BasicBlock *MatchEndHandler =
5585 CGF.createBasicBlock("match.end.handler");
5586 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattner8a569112009-04-22 02:15:23 +00005587 CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005588 Cont, MatchEndHandler,
5589 Args.begin(), Args.begin());
5590
5591 CGF.EmitBlock(Cont);
5592 if (Info.SwitchBlock)
5593 CGF.EmitBlock(Info.SwitchBlock);
5594 if (Info.EndBlock)
5595 CGF.EmitBlock(Info.EndBlock);
5596
5597 CGF.EmitBlock(MatchEndHandler);
5598 Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5599 // We are required to emit this call to satisfy LLVM, even
5600 // though we don't use the result.
5601 Args.clear();
5602 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005603 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Owen Andersona1cf15f2009-07-14 23:10:40 +00005604 Args.push_back(VMContext.getConstantInt(llvm::Type::Int32Ty,
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005605 0));
5606 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5607 CGF.Builder.CreateStore(Exc, RethrowPtr);
5608 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5609
5610 if (Next)
5611 CGF.EmitBlock(Next);
5612 } else {
5613 assert(!Next && "catchup should be last handler.");
5614
5615 CGF.Builder.CreateStore(Exc, RethrowPtr);
5616 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5617 }
5618 }
5619
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005620 // Pop the cleanup entry, the @finally is outside this cleanup
5621 // scope.
5622 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5623 CGF.setInvokeDest(PrevLandingPad);
5624
5625 CGF.EmitBlock(FinallyBlock);
5626
5627 if (isTry) {
5628 if (const ObjCAtFinallyStmt* FinallyStmt =
5629 cast<ObjCAtTryStmt>(S).getFinallyStmt())
5630 CGF.EmitStmt(FinallyStmt->getFinallyBody());
5631 } else {
5632 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
5633 // @synchronized.
Chris Lattnerbbccd612009-04-22 02:38:11 +00005634 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005635 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005636
5637 if (Info.SwitchBlock)
5638 CGF.EmitBlock(Info.SwitchBlock);
5639 if (Info.EndBlock)
5640 CGF.EmitBlock(Info.EndBlock);
5641
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005642 // Branch around the rethrow code.
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005643 CGF.EmitBranch(FinallyEnd);
5644
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005645 CGF.EmitBlock(FinallyRethrow);
Chris Lattner8a569112009-04-22 02:15:23 +00005646 CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005647 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005648 CGF.Builder.CreateUnreachable();
5649
5650 CGF.EmitBlock(FinallyEnd);
5651}
5652
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005653/// EmitThrowStmt - Generate code for a throw statement.
5654void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
5655 const ObjCAtThrowStmt &S) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005656 llvm::Value *Exception;
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005657 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005658 Exception = CGF.EmitScalarExpr(ThrowExpr);
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005659 } else {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005660 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
5661 "Unexpected rethrow outside @catch block.");
5662 Exception = CGF.ObjCEHValueStack.back();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005663 }
5664
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005665 llvm::Value *ExceptionAsObject =
5666 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
5667 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
5668 if (InvokeDest) {
5669 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattnerbbccd612009-04-22 02:38:11 +00005670 CGF.Builder.CreateInvoke(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005671 Cont, InvokeDest,
5672 &ExceptionAsObject, &ExceptionAsObject + 1);
5673 CGF.EmitBlock(Cont);
5674 } else
Chris Lattnerbbccd612009-04-22 02:38:11 +00005675 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005676 CGF.Builder.CreateUnreachable();
5677
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005678 // Clear the insertion point to indicate we are in unreachable code.
5679 CGF.Builder.ClearInsertionPoint();
5680}
Daniel Dunbare588b992009-03-01 04:46:24 +00005681
5682llvm::Value *
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005683CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
5684 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00005685 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00005686
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005687 // If we don't need a definition, return the entry if found or check
5688 // if we use an external reference.
5689 if (!ForDefinition) {
5690 if (Entry)
5691 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00005692
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005693 // If this type (or a super class) has the __objc_exception__
5694 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00005695 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005696 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005697 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005698 llvm::GlobalValue::ExternalLinkage,
5699 0,
5700 (std::string("OBJC_EHTYPE_$_") +
Owen Anderson1c431b32009-07-08 19:05:04 +00005701 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005702 }
5703
5704 // Otherwise we need to either make a new entry or fill in the
5705 // initializer.
5706 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005707 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00005708 std::string VTableName = "objc_ehtype_vtable";
5709 llvm::GlobalVariable *VTableGV =
5710 CGM.getModule().getGlobalVariable(VTableName);
5711 if (!VTableGV)
Owen Anderson1c431b32009-07-08 19:05:04 +00005712 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
5713 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00005714 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00005715 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00005716
Owen Andersona1cf15f2009-07-14 23:10:40 +00005717 llvm::Value *VTableIdx = VMContext.getConstantInt(llvm::Type::Int32Ty, 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00005718
5719 std::vector<llvm::Constant*> Values(3);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005720 Values[0] = VMContext.getConstantExprGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbare588b992009-03-01 04:46:24 +00005721 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005722 Values[2] = GetClassGlobal(ClassName);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005723 llvm::Constant *Init =
5724 VMContext.getConstantStruct(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00005725
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005726 if (Entry) {
5727 Entry->setInitializer(Init);
5728 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00005729 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005730 llvm::GlobalValue::WeakAnyLinkage,
5731 Init,
5732 (std::string("OBJC_EHTYPE_$_") +
Owen Anderson1c431b32009-07-08 19:05:04 +00005733 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005734 }
5735
Daniel Dunbar04d40782009-04-14 06:00:08 +00005736 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005737 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005738 Entry->setAlignment(8);
5739
5740 if (ForDefinition) {
5741 Entry->setSection("__DATA,__objc_const");
5742 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
5743 } else {
5744 Entry->setSection("__DATA,__datacoal_nt,coalesced");
5745 }
Daniel Dunbare588b992009-03-01 04:46:24 +00005746
5747 return Entry;
5748}
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005749
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005750/* *** */
5751
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005752CodeGen::CGObjCRuntime *
5753CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00005754 return new CGObjCMac(CGM);
5755}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005756
5757CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005758CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005759 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005760}