blob: 9b9cec661efa7cba654261615680c312f71be2a0 [file] [log] [blame]
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001//===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides Objective-C code generation targetting the Apple runtime.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000015
16#include "CodeGenModule.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000017#include "CodeGenFunction.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000019#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000020#include "clang/AST/DeclObjC.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Chris Lattner16f00492009-04-26 01:32:48 +000022#include "clang/AST/StmtObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000023#include "clang/Basic/LangOptions.h"
24
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +000025#include "llvm/Intrinsics.h"
Owen Anderson69243822009-07-13 04:10:07 +000026#include "llvm/LLVMContext.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000027#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000028#include "llvm/ADT/DenseSet.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000029#include "llvm/Target/TargetData.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000030#include <sstream>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000031
32using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000033using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000034
Daniel Dunbar97776872009-04-22 07:32:20 +000035// Common CGObjCRuntime functions, these don't belong here, but they
36// don't belong in CGObjCRuntime either so we will live with it for
37// now.
38
Daniel Dunbar532d4da2009-05-03 13:15:50 +000039/// FindIvarInterface - Find the interface containing the ivar.
Daniel Dunbara2435782009-04-22 12:00:04 +000040///
Daniel Dunbar532d4da2009-05-03 13:15:50 +000041/// FIXME: We shouldn't need to do this, the containing context should
42/// be fixed.
43static const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context,
44 const ObjCInterfaceDecl *OID,
45 const ObjCIvarDecl *OIVD,
46 unsigned &Index) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +000047 // FIXME: The index here is closely tied to how
48 // ASTContext::getObjCLayout is implemented. This should be fixed to
49 // get the information from the layout directly.
50 Index = 0;
Fariborz Jahanian98200742009-05-12 18:14:29 +000051 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +000052 Context.ShallowCollectObjCIvars(OID, Ivars);
Fariborz Jahanian98200742009-05-12 18:14:29 +000053 for (unsigned k = 0, e = Ivars.size(); k != e; ++k) {
54 if (OIVD == Ivars[k])
55 return OID;
56 ++Index;
Daniel Dunbara80a0f62009-04-22 17:43:55 +000057 }
Fariborz Jahanian98200742009-05-12 18:14:29 +000058
Daniel Dunbar532d4da2009-05-03 13:15:50 +000059 // Otherwise check in the super class.
Daniel Dunbara81419d2009-05-05 00:36:57 +000060 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Daniel Dunbar532d4da2009-05-03 13:15:50 +000061 return FindIvarInterface(Context, Super, OIVD, Index);
62
63 return 0;
Daniel Dunbara2435782009-04-22 12:00:04 +000064}
65
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000066static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM,
67 const ObjCInterfaceDecl *OID,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000068 const ObjCImplementationDecl *ID,
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000069 const ObjCIvarDecl *Ivar) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +000070 unsigned Index;
71 const ObjCInterfaceDecl *Container =
72 FindIvarInterface(CGM.getContext(), OID, Ivar, Index);
73 assert(Container && "Unable to find ivar container");
74
75 // If we know have an implementation (and the ivar is in it) then
76 // look up in the implementation layout.
77 const ASTRecordLayout *RL;
78 if (ID && ID->getClassInterface() == Container)
79 RL = &CGM.getContext().getASTObjCImplementationLayout(ID);
80 else
81 RL = &CGM.getContext().getASTObjCInterfaceLayout(Container);
82 return RL->getFieldOffset(Index);
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000083}
84
85uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
86 const ObjCInterfaceDecl *OID,
87 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000088 return LookupFieldBitOffset(CGM, OID, 0, Ivar) / 8;
89}
90
91uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
92 const ObjCImplementationDecl *OID,
93 const ObjCIvarDecl *Ivar) {
94 return LookupFieldBitOffset(CGM, OID->getClassInterface(), OID, Ivar) / 8;
Daniel Dunbar97776872009-04-22 07:32:20 +000095}
96
97LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
98 const ObjCInterfaceDecl *OID,
99 llvm::Value *BaseValue,
100 const ObjCIvarDecl *Ivar,
101 unsigned CVRQualifiers,
102 llvm::Value *Offset) {
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000103 // Compute (type*) ( (char *) BaseValue + Offset)
Owen Andersona1cf15f2009-07-14 23:10:40 +0000104 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
105 llvm::Type *I8Ptr = VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty);
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000106 QualType IvarTy = Ivar->getType();
107 const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy);
Daniel Dunbar97776872009-04-22 07:32:20 +0000108 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr);
Daniel Dunbar97776872009-04-22 07:32:20 +0000109 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
Owen Andersona1cf15f2009-07-14 23:10:40 +0000110 V = CGF.Builder.CreateBitCast(V, VMContext.getPointerTypeUnqual(LTy));
Daniel Dunbar97776872009-04-22 07:32:20 +0000111
112 if (Ivar->isBitField()) {
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +0000113 // We need to compute the bit offset for the bit-field, the offset
114 // is to the byte. Note, there is a subtle invariant here: we can
115 // only call this routine on non-sythesized ivars but we may be
116 // called for synthesized ivars. However, a synthesized ivar can
117 // never be a bit-field so this is safe.
118 uint64_t BitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar) % 8;
119
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000120 uint64_t BitFieldSize =
121 Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue();
122 return LValue::MakeBitfield(V, BitOffset, BitFieldSize,
Daniel Dunbare38df862009-05-03 07:52:00 +0000123 IvarTy->isSignedIntegerType(),
124 IvarTy.getCVRQualifiers()|CVRQualifiers);
Daniel Dunbar97776872009-04-22 07:32:20 +0000125 }
126
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000127 LValue LV = LValue::MakeAddr(V, IvarTy.getCVRQualifiers()|CVRQualifiers,
128 CGF.CGM.getContext().getObjCGCAttrKind(IvarTy));
Daniel Dunbar97776872009-04-22 07:32:20 +0000129 LValue::SetObjCIvar(LV, true);
130 return LV;
131}
132
133///
134
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000135namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000136
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000137 typedef std::vector<llvm::Constant*> ConstantVector;
138
Mike Stumpf5408fe2009-05-16 07:57:57 +0000139 // FIXME: We should find a nicer way to make the labels for metadata, string
140 // concatenation is lame.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000141
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000142class ObjCCommonTypesHelper {
Owen Andersona1cf15f2009-07-14 23:10:40 +0000143protected:
144 llvm::LLVMContext &VMContext;
145
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000146private:
147 llvm::Constant *getMessageSendFn() const {
148 // id objc_msgSend (id, SEL, ...)
149 std::vector<const llvm::Type*> Params;
150 Params.push_back(ObjectPtrTy);
151 Params.push_back(SelectorPtrTy);
152 return
Owen Andersona1cf15f2009-07-14 23:10:40 +0000153 CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000154 Params, true),
155 "objc_msgSend");
156 }
157
158 llvm::Constant *getMessageSendStretFn() const {
159 // id objc_msgSend_stret (id, SEL, ...)
160 std::vector<const llvm::Type*> Params;
161 Params.push_back(ObjectPtrTy);
162 Params.push_back(SelectorPtrTy);
163 return
Owen Andersona1cf15f2009-07-14 23:10:40 +0000164 CGM.CreateRuntimeFunction(VMContext.getFunctionType(llvm::Type::VoidTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000165 Params, true),
166 "objc_msgSend_stret");
167
168 }
169
170 llvm::Constant *getMessageSendFpretFn() const {
171 // FIXME: This should be long double on x86_64?
172 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
173 std::vector<const llvm::Type*> Params;
174 Params.push_back(ObjectPtrTy);
175 Params.push_back(SelectorPtrTy);
176 return
Owen Andersona1cf15f2009-07-14 23:10:40 +0000177 CGM.CreateRuntimeFunction(VMContext.getFunctionType(llvm::Type::DoubleTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000178 Params,
179 true),
180 "objc_msgSend_fpret");
181
182 }
183
184 llvm::Constant *getMessageSendSuperFn() const {
185 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
186 const char *SuperName = "objc_msgSendSuper";
187 std::vector<const llvm::Type*> Params;
188 Params.push_back(SuperPtrTy);
189 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000190 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000191 Params, true),
192 SuperName);
193 }
194
195 llvm::Constant *getMessageSendSuperFn2() const {
196 // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
197 const char *SuperName = "objc_msgSendSuper2";
198 std::vector<const llvm::Type*> Params;
199 Params.push_back(SuperPtrTy);
200 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000201 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000202 Params, true),
203 SuperName);
204 }
205
206 llvm::Constant *getMessageSendSuperStretFn() const {
207 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
208 // SEL op, ...)
209 std::vector<const llvm::Type*> Params;
210 Params.push_back(Int8PtrTy);
211 Params.push_back(SuperPtrTy);
212 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000213 return CGM.CreateRuntimeFunction(
214 VMContext.getFunctionType(llvm::Type::VoidTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000215 Params, true),
216 "objc_msgSendSuper_stret");
217 }
218
219 llvm::Constant *getMessageSendSuperStretFn2() const {
220 // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
221 // SEL op, ...)
222 std::vector<const llvm::Type*> Params;
223 Params.push_back(Int8PtrTy);
224 Params.push_back(SuperPtrTy);
225 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000226 return CGM.CreateRuntimeFunction(
227 VMContext.getFunctionType(llvm::Type::VoidTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000228 Params, true),
229 "objc_msgSendSuper2_stret");
230 }
231
232 llvm::Constant *getMessageSendSuperFpretFn() const {
233 // There is no objc_msgSendSuper_fpret? How can that work?
234 return getMessageSendSuperFn();
235 }
236
237 llvm::Constant *getMessageSendSuperFpretFn2() const {
238 // There is no objc_msgSendSuper_fpret? How can that work?
239 return getMessageSendSuperFn2();
240 }
241
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000242protected:
243 CodeGen::CodeGenModule &CGM;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000244
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000245public:
Fariborz Jahanian0a855d02009-03-23 19:10:40 +0000246 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000247 const llvm::Type *Int8PtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000248
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000249 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
250 const llvm::Type *ObjectPtrTy;
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000251
252 /// PtrObjectPtrTy - LLVM type for id *
253 const llvm::Type *PtrObjectPtrTy;
254
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000255 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000256 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000257 /// ProtocolPtrTy - LLVM type for external protocol handles
258 /// (typeof(Protocol))
259 const llvm::Type *ExternalProtocolPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000260
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000261 // SuperCTy - clang type for struct objc_super.
262 QualType SuperCTy;
263 // SuperPtrCTy - clang type for struct objc_super *.
264 QualType SuperPtrCTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000265
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000266 /// SuperTy - LLVM type for struct objc_super.
267 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000268 /// SuperPtrTy - LLVM type for struct objc_super *.
269 const llvm::Type *SuperPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000270
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000271 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
272 /// in GCC parlance).
273 const llvm::StructType *PropertyTy;
274
275 /// PropertyListTy - LLVM type for struct objc_property_list
276 /// (_prop_list_t in GCC parlance).
277 const llvm::StructType *PropertyListTy;
278 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
279 const llvm::Type *PropertyListPtrTy;
280
281 // MethodTy - LLVM type for struct objc_method.
282 const llvm::StructType *MethodTy;
283
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000284 /// CacheTy - LLVM type for struct objc_cache.
285 const llvm::Type *CacheTy;
286 /// CachePtrTy - LLVM type for struct objc_cache *.
287 const llvm::Type *CachePtrTy;
288
Chris Lattner72db6c32009-04-22 02:44:54 +0000289 llvm::Constant *getGetPropertyFn() {
290 CodeGen::CodeGenTypes &Types = CGM.getTypes();
291 ASTContext &Ctx = CGM.getContext();
292 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
293 llvm::SmallVector<QualType,16> Params;
294 QualType IdType = Ctx.getObjCIdType();
295 QualType SelType = Ctx.getObjCSelType();
296 Params.push_back(IdType);
297 Params.push_back(SelType);
298 Params.push_back(Ctx.LongTy);
299 Params.push_back(Ctx.BoolTy);
300 const llvm::FunctionType *FTy =
301 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
302 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
303 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000304
Chris Lattner72db6c32009-04-22 02:44:54 +0000305 llvm::Constant *getSetPropertyFn() {
306 CodeGen::CodeGenTypes &Types = CGM.getTypes();
307 ASTContext &Ctx = CGM.getContext();
308 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
309 llvm::SmallVector<QualType,16> Params;
310 QualType IdType = Ctx.getObjCIdType();
311 QualType SelType = Ctx.getObjCSelType();
312 Params.push_back(IdType);
313 Params.push_back(SelType);
314 Params.push_back(Ctx.LongTy);
315 Params.push_back(IdType);
316 Params.push_back(Ctx.BoolTy);
317 Params.push_back(Ctx.BoolTy);
318 const llvm::FunctionType *FTy =
319 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
320 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
321 }
322
323 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000324 CodeGen::CodeGenTypes &Types = CGM.getTypes();
325 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000326 // void objc_enumerationMutation (id)
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000327 llvm::SmallVector<QualType,16> Params;
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
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000844 /// GetNameForMethod - Return a name for the given method.
845 /// \param[out] NameOut - The return value.
846 void GetNameForMethod(const ObjCMethodDecl *OMD,
847 const ObjCContainerDecl *CD,
848 std::string &NameOut);
849
850 /// GetMethodVarName - Return a unique constant for the given
851 /// selector's name. The return value has type char *.
852 llvm::Constant *GetMethodVarName(Selector Sel);
853 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
854 llvm::Constant *GetMethodVarName(const std::string &Name);
855
856 /// GetMethodVarType - Return a unique constant for the given
857 /// selector's name. The return value has type char *.
858
859 // FIXME: This is a horrible name.
860 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000861 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000862
863 /// GetPropertyName - Return a unique constant for the given
864 /// name. The return value has type char *.
865 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
866
867 // FIXME: This can be dropped once string functions are unified.
868 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
869 const Decl *Container);
870
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000871 /// GetClassName - Return a unique constant for the given selector's
872 /// name. The return value has type char *.
873 llvm::Constant *GetClassName(IdentifierInfo *Ident);
874
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000875 /// BuildIvarLayout - Builds ivar layout bitmap for the class
876 /// implementation for the __strong or __weak case.
877 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000878 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
879 bool ForStrongLayout);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000880
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000881 void BuildAggrIvarRecordLayout(const RecordType *RT,
882 unsigned int BytePos, bool ForStrongLayout,
883 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000884 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000885 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000886 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +0000887 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000888 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000889 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000890
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000891 /// GetIvarLayoutName - Returns a unique constant for the given
892 /// ivar layout bitmap.
893 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
894 const ObjCCommonTypesHelper &ObjCTypes);
895
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000896 /// EmitPropertyList - Emit the given property list. The return
897 /// value has type PropertyListPtrTy.
898 llvm::Constant *EmitPropertyList(const std::string &Name,
899 const Decl *Container,
900 const ObjCContainerDecl *OCD,
901 const ObjCCommonTypesHelper &ObjCTypes);
902
Fariborz Jahanianda320092009-01-29 19:24:30 +0000903 /// GetProtocolRef - Return a reference to the internal protocol
904 /// description, creating an empty one if it has not been
905 /// defined. The return value has type ProtocolPtrTy.
906 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000907
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000908 /// CreateMetadataVar - Create a global variable with internal
909 /// linkage for use by the Objective-C runtime.
910 ///
911 /// This is a convenience wrapper which not only creates the
912 /// variable, but also sets the section and alignment and adds the
Chris Lattnerad64e022009-07-17 23:57:13 +0000913 /// global to the "llvm.used" list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000914 ///
915 /// \param Name - The variable name.
916 /// \param Init - The variable initializer; this is also used to
917 /// define the type of the variable.
918 /// \param Section - The section the variable should go into, or 0.
919 /// \param Align - The alignment for the variable, or 0.
920 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000921 /// "llvm.used".
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000922 llvm::GlobalVariable *CreateMetadataVar(const std::string &Name,
923 llvm::Constant *Init,
924 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000925 unsigned Align,
926 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000927
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000928 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
929 QualType ResultType,
930 llvm::Value *Sel,
931 llvm::Value *Arg0,
932 QualType Arg0Ty,
933 bool IsSuper,
934 const CallArgList &CallArgs,
935 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000936
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000937public:
Owen Anderson69243822009-07-13 04:10:07 +0000938 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
939 CGM(cgm), VMContext(cgm.getLLVMContext())
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000940 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000941
Steve Naroff33fdb732009-03-31 16:53:37 +0000942 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000943
944 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
945 const ObjCContainerDecl *CD=0);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000946
947 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
948
949 /// GetOrEmitProtocol - Get the protocol object for the given
950 /// declaration, emitting it if necessary. The return value has type
951 /// ProtocolPtrTy.
952 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
953
954 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
955 /// object for the given declaration, emitting it if needed. These
956 /// forward references will be filled in with empty bodies if no
957 /// definition is seen. The return value has type ProtocolPtrTy.
958 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000959};
960
961class CGObjCMac : public CGObjCCommonMac {
962private:
963 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000964 /// EmitImageInfo - Emit the image info marker used to encode some module
965 /// level information.
966 void EmitImageInfo();
967
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000968 /// EmitModuleInfo - Another marker encoding module level
969 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000970 void EmitModuleInfo();
971
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000972 /// EmitModuleSymols - Emit module symbols, the list of defined
973 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000974 llvm::Constant *EmitModuleSymbols();
975
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000976 /// FinishModule - Write out global data structures at the end of
977 /// processing a translation unit.
978 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000979
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000980 /// EmitClassExtension - Generate the class extension structure used
981 /// to store the weak ivar layout and properties. The return value
982 /// has type ClassExtensionPtrTy.
983 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
984
985 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
986 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000987 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000988 const ObjCInterfaceDecl *ID);
989
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000990 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000991 QualType ResultType,
992 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000993 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000994 QualType Arg0Ty,
995 bool IsSuper,
996 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000997
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000998 /// EmitIvarList - Emit the ivar list for the given
999 /// implementation. If ForClass is true the list of class ivars
1000 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1001 /// interface ivars will be emitted. The return value has type
1002 /// IvarListPtrTy.
1003 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001004 bool ForClass);
1005
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001006 /// EmitMetaClass - Emit a forward reference to the class structure
1007 /// for the metaclass of the given interface. The return value has
1008 /// type ClassPtrTy.
1009 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1010
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001011 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001012 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001013 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1014 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001015 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001016
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001017 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001018
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001019 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001020
1021 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001022 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001023 llvm::Constant *EmitMethodList(const std::string &Name,
1024 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001025 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001026
1027 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001028 /// method declarations.
1029 /// - TypeName: The name for the type containing the methods.
1030 /// - IsProtocol: True iff these methods are for a protocol.
1031 /// - ClassMethds: True iff these are class methods.
1032 /// - Required: When true, only "required" methods are
1033 /// listed. Similarly, when false only "optional" methods are
1034 /// listed. For classes this should always be true.
1035 /// - begin, end: The method list to output.
1036 ///
1037 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001038 llvm::Constant *EmitMethodDescList(const std::string &Name,
1039 const char *Section,
1040 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001041
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001042 /// GetOrEmitProtocol - Get the protocol object for the given
1043 /// declaration, emitting it if necessary. The return value has type
1044 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001045 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001046
1047 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1048 /// object for the given declaration, emitting it if needed. These
1049 /// forward references will be filled in with empty bodies if no
1050 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001051 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001052
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001053 /// EmitProtocolExtension - Generate the protocol extension
1054 /// structure used to store optional instance and class methods, and
1055 /// protocol properties. The return value has type
1056 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001057 llvm::Constant *
1058 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1059 const ConstantVector &OptInstanceMethods,
1060 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001061
1062 /// EmitProtocolList - Generate the list of referenced
1063 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +00001064 llvm::Constant *EmitProtocolList(const std::string &Name,
1065 ObjCProtocolDecl::protocol_iterator begin,
1066 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001067
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001068 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1069 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001070 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001071
Fariborz Jahanianda320092009-01-29 19:24:30 +00001072 public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001073 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001074
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001075 virtual llvm::Function *ModuleInitFunction();
1076
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001077 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001078 QualType ResultType,
1079 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001080 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001081 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001082 const CallArgList &CallArgs,
1083 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001084
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001085 virtual CodeGen::RValue
1086 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001087 QualType ResultType,
1088 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001089 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001090 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001091 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001092 bool IsClassMessage,
1093 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001094
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001095 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001096 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001097
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001098 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001099
1100 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1101 /// untyped one.
1102 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1103 const ObjCMethodDecl *Method);
1104
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001105 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001106
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001107 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001108
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001109 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001110 const ObjCProtocolDecl *PD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00001111
Chris Lattner74391b42009-03-22 21:03:39 +00001112 virtual llvm::Constant *GetPropertyGetFunction();
1113 virtual llvm::Constant *GetPropertySetFunction();
1114 virtual llvm::Constant *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001115
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001116 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1117 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001118 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1119 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001120 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001121 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001122 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1123 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001124 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1125 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001126 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1127 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001128 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1129 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001130 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1131 llvm::Value *dest, llvm::Value *src,
1132 unsigned long size);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001133
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001134 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1135 QualType ObjectTy,
1136 llvm::Value *BaseValue,
1137 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001138 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001139 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001140 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001141 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001142};
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001143
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001144class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001145private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001146 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001147 llvm::GlobalVariable* ObjCEmptyCacheVar;
1148 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001149
Daniel Dunbar11394522009-04-18 08:51:00 +00001150 /// SuperClassReferences - uniqued super class references.
1151 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
1152
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001153 /// MetaClassReferences - uniqued meta class references.
1154 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001155
1156 /// EHTypeReferences - uniqued class ehtype references.
1157 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001158
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001159 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001160 /// legacy messaging dispatch.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001161 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001162
1163 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001164 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001165 bool LegacyDispatchedSelector(Selector Sel);
1166
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001167 /// FinishNonFragileABIModule - Write out global data structures at the end of
1168 /// processing a translation unit.
1169 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001170
Daniel Dunbar463b8762009-05-15 21:48:48 +00001171 /// AddModuleClassList - Add the given list of class pointers to the
1172 /// module with the provided symbol and section names.
1173 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1174 const char *SymbolName,
1175 const char *SectionName);
1176
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00001177 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1178 unsigned InstanceStart,
1179 unsigned InstanceSize,
1180 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001181 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
1182 llvm::Constant *IsAGV,
1183 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001184 llvm::Constant *ClassRoGV,
1185 bool HiddenVisibility);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001186
1187 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
1188
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001189 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
1190
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001191 /// EmitMethodList - Emit the method list for the given
1192 /// implementation. The return value has type MethodListnfABITy.
1193 llvm::Constant *EmitMethodList(const std::string &Name,
1194 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001195 const ConstantVector &Methods);
1196 /// EmitIvarList - Emit the ivar list for the given
1197 /// implementation. If ForClass is true the list of class ivars
1198 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1199 /// interface ivars will be emitted. The return value has type
1200 /// IvarListnfABIPtrTy.
1201 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00001202
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001203 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001204 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001205 unsigned long int offset);
1206
Fariborz Jahanianda320092009-01-29 19:24:30 +00001207 /// GetOrEmitProtocol - Get the protocol object for the given
1208 /// declaration, emitting it if necessary. The return value has type
1209 /// ProtocolPtrTy.
1210 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
1211
1212 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1213 /// object for the given declaration, emitting it if needed. These
1214 /// forward references will be filled in with empty bodies if no
1215 /// definition is seen. The return value has type ProtocolPtrTy.
1216 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
1217
1218 /// EmitProtocolList - Generate the list of referenced
1219 /// protocols. The return value has type ProtocolListPtrTy.
1220 llvm::Constant *EmitProtocolList(const std::string &Name,
1221 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001222 ObjCProtocolDecl::protocol_iterator end);
1223
1224 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1225 QualType ResultType,
1226 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00001227 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001228 QualType Arg0Ty,
1229 bool IsSuper,
1230 const CallArgList &CallArgs);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001231
1232 /// GetClassGlobal - Return the global variable for the Objective-C
1233 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001234 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
1235
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001236 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001237 /// for the given class reference.
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001238 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001239 const ObjCInterfaceDecl *ID);
1240
1241 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1242 /// for the given super class reference.
1243 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1244 const ObjCInterfaceDecl *ID);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001245
1246 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1247 /// meta-data
1248 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
1249 const ObjCInterfaceDecl *ID);
1250
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001251 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1252 /// the given ivar.
1253 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001254 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00001255 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001256 const ObjCIvarDecl *Ivar);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001257
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001258 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1259 /// for the given selector.
1260 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbare588b992009-03-01 04:46:24 +00001261
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001262 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001263 /// interface. The return value has type EHTypePtrTy.
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001264 llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
1265 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001266
1267 const char *getMetaclassSymbolPrefix() const {
1268 return "OBJC_METACLASS_$_";
1269 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001270
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001271 const char *getClassSymbolPrefix() const {
1272 return "OBJC_CLASS_$_";
1273 }
1274
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001275 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001276 uint32_t &InstanceStart,
1277 uint32_t &InstanceSize);
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001278
1279 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001280 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001281 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1282 return CGM.getContext().Selectors.getSelector(0, &II);
1283 }
1284
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001285 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001286 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1287 return CGM.getContext().Selectors.getSelector(1, &II);
1288 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001289
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001290 /// ImplementationIsNonLazy - Check whether the given category or
1291 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001292 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001293
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001294public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001295 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001296 // FIXME. All stubs for now!
1297 virtual llvm::Function *ModuleInitFunction();
1298
1299 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
1300 QualType ResultType,
1301 Selector Sel,
1302 llvm::Value *Receiver,
1303 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001304 const CallArgList &CallArgs,
1305 const ObjCMethodDecl *Method);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001306
1307 virtual CodeGen::RValue
1308 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
1309 QualType ResultType,
1310 Selector Sel,
1311 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001312 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001313 llvm::Value *Receiver,
1314 bool IsClassMessage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001315 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001316
1317 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001318 const ObjCInterfaceDecl *ID);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001319
1320 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001321 { return EmitSelector(Builder, Sel); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001322
1323 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1324 /// untyped one.
1325 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1326 const ObjCMethodDecl *Method)
1327 { return EmitSelector(Builder, Method->getSelector()); }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001328
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001329 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001330
1331 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001332 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001333 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001334
Chris Lattner74391b42009-03-22 21:03:39 +00001335 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001336 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001337 }
Chris Lattner74391b42009-03-22 21:03:39 +00001338 virtual llvm::Constant *GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001339 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001340 }
Chris Lattner74391b42009-03-22 21:03:39 +00001341 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001342 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001343 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001344
1345 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00001346 const Stmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001347 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001348 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001349 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001350 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001351 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001352 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001353 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001354 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001355 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001356 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001357 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001358 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001359 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1360 llvm::Value *dest, llvm::Value *src,
1361 unsigned long size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001362 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1363 QualType ObjectTy,
1364 llvm::Value *BaseValue,
1365 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001366 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001367 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001368 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001369 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001370};
1371
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001372} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001373
1374/* *** Helper Functions *** */
1375
1376/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001377static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
1378 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001379 unsigned idx0,
1380 unsigned idx1) {
1381 llvm::Value *Idxs[] = {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001382 VMContext.getConstantInt(llvm::Type::Int32Ty, idx0),
1383 VMContext.getConstantInt(llvm::Type::Int32Ty, idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001384 };
Owen Andersona1cf15f2009-07-14 23:10:40 +00001385 return VMContext.getConstantExprGetElementPtr(C, Idxs, 2);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001386}
1387
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001388/// hasObjCExceptionAttribute - Return true if this class or any super
1389/// class has the __objc_exception__ attribute.
Douglas Gregor68584ed2009-06-18 16:11:24 +00001390static bool hasObjCExceptionAttribute(ASTContext &Context,
1391 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001392 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001393 return true;
1394 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001395 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001396 return false;
1397}
1398
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001399/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001400
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001401CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
1402 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001403{
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001404 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001405 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001406}
1407
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001408/// GetClass - Return a reference to the class for the given interface
1409/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001410llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001411 const ObjCInterfaceDecl *ID) {
1412 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001413}
1414
1415/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001416llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001417 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001418}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001419llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
1420 *Method) {
1421 return EmitSelector(Builder, Method->getSelector());
1422}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001423
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001424/// Generate a constant CFString object.
1425/*
1426 struct __builtin_CFString {
1427 const int *isa; // point to __CFConstantStringClassReference
1428 int flags;
1429 const char *str;
1430 long length;
1431 };
1432*/
1433
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001434llvm::Constant *CGObjCCommonMac::GenerateConstantString(
Steve Naroff33fdb732009-03-31 16:53:37 +00001435 const ObjCStringLiteral *SL) {
Steve Naroff8d4141f2009-04-01 13:55:36 +00001436 return CGM.GetAddrOfConstantCFString(SL->getString());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001437}
1438
1439/// Generates a message send where the super is the receiver. This is
1440/// a message send to self with special delivery semantics indicating
1441/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001442CodeGen::RValue
1443CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001444 QualType ResultType,
1445 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001446 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001447 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001448 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001449 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001450 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001451 // Create and init a super structure; this is a (receiver, class)
1452 // pair we will pass to objc_msgSendSuper.
1453 llvm::Value *ObjCSuper =
1454 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
1455 llvm::Value *ReceiverAsObject =
1456 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
1457 CGF.Builder.CreateStore(ReceiverAsObject,
1458 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001459
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001460 // If this is a class message the metaclass is passed as the target.
1461 llvm::Value *Target;
1462 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001463 if (isCategoryImpl) {
1464 // Message sent to 'super' in a class method defined in a category
1465 // implementation requires an odd treatment.
1466 // If we are in a class method, we must retrieve the
1467 // _metaclass_ for the current class, pointed at by
1468 // the class's "isa" pointer. The following assumes that
1469 // isa" is the first ivar in a class (which it must be).
1470 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1471 Target = CGF.Builder.CreateStructGEP(Target, 0);
1472 Target = CGF.Builder.CreateLoad(Target);
1473 }
1474 else {
1475 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1476 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1477 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1478 Target = Super;
1479 }
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001480 } else {
1481 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1482 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001483 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1484 // ObjCTypes types.
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001485 const llvm::Type *ClassTy =
1486 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001487 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001488 CGF.Builder.CreateStore(Target,
1489 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001490 return EmitLegacyMessageSend(CGF, ResultType,
1491 EmitSelector(CGF.Builder, Sel),
1492 ObjCSuper, ObjCTypes.SuperPtrCTy,
1493 true, CallArgs, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001494}
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001495
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001496/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001497CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001498 QualType ResultType,
1499 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001500 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001501 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001502 const CallArgList &CallArgs,
1503 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001504 return EmitLegacyMessageSend(CGF, ResultType,
1505 EmitSelector(CGF.Builder, Sel),
1506 Receiver, CGF.getContext().getObjCIdType(),
1507 false, CallArgs, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001508}
1509
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001510CodeGen::RValue CGObjCCommonMac::EmitLegacyMessageSend(
1511 CodeGen::CodeGenFunction &CGF,
1512 QualType ResultType,
1513 llvm::Value *Sel,
1514 llvm::Value *Arg0,
1515 QualType Arg0Ty,
1516 bool IsSuper,
1517 const CallArgList &CallArgs,
1518 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001519 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001520 if (!IsSuper)
1521 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001522 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001523 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001524 CGF.getContext().getObjCSelType()));
1525 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001526
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001527 CodeGenTypes &Types = CGM.getTypes();
1528 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001529 // In 64bit ABI, type must be assumed VARARG. In 32bit abi,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001530 // it seems not to matter.
1531 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, (ObjCABI == 2));
1532
1533 llvm::Constant *Fn = NULL;
Daniel Dunbar88b53962009-02-02 22:03:45 +00001534 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001535 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
1536 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001537 } else if (ResultType->isFloatingType()) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001538 if (ObjCABI == 2) {
1539 if (const BuiltinType *BT = ResultType->getAsBuiltinType()) {
1540 BuiltinType::Kind k = BT->getKind();
1541 Fn = (k == BuiltinType::LongDouble) ? ObjCTypes.getSendFpretFn2(IsSuper)
1542 : ObjCTypes.getSendFn2(IsSuper);
Daniel Dunbar42f963d2009-06-10 04:38:50 +00001543 } else {
1544 Fn = ObjCTypes.getSendFn2(IsSuper);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001545 }
1546 }
1547 else
Mike Stumpf5408fe2009-05-16 07:57:57 +00001548 // FIXME. This currently matches gcc's API for x86-32. May need to change
1549 // for others if we have their API.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001550 Fn = ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001551 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001552 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
1553 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001554 }
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001555 assert(Fn && "EmitLegacyMessageSend - unknown API");
Owen Andersona1cf15f2009-07-14 23:10:40 +00001556 Fn = VMContext.getConstantExprBitCast(Fn,
1557 VMContext.getPointerTypeUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +00001558 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001559}
1560
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001561llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001562 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001563 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001564 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001565 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1566
Owen Andersona1cf15f2009-07-14 23:10:40 +00001567 return VMContext.getConstantExprBitCast(GetProtocolRef(PD),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001568 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001569}
1570
Fariborz Jahanianda320092009-01-29 19:24:30 +00001571void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001572 // FIXME: We shouldn't need this, the protocol decl should contain enough
1573 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001574 DefinedProtocols.insert(PD->getIdentifier());
1575
1576 // If we have generated a forward reference to this protocol, emit
1577 // it now. Otherwise do nothing, the protocol objects are lazily
1578 // emitted.
1579 if (Protocols.count(PD->getIdentifier()))
1580 GetOrEmitProtocol(PD);
1581}
1582
Fariborz Jahanianda320092009-01-29 19:24:30 +00001583llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001584 if (DefinedProtocols.count(PD->getIdentifier()))
1585 return GetOrEmitProtocol(PD);
1586 return GetOrEmitProtocolRef(PD);
1587}
1588
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001589/*
1590 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1591 struct _objc_protocol {
1592 struct _objc_protocol_extension *isa;
1593 char *protocol_name;
1594 struct _objc_protocol_list *protocol_list;
1595 struct _objc__method_prototype_list *instance_methods;
1596 struct _objc__method_prototype_list *class_methods
1597 };
1598
1599 See EmitProtocolExtension().
1600*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001601llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1602 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1603
1604 // Early exit if a defining object has already been generated.
1605 if (Entry && Entry->hasInitializer())
1606 return Entry;
1607
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001608 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001609 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001610 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1611
Chris Lattner8ec03f52008-11-24 03:54:41 +00001612 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001613
1614 // Construct method lists.
1615 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1616 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00001617 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001618 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001619 ObjCMethodDecl *MD = *i;
1620 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1621 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1622 OptInstanceMethods.push_back(C);
1623 } else {
1624 InstanceMethods.push_back(C);
1625 }
1626 }
1627
Douglas Gregor6ab35242009-04-09 21:40:53 +00001628 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001629 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001630 ObjCMethodDecl *MD = *i;
1631 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1632 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1633 OptClassMethods.push_back(C);
1634 } else {
1635 ClassMethods.push_back(C);
1636 }
1637 }
1638
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001639 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001640 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001641 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001642 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001643 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001644 PD->protocol_begin(),
1645 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001646 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001647 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
1648 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001649 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1650 InstanceMethods);
1651 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001652 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
1653 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001654 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1655 ClassMethods);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001656 llvm::Constant *Init = VMContext.getConstantStruct(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001657 Values);
1658
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001659 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001660 // Already created, fix the linkage and update the initializer.
1661 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001662 Entry->setInitializer(Init);
1663 } else {
1664 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001665 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001666 llvm::GlobalValue::InternalLinkage,
1667 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00001668 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001669 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001670 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001671 // FIXME: Is this necessary? Why only for protocol?
1672 Entry->setAlignment(4);
1673 }
Chris Lattnerad64e022009-07-17 23:57:13 +00001674 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001675
1676 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001677}
1678
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001679llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001680 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1681
1682 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001683 // We use the initializer as a marker of whether this is a forward
1684 // reference or not. At module finalization we add the empty
1685 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001686 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001687 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001688 llvm::GlobalValue::ExternalLinkage,
1689 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00001690 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001691 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001692 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001693 // FIXME: Is this necessary? Why only for protocol?
1694 Entry->setAlignment(4);
1695 }
1696
1697 return Entry;
1698}
1699
1700/*
1701 struct _objc_protocol_extension {
1702 uint32_t size;
1703 struct objc_method_description_list *optional_instance_methods;
1704 struct objc_method_description_list *optional_class_methods;
1705 struct objc_property_list *instance_properties;
1706 };
1707*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001708llvm::Constant *
1709CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1710 const ConstantVector &OptInstanceMethods,
1711 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001712 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00001713 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001714 std::vector<llvm::Constant*> Values(4);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001715 Values[0] = VMContext.getConstantInt(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001716 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001717 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1718 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001719 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1720 OptInstanceMethods);
1721 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001722 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1723 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001724 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1725 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001726 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1727 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001728 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001729
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001730 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001731 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1732 Values[3]->isNullValue())
Owen Anderson69243822009-07-13 04:10:07 +00001733 return VMContext.getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001734
1735 llvm::Constant *Init =
Owen Andersona1cf15f2009-07-14 23:10:40 +00001736 VMContext.getConstantStruct(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001737
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001738 // No special section, but goes in llvm.used
1739 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
1740 Init,
1741 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001742}
1743
1744/*
1745 struct objc_protocol_list {
1746 struct objc_protocol_list *next;
1747 long count;
1748 Protocol *list[];
1749 };
1750*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001751llvm::Constant *
1752CGObjCMac::EmitProtocolList(const std::string &Name,
1753 ObjCProtocolDecl::protocol_iterator begin,
1754 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001755 std::vector<llvm::Constant*> ProtocolRefs;
1756
Daniel Dunbardbc933702008-08-21 21:57:41 +00001757 for (; begin != end; ++begin)
1758 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001759
1760 // Just return null for empty protocol lists
1761 if (ProtocolRefs.empty())
Owen Anderson69243822009-07-13 04:10:07 +00001762 return VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001763
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001764 // This list is null terminated.
Owen Anderson69243822009-07-13 04:10:07 +00001765 ProtocolRefs.push_back(VMContext.getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001766
1767 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001768 // This field is only used by the runtime.
Owen Anderson69243822009-07-13 04:10:07 +00001769 Values[0] = VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001770 Values[1] = VMContext.getConstantInt(ObjCTypes.LongTy,
1771 ProtocolRefs.size() - 1);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001772 Values[2] =
Owen Andersona1cf15f2009-07-14 23:10:40 +00001773 VMContext.getConstantArray(VMContext.getArrayType(ObjCTypes.ProtocolPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001774 ProtocolRefs.size()),
1775 ProtocolRefs);
1776
Owen Andersona1cf15f2009-07-14 23:10:40 +00001777 llvm::Constant *Init = VMContext.getConstantStruct(Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001778 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001779 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001780 4, false);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001781 return VMContext.getConstantExprBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001782}
1783
1784/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001785 struct _objc_property {
1786 const char * const name;
1787 const char * const attributes;
1788 };
1789
1790 struct _objc_property_list {
1791 uint32_t entsize; // sizeof (struct _objc_property)
1792 uint32_t prop_count;
1793 struct _objc_property[prop_count];
1794 };
1795*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001796llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1797 const Decl *Container,
1798 const ObjCContainerDecl *OCD,
1799 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001800 std::vector<llvm::Constant*> Properties, Prop(2);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001801 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1802 E = OCD->prop_end(); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00001803 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001804 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001805 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001806 Properties.push_back(VMContext.getConstantStruct(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001807 Prop));
1808 }
1809
1810 // Return null for empty list.
1811 if (Properties.empty())
Owen Anderson69243822009-07-13 04:10:07 +00001812 return VMContext.getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001813
1814 unsigned PropertySize =
Duncan Sands9408c452009-05-09 07:08:47 +00001815 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001816 std::vector<llvm::Constant*> Values(3);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001817 Values[0] = VMContext.getConstantInt(ObjCTypes.IntTy, PropertySize);
1818 Values[1] = VMContext.getConstantInt(ObjCTypes.IntTy, Properties.size());
1819 llvm::ArrayType *AT = VMContext.getArrayType(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001820 Properties.size());
Owen Andersona1cf15f2009-07-14 23:10:40 +00001821 Values[2] = VMContext.getConstantArray(AT, Properties);
1822 llvm::Constant *Init = VMContext.getConstantStruct(Values);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001823
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001824 llvm::GlobalVariable *GV =
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001825 CreateMetadataVar(Name, Init,
1826 (ObjCABI == 2) ? "__DATA, __objc_const" :
1827 "__OBJC,__property,regular,no_dead_strip",
1828 (ObjCABI == 2) ? 8 : 4,
1829 true);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001830 return VMContext.getConstantExprBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001831}
1832
1833/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001834 struct objc_method_description_list {
1835 int count;
1836 struct objc_method_description list[];
1837 };
1838*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001839llvm::Constant *
1840CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1841 std::vector<llvm::Constant*> Desc(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001842 Desc[0] =
1843 VMContext.getConstantExprBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001844 ObjCTypes.SelectorPtrTy);
1845 Desc[1] = GetMethodVarType(MD);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001846 return VMContext.getConstantStruct(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001847 Desc);
1848}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001849
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001850llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1851 const char *Section,
1852 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001853 // Return null for empty list.
1854 if (Methods.empty())
Owen Anderson69243822009-07-13 04:10:07 +00001855 return VMContext.getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001856
1857 std::vector<llvm::Constant*> Values(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001858 Values[0] = VMContext.getConstantInt(ObjCTypes.IntTy, Methods.size());
1859 llvm::ArrayType *AT = VMContext.getArrayType(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001860 Methods.size());
Owen Andersona1cf15f2009-07-14 23:10:40 +00001861 Values[1] = VMContext.getConstantArray(AT, Methods);
1862 llvm::Constant *Init = VMContext.getConstantStruct(Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001863
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001864 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001865 return VMContext.getConstantExprBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001866 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001867}
1868
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001869/*
1870 struct _objc_category {
1871 char *category_name;
1872 char *class_name;
1873 struct _objc_method_list *instance_methods;
1874 struct _objc_method_list *class_methods;
1875 struct _objc_protocol_list *protocols;
1876 uint32_t size; // <rdar://4585769>
1877 struct _objc_property_list *instance_properties;
1878 };
1879 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001880void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sands9408c452009-05-09 07:08:47 +00001881 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001882
Mike Stumpf5408fe2009-05-16 07:57:57 +00001883 // FIXME: This is poor design, the OCD should have a pointer to the category
1884 // decl. Additionally, note that Category can be null for the @implementation
1885 // w/o an @interface case. Sema should just create one for us as it does for
1886 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001887 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001888 const ObjCCategoryDecl *Category =
1889 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001890 std::string ExtName(Interface->getNameAsString() + "_" +
1891 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001892
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001893 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001894 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001895 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001896 // Instance methods should always be defined.
1897 InstanceMethods.push_back(GetMethodConstant(*i));
1898 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001899 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001900 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001901 // Class methods should always be defined.
1902 ClassMethods.push_back(GetMethodConstant(*i));
1903 }
1904
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001905 std::vector<llvm::Constant*> Values(7);
1906 Values[0] = GetClassName(OCD->getIdentifier());
1907 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00001908 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001909 Values[2] =
1910 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1911 ExtName,
1912 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001913 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001914 Values[3] =
1915 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001916 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001917 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001918 if (Category) {
1919 Values[4] =
1920 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1921 Category->protocol_begin(),
1922 Category->protocol_end());
1923 } else {
Owen Anderson69243822009-07-13 04:10:07 +00001924 Values[4] = VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001925 }
Owen Andersona1cf15f2009-07-14 23:10:40 +00001926 Values[5] = VMContext.getConstantInt(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001927
1928 // If there is no category @interface then there can be no properties.
1929 if (Category) {
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001930 Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001931 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001932 } else {
Owen Anderson69243822009-07-13 04:10:07 +00001933 Values[6] = VMContext.getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001934 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001935
Owen Andersona1cf15f2009-07-14 23:10:40 +00001936 llvm::Constant *Init = VMContext.getConstantStruct(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001937 Values);
1938
1939 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001940 CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init,
1941 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001942 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001943 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001944}
1945
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001946// FIXME: Get from somewhere?
1947enum ClassFlags {
1948 eClassFlags_Factory = 0x00001,
1949 eClassFlags_Meta = 0x00002,
1950 // <rdr://5142207>
1951 eClassFlags_HasCXXStructors = 0x02000,
1952 eClassFlags_Hidden = 0x20000,
1953 eClassFlags_ABI2_Hidden = 0x00010,
1954 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1955};
1956
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001957/*
1958 struct _objc_class {
1959 Class isa;
1960 Class super_class;
1961 const char *name;
1962 long version;
1963 long info;
1964 long instance_size;
1965 struct _objc_ivar_list *ivars;
1966 struct _objc_method_list *methods;
1967 struct _objc_cache *cache;
1968 struct _objc_protocol_list *protocols;
1969 // Objective-C 1.0 extensions (<rdr://4585769>)
1970 const char *ivar_layout;
1971 struct _objc_class_ext *ext;
1972 };
1973
1974 See EmitClassExtension();
1975 */
1976void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001977 DefinedSymbols.insert(ID->getIdentifier());
1978
Chris Lattner8ec03f52008-11-24 03:54:41 +00001979 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001980 // FIXME: Gross
1981 ObjCInterfaceDecl *Interface =
1982 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001983 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001984 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001985 Interface->protocol_begin(),
1986 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001987 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar2bebbf02009-05-03 10:46:44 +00001988 unsigned Size =
1989 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001990
1991 // FIXME: Set CXX-structors flag.
Daniel Dunbar04d40782009-04-14 06:00:08 +00001992 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001993 Flags |= eClassFlags_Hidden;
1994
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001995 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001996 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001997 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001998 // Instance methods should always be defined.
1999 InstanceMethods.push_back(GetMethodConstant(*i));
2000 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00002001 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002002 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002003 // Class methods should always be defined.
2004 ClassMethods.push_back(GetMethodConstant(*i));
2005 }
2006
Douglas Gregor653f1b12009-04-23 01:02:12 +00002007 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002008 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002009 ObjCPropertyImplDecl *PID = *i;
2010
2011 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2012 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2013
2014 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2015 if (llvm::Constant *C = GetMethodConstant(MD))
2016 InstanceMethods.push_back(C);
2017 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2018 if (llvm::Constant *C = GetMethodConstant(MD))
2019 InstanceMethods.push_back(C);
2020 }
2021 }
2022
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002023 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar5384b092009-05-03 08:56:52 +00002024 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002025 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002026 // Record a reference to the super class.
2027 LazySymbols.insert(Super->getIdentifier());
2028
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002029 Values[ 1] =
Owen Andersona1cf15f2009-07-14 23:10:40 +00002030 VMContext.getConstantExprBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002031 ObjCTypes.ClassPtrTy);
2032 } else {
Owen Anderson69243822009-07-13 04:10:07 +00002033 Values[ 1] = VMContext.getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002034 }
2035 Values[ 2] = GetClassName(ID->getIdentifier());
2036 // Version is always 0.
Owen Andersona1cf15f2009-07-14 23:10:40 +00002037 Values[ 3] = VMContext.getConstantInt(ObjCTypes.LongTy, 0);
2038 Values[ 4] = VMContext.getConstantInt(ObjCTypes.LongTy, Flags);
2039 Values[ 5] = VMContext.getConstantInt(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002040 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002041 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002042 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002043 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002044 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002045 // cache is always NULL.
Owen Anderson69243822009-07-13 04:10:07 +00002046 Values[ 8] = VMContext.getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002047 Values[ 9] = Protocols;
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002048 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002049 Values[11] = EmitClassExtension(ID);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002050 llvm::Constant *Init = VMContext.getConstantStruct(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002051 Values);
2052
2053 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002054 CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init,
2055 "__OBJC,__class,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002056 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002057 DefinedClasses.push_back(GV);
2058}
2059
2060llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2061 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002062 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002063 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002064 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002065
Daniel Dunbar04d40782009-04-14 06:00:08 +00002066 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002067 Flags |= eClassFlags_Hidden;
2068
2069 std::vector<llvm::Constant*> Values(12);
2070 // The isa for the metaclass is the root of the hierarchy.
2071 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2072 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2073 Root = Super;
2074 Values[ 0] =
Owen Andersona1cf15f2009-07-14 23:10:40 +00002075 VMContext.getConstantExprBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002076 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002077 // The super class for the metaclass is emitted as the name of the
2078 // super class. The runtime fixes this up to point to the
2079 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002080 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
2081 Values[ 1] =
Owen Andersona1cf15f2009-07-14 23:10:40 +00002082 VMContext.getConstantExprBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002083 ObjCTypes.ClassPtrTy);
2084 } else {
Owen Anderson69243822009-07-13 04:10:07 +00002085 Values[ 1] = VMContext.getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002086 }
2087 Values[ 2] = GetClassName(ID->getIdentifier());
2088 // Version is always 0.
Owen Andersona1cf15f2009-07-14 23:10:40 +00002089 Values[ 3] = VMContext.getConstantInt(ObjCTypes.LongTy, 0);
2090 Values[ 4] = VMContext.getConstantInt(ObjCTypes.LongTy, Flags);
2091 Values[ 5] = VMContext.getConstantInt(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002092 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002093 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002094 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002095 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002096 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002097 // cache is always NULL.
Owen Anderson69243822009-07-13 04:10:07 +00002098 Values[ 8] = VMContext.getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002099 Values[ 9] = Protocols;
2100 // ivar_layout for metaclass is always NULL.
Owen Anderson69243822009-07-13 04:10:07 +00002101 Values[10] = VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002102 // The class extension is always unused for metaclasses.
Owen Anderson69243822009-07-13 04:10:07 +00002103 Values[11] = VMContext.getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002104 llvm::Constant *Init = VMContext.getConstantStruct(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002105 Values);
2106
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002107 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002108 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002109
2110 // Check for a forward reference.
2111 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2112 if (GV) {
2113 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2114 "Forward metaclass reference has incorrect type.");
2115 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2116 GV->setInitializer(Init);
2117 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002118 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002119 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002120 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002121 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002122 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002123 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002124 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002125
2126 return GV;
2127}
2128
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002129llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002130 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002131
Mike Stumpf5408fe2009-05-16 07:57:57 +00002132 // FIXME: Should we look these up somewhere other than the module. Its a bit
2133 // silly since we only generate these while processing an implementation, so
2134 // exactly one pointer would work if know when we entered/exitted an
2135 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002136
2137 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002138 // Previously, metaclass with internal linkage may have been defined.
2139 // pass 'true' as 2nd argument so it is returned.
2140 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002141 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2142 "Forward metaclass reference has incorrect type.");
2143 return GV;
2144 } else {
2145 // Generate as an external reference to keep a consistent
2146 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002147 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002148 llvm::GlobalValue::ExternalLinkage,
2149 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002150 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002151 }
2152}
2153
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002154/*
2155 struct objc_class_ext {
2156 uint32_t size;
2157 const char *weak_ivar_layout;
2158 struct _objc_property_list *properties;
2159 };
2160*/
2161llvm::Constant *
2162CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
2163 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002164 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002165
2166 std::vector<llvm::Constant*> Values(3);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002167 Values[0] = VMContext.getConstantInt(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002168 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002169 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002170 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002171
2172 // Return null if no extension bits are used.
2173 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson69243822009-07-13 04:10:07 +00002174 return VMContext.getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002175
2176 llvm::Constant *Init =
Owen Andersona1cf15f2009-07-14 23:10:40 +00002177 VMContext.getConstantStruct(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002178 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002179 Init, "__OBJC,__class_ext,regular,no_dead_strip",
2180 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002181}
2182
2183/*
2184 struct objc_ivar {
2185 char *ivar_name;
2186 char *ivar_type;
2187 int ivar_offset;
2188 };
2189
2190 struct objc_ivar_list {
2191 int ivar_count;
2192 struct objc_ivar list[count];
2193 };
2194 */
2195llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002196 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002197 std::vector<llvm::Constant*> Ivars, Ivar(3);
2198
2199 // When emitting the root class GCC emits ivar entries for the
2200 // actual class structure. It is not clear if we need to follow this
2201 // behavior; for now lets try and get away with not doing it. If so,
2202 // the cleanest solution would be to make up an ObjCInterfaceDecl
2203 // for the class.
2204 if (ForClass)
Owen Anderson69243822009-07-13 04:10:07 +00002205 return VMContext.getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002206
2207 ObjCInterfaceDecl *OID =
2208 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002209
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002210 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002211 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002212
2213 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2214 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002215 // Ignore unnamed bit-fields.
2216 if (!IVD->getDeclName())
2217 continue;
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002218 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2219 Ivar[1] = GetMethodVarType(IVD);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002220 Ivar[2] = VMContext.getConstantInt(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002221 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Andersona1cf15f2009-07-14 23:10:40 +00002222 Ivars.push_back(VMContext.getConstantStruct(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002223 }
2224
2225 // Return null for empty list.
2226 if (Ivars.empty())
Owen Anderson69243822009-07-13 04:10:07 +00002227 return VMContext.getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002228
2229 std::vector<llvm::Constant*> Values(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002230 Values[0] = VMContext.getConstantInt(ObjCTypes.IntTy, Ivars.size());
2231 llvm::ArrayType *AT = VMContext.getArrayType(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002232 Ivars.size());
Owen Andersona1cf15f2009-07-14 23:10:40 +00002233 Values[1] = VMContext.getConstantArray(AT, Ivars);
2234 llvm::Constant *Init = VMContext.getConstantStruct(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002235
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002236 llvm::GlobalVariable *GV;
2237 if (ForClass)
2238 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(),
Daniel Dunbar58a29122009-03-09 22:18:41 +00002239 Init, "__OBJC,__class_vars,regular,no_dead_strip",
2240 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002241 else
2242 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_"
2243 + ID->getNameAsString(),
2244 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002245 4, true);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002246 return VMContext.getConstantExprBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002247}
2248
2249/*
2250 struct objc_method {
2251 SEL method_name;
2252 char *method_types;
2253 void *method;
2254 };
2255
2256 struct objc_method_list {
2257 struct objc_method_list *obsolete;
2258 int count;
2259 struct objc_method methods_list[count];
2260 };
2261*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002262
2263/// GetMethodConstant - Return a struct objc_method constant for the
2264/// given method if it has been defined. The result is null if the
2265/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002266llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002267 // FIXME: Use DenseMap::lookup
2268 llvm::Function *Fn = MethodDefinitions[MD];
2269 if (!Fn)
2270 return 0;
2271
2272 std::vector<llvm::Constant*> Method(3);
2273 Method[0] =
Owen Andersona1cf15f2009-07-14 23:10:40 +00002274 VMContext.getConstantExprBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002275 ObjCTypes.SelectorPtrTy);
2276 Method[1] = GetMethodVarType(MD);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002277 Method[2] = VMContext.getConstantExprBitCast(Fn, ObjCTypes.Int8PtrTy);
2278 return VMContext.getConstantStruct(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002279}
2280
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002281llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
2282 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002283 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002284 // Return null for empty list.
2285 if (Methods.empty())
Owen Anderson69243822009-07-13 04:10:07 +00002286 return VMContext.getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002287
2288 std::vector<llvm::Constant*> Values(3);
Owen Anderson69243822009-07-13 04:10:07 +00002289 Values[0] = VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002290 Values[1] = VMContext.getConstantInt(ObjCTypes.IntTy, Methods.size());
2291 llvm::ArrayType *AT = VMContext.getArrayType(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002292 Methods.size());
Owen Andersona1cf15f2009-07-14 23:10:40 +00002293 Values[2] = VMContext.getConstantArray(AT, Methods);
2294 llvm::Constant *Init = VMContext.getConstantStruct(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002295
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002296 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002297 return VMContext.getConstantExprBitCast(GV,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002298 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002299}
2300
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002301llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00002302 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002303 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002304 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002305
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002306 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002307 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002308 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002309 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002310 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002311 llvm::GlobalValue::InternalLinkage,
2312 Name,
2313 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002314 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002315
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002316 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002317}
2318
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002319llvm::GlobalVariable *
2320CGObjCCommonMac::CreateMetadataVar(const std::string &Name,
2321 llvm::Constant *Init,
2322 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002323 unsigned Align,
2324 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002325 const llvm::Type *Ty = Init->getType();
2326 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002327 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002328 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002329 if (Section)
2330 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002331 if (Align)
2332 GV->setAlignment(Align);
2333 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002334 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002335 return GV;
2336}
2337
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002338llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002339 // Abuse this interface function as a place to finalize.
2340 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002341 return NULL;
2342}
2343
Chris Lattner74391b42009-03-22 21:03:39 +00002344llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002345 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002346}
2347
Chris Lattner74391b42009-03-22 21:03:39 +00002348llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002349 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002350}
2351
Chris Lattner74391b42009-03-22 21:03:39 +00002352llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002353 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002354}
2355
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002356/*
2357
2358Objective-C setjmp-longjmp (sjlj) Exception Handling
2359--
2360
2361The basic framework for a @try-catch-finally is as follows:
2362{
2363 objc_exception_data d;
2364 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002365 bool _call_try_exit = true;
2366
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002367 objc_exception_try_enter(&d);
2368 if (!setjmp(d.jmp_buf)) {
2369 ... try body ...
2370 } else {
2371 // exception path
2372 id _caught = objc_exception_extract(&d);
2373
2374 // enter new try scope for handlers
2375 if (!setjmp(d.jmp_buf)) {
2376 ... match exception and execute catch blocks ...
2377
2378 // fell off end, rethrow.
2379 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00002380 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002381 } else {
2382 // exception in catch block
2383 _rethrow = objc_exception_extract(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002384 _call_try_exit = false;
2385 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002386 }
2387 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002388 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002389
2390finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002391 if (_call_try_exit)
2392 objc_exception_try_exit(&d);
2393
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002394 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002395 ... dispatch to finally destination ...
2396
2397finally_rethrow:
2398 objc_exception_throw(_rethrow);
2399
2400finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002401}
2402
2403This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00002404uses _rethrow to determine if objc_exception_try_exit should be called
2405and if the object should be rethrown. This breaks in the face of
2406throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002407
2408We specialize this framework for a few particular circumstances:
2409
2410 - If there are no catch blocks, then we avoid emitting the second
2411 exception handling context.
2412
2413 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2414 e)) we avoid emitting the code to rethrow an uncaught exception.
2415
2416 - FIXME: If there is no @finally block we can do a few more
2417 simplifications.
2418
2419Rethrows and Jumps-Through-Finally
2420--
2421
2422Support for implicit rethrows and jumping through the finally block is
2423handled by storing the current exception-handling context in
2424ObjCEHStack.
2425
Daniel Dunbar898d5082008-09-30 01:06:03 +00002426In order to implement proper @finally semantics, we support one basic
2427mechanism for jumping through the finally block to an arbitrary
2428destination. Constructs which generate exits from a @try or @catch
2429block use this mechanism to implement the proper semantics by chaining
2430jumps, as necessary.
2431
2432This mechanism works like the one used for indirect goto: we
2433arbitrarily assign an ID to each destination and store the ID for the
2434destination in a variable prior to entering the finally block. At the
2435end of the finally block we simply create a switch to the proper
2436destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002437
2438Code gen for @synchronized(expr) stmt;
2439Effectively generating code for:
2440objc_sync_enter(expr);
2441@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002442*/
2443
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002444void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2445 const Stmt &S) {
2446 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002447 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00002448 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002449 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar55e87422008-11-11 02:29:29 +00002450 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
2451 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
2452 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar1c566672009-02-24 01:43:46 +00002453
2454 // For @synchronized, call objc_sync_enter(sync.expr). The
2455 // evaluation of the expression must occur before we enter the
2456 // @synchronized. We can safely avoid a temp here because jumps into
2457 // @synchronized are illegal & this will dominate uses.
2458 llvm::Value *SyncArg = 0;
2459 if (!isTry) {
2460 SyncArg =
2461 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2462 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00002463 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002464 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002465
2466 // Push an EH context entry, used for handling rethrows and jumps
2467 // through finally.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002468 CGF.PushCleanupBlock(FinallyBlock);
2469
Anders Carlsson273558f2009-02-07 21:37:21 +00002470 CGF.ObjCEHValueStack.push_back(0);
2471
Daniel Dunbar898d5082008-09-30 01:06:03 +00002472 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00002473 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2474 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002475 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
2476 "_rethrow");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002477 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
2478 "_call_try_exit");
Owen Andersona1cf15f2009-07-14 23:10:40 +00002479 CGF.Builder.CreateStore(VMContext.getConstantIntTrue(), CallTryExitPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002480
Anders Carlsson80f25672008-09-09 17:59:25 +00002481 // Enter a new try block and call setjmp.
Chris Lattner34b02a12009-04-22 02:26:14 +00002482 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Anders Carlsson80f25672008-09-09 17:59:25 +00002483 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
2484 "jmpbufarray");
2485 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
Chris Lattner34b02a12009-04-22 02:26:14 +00002486 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlsson80f25672008-09-09 17:59:25 +00002487 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00002488
Daniel Dunbar55e87422008-11-11 02:29:29 +00002489 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2490 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002491 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002492 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002493
2494 // Emit the @try block.
2495 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002496 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
2497 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002498 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002499
2500 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002501 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002502
2503 // Retrieve the exception object. We may emit multiple blocks but
2504 // nothing can cross this so the value is already in SSA form.
Chris Lattner34b02a12009-04-22 02:26:14 +00002505 llvm::Value *Caught =
2506 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2507 ExceptionData, "caught");
Anders Carlsson273558f2009-02-07 21:37:21 +00002508 CGF.ObjCEHValueStack.back() = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002509 if (!isTry)
2510 {
2511 CGF.Builder.CreateStore(Caught, RethrowPtr);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002512 CGF.Builder.CreateStore(VMContext.getConstantIntFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002513 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002514 }
2515 else if (const ObjCAtCatchStmt* CatchStmt =
2516 cast<ObjCAtTryStmt>(S).getCatchStmts())
2517 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00002518 // Enter a new exception try block (in case a @catch block throws
2519 // an exception).
Chris Lattner34b02a12009-04-22 02:26:14 +00002520 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002521
Chris Lattner34b02a12009-04-22 02:26:14 +00002522 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlsson80f25672008-09-09 17:59:25 +00002523 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002524 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00002525
Daniel Dunbar55e87422008-11-11 02:29:29 +00002526 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
2527 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002528 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002529
2530 CGF.EmitBlock(CatchBlock);
2531
Daniel Dunbar55e40722008-09-27 07:03:52 +00002532 // Handle catch list. As a special case we check if everything is
2533 // matched and avoid generating code for falling off the end if
2534 // so.
2535 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00002536 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00002537 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00002538
Steve Naroff7ba138a2009-03-03 19:52:17 +00002539 const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00002540 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00002541
Anders Carlsson80f25672008-09-09 17:59:25 +00002542 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00002543 if (!CatchParam) {
2544 AllMatched = true;
2545 } else {
Steve Naroff14108da2009-07-10 23:34:53 +00002546 OPT = CatchParam->getType()->getAsObjCObjectPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002547
Daniel Dunbar97f61d12008-09-27 22:21:14 +00002548 // catch(id e) always matches.
2549 // FIXME: For the time being we also match id<X>; this should
2550 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00002551 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00002552 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00002553 }
2554
Daniel Dunbar55e40722008-09-27 07:03:52 +00002555 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00002556 if (CatchParam) {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002557 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002558 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002559 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002560 }
Anders Carlsson1452f552008-09-11 08:21:54 +00002561
Anders Carlssondde0a942008-09-11 09:15:33 +00002562 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002563 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002564 break;
2565 }
2566
Steve Naroff14108da2009-07-10 23:34:53 +00002567 assert(OPT && "Unexpected non-object pointer type in @catch");
2568 QualType T = OPT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00002569 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002570 assert(ObjCType && "Catch parameter must have Objective-C type!");
2571
2572 // Check if the @catch block matches the exception object.
2573 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
2574
Chris Lattner34b02a12009-04-22 02:26:14 +00002575 llvm::Value *Match =
2576 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
2577 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00002578
Daniel Dunbar55e87422008-11-11 02:29:29 +00002579 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00002580
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002581 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002582 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002583
2584 // Emit the @catch block.
2585 CGF.EmitBlock(MatchedBlock);
Steve Naroff7ba138a2009-03-03 19:52:17 +00002586 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002587 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002588
2589 llvm::Value *Tmp =
Steve Naroff7ba138a2009-03-03 19:52:17 +00002590 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002591 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002592 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002593
2594 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002595 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002596
2597 CGF.EmitBlock(NextCatchBlock);
2598 }
2599
Daniel Dunbar55e40722008-09-27 07:03:52 +00002600 if (!AllMatched) {
2601 // None of the handlers caught the exception, so store it to be
2602 // rethrown at the end of the @finally block.
2603 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002604 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002605 }
2606
2607 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002608 CGF.EmitBlock(CatchHandler);
Chris Lattner34b02a12009-04-22 02:26:14 +00002609 CGF.Builder.CreateStore(
2610 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2611 ExceptionData),
Daniel Dunbar55e40722008-09-27 07:03:52 +00002612 RethrowPtr);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002613 CGF.Builder.CreateStore(VMContext.getConstantIntFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002614 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002615 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00002616 CGF.Builder.CreateStore(Caught, RethrowPtr);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002617 CGF.Builder.CreateStore(VMContext.getConstantIntFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002618 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson80f25672008-09-09 17:59:25 +00002619 }
2620
Daniel Dunbar898d5082008-09-30 01:06:03 +00002621 // Pop the exception-handling stack entry. It is important to do
2622 // this now, because the code in the @finally block is not in this
2623 // context.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002624 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2625
Anders Carlsson273558f2009-02-07 21:37:21 +00002626 CGF.ObjCEHValueStack.pop_back();
2627
Anders Carlsson80f25672008-09-09 17:59:25 +00002628 // Emit the @finally block.
2629 CGF.EmitBlock(FinallyBlock);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002630 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2631
2632 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2633
2634 CGF.EmitBlock(FinallyExit);
Chris Lattner34b02a12009-04-22 02:26:14 +00002635 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002636
2637 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002638 if (isTry) {
2639 if (const ObjCAtFinallyStmt* FinallyStmt =
2640 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2641 CGF.EmitStmt(FinallyStmt->getFinallyBody());
Daniel Dunbar1c566672009-02-24 01:43:46 +00002642 } else {
2643 // Emit objc_sync_exit(expr); as finally's sole statement for
2644 // @synchronized.
Chris Lattnerbbccd612009-04-22 02:38:11 +00002645 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002646 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002647
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002648 // Emit the switch block
2649 if (Info.SwitchBlock)
2650 CGF.EmitBlock(Info.SwitchBlock);
2651 if (Info.EndBlock)
2652 CGF.EmitBlock(Info.EndBlock);
2653
Daniel Dunbar898d5082008-09-30 01:06:03 +00002654 CGF.EmitBlock(FinallyRethrow);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002655 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbar898d5082008-09-30 01:06:03 +00002656 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002657 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00002658
2659 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002660}
2661
2662void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002663 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002664 llvm::Value *ExceptionAsObject;
2665
2666 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2667 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2668 ExceptionAsObject =
2669 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2670 } else {
Anders Carlsson273558f2009-02-07 21:37:21 +00002671 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002672 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00002673 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002674 }
2675
Chris Lattnerbbccd612009-04-22 02:38:11 +00002676 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002677 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002678
2679 // Clear the insertion point to indicate we are in unreachable code.
2680 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002681}
2682
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002683/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002684/// object: objc_read_weak (id *src)
2685///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002686llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002687 llvm::Value *AddrWeakObj)
2688{
Eli Friedman8339b352009-03-07 03:57:15 +00002689 const llvm::Type* DestTy =
2690 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002691 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00002692 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002693 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00002694 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002695 return read_weak;
2696}
2697
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002698/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2699/// objc_assign_weak (id src, id *dst)
2700///
2701void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2702 llvm::Value *src, llvm::Value *dst)
2703{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002704 const llvm::Type * SrcTy = src->getType();
2705 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002706 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002707 assert(Size <= 8 && "does not support size > 8");
2708 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2709 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002710 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2711 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002712 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2713 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00002714 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002715 src, dst, "weakassign");
2716 return;
2717}
2718
Fariborz Jahanian58626502008-11-19 00:59:10 +00002719/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2720/// objc_assign_global (id src, id *dst)
2721///
2722void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2723 llvm::Value *src, llvm::Value *dst)
2724{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002725 const llvm::Type * SrcTy = src->getType();
2726 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002727 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002728 assert(Size <= 8 && "does not support size > 8");
2729 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2730 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002731 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2732 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002733 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2734 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002735 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00002736 src, dst, "globalassign");
2737 return;
2738}
2739
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002740/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2741/// objc_assign_ivar (id src, id *dst)
2742///
2743void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2744 llvm::Value *src, llvm::Value *dst)
2745{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002746 const llvm::Type * SrcTy = src->getType();
2747 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002748 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002749 assert(Size <= 8 && "does not support size > 8");
2750 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2751 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002752 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2753 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002754 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2755 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002756 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(),
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002757 src, dst, "assignivar");
2758 return;
2759}
2760
Fariborz Jahanian58626502008-11-19 00:59:10 +00002761/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2762/// objc_assign_strongCast (id src, id *dst)
2763///
2764void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2765 llvm::Value *src, llvm::Value *dst)
2766{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002767 const llvm::Type * SrcTy = src->getType();
2768 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002769 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002770 assert(Size <= 8 && "does not support size > 8");
2771 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2772 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002773 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2774 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002775 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2776 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002777 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00002778 src, dst, "weakassign");
2779 return;
2780}
2781
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002782void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
2783 llvm::Value *DestPtr,
2784 llvm::Value *SrcPtr,
2785 unsigned long size) {
2786 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
2787 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002788 llvm::Value *N = VMContext.getConstantInt(ObjCTypes.LongTy, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002789 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
2790 DestPtr, SrcPtr, N);
2791 return;
2792}
2793
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002794/// EmitObjCValueForIvar - Code Gen for ivar reference.
2795///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002796LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2797 QualType ObjectTy,
2798 llvm::Value *BaseValue,
2799 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002800 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00002801 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00002802 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
2803 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002804}
2805
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002806llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00002807 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002808 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00002809 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002810 return VMContext.getConstantInt(
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002811 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2812 Offset);
2813}
2814
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002815/* *** Private Interface *** */
2816
2817/// EmitImageInfo - Emit the image info marker used to encode some module
2818/// level information.
2819///
2820/// See: <rdr://4810609&4810587&4810587>
2821/// struct IMAGE_INFO {
2822/// unsigned version;
2823/// unsigned flags;
2824/// };
2825enum ImageInfoFlags {
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002826 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what
2827 // this implies.
2828 eImageInfo_GarbageCollected = (1 << 1),
2829 eImageInfo_GCOnly = (1 << 2),
2830 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
2831
2832 // A flag indicating that the module has no instances of an
2833 // @synthesize of a superclass variable. <rdar://problem/6803242>
2834 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002835};
2836
2837void CGObjCMac::EmitImageInfo() {
2838 unsigned version = 0; // Version is unused?
2839 unsigned flags = 0;
2840
2841 // FIXME: Fix and continue?
2842 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2843 flags |= eImageInfo_GarbageCollected;
2844 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2845 flags |= eImageInfo_GCOnly;
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002846
2847 // We never allow @synthesize of a superclass property.
2848 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002849
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002850 // Emitted as int[2];
2851 llvm::Constant *values[2] = {
Owen Andersona1cf15f2009-07-14 23:10:40 +00002852 VMContext.getConstantInt(llvm::Type::Int32Ty, version),
2853 VMContext.getConstantInt(llvm::Type::Int32Ty, flags)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002854 };
Owen Andersona1cf15f2009-07-14 23:10:40 +00002855 llvm::ArrayType *AT = VMContext.getArrayType(llvm::Type::Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002856
2857 const char *Section;
2858 if (ObjCABI == 1)
2859 Section = "__OBJC, __image_info,regular";
2860 else
2861 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002862 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002863 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Andersona1cf15f2009-07-14 23:10:40 +00002864 VMContext.getConstantArray(AT, values, 2),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002865 Section,
2866 0,
2867 true);
2868 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002869}
2870
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002871
2872// struct objc_module {
2873// unsigned long version;
2874// unsigned long size;
2875// const char *name;
2876// Symtab symtab;
2877// };
2878
2879// FIXME: Get from somewhere
2880static const int ModuleVersion = 7;
2881
2882void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00002883 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002884
2885 std::vector<llvm::Constant*> Values(4);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002886 Values[0] = VMContext.getConstantInt(ObjCTypes.LongTy, ModuleVersion);
2887 Values[1] = VMContext.getConstantInt(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002888 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002889 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002890 Values[3] = EmitModuleSymbols();
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002891 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Andersona1cf15f2009-07-14 23:10:40 +00002892 VMContext.getConstantStruct(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002893 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002894 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002895}
2896
2897llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002898 unsigned NumClasses = DefinedClasses.size();
2899 unsigned NumCategories = DefinedCategories.size();
2900
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002901 // Return null if no symbols were defined.
2902 if (!NumClasses && !NumCategories)
Owen Anderson69243822009-07-13 04:10:07 +00002903 return VMContext.getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002904
2905 std::vector<llvm::Constant*> Values(5);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002906 Values[0] = VMContext.getConstantInt(ObjCTypes.LongTy, 0);
Owen Anderson69243822009-07-13 04:10:07 +00002907 Values[1] = VMContext.getNullValue(ObjCTypes.SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002908 Values[2] = VMContext.getConstantInt(ObjCTypes.ShortTy, NumClasses);
2909 Values[3] = VMContext.getConstantInt(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002910
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002911 // The runtime expects exactly the list of defined classes followed
2912 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002913 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002914 for (unsigned i=0; i<NumClasses; i++)
Owen Andersona1cf15f2009-07-14 23:10:40 +00002915 Symbols[i] = VMContext.getConstantExprBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002916 ObjCTypes.Int8PtrTy);
2917 for (unsigned i=0; i<NumCategories; i++)
2918 Symbols[NumClasses + i] =
Owen Andersona1cf15f2009-07-14 23:10:40 +00002919 VMContext.getConstantExprBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002920 ObjCTypes.Int8PtrTy);
2921
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002922 Values[4] =
Owen Andersona1cf15f2009-07-14 23:10:40 +00002923 VMContext.getConstantArray(VMContext.getArrayType(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002924 NumClasses + NumCategories),
2925 Symbols);
2926
Owen Andersona1cf15f2009-07-14 23:10:40 +00002927 llvm::Constant *Init = VMContext.getConstantStruct(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002928
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002929 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002930 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
2931 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002932 4, true);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002933 return VMContext.getConstantExprBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002934}
2935
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002936llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002937 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002938 LazySymbols.insert(ID->getIdentifier());
2939
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002940 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2941
2942 if (!Entry) {
2943 llvm::Constant *Casted =
Owen Andersona1cf15f2009-07-14 23:10:40 +00002944 VMContext.getConstantExprBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002945 ObjCTypes.ClassPtrTy);
2946 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002947 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
2948 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002949 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002950 }
2951
2952 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002953}
2954
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002955llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002956 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2957
2958 if (!Entry) {
2959 llvm::Constant *Casted =
Owen Andersona1cf15f2009-07-14 23:10:40 +00002960 VMContext.getConstantExprBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002961 ObjCTypes.SelectorPtrTy);
2962 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002963 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
2964 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002965 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002966 }
2967
2968 return Builder.CreateLoad(Entry, false, "tmp");
2969}
2970
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002971llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002972 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002973
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002974 if (!Entry)
2975 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Owen Andersona1cf15f2009-07-14 23:10:40 +00002976 VMContext.getConstantArray(Ident->getName()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002977 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00002978 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002979
Owen Andersona1cf15f2009-07-14 23:10:40 +00002980 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002981}
2982
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00002983/// GetIvarLayoutName - Returns a unique constant for the given
2984/// ivar layout bitmap.
2985llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
2986 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson69243822009-07-13 04:10:07 +00002987 return VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00002988}
2989
Daniel Dunbard58edcb2009-05-03 14:10:34 +00002990static QualType::GCAttrTypes GetGCAttrTypeForType(ASTContext &Ctx,
2991 QualType FQT) {
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00002992 if (FQT.isObjCGCStrong())
2993 return QualType::Strong;
2994
2995 if (FQT.isObjCGCWeak())
2996 return QualType::Weak;
2997
Steve Narofff4954562009-07-16 15:41:00 +00002998 if (FQT->isObjCObjectPointerType())
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00002999 return QualType::Strong;
3000
Ted Kremenek35366a62009-07-17 17:50:17 +00003001 if (const PointerType *PT = FQT->getAsPointerType())
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003002 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003003
3004 return QualType::GCNone;
3005}
3006
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003007void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
3008 unsigned int BytePos,
3009 bool ForStrongLayout,
3010 bool &HasUnion) {
3011 const RecordDecl *RD = RT->getDecl();
3012 // FIXME - Use iterator.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003013 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003014 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
3015 const llvm::StructLayout *RecLayout =
3016 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
3017
3018 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3019 ForStrongLayout, HasUnion);
3020}
3021
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003022void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003023 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003024 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00003025 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003026 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003027 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003028 bool IsUnion = (RD && RD->isUnion());
3029 uint64_t MaxUnionIvarSize = 0;
3030 uint64_t MaxSkippedUnionIvarSize = 0;
3031 FieldDecl *MaxField = 0;
3032 FieldDecl *MaxSkippedField = 0;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003033 FieldDecl *LastFieldBitfield = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003034 uint64_t MaxFieldOffset = 0;
3035 uint64_t MaxSkippedFieldOffset = 0;
3036 uint64_t LastBitfieldOffset = 0;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003037
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003038 if (RecFields.empty())
3039 return;
Chris Lattnerf1690852009-03-31 08:48:01 +00003040 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3041 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3042
Chris Lattnerf1690852009-03-31 08:48:01 +00003043 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003044 FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003045 uint64_t FieldOffset;
3046 if (RD)
3047 FieldOffset =
3048 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
3049 else
3050 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003051
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003052 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003053 if (!Field->getIdentifier() || Field->isBitField()) {
3054 LastFieldBitfield = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003055 LastBitfieldOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003056 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003057 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003058
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003059 LastFieldBitfield = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003060 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003061 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003062 if (FQT->isUnionType())
3063 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003064
Ted Kremenek35366a62009-07-17 17:50:17 +00003065 BuildAggrIvarRecordLayout(FQT->getAsRecordType(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003066 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003067 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003068 continue;
3069 }
Chris Lattnerf1690852009-03-31 08:48:01 +00003070
3071 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003072 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003073 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003074 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003075 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003076 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003077 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3078 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003079 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003080 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003081 FQT = CArray->getElementType();
3082 }
3083
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003084 assert(!FQT->isUnionType() &&
3085 "layout for array of unions not supported");
3086 if (FQT->isRecordType()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003087 int OldIndex = IvarsInfo.size() - 1;
3088 int OldSkIndex = SkipIvars.size() -1;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003089
Ted Kremenek35366a62009-07-17 17:50:17 +00003090 const RecordType *RT = FQT->getAsRecordType();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003091 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003092 ForStrongLayout, HasUnion);
3093
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003094 // Replicate layout information for each array element. Note that
3095 // one element is already done.
3096 uint64_t ElIx = 1;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003097 for (int FirstIndex = IvarsInfo.size() - 1,
3098 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003099 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003100 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3101 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3102 IvarsInfo[i].ivar_size));
3103 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3104 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3105 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003106 }
3107 continue;
3108 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003109 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003110 // At this point, we are done with Record/Union and array there of.
3111 // For other arrays we are down to its element type.
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003112 QualType::GCAttrTypes GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003113
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003114 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003115 if ((ForStrongLayout && GCAttr == QualType::Strong)
3116 || (!ForStrongLayout && GCAttr == QualType::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003117 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003118 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003119 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003120 MaxUnionIvarSize = UnionIvarSize;
3121 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003122 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003123 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003124 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003125 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003126 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003127 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003128 } else if ((ForStrongLayout &&
3129 (GCAttr == QualType::GCNone || GCAttr == QualType::Weak))
3130 || (!ForStrongLayout && GCAttr != QualType::Weak)) {
3131 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003132 // FIXME: Why the asymmetry? We divide by word size in bits on other
3133 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003134 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003135 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003136 MaxSkippedUnionIvarSize = UnionIvarSize;
3137 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003138 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003139 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003140 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003141 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003142 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003143 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003144 }
3145 }
3146 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003147
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003148 if (LastFieldBitfield) {
3149 // Last field was a bitfield. Must update skip info.
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003150 Expr *BitWidth = LastFieldBitfield->getBitWidth();
3151 uint64_t BitFieldSize =
Eli Friedman9a901bb2009-04-26 19:19:15 +00003152 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Daniel Dunbar487993b2009-05-03 13:32:01 +00003153 GC_IVAR skivar;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003154 skivar.ivar_bytepos = BytePos + LastBitfieldOffset;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003155 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3156 + ((BitFieldSize % ByteSizeInBits) != 0);
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003157 SkipIvars.push_back(skivar);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003158 }
3159
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003160 if (MaxField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003161 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003162 MaxUnionIvarSize));
3163 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003164 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003165 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003166}
3167
3168/// BuildIvarLayout - Builds ivar layout bitmap for the class
3169/// implementation for the __strong or __weak case.
3170/// The layout map displays which words in ivar list must be skipped
3171/// and which must be scanned by GC (see below). String is built of bytes.
3172/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3173/// of words to skip and right nibble is count of words to scan. So, each
3174/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3175/// represented by a 0x00 byte which also ends the string.
3176/// 1. when ForStrongLayout is true, following ivars are scanned:
3177/// - id, Class
3178/// - object *
3179/// - __strong anything
3180///
3181/// 2. When ForStrongLayout is false, following ivars are scanned:
3182/// - __weak anything
3183///
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003184llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003185 const ObjCImplementationDecl *OMD,
3186 bool ForStrongLayout) {
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003187 bool hasUnion = false;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003188
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003189 unsigned int WordsToScan, WordsToSkip;
Owen Andersona1cf15f2009-07-14 23:10:40 +00003190 const llvm::Type *PtrTy = VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003191 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
Owen Anderson69243822009-07-13 04:10:07 +00003192 return VMContext.getNullValue(PtrTy);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003193
Chris Lattnerf1690852009-03-31 08:48:01 +00003194 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003195 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003196 CGM.getContext().CollectObjCIvars(OI, RecFields);
Fariborz Jahanian98200742009-05-12 18:14:29 +00003197
Daniel Dunbar37153282009-05-04 04:10:48 +00003198 // Add this implementations synthesized ivars.
Fariborz Jahanian98200742009-05-12 18:14:29 +00003199 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
3200 CGM.getContext().CollectSynthesizedIvars(OI, Ivars);
3201 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3202 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3203
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003204 if (RecFields.empty())
Owen Anderson69243822009-07-13 04:10:07 +00003205 return VMContext.getNullValue(PtrTy);
Chris Lattnerf1690852009-03-31 08:48:01 +00003206
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003207 SkipIvars.clear();
3208 IvarsInfo.clear();
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00003209
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003210 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003211 if (IvarsInfo.empty())
Owen Anderson69243822009-07-13 04:10:07 +00003212 return VMContext.getNullValue(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003213
3214 // Sort on byte position in case we encounterred a union nested in
3215 // the ivar list.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003216 if (hasUnion && !IvarsInfo.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003217 std::sort(IvarsInfo.begin(), IvarsInfo.end());
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003218 if (hasUnion && !SkipIvars.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003219 std::sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003220
3221 // Build the string of skip/scan nibbles
Fariborz Jahanian8c2f2d12009-04-24 17:15:27 +00003222 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003223 unsigned int WordSize =
Duncan Sands9408c452009-05-09 07:08:47 +00003224 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003225 if (IvarsInfo[0].ivar_bytepos == 0) {
3226 WordsToSkip = 0;
3227 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003228 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003229 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3230 WordsToScan = IvarsInfo[0].ivar_size;
3231 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003232 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003233 unsigned int TailPrevGCObjC =
3234 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003235 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003236 // consecutive 'scanned' object pointers.
3237 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003238 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003239 // Skip over 'gc'able object pointer which lay over each other.
3240 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3241 continue;
3242 // Must skip over 1 or more words. We save current skip/scan values
3243 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003244 SKIP_SCAN SkScan;
3245 SkScan.skip = WordsToSkip;
3246 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003247 SkipScanIvars.push_back(SkScan);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003248
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003249 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003250 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3251 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003252 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003253 WordsToSkip = 0;
3254 WordsToScan = IvarsInfo[i].ivar_size;
3255 }
3256 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003257 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003258 SKIP_SCAN SkScan;
3259 SkScan.skip = WordsToSkip;
3260 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003261 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003262 }
3263
3264 bool BytesSkipped = false;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003265 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003266 unsigned int LastIndex = SkipIvars.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003267 int LastByteSkipped =
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003268 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
3269 LastIndex = IvarsInfo.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003270 int LastByteScanned =
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003271 IvarsInfo[LastIndex].ivar_bytepos +
3272 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003273 BytesSkipped = (LastByteSkipped > LastByteScanned);
3274 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003275 if (BytesSkipped) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003276 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003277 SKIP_SCAN SkScan;
3278 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3279 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003280 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003281 }
3282 }
3283 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3284 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003285 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003286 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003287 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3288 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3289 // 0xM0 followed by 0x0N detected.
3290 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3291 for (int j = i+1; j < SkipScan; j++)
3292 SkipScanIvars[j] = SkipScanIvars[j+1];
3293 --SkipScan;
3294 }
3295 }
3296
3297 // Generate the string.
3298 std::string BitMap;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003299 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003300 unsigned char byte;
3301 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3302 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3303 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3304 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
3305
3306 if (skip_small > 0 || skip_big > 0)
3307 BytesSkipped = true;
3308 // first skip big.
3309 for (unsigned int ix = 0; ix < skip_big; ix++)
3310 BitMap += (unsigned char)(0xf0);
3311
3312 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003313 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003314 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003315 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003316 byte |= 0xf;
3317 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003318 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003319 byte |= scan_small;
3320 scan_small = 0;
3321 }
3322 BitMap += byte;
3323 }
3324 // next scan big
3325 for (unsigned int ix = 0; ix < scan_big; ix++)
3326 BitMap += (unsigned char)(0x0f);
3327 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003328 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003329 byte = scan_small;
3330 BitMap += byte;
3331 }
3332 }
3333 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003334 unsigned char zero = 0;
3335 BitMap += zero;
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003336
3337 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
3338 printf("\n%s ivar layout for class '%s': ",
3339 ForStrongLayout ? "strong" : "weak",
3340 OMD->getClassInterface()->getNameAsCString());
3341 const unsigned char *s = (unsigned char*)BitMap.c_str();
3342 for (unsigned i = 0; i < BitMap.size(); i++)
3343 if (!(s[i] & 0xf0))
3344 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3345 else
3346 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3347 printf("\n");
3348 }
3349
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003350 // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as
3351 // final layout.
3352 if (ForStrongLayout && !BytesSkipped)
Owen Anderson69243822009-07-13 04:10:07 +00003353 return VMContext.getNullValue(PtrTy);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003354 llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Owen Andersona1cf15f2009-07-14 23:10:40 +00003355 VMContext.getConstantArray(BitMap.c_str()),
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003356 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003357 1, true);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003358 return getConstantGEP(VMContext, Entry, 0, 0);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003359}
3360
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003361llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003362 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3363
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003364 // FIXME: Avoid std::string copying.
3365 if (!Entry)
3366 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Andersona1cf15f2009-07-14 23:10:40 +00003367 VMContext.getConstantArray(Sel.getAsString()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003368 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003369 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003370
Owen Andersona1cf15f2009-07-14 23:10:40 +00003371 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003372}
3373
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003374// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003375llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003376 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3377}
3378
3379// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003380llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003381 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
3382}
3383
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003384llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003385 std::string TypeStr;
3386 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3387
3388 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003389
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003390 if (!Entry)
3391 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Andersona1cf15f2009-07-14 23:10:40 +00003392 VMContext.getConstantArray(TypeStr),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003393 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003394 1, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003395
Owen Andersona1cf15f2009-07-14 23:10:40 +00003396 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003397}
3398
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003399llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003400 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003401 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3402 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00003403
3404 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3405
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003406 if (!Entry)
3407 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Andersona1cf15f2009-07-14 23:10:40 +00003408 VMContext.getConstantArray(TypeStr),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003409 "__TEXT,__cstring,cstring_literals",
3410 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00003411
Owen Andersona1cf15f2009-07-14 23:10:40 +00003412 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003413}
3414
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003415// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003416llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003417 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
3418
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003419 if (!Entry)
3420 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Owen Andersona1cf15f2009-07-14 23:10:40 +00003421 VMContext.getConstantArray(Ident->getName()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003422 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003423 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003424
Owen Andersona1cf15f2009-07-14 23:10:40 +00003425 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003426}
3427
3428// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003429// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003430llvm::Constant *
3431 CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3432 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003433 std::string TypeStr;
3434 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003435 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3436}
3437
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003438void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
3439 const ObjCContainerDecl *CD,
3440 std::string &NameOut) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00003441 NameOut = '\01';
3442 NameOut += (D->isInstanceMethod() ? '-' : '+');
Chris Lattner077bf5e2008-11-24 03:33:13 +00003443 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003444 assert (CD && "Missing container decl in GetNameForMethod");
3445 NameOut += CD->getNameAsString();
Fariborz Jahanian1e9aef32009-04-16 18:34:20 +00003446 if (const ObjCCategoryImplDecl *CID =
3447 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) {
3448 NameOut += '(';
3449 NameOut += CID->getNameAsString();
3450 NameOut+= ')';
3451 }
Chris Lattner077bf5e2008-11-24 03:33:13 +00003452 NameOut += ' ';
3453 NameOut += D->getSelector().getAsString();
3454 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003455}
3456
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003457void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003458 EmitModuleInfo();
3459
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003460 // Emit the dummy bodies for any protocols which were referenced but
3461 // never defined.
3462 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00003463 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
3464 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003465 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003466
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003467 std::vector<llvm::Constant*> Values(5);
Owen Anderson69243822009-07-13 04:10:07 +00003468 Values[0] = VMContext.getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00003469 Values[1] = GetClassName(I->first);
Owen Anderson69243822009-07-13 04:10:07 +00003470 Values[2] = VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003471 Values[3] = Values[4] =
Owen Anderson69243822009-07-13 04:10:07 +00003472 VMContext.getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00003473 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
3474 I->second->setInitializer(VMContext.getConstantStruct(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003475 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00003476 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003477 }
3478
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003479 // Add assembler directives to add lazy undefined symbol references
3480 // for classes which are referenced but not defined. This is
3481 // important for correct linker interaction.
3482
3483 // FIXME: Uh, this isn't particularly portable.
3484 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00003485
3486 if (!CGM.getModule().getModuleInlineAsm().empty())
3487 s << "\n";
3488
Chris Lattnerad64e022009-07-17 23:57:13 +00003489 for (std::set<IdentifierInfo*>::iterator I = LazySymbols.begin(),
3490 e = LazySymbols.end(); I != e; ++I) {
3491 s << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003492 }
Chris Lattnerad64e022009-07-17 23:57:13 +00003493 for (std::set<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
3494 e = DefinedSymbols.end(); I != e; ++I) {
3495 s << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
3496 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003497 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00003498
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003499 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003500}
3501
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003502CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003503 : CGObjCCommonMac(cgm),
3504 ObjCTypes(cgm)
3505{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003506 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003507 ObjCABI = 2;
3508}
3509
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003510/* *** */
3511
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003512ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Owen Andersona1cf15f2009-07-14 23:10:40 +00003513: VMContext(cgm.getLLVMContext()), CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003514{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003515 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3516 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003517
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003518 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003519 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003520 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003521 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003522 Int8PtrTy = VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003523
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003524 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Andersona1cf15f2009-07-14 23:10:40 +00003525 PtrObjectPtrTy = VMContext.getPointerTypeUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003526 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003527
Mike Stumpf5408fe2009-05-16 07:57:57 +00003528 // FIXME: It would be nice to unify this with the opaque type, so that the IR
3529 // comes out a bit cleaner.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003530 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Andersona1cf15f2009-07-14 23:10:40 +00003531 ExternalProtocolPtrTy = VMContext.getPointerTypeUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003532
3533 // I'm not sure I like this. The implicit coordination is a bit
3534 // gross. We should solve this in a reasonable fashion because this
3535 // is a pretty common task (match some runtime data structure with
3536 // an LLVM data structure).
3537
3538 // FIXME: This is leaked.
3539 // FIXME: Merge with rewriter code?
3540
3541 // struct _objc_super {
3542 // id self;
3543 // Class cls;
3544 // }
3545 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3546 SourceLocation(),
3547 &Ctx.Idents.get("_objc_super"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003548 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Douglas Gregor6ab35242009-04-09 21:40:53 +00003549 Ctx.getObjCIdType(), 0, false));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003550 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Douglas Gregor6ab35242009-04-09 21:40:53 +00003551 Ctx.getObjCClassType(), 0, false));
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003552 RD->completeDefinition(Ctx);
3553
3554 SuperCTy = Ctx.getTagDeclType(RD);
3555 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
3556
3557 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +00003558 SuperPtrTy = VMContext.getPointerTypeUnqual(SuperTy);
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003559
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003560 // struct _prop_t {
3561 // char *name;
3562 // char *attributes;
3563 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003564 PropertyTy = VMContext.getStructType(Int8PtrTy, Int8PtrTy, NULL);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003565 CGM.getModule().addTypeName("struct._prop_t",
3566 PropertyTy);
3567
3568 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003569 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003570 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003571 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003572 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003573 PropertyListTy = VMContext.getStructType(IntTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003574 IntTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00003575 VMContext.getArrayType(PropertyTy, 0),
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003576 NULL);
3577 CGM.getModule().addTypeName("struct._prop_list_t",
3578 PropertyListTy);
3579 // struct _prop_list_t *
Owen Andersona1cf15f2009-07-14 23:10:40 +00003580 PropertyListPtrTy = VMContext.getPointerTypeUnqual(PropertyListTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003581
3582 // struct _objc_method {
3583 // SEL _cmd;
3584 // char *method_type;
3585 // char *_imp;
3586 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003587 MethodTy = VMContext.getStructType(SelectorPtrTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003588 Int8PtrTy,
3589 Int8PtrTy,
3590 NULL);
3591 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003592
3593 // struct _objc_cache *
Owen Andersona1cf15f2009-07-14 23:10:40 +00003594 CacheTy = VMContext.getOpaqueType();
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003595 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003596 CachePtrTy = VMContext.getPointerTypeUnqual(CacheTy);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003597}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003598
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003599ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
3600 : ObjCCommonTypesHelper(cgm)
3601{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003602 // struct _objc_method_description {
3603 // SEL name;
3604 // char *types;
3605 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003606 MethodDescriptionTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +00003607 VMContext.getStructType(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003608 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003609 NULL);
3610 CGM.getModule().addTypeName("struct._objc_method_description",
3611 MethodDescriptionTy);
3612
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003613 // struct _objc_method_description_list {
3614 // int count;
3615 // struct _objc_method_description[1];
3616 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003617 MethodDescriptionListTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +00003618 VMContext.getStructType(IntTy,
3619 VMContext.getArrayType(MethodDescriptionTy, 0),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003620 NULL);
3621 CGM.getModule().addTypeName("struct._objc_method_description_list",
3622 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003623
3624 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003625 MethodDescriptionListPtrTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +00003626 VMContext.getPointerTypeUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003627
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003628 // Protocol description structures
3629
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003630 // struct _objc_protocol_extension {
3631 // uint32_t size; // sizeof(struct _objc_protocol_extension)
3632 // struct _objc_method_description_list *optional_instance_methods;
3633 // struct _objc_method_description_list *optional_class_methods;
3634 // struct _objc_property_list *instance_properties;
3635 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003636 ProtocolExtensionTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +00003637 VMContext.getStructType(IntTy,
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003638 MethodDescriptionListPtrTy,
3639 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003640 PropertyListPtrTy,
3641 NULL);
3642 CGM.getModule().addTypeName("struct._objc_protocol_extension",
3643 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003644
3645 // struct _objc_protocol_extension *
Owen Andersona1cf15f2009-07-14 23:10:40 +00003646 ProtocolExtensionPtrTy = VMContext.getPointerTypeUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003647
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003648 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003649
Owen Andersona1cf15f2009-07-14 23:10:40 +00003650 llvm::PATypeHolder ProtocolTyHolder = VMContext.getOpaqueType();
3651 llvm::PATypeHolder ProtocolListTyHolder = VMContext.getOpaqueType();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003652
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003653 const llvm::Type *T =
Owen Andersona1cf15f2009-07-14 23:10:40 +00003654 VMContext.getStructType(VMContext.getPointerTypeUnqual(ProtocolListTyHolder),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003655 LongTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00003656 VMContext.getArrayType(ProtocolTyHolder, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003657 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003658 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
3659
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003660 // struct _objc_protocol {
3661 // struct _objc_protocol_extension *isa;
3662 // char *protocol_name;
3663 // struct _objc_protocol **_objc_protocol_list;
3664 // struct _objc_method_description_list *instance_methods;
3665 // struct _objc_method_description_list *class_methods;
3666 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003667 T = VMContext.getStructType(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003668 Int8PtrTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00003669 VMContext.getPointerTypeUnqual(ProtocolListTyHolder),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003670 MethodDescriptionListPtrTy,
3671 MethodDescriptionListPtrTy,
3672 NULL);
3673 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
3674
3675 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
3676 CGM.getModule().addTypeName("struct._objc_protocol_list",
3677 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003678 // struct _objc_protocol_list *
Owen Andersona1cf15f2009-07-14 23:10:40 +00003679 ProtocolListPtrTy = VMContext.getPointerTypeUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003680
3681 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003682 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003683 ProtocolPtrTy = VMContext.getPointerTypeUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003684
3685 // Class description structures
3686
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003687 // struct _objc_ivar {
3688 // char *ivar_name;
3689 // char *ivar_type;
3690 // int ivar_offset;
3691 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003692 IvarTy = VMContext.getStructType(Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003693 Int8PtrTy,
3694 IntTy,
3695 NULL);
3696 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
3697
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003698 // struct _objc_ivar_list *
Owen Andersona1cf15f2009-07-14 23:10:40 +00003699 IvarListTy = VMContext.getOpaqueType();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003700 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003701 IvarListPtrTy = VMContext.getPointerTypeUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003702
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003703 // struct _objc_method_list *
Owen Andersona1cf15f2009-07-14 23:10:40 +00003704 MethodListTy = VMContext.getOpaqueType();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003705 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003706 MethodListPtrTy = VMContext.getPointerTypeUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003707
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003708 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003709 ClassExtensionTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +00003710 VMContext.getStructType(IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003711 Int8PtrTy,
3712 PropertyListPtrTy,
3713 NULL);
3714 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003715 ClassExtensionPtrTy = VMContext.getPointerTypeUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003716
Owen Andersona1cf15f2009-07-14 23:10:40 +00003717 llvm::PATypeHolder ClassTyHolder = VMContext.getOpaqueType();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003718
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003719 // struct _objc_class {
3720 // Class isa;
3721 // Class super_class;
3722 // char *name;
3723 // long version;
3724 // long info;
3725 // long instance_size;
3726 // struct _objc_ivar_list *ivars;
3727 // struct _objc_method_list *methods;
3728 // struct _objc_cache *cache;
3729 // struct _objc_protocol_list *protocols;
3730 // char *ivar_layout;
3731 // struct _objc_class_ext *ext;
3732 // };
Owen Andersona1cf15f2009-07-14 23:10:40 +00003733 T = VMContext.getStructType(VMContext.getPointerTypeUnqual(ClassTyHolder),
3734 VMContext.getPointerTypeUnqual(ClassTyHolder),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003735 Int8PtrTy,
3736 LongTy,
3737 LongTy,
3738 LongTy,
3739 IvarListPtrTy,
3740 MethodListPtrTy,
3741 CachePtrTy,
3742 ProtocolListPtrTy,
3743 Int8PtrTy,
3744 ClassExtensionPtrTy,
3745 NULL);
3746 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
3747
3748 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
3749 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003750 ClassPtrTy = VMContext.getPointerTypeUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003751
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003752 // struct _objc_category {
3753 // char *category_name;
3754 // char *class_name;
3755 // struct _objc_method_list *instance_method;
3756 // struct _objc_method_list *class_method;
3757 // uint32_t size; // sizeof(struct _objc_category)
3758 // struct _objc_property_list *instance_properties;// category's @property
3759 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003760 CategoryTy = VMContext.getStructType(Int8PtrTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003761 Int8PtrTy,
3762 MethodListPtrTy,
3763 MethodListPtrTy,
3764 ProtocolListPtrTy,
3765 IntTy,
3766 PropertyListPtrTy,
3767 NULL);
3768 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
3769
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003770 // Global metadata structures
3771
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003772 // struct _objc_symtab {
3773 // long sel_ref_cnt;
3774 // SEL *refs;
3775 // short cls_def_cnt;
3776 // short cat_def_cnt;
3777 // char *defs[cls_def_cnt + cat_def_cnt];
3778 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003779 SymtabTy = VMContext.getStructType(LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003780 SelectorPtrTy,
3781 ShortTy,
3782 ShortTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00003783 VMContext.getArrayType(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003784 NULL);
3785 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003786 SymtabPtrTy = VMContext.getPointerTypeUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003787
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003788 // struct _objc_module {
3789 // long version;
3790 // long size; // sizeof(struct _objc_module)
3791 // char *name;
3792 // struct _objc_symtab* symtab;
3793 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003794 ModuleTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +00003795 VMContext.getStructType(LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003796 LongTy,
3797 Int8PtrTy,
3798 SymtabPtrTy,
3799 NULL);
3800 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003801
Anders Carlsson2abd89c2008-08-31 04:05:03 +00003802
Mike Stumpf5408fe2009-05-16 07:57:57 +00003803 // FIXME: This is the size of the setjmp buffer and should be target
3804 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00003805 uint64_t SetJmpBufferSize = 18;
3806
3807 // Exceptions
Owen Andersona1cf15f2009-07-14 23:10:40 +00003808 const llvm::Type *StackPtrTy = VMContext.getArrayType(
3809 VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00003810
3811 ExceptionDataTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +00003812 VMContext.getStructType(VMContext.getArrayType(llvm::Type::Int32Ty,
Anders Carlsson124526b2008-09-09 10:10:21 +00003813 SetJmpBufferSize),
3814 StackPtrTy, NULL);
3815 CGM.getModule().addTypeName("struct._objc_exception_data",
3816 ExceptionDataTy);
3817
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003818}
3819
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003820ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003821: ObjCCommonTypesHelper(cgm)
3822{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003823 // struct _method_list_t {
3824 // uint32_t entsize; // sizeof(struct _objc_method)
3825 // uint32_t method_count;
3826 // struct _objc_method method_list[method_count];
3827 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003828 MethodListnfABITy = VMContext.getStructType(IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003829 IntTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00003830 VMContext.getArrayType(MethodTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003831 NULL);
3832 CGM.getModule().addTypeName("struct.__method_list_t",
3833 MethodListnfABITy);
3834 // struct method_list_t *
Owen Andersona1cf15f2009-07-14 23:10:40 +00003835 MethodListnfABIPtrTy = VMContext.getPointerTypeUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003836
3837 // struct _protocol_t {
3838 // id isa; // NULL
3839 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003840 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003841 // const struct method_list_t * const instance_methods;
3842 // const struct method_list_t * const class_methods;
3843 // const struct method_list_t *optionalInstanceMethods;
3844 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003845 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003846 // const uint32_t size; // sizeof(struct _protocol_t)
3847 // const uint32_t flags; // = 0
3848 // }
3849
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003850 // Holder for struct _protocol_list_t *
Owen Andersona1cf15f2009-07-14 23:10:40 +00003851 llvm::PATypeHolder ProtocolListTyHolder = VMContext.getOpaqueType();
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003852
Owen Andersona1cf15f2009-07-14 23:10:40 +00003853 ProtocolnfABITy = VMContext.getStructType(ObjectPtrTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003854 Int8PtrTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00003855 VMContext.getPointerTypeUnqual(
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003856 ProtocolListTyHolder),
3857 MethodListnfABIPtrTy,
3858 MethodListnfABIPtrTy,
3859 MethodListnfABIPtrTy,
3860 MethodListnfABIPtrTy,
3861 PropertyListPtrTy,
3862 IntTy,
3863 IntTy,
3864 NULL);
3865 CGM.getModule().addTypeName("struct._protocol_t",
3866 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003867
3868 // struct _protocol_t*
Owen Andersona1cf15f2009-07-14 23:10:40 +00003869 ProtocolnfABIPtrTy = VMContext.getPointerTypeUnqual(ProtocolnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003870
Fariborz Jahanianda320092009-01-29 19:24:30 +00003871 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003872 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00003873 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003874 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003875 ProtocolListnfABITy = VMContext.getStructType(LongTy,
3876 VMContext.getArrayType(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003877 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003878 NULL);
3879 CGM.getModule().addTypeName("struct._objc_protocol_list",
3880 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003881 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3882 ProtocolListnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003883
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003884 // struct _objc_protocol_list*
Owen Andersona1cf15f2009-07-14 23:10:40 +00003885 ProtocolListnfABIPtrTy = VMContext.getPointerTypeUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003886
3887 // struct _ivar_t {
3888 // unsigned long int *offset; // pointer to ivar offset location
3889 // char *name;
3890 // char *type;
3891 // uint32_t alignment;
3892 // uint32_t size;
3893 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003894 IvarnfABITy = VMContext.getStructType(VMContext.getPointerTypeUnqual(LongTy),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003895 Int8PtrTy,
3896 Int8PtrTy,
3897 IntTy,
3898 IntTy,
3899 NULL);
3900 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3901
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003902 // struct _ivar_list_t {
3903 // uint32 entsize; // sizeof(struct _ivar_t)
3904 // uint32 count;
3905 // struct _iver_t list[count];
3906 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003907 IvarListnfABITy = VMContext.getStructType(IntTy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003908 IntTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00003909 VMContext.getArrayType(
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003910 IvarnfABITy, 0),
3911 NULL);
3912 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3913
Owen Andersona1cf15f2009-07-14 23:10:40 +00003914 IvarListnfABIPtrTy = VMContext.getPointerTypeUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003915
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003916 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003917 // uint32_t const flags;
3918 // uint32_t const instanceStart;
3919 // uint32_t const instanceSize;
3920 // uint32_t const reserved; // only when building for 64bit targets
3921 // const uint8_t * const ivarLayout;
3922 // const char *const name;
3923 // const struct _method_list_t * const baseMethods;
3924 // const struct _objc_protocol_list *const baseProtocols;
3925 // const struct _ivar_list_t *const ivars;
3926 // const uint8_t * const weakIvarLayout;
3927 // const struct _prop_list_t * const properties;
3928 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003929
3930 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Andersona1cf15f2009-07-14 23:10:40 +00003931 ClassRonfABITy = VMContext.getStructType(IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003932 IntTy,
3933 IntTy,
3934 Int8PtrTy,
3935 Int8PtrTy,
3936 MethodListnfABIPtrTy,
3937 ProtocolListnfABIPtrTy,
3938 IvarListnfABIPtrTy,
3939 Int8PtrTy,
3940 PropertyListPtrTy,
3941 NULL);
3942 CGM.getModule().addTypeName("struct._class_ro_t",
3943 ClassRonfABITy);
3944
3945 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3946 std::vector<const llvm::Type*> Params;
3947 Params.push_back(ObjectPtrTy);
3948 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003949 ImpnfABITy = VMContext.getPointerTypeUnqual(
3950 VMContext.getFunctionType(ObjectPtrTy, Params, false));
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003951
3952 // struct _class_t {
3953 // struct _class_t *isa;
3954 // struct _class_t * const superclass;
3955 // void *cache;
3956 // IMP *vtable;
3957 // struct class_ro_t *ro;
3958 // }
3959
Owen Andersona1cf15f2009-07-14 23:10:40 +00003960 llvm::PATypeHolder ClassTyHolder = VMContext.getOpaqueType();
3961 ClassnfABITy =
3962 VMContext.getStructType(VMContext.getPointerTypeUnqual(ClassTyHolder),
3963 VMContext.getPointerTypeUnqual(ClassTyHolder),
3964 CachePtrTy,
3965 VMContext.getPointerTypeUnqual(ImpnfABITy),
3966 VMContext.getPointerTypeUnqual(ClassRonfABITy),
3967 NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003968 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3969
3970 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3971 ClassnfABITy);
3972
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003973 // LLVM for struct _class_t *
Owen Andersona1cf15f2009-07-14 23:10:40 +00003974 ClassnfABIPtrTy = VMContext.getPointerTypeUnqual(ClassnfABITy);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003975
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003976 // struct _category_t {
3977 // const char * const name;
3978 // struct _class_t *const cls;
3979 // const struct _method_list_t * const instance_methods;
3980 // const struct _method_list_t * const class_methods;
3981 // const struct _protocol_list_t * const protocols;
3982 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003983 // }
Owen Andersona1cf15f2009-07-14 23:10:40 +00003984 CategorynfABITy = VMContext.getStructType(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003985 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003986 MethodListnfABIPtrTy,
3987 MethodListnfABIPtrTy,
3988 ProtocolListnfABIPtrTy,
3989 PropertyListPtrTy,
3990 NULL);
3991 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003992
3993 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003994 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3995 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003996
3997 // MessageRefTy - LLVM for:
3998 // struct _message_ref_t {
3999 // IMP messenger;
4000 // SEL name;
4001 // };
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004002
4003 // First the clang type for struct _message_ref_t
4004 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
4005 SourceLocation(),
4006 &Ctx.Idents.get("_message_ref_t"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004007 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Douglas Gregor6ab35242009-04-09 21:40:53 +00004008 Ctx.VoidPtrTy, 0, false));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004009 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Douglas Gregor6ab35242009-04-09 21:40:53 +00004010 Ctx.getObjCSelType(), 0, false));
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004011 RD->completeDefinition(Ctx);
4012
4013 MessageRefCTy = Ctx.getTagDeclType(RD);
4014 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4015 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004016
4017 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Andersona1cf15f2009-07-14 23:10:40 +00004018 MessageRefPtrTy = VMContext.getPointerTypeUnqual(MessageRefTy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004019
4020 // SuperMessageRefTy - LLVM for:
4021 // struct _super_message_ref_t {
4022 // SUPER_IMP messenger;
4023 // SEL name;
4024 // };
Owen Andersona1cf15f2009-07-14 23:10:40 +00004025 SuperMessageRefTy = VMContext.getStructType(ImpnfABITy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004026 SelectorPtrTy,
4027 NULL);
4028 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
4029
4030 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Owen Andersona1cf15f2009-07-14 23:10:40 +00004031 SuperMessageRefPtrTy = VMContext.getPointerTypeUnqual(SuperMessageRefTy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004032
Daniel Dunbare588b992009-03-01 04:46:24 +00004033
4034 // struct objc_typeinfo {
4035 // const void** vtable; // objc_ehtype_vtable + 2
4036 // const char* name; // c++ typeinfo string
4037 // Class cls;
4038 // };
Owen Andersona1cf15f2009-07-14 23:10:40 +00004039 EHTypeTy = VMContext.getStructType(VMContext.getPointerTypeUnqual(Int8PtrTy),
Daniel Dunbare588b992009-03-01 04:46:24 +00004040 Int8PtrTy,
4041 ClassnfABIPtrTy,
4042 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004043 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004044 EHTypePtrTy = VMContext.getPointerTypeUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004045}
4046
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004047llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
4048 FinishNonFragileABIModule();
4049
4050 return NULL;
4051}
4052
Daniel Dunbar463b8762009-05-15 21:48:48 +00004053void CGObjCNonFragileABIMac::AddModuleClassList(const
4054 std::vector<llvm::GlobalValue*>
4055 &Container,
4056 const char *SymbolName,
4057 const char *SectionName) {
4058 unsigned NumClasses = Container.size();
4059
4060 if (!NumClasses)
4061 return;
4062
4063 std::vector<llvm::Constant*> Symbols(NumClasses);
4064 for (unsigned i=0; i<NumClasses; i++)
Owen Andersona1cf15f2009-07-14 23:10:40 +00004065 Symbols[i] = VMContext.getConstantExprBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004066 ObjCTypes.Int8PtrTy);
4067 llvm::Constant* Init =
Owen Andersona1cf15f2009-07-14 23:10:40 +00004068 VMContext.getConstantArray(VMContext.getArrayType(ObjCTypes.Int8PtrTy,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004069 NumClasses),
4070 Symbols);
4071
4072 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004073 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004074 llvm::GlobalValue::InternalLinkage,
4075 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004076 SymbolName);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004077 GV->setAlignment(8);
4078 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004079 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004080}
4081
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004082void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4083 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004084
Daniel Dunbar463b8762009-05-15 21:48:48 +00004085 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004086 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar463b8762009-05-15 21:48:48 +00004087 AddModuleClassList(DefinedClasses,
4088 "\01L_OBJC_LABEL_CLASS_$",
4089 "__DATA, __objc_classlist, regular, no_dead_strip");
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004090 AddModuleClassList(DefinedNonLazyClasses,
4091 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4092 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004093
4094 // Build list of all implemented category addresses in array
4095 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar463b8762009-05-15 21:48:48 +00004096 AddModuleClassList(DefinedCategories,
4097 "\01L_OBJC_LABEL_CATEGORY_$",
4098 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004099 AddModuleClassList(DefinedNonLazyCategories,
4100 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4101 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004102
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004103 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
4104 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
4105 std::vector<llvm::Constant*> Values(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004106 Values[0] = VMContext.getConstantInt(ObjCTypes.IntTy, 0);
Fariborz Jahanian067986e2009-02-24 21:08:09 +00004107 unsigned int flags = 0;
Fariborz Jahanian66a5c2c2009-02-24 23:34:44 +00004108 // FIXME: Fix and continue?
4109 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
4110 flags |= eImageInfo_GarbageCollected;
4111 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
4112 flags |= eImageInfo_GCOnly;
Owen Andersona1cf15f2009-07-14 23:10:40 +00004113 Values[1] = VMContext.getConstantInt(ObjCTypes.IntTy, flags);
4114 llvm::Constant* Init = VMContext.getConstantArray(
4115 VMContext.getArrayType(ObjCTypes.IntTy, 2),
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004116 Values);
4117 llvm::GlobalVariable *IMGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004118 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004119 llvm::GlobalValue::InternalLinkage,
4120 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004121 "\01L_OBJC_IMAGE_INFO");
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004122 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
Daniel Dunbar325f7582009-04-23 08:03:21 +00004123 IMGV->setConstant(true);
Chris Lattnerad64e022009-07-17 23:57:13 +00004124 CGM.AddUsedGlobal(IMGV);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004125}
4126
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004127/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004128/// NonLegacyDispatchMethods; false otherwise. What this means is that
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004129/// except for the 19 selectors in the list, we generate 32bit-style
4130/// message dispatch call for all the rest.
4131///
4132bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004133 if (NonLegacyDispatchMethods.empty()) {
4134 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4135 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4136 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4137 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4138 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4139 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4140 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4141 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4142 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4143 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
4144
4145 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4146 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4147 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4148 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4149 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4150 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4151 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4152 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004153 // "countByEnumeratingWithState:objects:count"
4154 IdentifierInfo *KeyIdents[] = {
4155 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4156 &CGM.getContext().Idents.get("objects"),
4157 &CGM.getContext().Idents.get("count")
4158 };
4159 NonLegacyDispatchMethods.insert(
4160 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004161 }
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004162 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004163}
4164
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004165// Metadata flags
4166enum MetaDataDlags {
4167 CLS = 0x0,
4168 CLS_META = 0x1,
4169 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004170 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004171 CLS_EXCEPTION = 0x20
4172};
4173/// BuildClassRoTInitializer - generate meta-data for:
4174/// struct _class_ro_t {
4175/// uint32_t const flags;
4176/// uint32_t const instanceStart;
4177/// uint32_t const instanceSize;
4178/// uint32_t const reserved; // only when building for 64bit targets
4179/// const uint8_t * const ivarLayout;
4180/// const char *const name;
4181/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004182/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004183/// const struct _ivar_list_t *const ivars;
4184/// const uint8_t * const weakIvarLayout;
4185/// const struct _prop_list_t * const properties;
4186/// }
4187///
4188llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
4189 unsigned flags,
4190 unsigned InstanceStart,
4191 unsigned InstanceSize,
4192 const ObjCImplementationDecl *ID) {
4193 std::string ClassName = ID->getNameAsString();
4194 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Andersona1cf15f2009-07-14 23:10:40 +00004195 Values[ 0] = VMContext.getConstantInt(ObjCTypes.IntTy, flags);
4196 Values[ 1] = VMContext.getConstantInt(ObjCTypes.IntTy, InstanceStart);
4197 Values[ 2] = VMContext.getConstantInt(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004198 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004199 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4200 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004201 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004202 // const struct _method_list_t * const baseMethods;
4203 std::vector<llvm::Constant*> Methods;
4204 std::string MethodListName("\01l_OBJC_$_");
4205 if (flags & CLS_META) {
4206 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004207 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004208 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004209 // Class methods should always be defined.
4210 Methods.push_back(GetMethodConstant(*i));
4211 }
4212 } else {
4213 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004214 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004215 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004216 // Instance methods should always be defined.
4217 Methods.push_back(GetMethodConstant(*i));
4218 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00004219 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004220 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004221 ObjCPropertyImplDecl *PID = *i;
4222
4223 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4224 ObjCPropertyDecl *PD = PID->getPropertyDecl();
4225
4226 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4227 if (llvm::Constant *C = GetMethodConstant(MD))
4228 Methods.push_back(C);
4229 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4230 if (llvm::Constant *C = GetMethodConstant(MD))
4231 Methods.push_back(C);
4232 }
4233 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004234 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004235 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004236 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004237
4238 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4239 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
4240 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
4241 + OID->getNameAsString(),
4242 OID->protocol_begin(),
4243 OID->protocol_end());
4244
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004245 if (flags & CLS_META)
Owen Anderson69243822009-07-13 04:10:07 +00004246 Values[ 7] = VMContext.getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004247 else
4248 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004249 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4250 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004251 if (flags & CLS_META)
Owen Anderson69243822009-07-13 04:10:07 +00004252 Values[ 9] = VMContext.getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004253 else
4254 Values[ 9] =
4255 EmitPropertyList(
4256 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
4257 ID, ID->getClassInterface(), ObjCTypes);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004258 llvm::Constant *Init = VMContext.getConstantStruct(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004259 Values);
4260 llvm::GlobalVariable *CLASS_RO_GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004261 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004262 llvm::GlobalValue::InternalLinkage,
4263 Init,
4264 (flags & CLS_META) ?
4265 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
Owen Anderson1c431b32009-07-08 19:05:04 +00004266 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004267 CLASS_RO_GV->setAlignment(
4268 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004269 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004270 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004271
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004272}
4273
4274/// BuildClassMetaData - This routine defines that to-level meta-data
4275/// for the given ClassName for:
4276/// struct _class_t {
4277/// struct _class_t *isa;
4278/// struct _class_t * const superclass;
4279/// void *cache;
4280/// IMP *vtable;
4281/// struct class_ro_t *ro;
4282/// }
4283///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004284llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
4285 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004286 llvm::Constant *IsAGV,
4287 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004288 llvm::Constant *ClassRoGV,
4289 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004290 std::vector<llvm::Constant*> Values(5);
4291 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004292 Values[1] = SuperClassGV
4293 ? SuperClassGV
Owen Anderson69243822009-07-13 04:10:07 +00004294 : VMContext.getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004295 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4296 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4297 Values[4] = ClassRoGV; // &CLASS_RO_GV
Owen Andersona1cf15f2009-07-14 23:10:40 +00004298 llvm::Constant *Init = VMContext.getConstantStruct(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004299 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004300 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4301 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004302 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004303 GV->setAlignment(
4304 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004305 if (HiddenVisibility)
4306 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004307 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004308}
4309
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004310bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00004311CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004312 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004313}
4314
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004315void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004316 uint32_t &InstanceStart,
4317 uint32_t &InstanceSize) {
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004318 const ASTRecordLayout &RL =
4319 CGM.getContext().getASTObjCImplementationLayout(OID);
4320
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004321 // InstanceSize is really instance end.
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004322 InstanceSize = llvm::RoundUpToAlignment(RL.getNextOffset(), 8) / 8;
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004323
4324 // If there are no fields, the start is the same as the end.
4325 if (!RL.getFieldCount())
4326 InstanceStart = InstanceSize;
4327 else
4328 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004329}
4330
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004331void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4332 std::string ClassName = ID->getNameAsString();
4333 if (!ObjCEmptyCacheVar) {
4334 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Owen Anderson1c431b32009-07-08 19:05:04 +00004335 CGM.getModule(),
Daniel Dunbar948e2582009-02-15 07:36:20 +00004336 ObjCTypes.CacheTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004337 false,
4338 llvm::GlobalValue::ExternalLinkage,
4339 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00004340 "_objc_empty_cache");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004341
4342 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Owen Anderson1c431b32009-07-08 19:05:04 +00004343 CGM.getModule(),
Daniel Dunbar948e2582009-02-15 07:36:20 +00004344 ObjCTypes.ImpnfABITy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004345 false,
4346 llvm::GlobalValue::ExternalLinkage,
4347 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00004348 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004349 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004350 assert(ID->getClassInterface() &&
4351 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004352 // FIXME: Is this correct (that meta class size is never computed)?
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004353 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00004354 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004355 uint32_t InstanceSize = InstanceStart;
4356 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004357 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4358 std::string ObjCClassName(getClassSymbolPrefix());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004359
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004360 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004361
Daniel Dunbar04d40782009-04-14 06:00:08 +00004362 bool classIsHidden =
4363 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004364 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004365 flags |= OBJC2_CLS_HIDDEN;
4366 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004367 // class is root
4368 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004369 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004370 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004371 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004372 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004373 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4374 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4375 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004376 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004377 // work on super class metadata symbol.
4378 std::string SuperClassName =
4379 ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004380 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004381 }
4382 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4383 InstanceStart,
4384 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004385 std::string TClassName = ObjCMetaClassName + ClassName;
4386 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004387 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4388 classIsHidden);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004389
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004390 // Metadata for the class
4391 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004392 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004393 flags |= OBJC2_CLS_HIDDEN;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004394
Douglas Gregor68584ed2009-06-18 16:11:24 +00004395 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004396 flags |= CLS_EXCEPTION;
4397
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004398 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004399 flags |= CLS_ROOT;
4400 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00004401 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004402 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004403 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004404 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004405 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004406 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004407 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004408 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004409 InstanceStart,
4410 InstanceSize,
4411 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004412
4413 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004414 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004415 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4416 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004417 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004418
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004419 // Determine if this class is also "non-lazy".
4420 if (ImplementationIsNonLazy(ID))
4421 DefinedNonLazyClasses.push_back(ClassMD);
4422
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004423 // Force the definition of the EHType if necessary.
4424 if (flags & CLS_EXCEPTION)
4425 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004426}
4427
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004428/// GenerateProtocolRef - This routine is called to generate code for
4429/// a protocol reference expression; as in:
4430/// @code
4431/// @protocol(Proto1);
4432/// @endcode
4433/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4434/// which will hold address of the protocol meta-data.
4435///
4436llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
4437 const ObjCProtocolDecl *PD) {
4438
Fariborz Jahanian960cd062009-04-10 18:47:34 +00004439 // This routine is called for @protocol only. So, we must build definition
4440 // of protocol's meta-data (not a reference to it!)
4441 //
Owen Andersona1cf15f2009-07-14 23:10:40 +00004442 llvm::Constant *Init =
4443 VMContext.getConstantExprBitCast(GetOrEmitProtocol(PD),
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004444 ObjCTypes.ExternalProtocolPtrTy);
4445
4446 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
4447 ProtocolName += PD->getNameAsCString();
4448
4449 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
4450 if (PTGV)
4451 return Builder.CreateLoad(PTGV, false, "tmp");
4452 PTGV = new llvm::GlobalVariable(
Owen Anderson1c431b32009-07-08 19:05:04 +00004453 CGM.getModule(),
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004454 Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00004455 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004456 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004457 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004458 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
4459 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00004460 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004461 return Builder.CreateLoad(PTGV, false, "tmp");
4462}
4463
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004464/// GenerateCategory - Build metadata for a category implementation.
4465/// struct _category_t {
4466/// const char * const name;
4467/// struct _class_t *const cls;
4468/// const struct _method_list_t * const instance_methods;
4469/// const struct _method_list_t * const class_methods;
4470/// const struct _protocol_list_t * const protocols;
4471/// const struct _prop_list_t * const properties;
4472/// }
4473///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004474void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004475 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004476 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
4477 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004478 "_$_" + OCD->getNameAsString());
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004479 std::string ExtClassName(getClassSymbolPrefix() +
4480 Interface->getNameAsString());
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004481
4482 std::vector<llvm::Constant*> Values(6);
4483 Values[0] = GetClassName(OCD->getIdentifier());
4484 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004485 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004486 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004487 std::vector<llvm::Constant*> Methods;
4488 std::string MethodListName(Prefix);
4489 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
4490 "_$_" + OCD->getNameAsString();
4491
Douglas Gregor653f1b12009-04-23 01:02:12 +00004492 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004493 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004494 // Instance methods should always be defined.
4495 Methods.push_back(GetMethodConstant(*i));
4496 }
4497
4498 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004499 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004500 Methods);
4501
4502 MethodListName = Prefix;
4503 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
4504 OCD->getNameAsString();
4505 Methods.clear();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004506 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004507 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004508 // Class methods should always be defined.
4509 Methods.push_back(GetMethodConstant(*i));
4510 }
4511
4512 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004513 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004514 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004515 const ObjCCategoryDecl *Category =
4516 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004517 if (Category) {
4518 std::string ExtName(Interface->getNameAsString() + "_$_" +
4519 OCD->getNameAsString());
4520 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
4521 + Interface->getNameAsString() + "_$_"
4522 + Category->getNameAsString(),
4523 Category->protocol_begin(),
4524 Category->protocol_end());
4525 Values[5] =
4526 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
4527 OCD, Category, ObjCTypes);
4528 }
4529 else {
Owen Anderson69243822009-07-13 04:10:07 +00004530 Values[4] = VMContext.getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4531 Values[5] = VMContext.getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004532 }
4533
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004534 llvm::Constant *Init =
Owen Andersona1cf15f2009-07-14 23:10:40 +00004535 VMContext.getConstantStruct(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004536 Values);
4537 llvm::GlobalVariable *GCATV
Owen Anderson1c431b32009-07-08 19:05:04 +00004538 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004539 false,
4540 llvm::GlobalValue::InternalLinkage,
4541 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004542 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004543 GCATV->setAlignment(
4544 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004545 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00004546 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004547 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004548
4549 // Determine if this category is also "non-lazy".
4550 if (ImplementationIsNonLazy(OCD))
4551 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004552}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004553
4554/// GetMethodConstant - Return a struct objc_method constant for the
4555/// given method if it has been defined. The result is null if the
4556/// method has not been defined. The return value has type MethodPtrTy.
4557llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
4558 const ObjCMethodDecl *MD) {
4559 // FIXME: Use DenseMap::lookup
4560 llvm::Function *Fn = MethodDefinitions[MD];
4561 if (!Fn)
4562 return 0;
4563
4564 std::vector<llvm::Constant*> Method(3);
4565 Method[0] =
Owen Andersona1cf15f2009-07-14 23:10:40 +00004566 VMContext.getConstantExprBitCast(GetMethodVarName(MD->getSelector()),
4567 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004568 Method[1] = GetMethodVarType(MD);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004569 Method[2] = VMContext.getConstantExprBitCast(Fn, ObjCTypes.Int8PtrTy);
4570 return VMContext.getConstantStruct(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004571}
4572
4573/// EmitMethodList - Build meta-data for method declarations
4574/// struct _method_list_t {
4575/// uint32_t entsize; // sizeof(struct _objc_method)
4576/// uint32_t method_count;
4577/// struct _objc_method method_list[method_count];
4578/// }
4579///
4580llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
4581 const std::string &Name,
4582 const char *Section,
4583 const ConstantVector &Methods) {
4584 // Return null for empty list.
4585 if (Methods.empty())
Owen Anderson69243822009-07-13 04:10:07 +00004586 return VMContext.getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004587
4588 std::vector<llvm::Constant*> Values(3);
4589 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00004590 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004591 Values[0] = VMContext.getConstantInt(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004592 // method_count
Owen Andersona1cf15f2009-07-14 23:10:40 +00004593 Values[1] = VMContext.getConstantInt(ObjCTypes.IntTy, Methods.size());
4594 llvm::ArrayType *AT = VMContext.getArrayType(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004595 Methods.size());
Owen Andersona1cf15f2009-07-14 23:10:40 +00004596 Values[2] = VMContext.getConstantArray(AT, Methods);
4597 llvm::Constant *Init = VMContext.getConstantStruct(Values);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004598
4599 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004600 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004601 llvm::GlobalValue::InternalLinkage,
4602 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004603 Name);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004604 GV->setAlignment(
4605 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004606 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00004607 CGM.AddUsedGlobal(GV);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004608 return VMContext.getConstantExprBitCast(GV,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004609 ObjCTypes.MethodListnfABIPtrTy);
4610}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004611
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004612/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
4613/// the given ivar.
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004614llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004615 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004616 const ObjCIvarDecl *Ivar) {
Daniel Dunbara81419d2009-05-05 00:36:57 +00004617 // FIXME: We shouldn't need to do this lookup.
4618 unsigned Index;
4619 const ObjCInterfaceDecl *Container =
4620 FindIvarInterface(CGM.getContext(), ID, Ivar, Index);
4621 assert(Container && "Unable to find ivar container!");
4622 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00004623 '.' + Ivar->getNameAsString();
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004624 llvm::GlobalVariable *IvarOffsetGV =
4625 CGM.getModule().getGlobalVariable(Name);
4626 if (!IvarOffsetGV)
4627 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004628 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004629 false,
4630 llvm::GlobalValue::ExternalLinkage,
4631 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00004632 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004633 return IvarOffsetGV;
4634}
4635
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004636llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004637 const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004638 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004639 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00004640 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004641 IvarOffsetGV->setInitializer(VMContext.getConstantInt(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00004642 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004643 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004644 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00004645
Mike Stumpf5408fe2009-05-16 07:57:57 +00004646 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
4647 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00004648 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
4649 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
4650 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004651 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00004652 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00004653 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004654 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004655 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004656}
4657
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004658/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00004659/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004660/// IvarListnfABIPtrTy.
4661/// struct _ivar_t {
4662/// unsigned long int *offset; // pointer to ivar offset location
4663/// char *name;
4664/// char *type;
4665/// uint32_t alignment;
4666/// uint32_t size;
4667/// }
4668/// struct _ivar_list_t {
4669/// uint32 entsize; // sizeof(struct _ivar_t)
4670/// uint32 count;
4671/// struct _iver_t list[count];
4672/// }
4673///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004674
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004675llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
4676 const ObjCImplementationDecl *ID) {
4677
4678 std::vector<llvm::Constant*> Ivars, Ivar(5);
4679
4680 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4681 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
4682
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004683 // FIXME. Consolidate this with similar code in GenerateClass.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00004684
Daniel Dunbar91636d62009-04-20 00:33:43 +00004685 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00004686 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00004687 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Fariborz Jahanian99eee362009-04-01 19:37:34 +00004688
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004689 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
4690 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00004691 // Ignore unnamed bit-fields.
4692 if (!IVD->getDeclName())
4693 continue;
Daniel Dunbar3eec8aa2009-04-20 05:53:40 +00004694 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004695 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004696 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
4697 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004698 const llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004699 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00004700 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004701 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004702 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004703 Align = llvm::Log2_32(Align);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004704 Ivar[3] = VMContext.getConstantInt(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00004705 // NOTE. Size of a bitfield does not match gcc's, because of the
4706 // way bitfields are treated special in each. But I am told that
4707 // 'size' for bitfield ivars is ignored by the runtime so it does
4708 // not matter. If it matters, there is enough info to get the
4709 // bitfield right!
Owen Andersona1cf15f2009-07-14 23:10:40 +00004710 Ivar[4] = VMContext.getConstantInt(ObjCTypes.IntTy, Size);
4711 Ivars.push_back(VMContext.getConstantStruct(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004712 }
4713 // Return null for empty list.
4714 if (Ivars.empty())
Owen Anderson69243822009-07-13 04:10:07 +00004715 return VMContext.getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004716 std::vector<llvm::Constant*> Values(3);
Duncan Sands9408c452009-05-09 07:08:47 +00004717 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004718 Values[0] = VMContext.getConstantInt(ObjCTypes.IntTy, Size);
4719 Values[1] = VMContext.getConstantInt(ObjCTypes.IntTy, Ivars.size());
4720 llvm::ArrayType *AT = VMContext.getArrayType(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004721 Ivars.size());
Owen Andersona1cf15f2009-07-14 23:10:40 +00004722 Values[2] = VMContext.getConstantArray(AT, Ivars);
4723 llvm::Constant *Init = VMContext.getConstantStruct(Values);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004724 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4725 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004726 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004727 llvm::GlobalValue::InternalLinkage,
4728 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004729 Prefix + OID->getNameAsString());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004730 GV->setAlignment(
4731 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004732 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004733
Chris Lattnerad64e022009-07-17 23:57:13 +00004734 CGM.AddUsedGlobal(GV);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004735 return VMContext.getConstantExprBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004736}
4737
4738llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4739 const ObjCProtocolDecl *PD) {
4740 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4741
4742 if (!Entry) {
4743 // We use the initializer as a marker of whether this is a forward
4744 // reference or not. At module finalization we add the empty
4745 // contents for protocols which were referenced but never defined.
4746 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00004747 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004748 llvm::GlobalValue::ExternalLinkage,
4749 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00004750 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString());
Fariborz Jahanianda320092009-01-29 19:24:30 +00004751 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004752 }
4753
4754 return Entry;
4755}
4756
4757/// GetOrEmitProtocol - Generate the protocol meta-data:
4758/// @code
4759/// struct _protocol_t {
4760/// id isa; // NULL
4761/// const char * const protocol_name;
4762/// const struct _protocol_list_t * protocol_list; // super protocols
4763/// const struct method_list_t * const instance_methods;
4764/// const struct method_list_t * const class_methods;
4765/// const struct method_list_t *optionalInstanceMethods;
4766/// const struct method_list_t *optionalClassMethods;
4767/// const struct _prop_list_t * properties;
4768/// const uint32_t size; // sizeof(struct _protocol_t)
4769/// const uint32_t flags; // = 0
4770/// }
4771/// @endcode
4772///
4773
4774llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4775 const ObjCProtocolDecl *PD) {
4776 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4777
4778 // Early exit if a defining object has already been generated.
4779 if (Entry && Entry->hasInitializer())
4780 return Entry;
4781
4782 const char *ProtocolName = PD->getNameAsCString();
4783
4784 // Construct method lists.
4785 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4786 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00004787 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004788 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004789 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004790 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004791 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4792 OptInstanceMethods.push_back(C);
4793 } else {
4794 InstanceMethods.push_back(C);
4795 }
4796 }
4797
Douglas Gregor6ab35242009-04-09 21:40:53 +00004798 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004799 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004800 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004801 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004802 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4803 OptClassMethods.push_back(C);
4804 } else {
4805 ClassMethods.push_back(C);
4806 }
4807 }
4808
4809 std::vector<llvm::Constant*> Values(10);
4810 // isa is NULL
Owen Anderson69243822009-07-13 04:10:07 +00004811 Values[0] = VMContext.getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004812 Values[1] = GetClassName(PD->getIdentifier());
4813 Values[2] = EmitProtocolList(
4814 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4815 PD->protocol_begin(),
4816 PD->protocol_end());
4817
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004818 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004819 + PD->getNameAsString(),
4820 "__DATA, __objc_const",
4821 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004822 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004823 + PD->getNameAsString(),
4824 "__DATA, __objc_const",
4825 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004826 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004827 + PD->getNameAsString(),
4828 "__DATA, __objc_const",
4829 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004830 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004831 + PD->getNameAsString(),
4832 "__DATA, __objc_const",
4833 OptClassMethods);
4834 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4835 0, PD, ObjCTypes);
4836 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00004837 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004838 Values[8] = VMContext.getConstantInt(ObjCTypes.IntTy, Size);
Owen Anderson69243822009-07-13 04:10:07 +00004839 Values[9] = VMContext.getNullValue(ObjCTypes.IntTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004840 llvm::Constant *Init = VMContext.getConstantStruct(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004841 Values);
4842
4843 if (Entry) {
4844 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00004845 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004846 Entry->setInitializer(Init);
4847 } else {
4848 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00004849 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004850 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004851 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004852 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004853 Entry->setAlignment(
4854 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004855 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004856 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004857 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00004858 CGM.AddUsedGlobal(Entry);
4859
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004860 // Use this protocol meta-data to build protocol list table in section
4861 // __DATA, __objc_protolist
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004862 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
Owen Anderson1c431b32009-07-08 19:05:04 +00004863 CGM.getModule(),
Daniel Dunbar948e2582009-02-15 07:36:20 +00004864 ObjCTypes.ProtocolnfABIPtrTy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004865 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004866 Entry,
4867 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
Owen Anderson1c431b32009-07-08 19:05:04 +00004868 +ProtocolName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004869 PTGV->setAlignment(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004870 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00004871 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004872 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00004873 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004874 return Entry;
4875}
4876
4877/// EmitProtocolList - Generate protocol list meta-data:
4878/// @code
4879/// struct _protocol_list_t {
4880/// long protocol_count; // Note, this is 32/64 bit
4881/// struct _protocol_t[protocol_count];
4882/// }
4883/// @endcode
4884///
4885llvm::Constant *
4886CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4887 ObjCProtocolDecl::protocol_iterator begin,
4888 ObjCProtocolDecl::protocol_iterator end) {
4889 std::vector<llvm::Constant*> ProtocolRefs;
4890
Fariborz Jahanianda320092009-01-29 19:24:30 +00004891 // Just return null for empty protocol lists
Daniel Dunbar948e2582009-02-15 07:36:20 +00004892 if (begin == end)
Owen Anderson69243822009-07-13 04:10:07 +00004893 return VMContext.getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004894
Daniel Dunbar948e2582009-02-15 07:36:20 +00004895 // FIXME: We shouldn't need to do this lookup here, should we?
Fariborz Jahanianda320092009-01-29 19:24:30 +00004896 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4897 if (GV)
Owen Andersona1cf15f2009-07-14 23:10:40 +00004898 return VMContext.getConstantExprBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00004899 ObjCTypes.ProtocolListnfABIPtrTy);
4900
4901 for (; begin != end; ++begin)
4902 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4903
Fariborz Jahanianda320092009-01-29 19:24:30 +00004904 // This list is null terminated.
Owen Anderson69243822009-07-13 04:10:07 +00004905 ProtocolRefs.push_back(VMContext.getNullValue(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004906 ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004907
4908 std::vector<llvm::Constant*> Values(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004909 Values[0] =
4910 VMContext.getConstantInt(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004911 Values[1] =
Owen Andersona1cf15f2009-07-14 23:10:40 +00004912 VMContext.getConstantArray(
4913 VMContext.getArrayType(ObjCTypes.ProtocolnfABIPtrTy,
4914 ProtocolRefs.size()),
4915 ProtocolRefs);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004916
Owen Andersona1cf15f2009-07-14 23:10:40 +00004917 llvm::Constant *Init = VMContext.getConstantStruct(Values);
Owen Anderson1c431b32009-07-08 19:05:04 +00004918 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004919 llvm::GlobalValue::InternalLinkage,
4920 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004921 Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004922 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004923 GV->setAlignment(
4924 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00004925 CGM.AddUsedGlobal(GV);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004926 return VMContext.getConstantExprBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00004927 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004928}
4929
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004930/// GetMethodDescriptionConstant - This routine build following meta-data:
4931/// struct _objc_method {
4932/// SEL _cmd;
4933/// char *method_type;
4934/// char *_imp;
4935/// }
4936
4937llvm::Constant *
4938CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4939 std::vector<llvm::Constant*> Desc(3);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004940 Desc[0] =
4941 VMContext.getConstantExprBitCast(GetMethodVarName(MD->getSelector()),
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004942 ObjCTypes.SelectorPtrTy);
4943 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004944 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson69243822009-07-13 04:10:07 +00004945 Desc[2] = VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004946 return VMContext.getConstantStruct(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004947}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004948
4949/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4950/// This code gen. amounts to generating code for:
4951/// @code
4952/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4953/// @encode
4954///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004955LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004956 CodeGen::CodeGenFunction &CGF,
4957 QualType ObjectTy,
4958 llvm::Value *BaseValue,
4959 const ObjCIvarDecl *Ivar,
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004960 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00004961 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00004962 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
4963 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004964}
4965
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004966llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
4967 CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00004968 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004969 const ObjCIvarDecl *Ivar) {
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00004970 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),
4971 false, "ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004972}
4973
Fariborz Jahanian46551122009-02-04 00:22:57 +00004974CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4975 CodeGen::CodeGenFunction &CGF,
4976 QualType ResultType,
4977 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004978 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00004979 QualType Arg0Ty,
4980 bool IsSuper,
4981 const CallArgList &CallArgs) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00004982 // FIXME. Even though IsSuper is passes. This function doese not handle calls
4983 // to 'super' receivers.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004984 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004985 llvm::Value *Arg0 = Receiver;
4986 if (!IsSuper)
4987 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004988
4989 // Find the message function name.
Mike Stumpf5408fe2009-05-16 07:57:57 +00004990 // FIXME. This is too much work to get the ABI-specific result type needed to
4991 // find the message name.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004992 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4993 llvm::SmallVector<QualType, 16>());
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00004994 llvm::Constant *Fn = 0;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004995 std::string Name("\01l_");
4996 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004997#if 0
4998 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004999 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005000 Fn = ObjCTypes.getMessageSendIdStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005001 // FIXME. Is there a better way of getting these names.
5002 // They are available in RuntimeFunctions vector pair.
5003 Name += "objc_msgSendId_stret_fixup";
5004 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005005 else
5006#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005007 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005008 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005009 Name += "objc_msgSendSuper2_stret_fixup";
5010 }
5011 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005012 {
Chris Lattner1c02f862009-04-22 02:53:24 +00005013 Fn = ObjCTypes.getMessageSendStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005014 Name += "objc_msgSend_stret_fixup";
5015 }
5016 }
Fariborz Jahanian5b2bad02009-04-30 16:31:11 +00005017 else if (!IsSuper && ResultType->isFloatingType()) {
Daniel Dunbarc0183e82009-06-26 18:32:06 +00005018 if (ResultType->isSpecificBuiltinType(BuiltinType::LongDouble)) {
5019 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5020 Name += "objc_msgSend_fpret_fixup";
5021 }
5022 else {
5023 Fn = ObjCTypes.getMessageSendFixupFn();
5024 Name += "objc_msgSend_fixup";
Fariborz Jahanian5b2bad02009-04-30 16:31:11 +00005025 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005026 }
5027 else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005028#if 0
5029// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005030 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005031 Fn = ObjCTypes.getMessageSendIdFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005032 Name += "objc_msgSendId_fixup";
5033 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005034 else
5035#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005036 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005037 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005038 Name += "objc_msgSendSuper2_fixup";
5039 }
5040 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005041 {
Chris Lattner1c02f862009-04-22 02:53:24 +00005042 Fn = ObjCTypes.getMessageSendFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005043 Name += "objc_msgSend_fixup";
5044 }
5045 }
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005046 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005047 Name += '_';
5048 std::string SelName(Sel.getAsString());
5049 // Replace all ':' in selector name with '_' ouch!
5050 for(unsigned i = 0; i < SelName.size(); i++)
5051 if (SelName[i] == ':')
5052 SelName[i] = '_';
5053 Name += SelName;
5054 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5055 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005056 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005057 std::vector<llvm::Constant*> Values(2);
5058 Values[0] = Fn;
5059 Values[1] = GetMethodVarName(Sel);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005060 llvm::Constant *Init = VMContext.getConstantStruct(Values);
Owen Anderson1c431b32009-07-08 19:05:04 +00005061 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005062 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005063 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005064 Name);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005065 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005066 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005067 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005068 }
5069 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005070
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005071 CallArgList ActualArgs;
5072 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
5073 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
5074 ObjCTypes.MessageRefCPtrTy));
5075 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005076 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
5077 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5078 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00005079 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005080 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Andersona1cf15f2009-07-14 23:10:40 +00005081 VMContext.getPointerTypeUnqual(FTy));
Fariborz Jahanianef163782009-02-05 01:13:09 +00005082 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005083}
5084
5085/// Generate code for a message send expression in the nonfragile abi.
5086CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
5087 CodeGen::CodeGenFunction &CGF,
5088 QualType ResultType,
5089 Selector Sel,
5090 llvm::Value *Receiver,
5091 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00005092 const CallArgList &CallArgs,
5093 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005094 return LegacyDispatchedSelector(Sel)
5095 ? EmitLegacyMessageSend(CGF, ResultType, EmitSelector(CGF.Builder, Sel),
5096 Receiver, CGF.getContext().getObjCIdType(),
5097 false, CallArgs, ObjCTypes)
5098 : EmitMessageSend(CGF, ResultType, Sel,
5099 Receiver, CGF.getContext().getObjCIdType(),
5100 false, CallArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005101}
5102
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005103llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005104CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005105 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5106
Daniel Dunbardfff2302009-03-02 05:18:14 +00005107 if (!GV) {
Owen Anderson1c431b32009-07-08 19:05:04 +00005108 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
5109 false, llvm::GlobalValue::ExternalLinkage,
5110 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005111 }
5112
5113 return GV;
5114}
5115
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005116llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005117 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005118 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
5119
5120 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005121 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005122 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005123 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005124 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5125 false, llvm::GlobalValue::InternalLinkage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005126 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005127 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005128 Entry->setAlignment(
5129 CGM.getTargetData().getPrefTypeAlignment(
5130 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005131 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005132 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005133 }
5134
5135 return Builder.CreateLoad(Entry, false, "tmp");
5136}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005137
Daniel Dunbar11394522009-04-18 08:51:00 +00005138llvm::Value *
5139CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
5140 const ObjCInterfaceDecl *ID) {
5141 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
5142
5143 if (!Entry) {
5144 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5145 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5146 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005147 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5148 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar11394522009-04-18 08:51:00 +00005149 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005150 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005151 Entry->setAlignment(
5152 CGM.getTargetData().getPrefTypeAlignment(
5153 ObjCTypes.ClassnfABIPtrTy));
5154 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005155 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005156 }
5157
5158 return Builder.CreateLoad(Entry, false, "tmp");
5159}
5160
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005161/// EmitMetaClassRef - Return a Value * of the address of _class_t
5162/// meta-data
5163///
5164llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5165 const ObjCInterfaceDecl *ID) {
5166 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5167 if (Entry)
5168 return Builder.CreateLoad(Entry, false, "tmp");
5169
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005170 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005171 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005172 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005173 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005174 llvm::GlobalValue::InternalLinkage,
5175 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005176 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005177 Entry->setAlignment(
5178 CGM.getTargetData().getPrefTypeAlignment(
5179 ObjCTypes.ClassnfABIPtrTy));
5180
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005181 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005182 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005183
5184 return Builder.CreateLoad(Entry, false, "tmp");
5185}
5186
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005187/// GetClass - Return a reference to the class for the given interface
5188/// decl.
5189llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5190 const ObjCInterfaceDecl *ID) {
5191 return EmitClassRef(Builder, ID);
5192}
5193
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005194/// Generates a message send where the super is the receiver. This is
5195/// a message send to self with special delivery semantics indicating
5196/// which class's method should be called.
5197CodeGen::RValue
5198CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
5199 QualType ResultType,
5200 Selector Sel,
5201 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005202 bool isCategoryImpl,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005203 llvm::Value *Receiver,
5204 bool IsClassMessage,
5205 const CodeGen::CallArgList &CallArgs) {
5206 // ...
5207 // Create and init a super structure; this is a (receiver, class)
5208 // pair we will pass to objc_msgSendSuper.
5209 llvm::Value *ObjCSuper =
5210 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
5211
5212 llvm::Value *ReceiverAsObject =
5213 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5214 CGF.Builder.CreateStore(ReceiverAsObject,
5215 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
5216
5217 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005218 llvm::Value *Target;
5219 if (IsClassMessage) {
5220 if (isCategoryImpl) {
5221 // Message sent to "super' in a class method defined in
5222 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005223 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005224 Target = CGF.Builder.CreateStructGEP(Target, 0);
5225 Target = CGF.Builder.CreateLoad(Target);
5226 }
5227 else
5228 Target = EmitMetaClassRef(CGF.Builder, Class);
5229 }
5230 else
Daniel Dunbar11394522009-04-18 08:51:00 +00005231 Target = EmitSuperClassRef(CGF.Builder, Class);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005232
Mike Stumpf5408fe2009-05-16 07:57:57 +00005233 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5234 // ObjCTypes types.
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005235 const llvm::Type *ClassTy =
5236 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5237 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5238 CGF.Builder.CreateStore(Target,
5239 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
5240
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005241 return (LegacyDispatchedSelector(Sel))
5242 ? EmitLegacyMessageSend(CGF, ResultType,EmitSelector(CGF.Builder, Sel),
5243 ObjCSuper, ObjCTypes.SuperPtrCTy,
5244 true, CallArgs,
5245 ObjCTypes)
5246 : EmitMessageSend(CGF, ResultType, Sel,
5247 ObjCSuper, ObjCTypes.SuperPtrCTy,
5248 true, CallArgs);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005249}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005250
5251llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
5252 Selector Sel) {
5253 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
5254
5255 if (!Entry) {
5256 llvm::Constant *Casted =
Owen Andersona1cf15f2009-07-14 23:10:40 +00005257 VMContext.getConstantExprBitCast(GetMethodVarName(Sel),
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005258 ObjCTypes.SelectorPtrTy);
5259 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005260 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005261 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00005262 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005263 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005264 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005265 }
5266
5267 return Builder.CreateLoad(Entry, false, "tmp");
5268}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005269/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
5270/// objc_assign_ivar (id src, id *dst)
5271///
5272void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
5273 llvm::Value *src, llvm::Value *dst)
5274{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005275 const llvm::Type * SrcTy = src->getType();
5276 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005277 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005278 assert(Size <= 8 && "does not support size > 8");
5279 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5280 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005281 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5282 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005283 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5284 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005285 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005286 src, dst, "assignivar");
5287 return;
5288}
5289
5290/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5291/// objc_assign_strongCast (id src, id *dst)
5292///
5293void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
5294 CodeGen::CodeGenFunction &CGF,
5295 llvm::Value *src, llvm::Value *dst)
5296{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005297 const llvm::Type * SrcTy = src->getType();
5298 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005299 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005300 assert(Size <= 8 && "does not support size > 8");
5301 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5302 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005303 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5304 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005305 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5306 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005307 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005308 src, dst, "weakassign");
5309 return;
5310}
5311
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005312void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
5313 CodeGen::CodeGenFunction &CGF,
5314 llvm::Value *DestPtr,
5315 llvm::Value *SrcPtr,
5316 unsigned long size) {
5317 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5318 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005319 llvm::Value *N = VMContext.getConstantInt(ObjCTypes.LongTy, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005320 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
5321 DestPtr, SrcPtr, N);
5322 return;
5323}
5324
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005325/// EmitObjCWeakRead - Code gen for loading value of a __weak
5326/// object: objc_read_weak (id *src)
5327///
5328llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
5329 CodeGen::CodeGenFunction &CGF,
5330 llvm::Value *AddrWeakObj)
5331{
Eli Friedman8339b352009-03-07 03:57:15 +00005332 const llvm::Type* DestTy =
5333 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005334 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005335 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005336 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005337 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005338 return read_weak;
5339}
5340
5341/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5342/// objc_assign_weak (id src, id *dst)
5343///
5344void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
5345 llvm::Value *src, llvm::Value *dst)
5346{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005347 const llvm::Type * SrcTy = src->getType();
5348 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005349 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005350 assert(Size <= 8 && "does not support size > 8");
5351 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5352 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005353 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5354 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005355 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5356 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005357 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005358 src, dst, "weakassign");
5359 return;
5360}
5361
5362/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5363/// objc_assign_global (id src, id *dst)
5364///
5365void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
5366 llvm::Value *src, llvm::Value *dst)
5367{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005368 const llvm::Type * SrcTy = src->getType();
5369 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005370 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005371 assert(Size <= 8 && "does not support size > 8");
5372 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5373 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005374 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5375 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005376 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5377 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005378 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005379 src, dst, "globalassign");
5380 return;
5381}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005382
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005383void
5384CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5385 const Stmt &S) {
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005386 bool isTry = isa<ObjCAtTryStmt>(S);
5387 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
5388 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005389 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005390 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005391 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005392 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
5393
5394 // For @synchronized, call objc_sync_enter(sync.expr). The
5395 // evaluation of the expression must occur before we enter the
5396 // @synchronized. We can safely avoid a temp here because jumps into
5397 // @synchronized are illegal & this will dominate uses.
5398 llvm::Value *SyncArg = 0;
5399 if (!isTry) {
5400 SyncArg =
5401 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
5402 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005403 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005404 }
5405
5406 // Push an EH context entry, used for handling rethrows and jumps
5407 // through finally.
5408 CGF.PushCleanupBlock(FinallyBlock);
5409
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005410 CGF.setInvokeDest(TryHandler);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005411
5412 CGF.EmitBlock(TryBlock);
5413 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
5414 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
5415 CGF.EmitBranchThroughCleanup(FinallyEnd);
5416
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005417 // Emit the exception handler.
5418
5419 CGF.EmitBlock(TryHandler);
5420
5421 llvm::Value *llvm_eh_exception =
5422 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
5423 llvm::Value *llvm_eh_selector_i64 =
5424 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64);
5425 llvm::Value *llvm_eh_typeid_for_i64 =
5426 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64);
5427 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5428 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow");
5429
5430 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
5431 SelectorArgs.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005432 SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005433
5434 // Construct the lists of (type, catch body) to handle.
Daniel Dunbarede8de92009-03-06 00:01:21 +00005435 llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005436 bool HasCatchAll = false;
5437 if (isTry) {
5438 if (const ObjCAtCatchStmt* CatchStmt =
5439 cast<ObjCAtTryStmt>(S).getCatchStmts()) {
5440 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005441 const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Steve Naroff7ba138a2009-03-03 19:52:17 +00005442 Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody()));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005443
5444 // catch(...) always matches.
Steve Naroff7ba138a2009-03-03 19:52:17 +00005445 if (!CatchDecl) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005446 // Use i8* null here to signal this is a catch all, not a cleanup.
Owen Anderson69243822009-07-13 04:10:07 +00005447 llvm::Value *Null = VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005448 SelectorArgs.push_back(Null);
5449 HasCatchAll = true;
5450 break;
5451 }
5452
Steve Naroff14108da2009-07-10 23:34:53 +00005453 if (CatchDecl->getType()->isObjCIdType() ||
Daniel Dunbarede8de92009-03-06 00:01:21 +00005454 CatchDecl->getType()->isObjCQualifiedIdType()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005455 llvm::Value *IDEHType =
5456 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5457 if (!IDEHType)
5458 IDEHType =
Owen Anderson1c431b32009-07-08 19:05:04 +00005459 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
5460 false,
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005461 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00005462 0, "OBJC_EHTYPE_id");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005463 SelectorArgs.push_back(IDEHType);
5464 HasCatchAll = true;
5465 break;
5466 }
5467
5468 // All other types should be Objective-C interface pointer types.
Steve Naroff14108da2009-07-10 23:34:53 +00005469 const ObjCObjectPointerType *PT =
5470 CatchDecl->getType()->getAsObjCObjectPointerType();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005471 assert(PT && "Invalid @catch type.");
Steve Naroff14108da2009-07-10 23:34:53 +00005472 const ObjCInterfaceType *IT = PT->getInterfaceType();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005473 assert(IT && "Invalid @catch type.");
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005474 llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005475 SelectorArgs.push_back(EHType);
5476 }
5477 }
5478 }
5479
5480 // We use a cleanup unless there was already a catch all.
5481 if (!HasCatchAll) {
Owen Andersona1cf15f2009-07-14 23:10:40 +00005482 SelectorArgs.push_back(VMContext.getConstantInt(llvm::Type::Int32Ty, 0));
Daniel Dunbarede8de92009-03-06 00:01:21 +00005483 Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005484 }
5485
5486 llvm::Value *Selector =
5487 CGF.Builder.CreateCall(llvm_eh_selector_i64,
5488 SelectorArgs.begin(), SelectorArgs.end(),
5489 "selector");
5490 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005491 const ParmVarDecl *CatchParam = Handlers[i].first;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005492 const Stmt *CatchBody = Handlers[i].second;
5493
5494 llvm::BasicBlock *Next = 0;
5495
5496 // The last handler always matches.
5497 if (i + 1 != e) {
5498 assert(CatchParam && "Only last handler can be a catch all.");
5499
5500 llvm::BasicBlock *Match = CGF.createBasicBlock("match");
5501 Next = CGF.createBasicBlock("catch.next");
5502 llvm::Value *Id =
5503 CGF.Builder.CreateCall(llvm_eh_typeid_for_i64,
5504 CGF.Builder.CreateBitCast(SelectorArgs[i+2],
5505 ObjCTypes.Int8PtrTy));
5506 CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id),
5507 Match, Next);
5508
5509 CGF.EmitBlock(Match);
5510 }
5511
5512 if (CatchBody) {
5513 llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end");
5514 llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler");
5515
5516 // Cleanups must call objc_end_catch.
5517 //
Mike Stumpf5408fe2009-05-16 07:57:57 +00005518 // FIXME: It seems incorrect for objc_begin_catch to be inside this
5519 // context, but this matches gcc.
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005520 CGF.PushCleanupBlock(MatchEnd);
5521 CGF.setInvokeDest(MatchHandler);
5522
5523 llvm::Value *ExcObject =
Chris Lattner8a569112009-04-22 02:15:23 +00005524 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005525
5526 // Bind the catch parameter if it exists.
5527 if (CatchParam) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005528 ExcObject =
5529 CGF.Builder.CreateBitCast(ExcObject,
5530 CGF.ConvertType(CatchParam->getType()));
5531 // CatchParam is a ParmVarDecl because of the grammar
5532 // construction used to handle this, but for codegen purposes
5533 // we treat this as a local decl.
5534 CGF.EmitLocalBlockVarDecl(*CatchParam);
5535 CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005536 }
5537
5538 CGF.ObjCEHValueStack.push_back(ExcObject);
5539 CGF.EmitStmt(CatchBody);
5540 CGF.ObjCEHValueStack.pop_back();
5541
5542 CGF.EmitBranchThroughCleanup(FinallyEnd);
5543
5544 CGF.EmitBlock(MatchHandler);
5545
5546 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5547 // We are required to emit this call to satisfy LLVM, even
5548 // though we don't use the result.
5549 llvm::SmallVector<llvm::Value*, 8> Args;
5550 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005551 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Owen Andersona1cf15f2009-07-14 23:10:40 +00005552 Args.push_back(VMContext.getConstantInt(llvm::Type::Int32Ty,
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005553 0));
5554 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5555 CGF.Builder.CreateStore(Exc, RethrowPtr);
5556 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5557
5558 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5559
5560 CGF.EmitBlock(MatchEnd);
5561
5562 // Unfortunately, we also have to generate another EH frame here
5563 // in case this throws.
5564 llvm::BasicBlock *MatchEndHandler =
5565 CGF.createBasicBlock("match.end.handler");
5566 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattner8a569112009-04-22 02:15:23 +00005567 CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005568 Cont, MatchEndHandler,
5569 Args.begin(), Args.begin());
5570
5571 CGF.EmitBlock(Cont);
5572 if (Info.SwitchBlock)
5573 CGF.EmitBlock(Info.SwitchBlock);
5574 if (Info.EndBlock)
5575 CGF.EmitBlock(Info.EndBlock);
5576
5577 CGF.EmitBlock(MatchEndHandler);
5578 Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5579 // We are required to emit this call to satisfy LLVM, even
5580 // though we don't use the result.
5581 Args.clear();
5582 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005583 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Owen Andersona1cf15f2009-07-14 23:10:40 +00005584 Args.push_back(VMContext.getConstantInt(llvm::Type::Int32Ty,
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005585 0));
5586 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5587 CGF.Builder.CreateStore(Exc, RethrowPtr);
5588 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5589
5590 if (Next)
5591 CGF.EmitBlock(Next);
5592 } else {
5593 assert(!Next && "catchup should be last handler.");
5594
5595 CGF.Builder.CreateStore(Exc, RethrowPtr);
5596 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5597 }
5598 }
5599
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005600 // Pop the cleanup entry, the @finally is outside this cleanup
5601 // scope.
5602 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5603 CGF.setInvokeDest(PrevLandingPad);
5604
5605 CGF.EmitBlock(FinallyBlock);
5606
5607 if (isTry) {
5608 if (const ObjCAtFinallyStmt* FinallyStmt =
5609 cast<ObjCAtTryStmt>(S).getFinallyStmt())
5610 CGF.EmitStmt(FinallyStmt->getFinallyBody());
5611 } else {
5612 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
5613 // @synchronized.
Chris Lattnerbbccd612009-04-22 02:38:11 +00005614 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005615 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005616
5617 if (Info.SwitchBlock)
5618 CGF.EmitBlock(Info.SwitchBlock);
5619 if (Info.EndBlock)
5620 CGF.EmitBlock(Info.EndBlock);
5621
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005622 // Branch around the rethrow code.
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005623 CGF.EmitBranch(FinallyEnd);
5624
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005625 CGF.EmitBlock(FinallyRethrow);
Chris Lattner8a569112009-04-22 02:15:23 +00005626 CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005627 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005628 CGF.Builder.CreateUnreachable();
5629
5630 CGF.EmitBlock(FinallyEnd);
5631}
5632
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005633/// EmitThrowStmt - Generate code for a throw statement.
5634void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
5635 const ObjCAtThrowStmt &S) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005636 llvm::Value *Exception;
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005637 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005638 Exception = CGF.EmitScalarExpr(ThrowExpr);
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005639 } else {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005640 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
5641 "Unexpected rethrow outside @catch block.");
5642 Exception = CGF.ObjCEHValueStack.back();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005643 }
5644
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005645 llvm::Value *ExceptionAsObject =
5646 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
5647 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
5648 if (InvokeDest) {
5649 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattnerbbccd612009-04-22 02:38:11 +00005650 CGF.Builder.CreateInvoke(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005651 Cont, InvokeDest,
5652 &ExceptionAsObject, &ExceptionAsObject + 1);
5653 CGF.EmitBlock(Cont);
5654 } else
Chris Lattnerbbccd612009-04-22 02:38:11 +00005655 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005656 CGF.Builder.CreateUnreachable();
5657
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005658 // Clear the insertion point to indicate we are in unreachable code.
5659 CGF.Builder.ClearInsertionPoint();
5660}
Daniel Dunbare588b992009-03-01 04:46:24 +00005661
5662llvm::Value *
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005663CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
5664 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00005665 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00005666
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005667 // If we don't need a definition, return the entry if found or check
5668 // if we use an external reference.
5669 if (!ForDefinition) {
5670 if (Entry)
5671 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00005672
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005673 // If this type (or a super class) has the __objc_exception__
5674 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00005675 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005676 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005677 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005678 llvm::GlobalValue::ExternalLinkage,
5679 0,
5680 (std::string("OBJC_EHTYPE_$_") +
Owen Anderson1c431b32009-07-08 19:05:04 +00005681 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005682 }
5683
5684 // Otherwise we need to either make a new entry or fill in the
5685 // initializer.
5686 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005687 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00005688 std::string VTableName = "objc_ehtype_vtable";
5689 llvm::GlobalVariable *VTableGV =
5690 CGM.getModule().getGlobalVariable(VTableName);
5691 if (!VTableGV)
Owen Anderson1c431b32009-07-08 19:05:04 +00005692 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
5693 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00005694 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00005695 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00005696
Owen Andersona1cf15f2009-07-14 23:10:40 +00005697 llvm::Value *VTableIdx = VMContext.getConstantInt(llvm::Type::Int32Ty, 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00005698
5699 std::vector<llvm::Constant*> Values(3);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005700 Values[0] = VMContext.getConstantExprGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbare588b992009-03-01 04:46:24 +00005701 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005702 Values[2] = GetClassGlobal(ClassName);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005703 llvm::Constant *Init =
5704 VMContext.getConstantStruct(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00005705
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005706 if (Entry) {
5707 Entry->setInitializer(Init);
5708 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00005709 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005710 llvm::GlobalValue::WeakAnyLinkage,
5711 Init,
5712 (std::string("OBJC_EHTYPE_$_") +
Owen Anderson1c431b32009-07-08 19:05:04 +00005713 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005714 }
5715
Daniel Dunbar04d40782009-04-14 06:00:08 +00005716 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005717 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005718 Entry->setAlignment(8);
5719
5720 if (ForDefinition) {
5721 Entry->setSection("__DATA,__objc_const");
5722 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
5723 } else {
5724 Entry->setSection("__DATA,__datacoal_nt,coalesced");
5725 }
Daniel Dunbare588b992009-03-01 04:46:24 +00005726
5727 return Entry;
5728}
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005729
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005730/* *** */
5731
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005732CodeGen::CGObjCRuntime *
5733CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00005734 return new CGObjCMac(CGM);
5735}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005736
5737CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005738CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005739 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005740}