blob: 4b4605ef76651a1d808aa660279c539416ceb54d [file] [log] [blame]
Daniel Dunbar8c85fac2008-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 Dunbar1be1df32008-08-11 21:35:06 +000015
16#include "CodeGenModule.h"
Daniel Dunbarace33292008-08-16 03:19:19 +000017#include "CodeGenFunction.h"
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000019#include "clang/AST/Decl.h"
Daniel Dunbarcffcdac2008-08-13 03:21:16 +000020#include "clang/AST/DeclObjC.h"
Anders Carlsson63f1ad92009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Chris Lattner4a9e9272009-04-26 01:32:48 +000022#include "clang/AST/StmtObjC.h"
Daniel Dunbar1be1df32008-08-11 21:35:06 +000023#include "clang/Basic/LangOptions.h"
24
Daniel Dunbar75de89f2009-02-24 07:47:38 +000025#include "llvm/Intrinsics.h"
Owen Anderson5f1adc22009-07-13 04:10:07 +000026#include "llvm/LLVMContext.h"
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000027#include "llvm/Module.h"
Daniel Dunbar35b777f2008-10-29 22:36:39 +000028#include "llvm/ADT/DenseSet.h"
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +000029#include "llvm/Target/TargetData.h"
Daniel Dunbarace33292008-08-16 03:19:19 +000030#include <sstream>
Daniel Dunbar8c85fac2008-08-11 02:45:11 +000031
32using namespace clang;
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +000033using namespace CodeGen;
Daniel Dunbar8c85fac2008-08-11 02:45:11 +000034
Daniel Dunbar85d37542009-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 Dunbar94d2ede2009-05-03 13:15:50 +000039/// FindIvarInterface - Find the interface containing the ivar.
Daniel Dunbarfb65bfb2009-04-22 12:00:04 +000040///
Daniel Dunbar94d2ede2009-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 Dunbar94d2ede2009-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 Jahanian02ebfa82009-05-12 18:14:29 +000051 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
Fariborz Jahanianb290be02009-06-04 01:19:09 +000052 Context.ShallowCollectObjCIvars(OID, Ivars);
Fariborz Jahanian02ebfa82009-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 Dunbar1af336e2009-04-22 17:43:55 +000057 }
Fariborz Jahanian02ebfa82009-05-12 18:14:29 +000058
Daniel Dunbar94d2ede2009-05-03 13:15:50 +000059 // Otherwise check in the super class.
Daniel Dunbar671e8a22009-05-05 00:36:57 +000060 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Daniel Dunbar94d2ede2009-05-03 13:15:50 +000061 return FindIvarInterface(Context, Super, OIVD, Index);
62
63 return 0;
Daniel Dunbarfb65bfb2009-04-22 12:00:04 +000064}
65
Daniel Dunbar35403f92009-05-03 08:55:17 +000066static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM,
67 const ObjCInterfaceDecl *OID,
Daniel Dunbared4d5962009-05-03 12:57:56 +000068 const ObjCImplementationDecl *ID,
Daniel Dunbar35403f92009-05-03 08:55:17 +000069 const ObjCIvarDecl *Ivar) {
Daniel Dunbar94d2ede2009-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 Dunbar35403f92009-05-03 08:55:17 +000083}
84
85uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
86 const ObjCInterfaceDecl *OID,
87 const ObjCIvarDecl *Ivar) {
Daniel Dunbared4d5962009-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 Dunbar85d37542009-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 Dunbar35403f92009-05-03 08:55:17 +0000103 // Compute (type*) ( (char *) BaseValue + Offset)
Owen Anderson73e7f802009-07-14 23:10:40 +0000104 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
105 llvm::Type *I8Ptr = VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty);
Daniel Dunbar35403f92009-05-03 08:55:17 +0000106 QualType IvarTy = Ivar->getType();
107 const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy);
Daniel Dunbar85d37542009-04-22 07:32:20 +0000108 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr);
Daniel Dunbar85d37542009-04-22 07:32:20 +0000109 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
Owen Anderson73e7f802009-07-14 23:10:40 +0000110 V = CGF.Builder.CreateBitCast(V, VMContext.getPointerTypeUnqual(LTy));
Daniel Dunbar85d37542009-04-22 07:32:20 +0000111
112 if (Ivar->isBitField()) {
Daniel Dunbared4d5962009-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 Dunbar35403f92009-05-03 08:55:17 +0000120 uint64_t BitFieldSize =
121 Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue();
122 return LValue::MakeBitfield(V, BitOffset, BitFieldSize,
Daniel Dunbar0e453312009-05-03 07:52:00 +0000123 IvarTy->isSignedIntegerType(),
124 IvarTy.getCVRQualifiers()|CVRQualifiers);
Daniel Dunbar85d37542009-04-22 07:32:20 +0000125 }
126
Daniel Dunbar35403f92009-05-03 08:55:17 +0000127 LValue LV = LValue::MakeAddr(V, IvarTy.getCVRQualifiers()|CVRQualifiers,
128 CGF.CGM.getContext().getObjCGCAttrKind(IvarTy));
Daniel Dunbar85d37542009-04-22 07:32:20 +0000129 LValue::SetObjCIvar(LV, true);
130 return LV;
131}
132
133///
134
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000135namespace {
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000136
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000137 typedef std::vector<llvm::Constant*> ConstantVector;
138
Mike Stumpba2cb0e2009-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 Dunbarcffcdac2008-08-13 03:21:16 +0000141
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000142class ObjCCommonTypesHelper {
Owen Anderson73e7f802009-07-14 23:10:40 +0000143protected:
144 llvm::LLVMContext &VMContext;
145
Fariborz Jahanian5c76fd32009-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 Anderson73e7f802009-07-14 23:10:40 +0000153 CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Fariborz Jahanian5c76fd32009-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 Anderson73e7f802009-07-14 23:10:40 +0000164 CGM.CreateRuntimeFunction(VMContext.getFunctionType(llvm::Type::VoidTy,
Fariborz Jahanian5c76fd32009-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 Anderson73e7f802009-07-14 23:10:40 +0000177 CGM.CreateRuntimeFunction(VMContext.getFunctionType(llvm::Type::DoubleTy,
Fariborz Jahanian5c76fd32009-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 Anderson73e7f802009-07-14 23:10:40 +0000190 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Fariborz Jahanian5c76fd32009-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 Anderson73e7f802009-07-14 23:10:40 +0000201 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Fariborz Jahanian5c76fd32009-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 Anderson73e7f802009-07-14 23:10:40 +0000213 return CGM.CreateRuntimeFunction(
214 VMContext.getFunctionType(llvm::Type::VoidTy,
Fariborz Jahanian5c76fd32009-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 Anderson73e7f802009-07-14 23:10:40 +0000226 return CGM.CreateRuntimeFunction(
227 VMContext.getFunctionType(llvm::Type::VoidTy,
Fariborz Jahanian5c76fd32009-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 Jahanian48543f52009-01-21 22:04:16 +0000242protected:
243 CodeGen::CodeGenModule &CGM;
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000244
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000245public:
Fariborz Jahanianad51ca02009-03-23 19:10:40 +0000246 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000247 const llvm::Type *Int8PtrTy;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000248
Daniel Dunbar6fa3daf2008-08-12 05:28:47 +0000249 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
250 const llvm::Type *ObjectPtrTy;
Fariborz Jahanianc192d4d2008-11-18 20:18:11 +0000251
252 /// PtrObjectPtrTy - LLVM type for id *
253 const llvm::Type *PtrObjectPtrTy;
254
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000255 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar6fa3daf2008-08-12 05:28:47 +0000256 const llvm::Type *SelectorPtrTy;
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000257 /// ProtocolPtrTy - LLVM type for external protocol handles
258 /// (typeof(Protocol))
259 const llvm::Type *ExternalProtocolPtrTy;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000260
Daniel Dunbar0ed60b02008-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 Jahanian48543f52009-01-21 22:04:16 +0000265
Daniel Dunbar15245e52008-08-23 04:28:29 +0000266 /// SuperTy - LLVM type for struct objc_super.
267 const llvm::StructType *SuperTy;
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000268 /// SuperPtrTy - LLVM type for struct objc_super *.
269 const llvm::Type *SuperPtrTy;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000270
Fariborz Jahaniand0374812009-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 Jahanian781f2732009-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 Lattnera7ecda42009-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 Jahanian4b161702009-01-22 00:37:21 +0000304
Chris Lattnera7ecda42009-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 Dunbar9dc58d22009-07-11 20:32:50 +0000324 CodeGen::CodeGenTypes &Types = CGM.getTypes();
325 ASTContext &Ctx = CGM.getContext();
Chris Lattnera7ecda42009-04-22 02:44:54 +0000326 // void objc_enumerationMutation (id)
Daniel Dunbar9dc58d22009-07-11 20:32:50 +0000327 llvm::SmallVector<QualType,16> Params;
Daniel Dunbar16ecb152009-07-24 07:40:24 +0000328 Params.push_back(Ctx.getObjCIdType());
Daniel Dunbar9dc58d22009-07-11 20:32:50 +0000329 const llvm::FunctionType *FTy =
330 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
Chris Lattnera7ecda42009-04-22 02:44:54 +0000331 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
332 }
Fariborz Jahanian4b161702009-01-22 00:37:21 +0000333
334 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattnera7ecda42009-04-22 02:44:54 +0000335 llvm::Constant *getGcReadWeakFn() {
336 // id objc_read_weak (id *)
337 std::vector<const llvm::Type*> Args;
338 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Anderson73e7f802009-07-14 23:10:40 +0000339 llvm::FunctionType *FTy =
340 VMContext.getFunctionType(ObjectPtrTy, Args, false);
Chris Lattnera7ecda42009-04-22 02:44:54 +0000341 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
342 }
Fariborz Jahanian4b161702009-01-22 00:37:21 +0000343
344 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner293c1d32009-04-17 22:12:36 +0000345 llvm::Constant *getGcAssignWeakFn() {
346 // id objc_assign_weak (id, id *)
347 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
348 Args.push_back(ObjectPtrTy->getPointerTo());
349 llvm::FunctionType *FTy =
Owen Anderson73e7f802009-07-14 23:10:40 +0000350 VMContext.getFunctionType(ObjectPtrTy, Args, false);
Chris Lattner293c1d32009-04-17 22:12:36 +0000351 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
352 }
Fariborz Jahanian4b161702009-01-22 00:37:21 +0000353
354 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerf6ec7e42009-04-22 02:38:11 +0000355 llvm::Constant *getGcAssignGlobalFn() {
356 // id objc_assign_global(id, id *)
357 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
358 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Anderson73e7f802009-07-14 23:10:40 +0000359 llvm::FunctionType *FTy =
360 VMContext.getFunctionType(ObjectPtrTy, Args, false);
Chris Lattnerf6ec7e42009-04-22 02:38:11 +0000361 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
362 }
Fariborz Jahanian4b161702009-01-22 00:37:21 +0000363
364 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerf6ec7e42009-04-22 02:38:11 +0000365 llvm::Constant *getGcAssignIvarFn() {
366 // id objc_assign_ivar(id, id *)
367 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
368 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Anderson73e7f802009-07-14 23:10:40 +0000369 llvm::FunctionType *FTy =
370 VMContext.getFunctionType(ObjectPtrTy, Args, false);
Chris Lattnerf6ec7e42009-04-22 02:38:11 +0000371 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
372 }
Fariborz Jahanian4b161702009-01-22 00:37:21 +0000373
Fariborz Jahanian614e8f02009-07-08 01:18:33 +0000374 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
375 llvm::Constant *GcMemmoveCollectableFn() {
376 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
377 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
378 Args.push_back(Int8PtrTy);
379 Args.push_back(LongTy);
Owen Anderson73e7f802009-07-14 23:10:40 +0000380 llvm::FunctionType *FTy = VMContext.getFunctionType(Int8PtrTy, Args, false);
Fariborz Jahanian614e8f02009-07-08 01:18:33 +0000381 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
382 }
383
Fariborz Jahanian4b161702009-01-22 00:37:21 +0000384 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerf6ec7e42009-04-22 02:38:11 +0000385 llvm::Constant *getGcAssignStrongCastFn() {
386 // id objc_assign_global(id, id *)
387 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
388 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Anderson73e7f802009-07-14 23:10:40 +0000389 llvm::FunctionType *FTy =
390 VMContext.getFunctionType(ObjectPtrTy, Args, false);
Chris Lattnerf6ec7e42009-04-22 02:38:11 +0000391 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
392 }
Anders Carlsson1cf75362009-02-16 22:59:18 +0000393
394 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerf6ec7e42009-04-22 02:38:11 +0000395 llvm::Constant *getExceptionThrowFn() {
396 // void objc_exception_throw(id)
397 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
398 llvm::FunctionType *FTy =
Owen Anderson73e7f802009-07-14 23:10:40 +0000399 VMContext.getFunctionType(llvm::Type::VoidTy, Args, false);
Chris Lattnerf6ec7e42009-04-22 02:38:11 +0000400 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
401 }
Anders Carlsson1cf75362009-02-16 22:59:18 +0000402
Daniel Dunbar34416d62009-02-24 01:43:46 +0000403 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattner23e24652009-04-06 16:53:45 +0000404 llvm::Constant *getSyncEnterFn() {
405 // void objc_sync_enter (id)
406 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
407 llvm::FunctionType *FTy =
Owen Anderson73e7f802009-07-14 23:10:40 +0000408 VMContext.getFunctionType(llvm::Type::VoidTy, Args, false);
Chris Lattner23e24652009-04-06 16:53:45 +0000409 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
410 }
Daniel Dunbar34416d62009-02-24 01:43:46 +0000411
412 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerf6ec7e42009-04-22 02:38:11 +0000413 llvm::Constant *getSyncExitFn() {
414 // void objc_sync_exit (id)
415 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
416 llvm::FunctionType *FTy =
Owen Anderson73e7f802009-07-14 23:10:40 +0000417 VMContext.getFunctionType(llvm::Type::VoidTy, Args, false);
Chris Lattnerf6ec7e42009-04-22 02:38:11 +0000418 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
419 }
Daniel Dunbar34416d62009-02-24 01:43:46 +0000420
Fariborz Jahanian5c76fd32009-05-11 19:25:47 +0000421 llvm::Constant *getSendFn(bool IsSuper) const {
422 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
423 }
424
425 llvm::Constant *getSendFn2(bool IsSuper) const {
426 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
427 }
428
429 llvm::Constant *getSendStretFn(bool IsSuper) const {
430 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
431 }
432
433 llvm::Constant *getSendStretFn2(bool IsSuper) const {
434 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
435 }
436
437 llvm::Constant *getSendFpretFn(bool IsSuper) const {
438 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
439 }
440
441 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
442 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
443 }
444
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000445 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
446 ~ObjCCommonTypesHelper(){}
447};
Daniel Dunbar15245e52008-08-23 04:28:29 +0000448
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000449/// ObjCTypesHelper - Helper class that encapsulates lazy
450/// construction of varies types used during ObjC generation.
451class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000452public:
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000453 /// SymtabTy - LLVM type for struct objc_symtab.
454 const llvm::StructType *SymtabTy;
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000455 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
456 const llvm::Type *SymtabPtrTy;
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000457 /// ModuleTy - LLVM type for struct objc_module.
458 const llvm::StructType *ModuleTy;
Daniel Dunbar5eec6142008-08-12 03:39:23 +0000459
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000460 /// ProtocolTy - LLVM type for struct objc_protocol.
461 const llvm::StructType *ProtocolTy;
462 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
463 const llvm::Type *ProtocolPtrTy;
464 /// ProtocolExtensionTy - LLVM type for struct
465 /// objc_protocol_extension.
466 const llvm::StructType *ProtocolExtensionTy;
467 /// ProtocolExtensionTy - LLVM type for struct
468 /// objc_protocol_extension *.
469 const llvm::Type *ProtocolExtensionPtrTy;
470 /// MethodDescriptionTy - LLVM type for struct
471 /// objc_method_description.
472 const llvm::StructType *MethodDescriptionTy;
473 /// MethodDescriptionListTy - LLVM type for struct
474 /// objc_method_description_list.
475 const llvm::StructType *MethodDescriptionListTy;
476 /// MethodDescriptionListPtrTy - LLVM type for struct
477 /// objc_method_description_list *.
478 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000479 /// ProtocolListTy - LLVM type for struct objc_property_list.
480 const llvm::Type *ProtocolListTy;
481 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
482 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar4246a8b2008-08-22 20:34:54 +0000483 /// CategoryTy - LLVM type for struct objc_category.
484 const llvm::StructType *CategoryTy;
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000485 /// ClassTy - LLVM type for struct objc_class.
486 const llvm::StructType *ClassTy;
487 /// ClassPtrTy - LLVM type for struct objc_class *.
488 const llvm::Type *ClassPtrTy;
489 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
490 const llvm::StructType *ClassExtensionTy;
491 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
492 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000493 // IvarTy - LLVM type for struct objc_ivar.
494 const llvm::StructType *IvarTy;
495 /// IvarListTy - LLVM type for struct objc_ivar_list.
496 const llvm::Type *IvarListTy;
497 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
498 const llvm::Type *IvarListPtrTy;
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000499 /// MethodListTy - LLVM type for struct objc_method_list.
500 const llvm::Type *MethodListTy;
501 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
502 const llvm::Type *MethodListPtrTy;
Anders Carlsson9acb0a42008-09-09 10:10:21 +0000503
504 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
505 const llvm::Type *ExceptionDataTy;
506
Anders Carlsson9acb0a42008-09-09 10:10:21 +0000507 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattnere05d4cb2009-04-22 02:26:14 +0000508 llvm::Constant *getExceptionTryEnterFn() {
509 std::vector<const llvm::Type*> Params;
Owen Anderson73e7f802009-07-14 23:10:40 +0000510 Params.push_back(VMContext.getPointerTypeUnqual(ExceptionDataTy));
511 return CGM.CreateRuntimeFunction(
512 VMContext.getFunctionType(llvm::Type::VoidTy,
Chris Lattnere05d4cb2009-04-22 02:26:14 +0000513 Params, false),
514 "objc_exception_try_enter");
515 }
Anders Carlsson9acb0a42008-09-09 10:10:21 +0000516
517 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattnere05d4cb2009-04-22 02:26:14 +0000518 llvm::Constant *getExceptionTryExitFn() {
519 std::vector<const llvm::Type*> Params;
Owen Anderson73e7f802009-07-14 23:10:40 +0000520 Params.push_back(VMContext.getPointerTypeUnqual(ExceptionDataTy));
521 return CGM.CreateRuntimeFunction(
522 VMContext.getFunctionType(llvm::Type::VoidTy,
Chris Lattnere05d4cb2009-04-22 02:26:14 +0000523 Params, false),
524 "objc_exception_try_exit");
525 }
Anders Carlsson9acb0a42008-09-09 10:10:21 +0000526
527 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattnere05d4cb2009-04-22 02:26:14 +0000528 llvm::Constant *getExceptionExtractFn() {
529 std::vector<const llvm::Type*> Params;
Owen Anderson73e7f802009-07-14 23:10:40 +0000530 Params.push_back(VMContext.getPointerTypeUnqual(ExceptionDataTy));
531 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Chris Lattnere05d4cb2009-04-22 02:26:14 +0000532 Params, false),
533 "objc_exception_extract");
534
535 }
Anders Carlsson9acb0a42008-09-09 10:10:21 +0000536
537 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattnere05d4cb2009-04-22 02:26:14 +0000538 llvm::Constant *getExceptionMatchFn() {
539 std::vector<const llvm::Type*> Params;
540 Params.push_back(ClassPtrTy);
541 Params.push_back(ObjectPtrTy);
Owen Anderson73e7f802009-07-14 23:10:40 +0000542 return CGM.CreateRuntimeFunction(
543 VMContext.getFunctionType(llvm::Type::Int32Ty,
Chris Lattnere05d4cb2009-04-22 02:26:14 +0000544 Params, false),
545 "objc_exception_match");
546
547 }
Anders Carlsson9acb0a42008-09-09 10:10:21 +0000548
549 /// SetJmpFn - LLVM _setjmp function.
Chris Lattnere05d4cb2009-04-22 02:26:14 +0000550 llvm::Constant *getSetJmpFn() {
551 std::vector<const llvm::Type*> Params;
Owen Anderson73e7f802009-07-14 23:10:40 +0000552 Params.push_back(VMContext.getPointerTypeUnqual(llvm::Type::Int32Ty));
Chris Lattnere05d4cb2009-04-22 02:26:14 +0000553 return
Owen Anderson73e7f802009-07-14 23:10:40 +0000554 CGM.CreateRuntimeFunction(VMContext.getFunctionType(llvm::Type::Int32Ty,
Chris Lattnere05d4cb2009-04-22 02:26:14 +0000555 Params, false),
556 "_setjmp");
557
558 }
Chris Lattnerdd978702008-11-15 21:26:17 +0000559
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000560public:
561 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000562 ~ObjCTypesHelper() {}
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000563};
564
Fariborz Jahaniand0374812009-01-22 23:02:58 +0000565/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000566/// modern abi
Fariborz Jahaniand0374812009-01-22 23:02:58 +0000567class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianf52110f2009-02-04 20:42:28 +0000568public:
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +0000569
Fariborz Jahanian781f2732009-01-23 01:46:23 +0000570 // MethodListnfABITy - LLVM for struct _method_list_t
571 const llvm::StructType *MethodListnfABITy;
572
573 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
574 const llvm::Type *MethodListnfABIPtrTy;
575
576 // ProtocolnfABITy = LLVM for struct _protocol_t
577 const llvm::StructType *ProtocolnfABITy;
578
Daniel Dunbar1f42bb02009-02-15 07:36:20 +0000579 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
580 const llvm::Type *ProtocolnfABIPtrTy;
581
Fariborz Jahanian781f2732009-01-23 01:46:23 +0000582 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
583 const llvm::StructType *ProtocolListnfABITy;
584
585 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
586 const llvm::Type *ProtocolListnfABIPtrTy;
587
588 // ClassnfABITy - LLVM for struct _class_t
589 const llvm::StructType *ClassnfABITy;
590
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000591 // ClassnfABIPtrTy - LLVM for struct _class_t*
592 const llvm::Type *ClassnfABIPtrTy;
593
Fariborz Jahanian781f2732009-01-23 01:46:23 +0000594 // IvarnfABITy - LLVM for struct _ivar_t
595 const llvm::StructType *IvarnfABITy;
596
597 // IvarListnfABITy - LLVM for struct _ivar_list_t
598 const llvm::StructType *IvarListnfABITy;
599
600 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
601 const llvm::Type *IvarListnfABIPtrTy;
602
603 // ClassRonfABITy - LLVM for struct _class_ro_t
604 const llvm::StructType *ClassRonfABITy;
605
606 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
607 const llvm::Type *ImpnfABITy;
608
609 // CategorynfABITy - LLVM for struct _category_t
610 const llvm::StructType *CategorynfABITy;
611
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +0000612 // New types for nonfragile abi messaging.
613
614 // MessageRefTy - LLVM for:
615 // struct _message_ref_t {
616 // IMP messenger;
617 // SEL name;
618 // };
619 const llvm::StructType *MessageRefTy;
Fariborz Jahanianf52110f2009-02-04 20:42:28 +0000620 // MessageRefCTy - clang type for struct _message_ref_t
621 QualType MessageRefCTy;
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +0000622
623 // MessageRefPtrTy - LLVM for struct _message_ref_t*
624 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanianf52110f2009-02-04 20:42:28 +0000625 // MessageRefCPtrTy - clang type for struct _message_ref_t*
626 QualType MessageRefCPtrTy;
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +0000627
Fariborz Jahanian10d69ea2009-02-05 01:13:09 +0000628 // MessengerTy - Type of the messenger (shown as IMP above)
629 const llvm::FunctionType *MessengerTy;
630
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +0000631 // SuperMessageRefTy - LLVM for:
632 // struct _super_message_ref_t {
633 // SUPER_IMP messenger;
634 // SEL name;
635 // };
636 const llvm::StructType *SuperMessageRefTy;
637
638 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
639 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar75de89f2009-02-24 07:47:38 +0000640
Chris Lattnerada416b2009-04-22 02:53:24 +0000641 llvm::Constant *getMessageSendFixupFn() {
642 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
643 std::vector<const llvm::Type*> Params;
644 Params.push_back(ObjectPtrTy);
645 Params.push_back(MessageRefPtrTy);
Owen Anderson73e7f802009-07-14 23:10:40 +0000646 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Chris Lattnerada416b2009-04-22 02:53:24 +0000647 Params, true),
648 "objc_msgSend_fixup");
649 }
650
651 llvm::Constant *getMessageSendFpretFixupFn() {
652 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
653 std::vector<const llvm::Type*> Params;
654 Params.push_back(ObjectPtrTy);
655 Params.push_back(MessageRefPtrTy);
Owen Anderson73e7f802009-07-14 23:10:40 +0000656 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Chris Lattnerada416b2009-04-22 02:53:24 +0000657 Params, true),
658 "objc_msgSend_fpret_fixup");
659 }
660
661 llvm::Constant *getMessageSendStretFixupFn() {
662 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
663 std::vector<const llvm::Type*> Params;
664 Params.push_back(ObjectPtrTy);
665 Params.push_back(MessageRefPtrTy);
Owen Anderson73e7f802009-07-14 23:10:40 +0000666 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Chris Lattnerada416b2009-04-22 02:53:24 +0000667 Params, true),
668 "objc_msgSend_stret_fixup");
669 }
670
671 llvm::Constant *getMessageSendIdFixupFn() {
672 // id objc_msgSendId_fixup(id, struct message_ref_t*, ...)
673 std::vector<const llvm::Type*> Params;
674 Params.push_back(ObjectPtrTy);
675 Params.push_back(MessageRefPtrTy);
Owen Anderson73e7f802009-07-14 23:10:40 +0000676 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Chris Lattnerada416b2009-04-22 02:53:24 +0000677 Params, true),
678 "objc_msgSendId_fixup");
679 }
680
681 llvm::Constant *getMessageSendIdStretFixupFn() {
682 // id objc_msgSendId_stret_fixup(id, struct message_ref_t*, ...)
683 std::vector<const llvm::Type*> Params;
684 Params.push_back(ObjectPtrTy);
685 Params.push_back(MessageRefPtrTy);
Owen Anderson73e7f802009-07-14 23:10:40 +0000686 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Chris Lattnerada416b2009-04-22 02:53:24 +0000687 Params, true),
688 "objc_msgSendId_stret_fixup");
689 }
690 llvm::Constant *getMessageSendSuper2FixupFn() {
691 // id objc_msgSendSuper2_fixup (struct objc_super *,
692 // struct _super_message_ref_t*, ...)
693 std::vector<const llvm::Type*> Params;
694 Params.push_back(SuperPtrTy);
695 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson73e7f802009-07-14 23:10:40 +0000696 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Chris Lattnerada416b2009-04-22 02:53:24 +0000697 Params, true),
698 "objc_msgSendSuper2_fixup");
699 }
700
701 llvm::Constant *getMessageSendSuper2StretFixupFn() {
702 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
703 // struct _super_message_ref_t*, ...)
704 std::vector<const llvm::Type*> Params;
705 Params.push_back(SuperPtrTy);
706 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson73e7f802009-07-14 23:10:40 +0000707 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy,
Chris Lattnerada416b2009-04-22 02:53:24 +0000708 Params, true),
709 "objc_msgSendSuper2_stret_fixup");
710 }
711
712
713
Daniel Dunbar75de89f2009-02-24 07:47:38 +0000714 /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C
715 /// exception personality function.
Chris Lattner23e24652009-04-06 16:53:45 +0000716 llvm::Value *getEHPersonalityPtr() {
717 llvm::Constant *Personality =
Owen Anderson73e7f802009-07-14 23:10:40 +0000718 CGM.CreateRuntimeFunction(VMContext.getFunctionType(llvm::Type::Int32Ty,
Chris Lattner23e24652009-04-06 16:53:45 +0000719 true),
720 "__objc_personality_v0");
Owen Anderson73e7f802009-07-14 23:10:40 +0000721 return VMContext.getConstantExprBitCast(Personality, Int8PtrTy);
Chris Lattner23e24652009-04-06 16:53:45 +0000722 }
Daniel Dunbar75de89f2009-02-24 07:47:38 +0000723
Chris Lattner93dca5b2009-04-22 02:15:23 +0000724 llvm::Constant *getUnwindResumeOrRethrowFn() {
725 std::vector<const llvm::Type*> Params;
726 Params.push_back(Int8PtrTy);
Owen Anderson73e7f802009-07-14 23:10:40 +0000727 return CGM.CreateRuntimeFunction(
728 VMContext.getFunctionType(llvm::Type::VoidTy,
Chris Lattner93dca5b2009-04-22 02:15:23 +0000729 Params, false),
730 "_Unwind_Resume_or_Rethrow");
731 }
732
733 llvm::Constant *getObjCEndCatchFn() {
Owen Anderson73e7f802009-07-14 23:10:40 +0000734 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(llvm::Type::VoidTy,
Chris Lattner89a7b0a2009-07-01 04:13:52 +0000735 false),
Chris Lattner93dca5b2009-04-22 02:15:23 +0000736 "objc_end_catch");
737
738 }
739
740 llvm::Constant *getObjCBeginCatchFn() {
741 std::vector<const llvm::Type*> Params;
742 Params.push_back(Int8PtrTy);
Owen Anderson73e7f802009-07-14 23:10:40 +0000743 return CGM.CreateRuntimeFunction(VMContext.getFunctionType(Int8PtrTy,
Chris Lattner93dca5b2009-04-22 02:15:23 +0000744 Params, false),
745 "objc_begin_catch");
746 }
Daniel Dunbar9c285e72009-03-01 04:46:24 +0000747
748 const llvm::StructType *EHTypeTy;
749 const llvm::Type *EHTypePtrTy;
Daniel Dunbar75de89f2009-02-24 07:47:38 +0000750
Fariborz Jahaniand0374812009-01-22 23:02:58 +0000751 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
752 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000753};
754
755class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +0000756public:
757 // FIXME - accessibility
Fariborz Jahanian37931062009-03-10 16:22:08 +0000758 class GC_IVAR {
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +0000759 public:
Daniel Dunbar59df30d2009-05-03 13:44:42 +0000760 unsigned ivar_bytepos;
761 unsigned ivar_size;
762 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
763 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar48445182009-04-23 01:29:05 +0000764
765 // Allow sorting based on byte pos.
766 bool operator<(const GC_IVAR &b) const {
767 return ivar_bytepos < b.ivar_bytepos;
768 }
Fariborz Jahanian37931062009-03-10 16:22:08 +0000769 };
770
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +0000771 class SKIP_SCAN {
Daniel Dunbar59df30d2009-05-03 13:44:42 +0000772 public:
773 unsigned skip;
774 unsigned scan;
775 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
776 : skip(_skip), scan(_scan) {}
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +0000777 };
778
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000779protected:
780 CodeGen::CodeGenModule &CGM;
Owen Anderson5f1adc22009-07-13 04:10:07 +0000781 llvm::LLVMContext &VMContext;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000782 // FIXME! May not be needing this after all.
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000783 unsigned ObjCABI;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000784
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +0000785 // gc ivar layout bitmap calculation helper caches.
786 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
787 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Fariborz Jahanian37931062009-03-10 16:22:08 +0000788
Daniel Dunbar8ede0052008-08-25 06:02:07 +0000789 /// LazySymbols - Symbols to generate a lazy reference for. See
790 /// DefinedSymbols and FinishModule().
791 std::set<IdentifierInfo*> LazySymbols;
792
793 /// DefinedSymbols - External symbols which are defined by this
794 /// module. The symbols in this list and LazySymbols are used to add
795 /// special linker symbols which ensure that Objective-C modules are
796 /// linked properly.
797 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000798
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000799 /// ClassNames - uniqued class names.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000800 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000801
Daniel Dunbar5eec6142008-08-12 03:39:23 +0000802 /// MethodVarNames - uniqued method variable names.
803 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000804
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000805 /// MethodVarTypes - uniqued method type signatures. We have to use
806 /// a StringMap here because have no other unique reference.
807 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000808
Daniel Dunbar12996f52008-08-26 21:51:14 +0000809 /// MethodDefinitions - map of methods which have been defined in
810 /// this translation unit.
811 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000812
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000813 /// PropertyNames - uniqued method variable names.
814 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000815
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000816 /// ClassReferences - uniqued class references.
817 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000818
Daniel Dunbar5eec6142008-08-12 03:39:23 +0000819 /// SelectorReferences - uniqued selector references.
820 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000821
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000822 /// Protocols - Protocols for which an objc_protocol structure has
823 /// been emitted. Forward declarations are handled by creating an
824 /// empty structure whose initializer is filled in when/if defined.
825 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000826
Daniel Dunbar35b777f2008-10-29 22:36:39 +0000827 /// DefinedProtocols - Protocols which have actually been
828 /// defined. We should not need this, see FIXME in GenerateProtocol.
829 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000830
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000831 /// DefinedClasses - List of defined classes.
832 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar1dd092b2009-05-15 22:33:15 +0000833
834 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
835 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000836
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000837 /// DefinedCategories - List of defined categories.
838 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000839
Daniel Dunbar1dd092b2009-05-15 22:33:15 +0000840 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
841 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
842
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +0000843 /// GetNameForMethod - Return a name for the given method.
844 /// \param[out] NameOut - The return value.
845 void GetNameForMethod(const ObjCMethodDecl *OMD,
846 const ObjCContainerDecl *CD,
847 std::string &NameOut);
848
849 /// GetMethodVarName - Return a unique constant for the given
850 /// selector's name. The return value has type char *.
851 llvm::Constant *GetMethodVarName(Selector Sel);
852 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
853 llvm::Constant *GetMethodVarName(const std::string &Name);
854
855 /// GetMethodVarType - Return a unique constant for the given
856 /// selector's name. The return value has type char *.
857
858 // FIXME: This is a horrible name.
859 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar356f0742009-04-20 06:54:31 +0000860 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +0000861
862 /// GetPropertyName - Return a unique constant for the given
863 /// name. The return value has type char *.
864 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
865
866 // FIXME: This can be dropped once string functions are unified.
867 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
868 const Decl *Container);
869
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +0000870 /// GetClassName - Return a unique constant for the given selector's
871 /// name. The return value has type char *.
872 llvm::Constant *GetClassName(IdentifierInfo *Ident);
873
Fariborz Jahanian01b3e342009-03-05 22:39:55 +0000874 /// BuildIvarLayout - Builds ivar layout bitmap for the class
875 /// implementation for the __strong or __weak case.
876 ///
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +0000877 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
878 bool ForStrongLayout);
Fariborz Jahanian01b3e342009-03-05 22:39:55 +0000879
Daniel Dunbar7b1da722009-05-03 14:10:34 +0000880 void BuildAggrIvarRecordLayout(const RecordType *RT,
881 unsigned int BytePos, bool ForStrongLayout,
882 bool &HasUnion);
Daniel Dunbarb1d3b8e2009-05-03 21:05:10 +0000883 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +0000884 const llvm::StructLayout *Layout,
Fariborz Jahanian37931062009-03-10 16:22:08 +0000885 const RecordDecl *RD,
Chris Lattner9329cf52009-03-31 08:48:01 +0000886 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahanian01b3e342009-03-05 22:39:55 +0000887 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian06facb72009-04-24 16:17:09 +0000888 bool &HasUnion);
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +0000889
Fariborz Jahanian7345eba2009-03-05 19:17:31 +0000890 /// GetIvarLayoutName - Returns a unique constant for the given
891 /// ivar layout bitmap.
892 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
893 const ObjCCommonTypesHelper &ObjCTypes);
894
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +0000895 /// EmitPropertyList - Emit the given property list. The return
896 /// value has type PropertyListPtrTy.
897 llvm::Constant *EmitPropertyList(const std::string &Name,
898 const Decl *Container,
899 const ObjCContainerDecl *OCD,
900 const ObjCCommonTypesHelper &ObjCTypes);
901
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +0000902 /// GetProtocolRef - Return a reference to the internal protocol
903 /// description, creating an empty one if it has not been
904 /// defined. The return value has type ProtocolPtrTy.
905 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahaniand65949b2009-03-08 20:18:37 +0000906
Daniel Dunbarc4594f22009-03-09 20:09:19 +0000907 /// CreateMetadataVar - Create a global variable with internal
908 /// linkage for use by the Objective-C runtime.
909 ///
910 /// This is a convenience wrapper which not only creates the
911 /// variable, but also sets the section and alignment and adds the
Chris Lattner5f38e5d2009-07-17 23:57:13 +0000912 /// global to the "llvm.used" list.
Daniel Dunbareddddd22009-03-09 20:50:13 +0000913 ///
914 /// \param Name - The variable name.
915 /// \param Init - The variable initializer; this is also used to
916 /// define the type of the variable.
917 /// \param Section - The section the variable should go into, or 0.
918 /// \param Align - The alignment for the variable, or 0.
919 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbar6b343692009-04-14 17:42:51 +0000920 /// "llvm.used".
Daniel Dunbarc4594f22009-03-09 20:09:19 +0000921 llvm::GlobalVariable *CreateMetadataVar(const std::string &Name,
922 llvm::Constant *Init,
923 const char *Section,
Daniel Dunbareddddd22009-03-09 20:50:13 +0000924 unsigned Align,
925 bool AddToUsed);
Daniel Dunbarc4594f22009-03-09 20:09:19 +0000926
Fariborz Jahanian5c76fd32009-05-11 19:25:47 +0000927 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
928 QualType ResultType,
929 llvm::Value *Sel,
930 llvm::Value *Arg0,
931 QualType Arg0Ty,
932 bool IsSuper,
933 const CallArgList &CallArgs,
934 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar356f0742009-04-20 06:54:31 +0000935
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000936public:
Owen Anderson5f1adc22009-07-13 04:10:07 +0000937 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
938 CGM(cgm), VMContext(cgm.getLLVMContext())
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000939 { }
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000940
Steve Naroff2c8a08e2009-03-31 16:53:37 +0000941 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL);
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +0000942
943 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
944 const ObjCContainerDecl *CD=0);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +0000945
946 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
947
948 /// GetOrEmitProtocol - Get the protocol object for the given
949 /// declaration, emitting it if necessary. The return value has type
950 /// ProtocolPtrTy.
951 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
952
953 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
954 /// object for the given declaration, emitting it if needed. These
955 /// forward references will be filled in with empty bodies if no
956 /// definition is seen. The return value has type ProtocolPtrTy.
957 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000958};
959
960class CGObjCMac : public CGObjCCommonMac {
961private:
962 ObjCTypesHelper ObjCTypes;
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000963 /// EmitImageInfo - Emit the image info marker used to encode some module
964 /// level information.
965 void EmitImageInfo();
966
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000967 /// EmitModuleInfo - Another marker encoding module level
968 /// information.
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000969 void EmitModuleInfo();
970
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000971 /// EmitModuleSymols - Emit module symbols, the list of defined
972 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000973 llvm::Constant *EmitModuleSymbols();
974
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000975 /// FinishModule - Write out global data structures at the end of
976 /// processing a translation unit.
977 void FinishModule();
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000978
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000979 /// EmitClassExtension - Generate the class extension structure used
980 /// to store the weak ivar layout and properties. The return value
981 /// has type ClassExtensionPtrTy.
982 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
983
984 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
985 /// for the given class.
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000986 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000987 const ObjCInterfaceDecl *ID);
988
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000989 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000990 QualType ResultType,
991 Selector Sel,
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000992 llvm::Value *Arg0,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000993 QualType Arg0Ty,
994 bool IsSuper,
995 const CallArgList &CallArgs);
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000996
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000997 /// EmitIvarList - Emit the ivar list for the given
998 /// implementation. If ForClass is true the list of class ivars
999 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1000 /// interface ivars will be emitted. The return value has type
1001 /// IvarListPtrTy.
1002 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00001003 bool ForClass);
1004
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001005 /// EmitMetaClass - Emit a forward reference to the class structure
1006 /// for the metaclass of the given interface. The return value has
1007 /// type ClassPtrTy.
1008 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1009
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001010 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001011 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001012 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1013 llvm::Constant *Protocols,
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001014 const ConstantVector &Methods);
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00001015
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001016 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00001017
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001018 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001019
1020 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar6b57d432008-08-26 08:29:31 +00001021 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001022 llvm::Constant *EmitMethodList(const std::string &Name,
1023 const char *Section,
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001024 const ConstantVector &Methods);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001025
1026 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001027 /// method declarations.
1028 /// - TypeName: The name for the type containing the methods.
1029 /// - IsProtocol: True iff these methods are for a protocol.
1030 /// - ClassMethds: True iff these are class methods.
1031 /// - Required: When true, only "required" methods are
1032 /// listed. Similarly, when false only "optional" methods are
1033 /// listed. For classes this should always be true.
1034 /// - begin, end: The method list to output.
1035 ///
1036 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001037 llvm::Constant *EmitMethodDescList(const std::string &Name,
1038 const char *Section,
1039 const ConstantVector &Methods);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001040
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001041 /// GetOrEmitProtocol - Get the protocol object for the given
1042 /// declaration, emitting it if necessary. The return value has type
1043 /// ProtocolPtrTy.
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00001044 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001045
1046 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1047 /// object for the given declaration, emitting it if needed. These
1048 /// forward references will be filled in with empty bodies if no
1049 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00001050 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001051
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001052 /// EmitProtocolExtension - Generate the protocol extension
1053 /// structure used to store optional instance and class methods, and
1054 /// protocol properties. The return value has type
1055 /// ProtocolExtensionPtrTy.
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001056 llvm::Constant *
1057 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1058 const ConstantVector &OptInstanceMethods,
1059 const ConstantVector &OptClassMethods);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001060
1061 /// EmitProtocolList - Generate the list of referenced
1062 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar67e778b2008-08-21 21:57:41 +00001063 llvm::Constant *EmitProtocolList(const std::string &Name,
1064 ObjCProtocolDecl::protocol_iterator begin,
1065 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001066
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001067 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1068 /// for the given selector.
Daniel Dunbard916e6e2008-11-01 01:53:16 +00001069 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00001070
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00001071 public:
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001072 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001073
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001074 virtual llvm::Function *ModuleInitFunction();
1075
Daniel Dunbara04840b2008-08-23 03:46:30 +00001076 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +00001077 QualType ResultType,
1078 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001079 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +00001080 bool IsClassMessage,
Fariborz Jahanianc8007152009-05-05 21:36:57 +00001081 const CallArgList &CallArgs,
1082 const ObjCMethodDecl *Method);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001083
Daniel Dunbara04840b2008-08-23 03:46:30 +00001084 virtual CodeGen::RValue
1085 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +00001086 QualType ResultType,
1087 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001088 const ObjCInterfaceDecl *Class,
Fariborz Jahanian17636fa2009-02-28 20:07:56 +00001089 bool isCategoryImpl,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001090 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +00001091 bool IsClassMessage,
1092 const CallArgList &CallArgs);
Daniel Dunbar434627a2008-08-16 00:25:02 +00001093
Daniel Dunbard916e6e2008-11-01 01:53:16 +00001094 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001095 const ObjCInterfaceDecl *ID);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001096
Daniel Dunbard916e6e2008-11-01 01:53:16 +00001097 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanianc8007152009-05-05 21:36:57 +00001098
1099 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1100 /// untyped one.
1101 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1102 const ObjCMethodDecl *Method);
1103
Daniel Dunbarac93e472008-08-15 22:20:32 +00001104 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001105
Daniel Dunbarac93e472008-08-15 22:20:32 +00001106 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001107
Daniel Dunbard916e6e2008-11-01 01:53:16 +00001108 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar84bb85f2008-08-13 00:59:25 +00001109 const ObjCProtocolDecl *PD);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00001110
Chris Lattneraea1aee2009-03-22 21:03:39 +00001111 virtual llvm::Constant *GetPropertyGetFunction();
1112 virtual llvm::Constant *GetPropertySetFunction();
1113 virtual llvm::Constant *EnumerationMutationFunction();
Anders Carlssonb01a2112008-09-09 10:04:29 +00001114
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00001115 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1116 const Stmt &S);
Anders Carlssonb01a2112008-09-09 10:04:29 +00001117 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1118 const ObjCAtThrowStmt &S);
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00001119 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian3305ad32008-11-18 21:45:40 +00001120 llvm::Value *AddrWeakObj);
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00001121 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1122 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian17958902008-11-19 00:59:10 +00001123 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1124 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianf310b592008-11-20 19:23:36 +00001125 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1126 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian17958902008-11-19 00:59:10 +00001127 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1128 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian614e8f02009-07-08 01:18:33 +00001129 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1130 llvm::Value *dest, llvm::Value *src,
1131 unsigned long size);
Fariborz Jahanian4337afe2009-02-02 20:02:29 +00001132
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00001133 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1134 QualType ObjectTy,
1135 llvm::Value *BaseValue,
1136 const ObjCIvarDecl *Ivar,
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00001137 unsigned CVRQualifiers);
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00001138 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar61e14a62009-04-22 05:08:15 +00001139 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00001140 const ObjCIvarDecl *Ivar);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001141};
Fariborz Jahanian48543f52009-01-21 22:04:16 +00001142
Fariborz Jahaniand0374812009-01-22 23:02:58 +00001143class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanian48543f52009-01-21 22:04:16 +00001144private:
Fariborz Jahaniand0374812009-01-22 23:02:58 +00001145 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001146 llvm::GlobalVariable* ObjCEmptyCacheVar;
1147 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbarc0318b22009-03-02 06:08:11 +00001148
Daniel Dunbar3c190812009-04-18 08:51:00 +00001149 /// SuperClassReferences - uniqued super class references.
1150 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
1151
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00001152 /// MetaClassReferences - uniqued meta class references.
1153 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbar9c285e72009-03-01 04:46:24 +00001154
1155 /// EHTypeReferences - uniqued class ehtype references.
1156 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbarc0318b22009-03-02 06:08:11 +00001157
Fariborz Jahaniane142bf42009-05-12 20:06:41 +00001158 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahanian5c76fd32009-05-11 19:25:47 +00001159 /// legacy messaging dispatch.
Fariborz Jahaniane142bf42009-05-12 20:06:41 +00001160 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Fariborz Jahanian5c76fd32009-05-11 19:25:47 +00001161
1162 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahaniane142bf42009-05-12 20:06:41 +00001163 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahanian5c76fd32009-05-11 19:25:47 +00001164 bool LegacyDispatchedSelector(Selector Sel);
1165
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001166 /// FinishNonFragileABIModule - Write out global data structures at the end of
1167 /// processing a translation unit.
1168 void FinishNonFragileABIModule();
Daniel Dunbarc2129532009-04-08 04:21:03 +00001169
Daniel Dunbar57d4e112009-05-15 21:48:48 +00001170 /// AddModuleClassList - Add the given list of class pointers to the
1171 /// module with the provided symbol and section names.
1172 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1173 const char *SymbolName,
1174 const char *SectionName);
1175
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00001176 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1177 unsigned InstanceStart,
1178 unsigned InstanceSize,
1179 const ObjCImplementationDecl *ID);
Fariborz Jahanian06726462009-01-24 21:21:53 +00001180 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
1181 llvm::Constant *IsAGV,
1182 llvm::Constant *SuperClassGV,
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +00001183 llvm::Constant *ClassRoGV,
1184 bool HiddenVisibility);
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00001185
1186 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
1187
Fariborz Jahanian151747b2009-01-30 00:46:37 +00001188 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
1189
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00001190 /// EmitMethodList - Emit the method list for the given
1191 /// implementation. The return value has type MethodListnfABITy.
1192 llvm::Constant *EmitMethodList(const std::string &Name,
1193 const char *Section,
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00001194 const ConstantVector &Methods);
1195 /// EmitIvarList - Emit the ivar list for the given
1196 /// implementation. If ForClass is true the list of class ivars
1197 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1198 /// interface ivars will be emitted. The return value has type
1199 /// IvarListnfABIPtrTy.
1200 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00001201
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00001202 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian150f7732009-01-28 01:36:42 +00001203 const ObjCIvarDecl *Ivar,
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00001204 unsigned long int offset);
1205
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00001206 /// GetOrEmitProtocol - Get the protocol object for the given
1207 /// declaration, emitting it if necessary. The return value has type
1208 /// ProtocolPtrTy.
1209 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
1210
1211 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1212 /// object for the given declaration, emitting it if needed. These
1213 /// forward references will be filled in with empty bodies if no
1214 /// definition is seen. The return value has type ProtocolPtrTy.
1215 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
1216
1217 /// EmitProtocolList - Generate the list of referenced
1218 /// protocols. The return value has type ProtocolListPtrTy.
1219 llvm::Constant *EmitProtocolList(const std::string &Name,
1220 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian7e881162009-02-04 00:22:57 +00001221 ObjCProtocolDecl::protocol_iterator end);
1222
1223 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1224 QualType ResultType,
1225 Selector Sel,
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00001226 llvm::Value *Receiver,
Fariborz Jahanian7e881162009-02-04 00:22:57 +00001227 QualType Arg0Ty,
1228 bool IsSuper,
1229 const CallArgList &CallArgs);
Daniel Dunbarabbda222009-03-01 04:40:10 +00001230
1231 /// GetClassGlobal - Return the global variable for the Objective-C
1232 /// class of the given name.
Fariborz Jahanianab438842009-04-14 18:41:56 +00001233 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
1234
Fariborz Jahanian917c0402009-02-05 20:41:40 +00001235 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar3c190812009-04-18 08:51:00 +00001236 /// for the given class reference.
Fariborz Jahanian917c0402009-02-05 20:41:40 +00001237 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar3c190812009-04-18 08:51:00 +00001238 const ObjCInterfaceDecl *ID);
1239
1240 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1241 /// for the given super class reference.
1242 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1243 const ObjCInterfaceDecl *ID);
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00001244
1245 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1246 /// meta-data
1247 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
1248 const ObjCInterfaceDecl *ID);
1249
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00001250 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1251 /// the given ivar.
1252 ///
Daniel Dunbar07d204a2009-04-19 00:31:15 +00001253 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Fariborz Jahaniana09a5142009-02-12 18:51:23 +00001254 const ObjCInterfaceDecl *ID,
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00001255 const ObjCIvarDecl *Ivar);
Fariborz Jahanian917c0402009-02-05 20:41:40 +00001256
Fariborz Jahanianebb82c62009-02-11 20:51:17 +00001257 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1258 /// for the given selector.
1259 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbar9c285e72009-03-01 04:46:24 +00001260
Daniel Dunbarc2129532009-04-08 04:21:03 +00001261 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbar9c285e72009-03-01 04:46:24 +00001262 /// interface. The return value has type EHTypePtrTy.
Daniel Dunbarc2129532009-04-08 04:21:03 +00001263 llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
1264 bool ForDefinition);
Daniel Dunbara2d275d2009-04-07 05:48:37 +00001265
1266 const char *getMetaclassSymbolPrefix() const {
1267 return "OBJC_METACLASS_$_";
1268 }
Daniel Dunbarc0318b22009-03-02 06:08:11 +00001269
Daniel Dunbara2d275d2009-04-07 05:48:37 +00001270 const char *getClassSymbolPrefix() const {
1271 return "OBJC_CLASS_$_";
1272 }
1273
Daniel Dunbared4d5962009-05-03 12:57:56 +00001274 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarecb5d402009-04-19 23:41:48 +00001275 uint32_t &InstanceStart,
1276 uint32_t &InstanceSize);
Fariborz Jahaniane142bf42009-05-12 20:06:41 +00001277
1278 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar1dd092b2009-05-15 22:33:15 +00001279 Selector GetNullarySelector(const char* name) const {
Fariborz Jahaniane142bf42009-05-12 20:06:41 +00001280 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1281 return CGM.getContext().Selectors.getSelector(0, &II);
1282 }
1283
Daniel Dunbar1dd092b2009-05-15 22:33:15 +00001284 Selector GetUnarySelector(const char* name) const {
Fariborz Jahaniane142bf42009-05-12 20:06:41 +00001285 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1286 return CGM.getContext().Selectors.getSelector(1, &II);
1287 }
Daniel Dunbarecb5d402009-04-19 23:41:48 +00001288
Daniel Dunbar1dd092b2009-05-15 22:33:15 +00001289 /// ImplementationIsNonLazy - Check whether the given category or
1290 /// class implementation is "non-lazy".
Fariborz Jahanian86503952009-05-21 01:03:45 +00001291 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar1dd092b2009-05-15 22:33:15 +00001292
Fariborz Jahanian48543f52009-01-21 22:04:16 +00001293public:
Fariborz Jahaniand0374812009-01-22 23:02:58 +00001294 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001295 // FIXME. All stubs for now!
1296 virtual llvm::Function *ModuleInitFunction();
1297
1298 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
1299 QualType ResultType,
1300 Selector Sel,
1301 llvm::Value *Receiver,
1302 bool IsClassMessage,
Fariborz Jahanianc8007152009-05-05 21:36:57 +00001303 const CallArgList &CallArgs,
1304 const ObjCMethodDecl *Method);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001305
1306 virtual CodeGen::RValue
1307 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
1308 QualType ResultType,
1309 Selector Sel,
1310 const ObjCInterfaceDecl *Class,
Fariborz Jahanian17636fa2009-02-28 20:07:56 +00001311 bool isCategoryImpl,
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001312 llvm::Value *Receiver,
1313 bool IsClassMessage,
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00001314 const CallArgList &CallArgs);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001315
1316 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian917c0402009-02-05 20:41:40 +00001317 const ObjCInterfaceDecl *ID);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001318
1319 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
Fariborz Jahanianebb82c62009-02-11 20:51:17 +00001320 { return EmitSelector(Builder, Sel); }
Fariborz Jahanianc8007152009-05-05 21:36:57 +00001321
1322 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1323 /// untyped one.
1324 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1325 const ObjCMethodDecl *Method)
1326 { return EmitSelector(Builder, Method->getSelector()); }
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001327
Fariborz Jahanianfe49a092009-01-26 18:32:24 +00001328 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001329
1330 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001331 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian5d13ab12009-01-30 18:58:59 +00001332 const ObjCProtocolDecl *PD);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001333
Chris Lattneraea1aee2009-03-22 21:03:39 +00001334 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattnera7ecda42009-04-22 02:44:54 +00001335 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00001336 }
Chris Lattneraea1aee2009-03-22 21:03:39 +00001337 virtual llvm::Constant *GetPropertySetFunction() {
Chris Lattnera7ecda42009-04-22 02:44:54 +00001338 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00001339 }
Chris Lattneraea1aee2009-03-22 21:03:39 +00001340 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattnera7ecda42009-04-22 02:44:54 +00001341 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar978d2be2009-02-16 18:48:45 +00001342 }
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001343
1344 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar75de89f2009-02-24 07:47:38 +00001345 const Stmt &S);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001346 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlsson1cf75362009-02-16 22:59:18 +00001347 const ObjCAtThrowStmt &S);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001348 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00001349 llvm::Value *AddrWeakObj);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001350 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00001351 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001352 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00001353 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001354 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00001355 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001356 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00001357 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian614e8f02009-07-08 01:18:33 +00001358 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1359 llvm::Value *dest, llvm::Value *src,
1360 unsigned long size);
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00001361 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1362 QualType ObjectTy,
1363 llvm::Value *BaseValue,
1364 const ObjCIvarDecl *Ivar,
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00001365 unsigned CVRQualifiers);
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00001366 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar61e14a62009-04-22 05:08:15 +00001367 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00001368 const ObjCIvarDecl *Ivar);
Fariborz Jahanian48543f52009-01-21 22:04:16 +00001369};
1370
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001371} // end anonymous namespace
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00001372
1373/* *** Helper Functions *** */
1374
1375/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Anderson73e7f802009-07-14 23:10:40 +00001376static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
1377 llvm::Constant *C,
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00001378 unsigned idx0,
1379 unsigned idx1) {
1380 llvm::Value *Idxs[] = {
Owen Anderson73e7f802009-07-14 23:10:40 +00001381 VMContext.getConstantInt(llvm::Type::Int32Ty, idx0),
1382 VMContext.getConstantInt(llvm::Type::Int32Ty, idx1)
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00001383 };
Owen Anderson73e7f802009-07-14 23:10:40 +00001384 return VMContext.getConstantExprGetElementPtr(C, Idxs, 2);
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00001385}
1386
Daniel Dunbarc2129532009-04-08 04:21:03 +00001387/// hasObjCExceptionAttribute - Return true if this class or any super
1388/// class has the __objc_exception__ attribute.
Douglas Gregor98da6ae2009-06-18 16:11:24 +00001389static bool hasObjCExceptionAttribute(ASTContext &Context,
1390 const ObjCInterfaceDecl *OID) {
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +00001391 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbarc2129532009-04-08 04:21:03 +00001392 return true;
1393 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor98da6ae2009-06-18 16:11:24 +00001394 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbarc2129532009-04-08 04:21:03 +00001395 return false;
1396}
1397
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00001398/* *** CGObjCMac Public Interface *** */
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001399
Fariborz Jahanian48543f52009-01-21 22:04:16 +00001400CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
1401 ObjCTypes(cgm)
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00001402{
Fariborz Jahanian48543f52009-01-21 22:04:16 +00001403 ObjCABI = 1;
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00001404 EmitImageInfo();
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001405}
1406
Daniel Dunbar434627a2008-08-16 00:25:02 +00001407/// GetClass - Return a reference to the class for the given interface
1408/// decl.
Daniel Dunbard916e6e2008-11-01 01:53:16 +00001409llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001410 const ObjCInterfaceDecl *ID) {
1411 return EmitClassRef(Builder, ID);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001412}
1413
1414/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbard916e6e2008-11-01 01:53:16 +00001415llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar5eec6142008-08-12 03:39:23 +00001416 return EmitSelector(Builder, Sel);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001417}
Fariborz Jahanianc8007152009-05-05 21:36:57 +00001418llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
1419 *Method) {
1420 return EmitSelector(Builder, Method->getSelector());
1421}
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001422
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00001423/// Generate a constant CFString object.
1424/*
1425 struct __builtin_CFString {
1426 const int *isa; // point to __CFConstantStringClassReference
1427 int flags;
1428 const char *str;
1429 long length;
1430 };
1431*/
1432
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001433llvm::Constant *CGObjCCommonMac::GenerateConstantString(
Steve Naroff2c8a08e2009-03-31 16:53:37 +00001434 const ObjCStringLiteral *SL) {
Steve Naroff9a744e52009-04-01 13:55:36 +00001435 return CGM.GetAddrOfConstantCFString(SL->getString());
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001436}
1437
1438/// Generates a message send where the super is the receiver. This is
1439/// a message send to self with special delivery semantics indicating
1440/// which class's method should be called.
Daniel Dunbara04840b2008-08-23 03:46:30 +00001441CodeGen::RValue
1442CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +00001443 QualType ResultType,
1444 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001445 const ObjCInterfaceDecl *Class,
Fariborz Jahanian17636fa2009-02-28 20:07:56 +00001446 bool isCategoryImpl,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001447 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +00001448 bool IsClassMessage,
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +00001449 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbar15245e52008-08-23 04:28:29 +00001450 // Create and init a super structure; this is a (receiver, class)
1451 // pair we will pass to objc_msgSendSuper.
1452 llvm::Value *ObjCSuper =
1453 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
1454 llvm::Value *ReceiverAsObject =
1455 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
1456 CGF.Builder.CreateStore(ReceiverAsObject,
1457 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar15245e52008-08-23 04:28:29 +00001458
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001459 // If this is a class message the metaclass is passed as the target.
1460 llvm::Value *Target;
1461 if (IsClassMessage) {
Fariborz Jahanian17636fa2009-02-28 20:07:56 +00001462 if (isCategoryImpl) {
1463 // Message sent to 'super' in a class method defined in a category
1464 // implementation requires an odd treatment.
1465 // If we are in a class method, we must retrieve the
1466 // _metaclass_ for the current class, pointed at by
1467 // the class's "isa" pointer. The following assumes that
1468 // isa" is the first ivar in a class (which it must be).
1469 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1470 Target = CGF.Builder.CreateStructGEP(Target, 0);
1471 Target = CGF.Builder.CreateLoad(Target);
1472 }
1473 else {
1474 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1475 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1476 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1477 Target = Super;
1478 }
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001479 } else {
1480 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1481 }
Mike Stumpba2cb0e2009-05-16 07:57:57 +00001482 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1483 // ObjCTypes types.
Daniel Dunbar0ed60b02008-08-30 03:02:31 +00001484 const llvm::Type *ClassTy =
1485 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001486 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001487 CGF.Builder.CreateStore(Target,
1488 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Fariborz Jahanian5c76fd32009-05-11 19:25:47 +00001489 return EmitLegacyMessageSend(CGF, ResultType,
1490 EmitSelector(CGF.Builder, Sel),
1491 ObjCSuper, ObjCTypes.SuperPtrCTy,
1492 true, CallArgs, ObjCTypes);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001493}
Daniel Dunbar87062ff2008-08-23 09:25:55 +00001494
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001495/// Generate code for a message send expression.
Daniel Dunbara04840b2008-08-23 03:46:30 +00001496CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +00001497 QualType ResultType,
1498 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001499 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +00001500 bool IsClassMessage,
Fariborz Jahanianc8007152009-05-05 21:36:57 +00001501 const CallArgList &CallArgs,
1502 const ObjCMethodDecl *Method) {
Fariborz Jahanian5c76fd32009-05-11 19:25:47 +00001503 return EmitLegacyMessageSend(CGF, ResultType,
1504 EmitSelector(CGF.Builder, Sel),
1505 Receiver, CGF.getContext().getObjCIdType(),
1506 false, CallArgs, ObjCTypes);
Daniel Dunbar87062ff2008-08-23 09:25:55 +00001507}
1508
Fariborz Jahanian5c76fd32009-05-11 19:25:47 +00001509CodeGen::RValue CGObjCCommonMac::EmitLegacyMessageSend(
1510 CodeGen::CodeGenFunction &CGF,
1511 QualType ResultType,
1512 llvm::Value *Sel,
1513 llvm::Value *Arg0,
1514 QualType Arg0Ty,
1515 bool IsSuper,
1516 const CallArgList &CallArgs,
1517 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar0ed60b02008-08-30 03:02:31 +00001518 CallArgList ActualArgs;
Fariborz Jahanian8978f592009-04-24 21:07:43 +00001519 if (!IsSuper)
1520 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +00001521 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahanian5c76fd32009-05-11 19:25:47 +00001522 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbar0ed60b02008-08-30 03:02:31 +00001523 CGF.getContext().getObjCSelType()));
1524 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanian5c76fd32009-05-11 19:25:47 +00001525
Daniel Dunbar34bda882009-02-02 23:23:47 +00001526 CodeGenTypes &Types = CGM.getTypes();
1527 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
Fariborz Jahaniane142bf42009-05-12 20:06:41 +00001528 // In 64bit ABI, type must be assumed VARARG. In 32bit abi,
Fariborz Jahanian5c76fd32009-05-11 19:25:47 +00001529 // it seems not to matter.
1530 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, (ObjCABI == 2));
1531
1532 llvm::Constant *Fn = NULL;
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001533 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanian5c76fd32009-05-11 19:25:47 +00001534 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
1535 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbaraecef4c2008-10-17 03:24:53 +00001536 } else if (ResultType->isFloatingType()) {
Fariborz Jahanian5c76fd32009-05-11 19:25:47 +00001537 if (ObjCABI == 2) {
1538 if (const BuiltinType *BT = ResultType->getAsBuiltinType()) {
1539 BuiltinType::Kind k = BT->getKind();
1540 Fn = (k == BuiltinType::LongDouble) ? ObjCTypes.getSendFpretFn2(IsSuper)
1541 : ObjCTypes.getSendFn2(IsSuper);
Daniel Dunbar41459b72009-06-10 04:38:50 +00001542 } else {
1543 Fn = ObjCTypes.getSendFn2(IsSuper);
Fariborz Jahanian5c76fd32009-05-11 19:25:47 +00001544 }
1545 }
1546 else
Mike Stumpba2cb0e2009-05-16 07:57:57 +00001547 // FIXME. This currently matches gcc's API for x86-32. May need to change
1548 // for others if we have their API.
Fariborz Jahanian5c76fd32009-05-11 19:25:47 +00001549 Fn = ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbaraecef4c2008-10-17 03:24:53 +00001550 } else {
Fariborz Jahanian5c76fd32009-05-11 19:25:47 +00001551 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
1552 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbaraecef4c2008-10-17 03:24:53 +00001553 }
Fariborz Jahanian5c76fd32009-05-11 19:25:47 +00001554 assert(Fn && "EmitLegacyMessageSend - unknown API");
Owen Anderson73e7f802009-07-14 23:10:40 +00001555 Fn = VMContext.getConstantExprBitCast(Fn,
1556 VMContext.getPointerTypeUnqual(FTy));
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001557 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001558}
1559
Daniel Dunbard916e6e2008-11-01 01:53:16 +00001560llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar84bb85f2008-08-13 00:59:25 +00001561 const ObjCProtocolDecl *PD) {
Daniel Dunbarb3518152008-09-04 04:33:15 +00001562 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpba2cb0e2009-05-16 07:57:57 +00001563 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarb3518152008-09-04 04:33:15 +00001564 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1565
Owen Anderson73e7f802009-07-14 23:10:40 +00001566 return VMContext.getConstantExprBitCast(GetProtocolRef(PD),
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001567 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001568}
1569
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00001570void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpba2cb0e2009-05-16 07:57:57 +00001571 // FIXME: We shouldn't need this, the protocol decl should contain enough
1572 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001573 DefinedProtocols.insert(PD->getIdentifier());
1574
1575 // If we have generated a forward reference to this protocol, emit
1576 // it now. Otherwise do nothing, the protocol objects are lazily
1577 // emitted.
1578 if (Protocols.count(PD->getIdentifier()))
1579 GetOrEmitProtocol(PD);
1580}
1581
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00001582llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001583 if (DefinedProtocols.count(PD->getIdentifier()))
1584 return GetOrEmitProtocol(PD);
1585 return GetOrEmitProtocolRef(PD);
1586}
1587
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001588/*
1589 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1590 struct _objc_protocol {
1591 struct _objc_protocol_extension *isa;
1592 char *protocol_name;
1593 struct _objc_protocol_list *protocol_list;
1594 struct _objc__method_prototype_list *instance_methods;
1595 struct _objc__method_prototype_list *class_methods
1596 };
1597
1598 See EmitProtocolExtension().
1599*/
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001600llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1601 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1602
1603 // Early exit if a defining object has already been generated.
1604 if (Entry && Entry->hasInitializer())
1605 return Entry;
1606
Daniel Dunbar8ede0052008-08-25 06:02:07 +00001607 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpba2cb0e2009-05-16 07:57:57 +00001608 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar8ede0052008-08-25 06:02:07 +00001609 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1610
Chris Lattnerd120b9e2008-11-24 03:54:41 +00001611 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001612
1613 // Construct method lists.
1614 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1615 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001616 for (ObjCProtocolDecl::instmeth_iterator
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001617 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001618 ObjCMethodDecl *MD = *i;
1619 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1620 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1621 OptInstanceMethods.push_back(C);
1622 } else {
1623 InstanceMethods.push_back(C);
1624 }
1625 }
1626
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001627 for (ObjCProtocolDecl::classmeth_iterator
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001628 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001629 ObjCMethodDecl *MD = *i;
1630 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1631 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1632 OptClassMethods.push_back(C);
1633 } else {
1634 ClassMethods.push_back(C);
1635 }
1636 }
1637
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001638 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001639 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001640 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar67e778b2008-08-21 21:57:41 +00001641 Values[2] =
Chris Lattner271d4c22008-11-24 05:29:24 +00001642 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbar67e778b2008-08-21 21:57:41 +00001643 PD->protocol_begin(),
1644 PD->protocol_end());
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001645 Values[3] =
Chris Lattner271d4c22008-11-24 05:29:24 +00001646 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
1647 + PD->getNameAsString(),
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001648 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1649 InstanceMethods);
1650 Values[4] =
Chris Lattner271d4c22008-11-24 05:29:24 +00001651 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
1652 + PD->getNameAsString(),
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001653 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1654 ClassMethods);
Owen Anderson73e7f802009-07-14 23:10:40 +00001655 llvm::Constant *Init = VMContext.getConstantStruct(ObjCTypes.ProtocolTy,
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001656 Values);
1657
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001658 if (Entry) {
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001659 // Already created, fix the linkage and update the initializer.
1660 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001661 Entry->setInitializer(Init);
1662 } else {
1663 Entry =
Owen Anderson94148482009-07-08 19:05:04 +00001664 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001665 llvm::GlobalValue::InternalLinkage,
1666 Init,
Owen Anderson94148482009-07-08 19:05:04 +00001667 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001668 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar56756c32009-03-09 22:18:41 +00001669 Entry->setAlignment(4);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001670 // FIXME: Is this necessary? Why only for protocol?
1671 Entry->setAlignment(4);
1672 }
Chris Lattner5f38e5d2009-07-17 23:57:13 +00001673 CGM.AddUsedGlobal(Entry);
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001674
1675 return Entry;
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001676}
1677
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001678llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001679 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1680
1681 if (!Entry) {
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001682 // We use the initializer as a marker of whether this is a forward
1683 // reference or not. At module finalization we add the empty
1684 // contents for protocols which were referenced but never defined.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001685 Entry =
Owen Anderson94148482009-07-08 19:05:04 +00001686 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001687 llvm::GlobalValue::ExternalLinkage,
1688 0,
Owen Anderson94148482009-07-08 19:05:04 +00001689 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString());
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001690 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar56756c32009-03-09 22:18:41 +00001691 Entry->setAlignment(4);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001692 // FIXME: Is this necessary? Why only for protocol?
1693 Entry->setAlignment(4);
1694 }
1695
1696 return Entry;
1697}
1698
1699/*
1700 struct _objc_protocol_extension {
1701 uint32_t size;
1702 struct objc_method_description_list *optional_instance_methods;
1703 struct objc_method_description_list *optional_class_methods;
1704 struct objc_property_list *instance_properties;
1705 };
1706*/
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001707llvm::Constant *
1708CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1709 const ConstantVector &OptInstanceMethods,
1710 const ConstantVector &OptClassMethods) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001711 uint64_t Size =
Duncan Sandsee6f6f82009-05-09 07:08:47 +00001712 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001713 std::vector<llvm::Constant*> Values(4);
Owen Anderson73e7f802009-07-14 23:10:40 +00001714 Values[0] = VMContext.getConstantInt(ObjCTypes.IntTy, Size);
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001715 Values[1] =
Chris Lattner271d4c22008-11-24 05:29:24 +00001716 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1717 + PD->getNameAsString(),
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001718 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1719 OptInstanceMethods);
1720 Values[2] =
Chris Lattner271d4c22008-11-24 05:29:24 +00001721 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1722 + PD->getNameAsString(),
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001723 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1724 OptClassMethods);
Chris Lattner271d4c22008-11-24 05:29:24 +00001725 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1726 PD->getNameAsString(),
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00001727 0, PD, ObjCTypes);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001728
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001729 // Return null if no extension bits are used.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001730 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1731 Values[3]->isNullValue())
Owen Anderson5f1adc22009-07-13 04:10:07 +00001732 return VMContext.getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001733
1734 llvm::Constant *Init =
Owen Anderson73e7f802009-07-14 23:10:40 +00001735 VMContext.getConstantStruct(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001736
Daniel Dunbar90d88f92009-03-09 21:49:58 +00001737 // No special section, but goes in llvm.used
1738 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
1739 Init,
1740 0, 0, true);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001741}
1742
1743/*
1744 struct objc_protocol_list {
1745 struct objc_protocol_list *next;
1746 long count;
1747 Protocol *list[];
1748 };
1749*/
Daniel Dunbar67e778b2008-08-21 21:57:41 +00001750llvm::Constant *
1751CGObjCMac::EmitProtocolList(const std::string &Name,
1752 ObjCProtocolDecl::protocol_iterator begin,
1753 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001754 std::vector<llvm::Constant*> ProtocolRefs;
1755
Daniel Dunbar67e778b2008-08-21 21:57:41 +00001756 for (; begin != end; ++begin)
1757 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001758
1759 // Just return null for empty protocol lists
1760 if (ProtocolRefs.empty())
Owen Anderson5f1adc22009-07-13 04:10:07 +00001761 return VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001762
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001763 // This list is null terminated.
Owen Anderson5f1adc22009-07-13 04:10:07 +00001764 ProtocolRefs.push_back(VMContext.getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001765
1766 std::vector<llvm::Constant*> Values(3);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001767 // This field is only used by the runtime.
Owen Anderson5f1adc22009-07-13 04:10:07 +00001768 Values[0] = VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson73e7f802009-07-14 23:10:40 +00001769 Values[1] = VMContext.getConstantInt(ObjCTypes.LongTy,
1770 ProtocolRefs.size() - 1);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001771 Values[2] =
Owen Anderson73e7f802009-07-14 23:10:40 +00001772 VMContext.getConstantArray(VMContext.getArrayType(ObjCTypes.ProtocolPtrTy,
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001773 ProtocolRefs.size()),
1774 ProtocolRefs);
1775
Owen Anderson73e7f802009-07-14 23:10:40 +00001776 llvm::Constant *Init = VMContext.getConstantStruct(Values);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001777 llvm::GlobalVariable *GV =
Daniel Dunbar90d88f92009-03-09 21:49:58 +00001778 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar56756c32009-03-09 22:18:41 +00001779 4, false);
Owen Anderson73e7f802009-07-14 23:10:40 +00001780 return VMContext.getConstantExprBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001781}
1782
1783/*
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001784 struct _objc_property {
1785 const char * const name;
1786 const char * const attributes;
1787 };
1788
1789 struct _objc_property_list {
1790 uint32_t entsize; // sizeof (struct _objc_property)
1791 uint32_t prop_count;
1792 struct _objc_property[prop_count];
1793 };
1794*/
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00001795llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1796 const Decl *Container,
1797 const ObjCContainerDecl *OCD,
1798 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001799 std::vector<llvm::Constant*> Properties, Prop(2);
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001800 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1801 E = OCD->prop_end(); I != E; ++I) {
Steve Naroffdcf1e842009-01-11 12:47:58 +00001802 const ObjCPropertyDecl *PD = *I;
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001803 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001804 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson73e7f802009-07-14 23:10:40 +00001805 Properties.push_back(VMContext.getConstantStruct(ObjCTypes.PropertyTy,
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001806 Prop));
1807 }
1808
1809 // Return null for empty list.
1810 if (Properties.empty())
Owen Anderson5f1adc22009-07-13 04:10:07 +00001811 return VMContext.getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001812
1813 unsigned PropertySize =
Duncan Sandsee6f6f82009-05-09 07:08:47 +00001814 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001815 std::vector<llvm::Constant*> Values(3);
Owen Anderson73e7f802009-07-14 23:10:40 +00001816 Values[0] = VMContext.getConstantInt(ObjCTypes.IntTy, PropertySize);
1817 Values[1] = VMContext.getConstantInt(ObjCTypes.IntTy, Properties.size());
1818 llvm::ArrayType *AT = VMContext.getArrayType(ObjCTypes.PropertyTy,
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001819 Properties.size());
Owen Anderson73e7f802009-07-14 23:10:40 +00001820 Values[2] = VMContext.getConstantArray(AT, Properties);
1821 llvm::Constant *Init = VMContext.getConstantStruct(Values);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001822
Daniel Dunbar90d88f92009-03-09 21:49:58 +00001823 llvm::GlobalVariable *GV =
Daniel Dunbar9dfd3a72009-04-15 02:56:18 +00001824 CreateMetadataVar(Name, Init,
1825 (ObjCABI == 2) ? "__DATA, __objc_const" :
1826 "__OBJC,__property,regular,no_dead_strip",
1827 (ObjCABI == 2) ? 8 : 4,
1828 true);
Owen Anderson73e7f802009-07-14 23:10:40 +00001829 return VMContext.getConstantExprBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001830}
1831
1832/*
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001833 struct objc_method_description_list {
1834 int count;
1835 struct objc_method_description list[];
1836 };
1837*/
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001838llvm::Constant *
1839CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1840 std::vector<llvm::Constant*> Desc(2);
Owen Anderson73e7f802009-07-14 23:10:40 +00001841 Desc[0] =
1842 VMContext.getConstantExprBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001843 ObjCTypes.SelectorPtrTy);
1844 Desc[1] = GetMethodVarType(MD);
Owen Anderson73e7f802009-07-14 23:10:40 +00001845 return VMContext.getConstantStruct(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001846 Desc);
1847}
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001848
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001849llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1850 const char *Section,
1851 const ConstantVector &Methods) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001852 // Return null for empty list.
1853 if (Methods.empty())
Owen Anderson5f1adc22009-07-13 04:10:07 +00001854 return VMContext.getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001855
1856 std::vector<llvm::Constant*> Values(2);
Owen Anderson73e7f802009-07-14 23:10:40 +00001857 Values[0] = VMContext.getConstantInt(ObjCTypes.IntTy, Methods.size());
1858 llvm::ArrayType *AT = VMContext.getArrayType(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001859 Methods.size());
Owen Anderson73e7f802009-07-14 23:10:40 +00001860 Values[1] = VMContext.getConstantArray(AT, Methods);
1861 llvm::Constant *Init = VMContext.getConstantStruct(Values);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001862
Daniel Dunbar9dfd3a72009-04-15 02:56:18 +00001863 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Anderson73e7f802009-07-14 23:10:40 +00001864 return VMContext.getConstantExprBitCast(GV,
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001865 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001866}
1867
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001868/*
1869 struct _objc_category {
1870 char *category_name;
1871 char *class_name;
1872 struct _objc_method_list *instance_methods;
1873 struct _objc_method_list *class_methods;
1874 struct _objc_protocol_list *protocols;
1875 uint32_t size; // <rdar://4585769>
1876 struct _objc_property_list *instance_properties;
1877 };
1878 */
Daniel Dunbarac93e472008-08-15 22:20:32 +00001879void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sandsee6f6f82009-05-09 07:08:47 +00001880 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001881
Mike Stumpba2cb0e2009-05-16 07:57:57 +00001882 // FIXME: This is poor design, the OCD should have a pointer to the category
1883 // decl. Additionally, note that Category can be null for the @implementation
1884 // w/o an @interface case. Sema should just create one for us as it does for
1885 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001886 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar0cd49032008-08-26 23:03:11 +00001887 const ObjCCategoryDecl *Category =
1888 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattner271d4c22008-11-24 05:29:24 +00001889 std::string ExtName(Interface->getNameAsString() + "_" +
1890 OCD->getNameAsString());
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001891
Daniel Dunbar12996f52008-08-26 21:51:14 +00001892 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregorcd19b572009-04-23 01:02:12 +00001893 for (ObjCCategoryImplDecl::instmeth_iterator
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001894 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbar12996f52008-08-26 21:51:14 +00001895 // Instance methods should always be defined.
1896 InstanceMethods.push_back(GetMethodConstant(*i));
1897 }
Douglas Gregorcd19b572009-04-23 01:02:12 +00001898 for (ObjCCategoryImplDecl::classmeth_iterator
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001899 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbar12996f52008-08-26 21:51:14 +00001900 // Class methods should always be defined.
1901 ClassMethods.push_back(GetMethodConstant(*i));
1902 }
1903
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001904 std::vector<llvm::Constant*> Values(7);
1905 Values[0] = GetClassName(OCD->getIdentifier());
1906 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahaniand11bc1d2009-04-29 20:40:05 +00001907 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001908 Values[2] =
1909 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1910 ExtName,
1911 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar12996f52008-08-26 21:51:14 +00001912 InstanceMethods);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001913 Values[3] =
1914 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
Daniel Dunbar9dfd3a72009-04-15 02:56:18 +00001915 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar12996f52008-08-26 21:51:14 +00001916 ClassMethods);
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001917 if (Category) {
1918 Values[4] =
1919 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1920 Category->protocol_begin(),
1921 Category->protocol_end());
1922 } else {
Owen Anderson5f1adc22009-07-13 04:10:07 +00001923 Values[4] = VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001924 }
Owen Anderson73e7f802009-07-14 23:10:40 +00001925 Values[5] = VMContext.getConstantInt(ObjCTypes.IntTy, Size);
Daniel Dunbar0cd49032008-08-26 23:03:11 +00001926
1927 // If there is no category @interface then there can be no properties.
1928 if (Category) {
Daniel Dunbar9dfd3a72009-04-15 02:56:18 +00001929 Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00001930 OCD, Category, ObjCTypes);
Daniel Dunbar0cd49032008-08-26 23:03:11 +00001931 } else {
Owen Anderson5f1adc22009-07-13 04:10:07 +00001932 Values[6] = VMContext.getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar0cd49032008-08-26 23:03:11 +00001933 }
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001934
Owen Anderson73e7f802009-07-14 23:10:40 +00001935 llvm::Constant *Init = VMContext.getConstantStruct(ObjCTypes.CategoryTy,
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001936 Values);
1937
1938 llvm::GlobalVariable *GV =
Daniel Dunbar90d88f92009-03-09 21:49:58 +00001939 CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init,
1940 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar56756c32009-03-09 22:18:41 +00001941 4, true);
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001942 DefinedCategories.push_back(GV);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001943}
1944
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001945// FIXME: Get from somewhere?
1946enum ClassFlags {
1947 eClassFlags_Factory = 0x00001,
1948 eClassFlags_Meta = 0x00002,
1949 // <rdr://5142207>
1950 eClassFlags_HasCXXStructors = 0x02000,
1951 eClassFlags_Hidden = 0x20000,
1952 eClassFlags_ABI2_Hidden = 0x00010,
1953 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1954};
1955
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001956/*
1957 struct _objc_class {
1958 Class isa;
1959 Class super_class;
1960 const char *name;
1961 long version;
1962 long info;
1963 long instance_size;
1964 struct _objc_ivar_list *ivars;
1965 struct _objc_method_list *methods;
1966 struct _objc_cache *cache;
1967 struct _objc_protocol_list *protocols;
1968 // Objective-C 1.0 extensions (<rdr://4585769>)
1969 const char *ivar_layout;
1970 struct _objc_class_ext *ext;
1971 };
1972
1973 See EmitClassExtension();
1974 */
1975void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar8ede0052008-08-25 06:02:07 +00001976 DefinedSymbols.insert(ID->getIdentifier());
1977
Chris Lattnerd120b9e2008-11-24 03:54:41 +00001978 std::string ClassName = ID->getNameAsString();
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001979 // FIXME: Gross
1980 ObjCInterfaceDecl *Interface =
1981 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar67e778b2008-08-21 21:57:41 +00001982 llvm::Constant *Protocols =
Chris Lattner271d4c22008-11-24 05:29:24 +00001983 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbar67e778b2008-08-21 21:57:41 +00001984 Interface->protocol_begin(),
1985 Interface->protocol_end());
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001986 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar07ddfa92009-05-03 10:46:44 +00001987 unsigned Size =
1988 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001989
1990 // FIXME: Set CXX-structors flag.
Daniel Dunbar8394fda2009-04-14 06:00:08 +00001991 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001992 Flags |= eClassFlags_Hidden;
1993
Daniel Dunbar12996f52008-08-26 21:51:14 +00001994 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregorcd19b572009-04-23 01:02:12 +00001995 for (ObjCImplementationDecl::instmeth_iterator
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001996 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbar12996f52008-08-26 21:51:14 +00001997 // Instance methods should always be defined.
1998 InstanceMethods.push_back(GetMethodConstant(*i));
1999 }
Douglas Gregorcd19b572009-04-23 01:02:12 +00002000 for (ObjCImplementationDecl::classmeth_iterator
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00002001 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbar12996f52008-08-26 21:51:14 +00002002 // Class methods should always be defined.
2003 ClassMethods.push_back(GetMethodConstant(*i));
2004 }
2005
Douglas Gregorcd19b572009-04-23 01:02:12 +00002006 for (ObjCImplementationDecl::propimpl_iterator
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00002007 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbar12996f52008-08-26 21:51:14 +00002008 ObjCPropertyImplDecl *PID = *i;
2009
2010 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2011 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2012
2013 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2014 if (llvm::Constant *C = GetMethodConstant(MD))
2015 InstanceMethods.push_back(C);
2016 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2017 if (llvm::Constant *C = GetMethodConstant(MD))
2018 InstanceMethods.push_back(C);
2019 }
2020 }
2021
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002022 std::vector<llvm::Constant*> Values(12);
Daniel Dunbara3e92cd2009-05-03 08:56:52 +00002023 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002024 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar8ede0052008-08-25 06:02:07 +00002025 // Record a reference to the super class.
2026 LazySymbols.insert(Super->getIdentifier());
2027
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002028 Values[ 1] =
Owen Anderson73e7f802009-07-14 23:10:40 +00002029 VMContext.getConstantExprBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002030 ObjCTypes.ClassPtrTy);
2031 } else {
Owen Anderson5f1adc22009-07-13 04:10:07 +00002032 Values[ 1] = VMContext.getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002033 }
2034 Values[ 2] = GetClassName(ID->getIdentifier());
2035 // Version is always 0.
Owen Anderson73e7f802009-07-14 23:10:40 +00002036 Values[ 3] = VMContext.getConstantInt(ObjCTypes.LongTy, 0);
2037 Values[ 4] = VMContext.getConstantInt(ObjCTypes.LongTy, Flags);
2038 Values[ 5] = VMContext.getConstantInt(ObjCTypes.LongTy, Size);
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00002039 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00002040 Values[ 7] =
Chris Lattner271d4c22008-11-24 05:29:24 +00002041 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00002042 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar12996f52008-08-26 21:51:14 +00002043 InstanceMethods);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002044 // cache is always NULL.
Owen Anderson5f1adc22009-07-13 04:10:07 +00002045 Values[ 8] = VMContext.getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002046 Values[ 9] = Protocols;
Fariborz Jahanian31b96492009-04-22 23:00:43 +00002047 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002048 Values[11] = EmitClassExtension(ID);
Owen Anderson73e7f802009-07-14 23:10:40 +00002049 llvm::Constant *Init = VMContext.getConstantStruct(ObjCTypes.ClassTy,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002050 Values);
2051
2052 llvm::GlobalVariable *GV =
Daniel Dunbar90d88f92009-03-09 21:49:58 +00002053 CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init,
2054 "__OBJC,__class,regular,no_dead_strip",
Daniel Dunbar56756c32009-03-09 22:18:41 +00002055 4, true);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002056 DefinedClasses.push_back(GV);
2057}
2058
2059llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2060 llvm::Constant *Protocols,
Daniel Dunbarfe131f02008-08-27 02:31:56 +00002061 const ConstantVector &Methods) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002062 unsigned Flags = eClassFlags_Meta;
Duncan Sandsee6f6f82009-05-09 07:08:47 +00002063 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002064
Daniel Dunbar8394fda2009-04-14 06:00:08 +00002065 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002066 Flags |= eClassFlags_Hidden;
2067
2068 std::vector<llvm::Constant*> Values(12);
2069 // The isa for the metaclass is the root of the hierarchy.
2070 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2071 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2072 Root = Super;
2073 Values[ 0] =
Owen Anderson73e7f802009-07-14 23:10:40 +00002074 VMContext.getConstantExprBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002075 ObjCTypes.ClassPtrTy);
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00002076 // The super class for the metaclass is emitted as the name of the
2077 // super class. The runtime fixes this up to point to the
2078 // *metaclass* for the super class.
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002079 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
2080 Values[ 1] =
Owen Anderson73e7f802009-07-14 23:10:40 +00002081 VMContext.getConstantExprBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002082 ObjCTypes.ClassPtrTy);
2083 } else {
Owen Anderson5f1adc22009-07-13 04:10:07 +00002084 Values[ 1] = VMContext.getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002085 }
2086 Values[ 2] = GetClassName(ID->getIdentifier());
2087 // Version is always 0.
Owen Anderson73e7f802009-07-14 23:10:40 +00002088 Values[ 3] = VMContext.getConstantInt(ObjCTypes.LongTy, 0);
2089 Values[ 4] = VMContext.getConstantInt(ObjCTypes.LongTy, Flags);
2090 Values[ 5] = VMContext.getConstantInt(ObjCTypes.LongTy, Size);
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00002091 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00002092 Values[ 7] =
Chris Lattner271d4c22008-11-24 05:29:24 +00002093 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar9dfd3a72009-04-15 02:56:18 +00002094 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbar12996f52008-08-26 21:51:14 +00002095 Methods);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002096 // cache is always NULL.
Owen Anderson5f1adc22009-07-13 04:10:07 +00002097 Values[ 8] = VMContext.getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002098 Values[ 9] = Protocols;
2099 // ivar_layout for metaclass is always NULL.
Owen Anderson5f1adc22009-07-13 04:10:07 +00002100 Values[10] = VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002101 // The class extension is always unused for metaclasses.
Owen Anderson5f1adc22009-07-13 04:10:07 +00002102 Values[11] = VMContext.getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson73e7f802009-07-14 23:10:40 +00002103 llvm::Constant *Init = VMContext.getConstantStruct(ObjCTypes.ClassTy,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002104 Values);
2105
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00002106 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattnerd120b9e2008-11-24 03:54:41 +00002107 Name += ID->getNameAsCString();
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00002108
2109 // Check for a forward reference.
2110 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2111 if (GV) {
2112 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2113 "Forward metaclass reference has incorrect type.");
2114 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2115 GV->setInitializer(Init);
2116 } else {
Owen Anderson94148482009-07-08 19:05:04 +00002117 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00002118 llvm::GlobalValue::InternalLinkage,
Owen Anderson94148482009-07-08 19:05:04 +00002119 Init, Name);
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00002120 }
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002121 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar56756c32009-03-09 22:18:41 +00002122 GV->setAlignment(4);
Chris Lattner5f38e5d2009-07-17 23:57:13 +00002123 CGM.AddUsedGlobal(GV);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002124
2125 return GV;
2126}
2127
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00002128llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattner271d4c22008-11-24 05:29:24 +00002129 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00002130
Mike Stumpba2cb0e2009-05-16 07:57:57 +00002131 // FIXME: Should we look these up somewhere other than the module. Its a bit
2132 // silly since we only generate these while processing an implementation, so
2133 // exactly one pointer would work if know when we entered/exitted an
2134 // implementation block.
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00002135
2136 // Check for an existing forward reference.
Fariborz Jahanian5fe09f72009-01-07 20:11:22 +00002137 // Previously, metaclass with internal linkage may have been defined.
2138 // pass 'true' as 2nd argument so it is returned.
2139 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00002140 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2141 "Forward metaclass reference has incorrect type.");
2142 return GV;
2143 } else {
2144 // Generate as an external reference to keep a consistent
2145 // module. This will be patched up when we emit the metaclass.
Owen Anderson94148482009-07-08 19:05:04 +00002146 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00002147 llvm::GlobalValue::ExternalLinkage,
2148 0,
Owen Anderson94148482009-07-08 19:05:04 +00002149 Name);
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00002150 }
2151}
2152
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002153/*
2154 struct objc_class_ext {
2155 uint32_t size;
2156 const char *weak_ivar_layout;
2157 struct _objc_property_list *properties;
2158 };
2159*/
2160llvm::Constant *
2161CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
2162 uint64_t Size =
Duncan Sandsee6f6f82009-05-09 07:08:47 +00002163 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002164
2165 std::vector<llvm::Constant*> Values(3);
Owen Anderson73e7f802009-07-14 23:10:40 +00002166 Values[0] = VMContext.getConstantInt(ObjCTypes.IntTy, Size);
Fariborz Jahanian31b96492009-04-22 23:00:43 +00002167 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9dfd3a72009-04-15 02:56:18 +00002168 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00002169 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002170
2171 // Return null if no extension bits are used.
2172 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson5f1adc22009-07-13 04:10:07 +00002173 return VMContext.getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002174
2175 llvm::Constant *Init =
Owen Anderson73e7f802009-07-14 23:10:40 +00002176 VMContext.getConstantStruct(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar90d88f92009-03-09 21:49:58 +00002177 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar9dfd3a72009-04-15 02:56:18 +00002178 Init, "__OBJC,__class_ext,regular,no_dead_strip",
2179 4, true);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002180}
2181
2182/*
2183 struct objc_ivar {
2184 char *ivar_name;
2185 char *ivar_type;
2186 int ivar_offset;
2187 };
2188
2189 struct objc_ivar_list {
2190 int ivar_count;
2191 struct objc_ivar list[count];
2192 };
2193 */
2194llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00002195 bool ForClass) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002196 std::vector<llvm::Constant*> Ivars, Ivar(3);
2197
2198 // When emitting the root class GCC emits ivar entries for the
2199 // actual class structure. It is not clear if we need to follow this
2200 // behavior; for now lets try and get away with not doing it. If so,
2201 // the cleanest solution would be to make up an ObjCInterfaceDecl
2202 // for the class.
2203 if (ForClass)
Owen Anderson5f1adc22009-07-13 04:10:07 +00002204 return VMContext.getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00002205
2206 ObjCInterfaceDecl *OID =
2207 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00002208
Daniel Dunbar356f0742009-04-20 06:54:31 +00002209 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanianb290be02009-06-04 01:19:09 +00002210 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar356f0742009-04-20 06:54:31 +00002211
2212 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2213 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanianb290be02009-06-04 01:19:09 +00002214 // Ignore unnamed bit-fields.
2215 if (!IVD->getDeclName())
2216 continue;
Daniel Dunbare42aede2009-04-22 08:22:17 +00002217 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2218 Ivar[1] = GetMethodVarType(IVD);
Owen Anderson73e7f802009-07-14 23:10:40 +00002219 Ivar[2] = VMContext.getConstantInt(ObjCTypes.IntTy,
Daniel Dunbar85d37542009-04-22 07:32:20 +00002220 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson73e7f802009-07-14 23:10:40 +00002221 Ivars.push_back(VMContext.getConstantStruct(ObjCTypes.IvarTy, Ivar));
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002222 }
2223
2224 // Return null for empty list.
2225 if (Ivars.empty())
Owen Anderson5f1adc22009-07-13 04:10:07 +00002226 return VMContext.getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002227
2228 std::vector<llvm::Constant*> Values(2);
Owen Anderson73e7f802009-07-14 23:10:40 +00002229 Values[0] = VMContext.getConstantInt(ObjCTypes.IntTy, Ivars.size());
2230 llvm::ArrayType *AT = VMContext.getArrayType(ObjCTypes.IvarTy,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002231 Ivars.size());
Owen Anderson73e7f802009-07-14 23:10:40 +00002232 Values[1] = VMContext.getConstantArray(AT, Ivars);
2233 llvm::Constant *Init = VMContext.getConstantStruct(Values);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002234
Daniel Dunbar90d88f92009-03-09 21:49:58 +00002235 llvm::GlobalVariable *GV;
2236 if (ForClass)
2237 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(),
Daniel Dunbar56756c32009-03-09 22:18:41 +00002238 Init, "__OBJC,__class_vars,regular,no_dead_strip",
2239 4, true);
Daniel Dunbar90d88f92009-03-09 21:49:58 +00002240 else
2241 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_"
2242 + ID->getNameAsString(),
2243 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar9dfd3a72009-04-15 02:56:18 +00002244 4, true);
Owen Anderson73e7f802009-07-14 23:10:40 +00002245 return VMContext.getConstantExprBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002246}
2247
2248/*
2249 struct objc_method {
2250 SEL method_name;
2251 char *method_types;
2252 void *method;
2253 };
2254
2255 struct objc_method_list {
2256 struct objc_method_list *obsolete;
2257 int count;
2258 struct objc_method methods_list[count];
2259 };
2260*/
Daniel Dunbar12996f52008-08-26 21:51:14 +00002261
2262/// GetMethodConstant - Return a struct objc_method constant for the
2263/// given method if it has been defined. The result is null if the
2264/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarfe131f02008-08-27 02:31:56 +00002265llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbar12996f52008-08-26 21:51:14 +00002266 // FIXME: Use DenseMap::lookup
2267 llvm::Function *Fn = MethodDefinitions[MD];
2268 if (!Fn)
2269 return 0;
2270
2271 std::vector<llvm::Constant*> Method(3);
2272 Method[0] =
Owen Anderson73e7f802009-07-14 23:10:40 +00002273 VMContext.getConstantExprBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar12996f52008-08-26 21:51:14 +00002274 ObjCTypes.SelectorPtrTy);
2275 Method[1] = GetMethodVarType(MD);
Owen Anderson73e7f802009-07-14 23:10:40 +00002276 Method[2] = VMContext.getConstantExprBitCast(Fn, ObjCTypes.Int8PtrTy);
2277 return VMContext.getConstantStruct(ObjCTypes.MethodTy, Method);
Daniel Dunbar12996f52008-08-26 21:51:14 +00002278}
2279
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00002280llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
2281 const char *Section,
Daniel Dunbarfe131f02008-08-27 02:31:56 +00002282 const ConstantVector &Methods) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002283 // Return null for empty list.
2284 if (Methods.empty())
Owen Anderson5f1adc22009-07-13 04:10:07 +00002285 return VMContext.getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002286
2287 std::vector<llvm::Constant*> Values(3);
Owen Anderson5f1adc22009-07-13 04:10:07 +00002288 Values[0] = VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson73e7f802009-07-14 23:10:40 +00002289 Values[1] = VMContext.getConstantInt(ObjCTypes.IntTy, Methods.size());
2290 llvm::ArrayType *AT = VMContext.getArrayType(ObjCTypes.MethodTy,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002291 Methods.size());
Owen Anderson73e7f802009-07-14 23:10:40 +00002292 Values[2] = VMContext.getConstantArray(AT, Methods);
2293 llvm::Constant *Init = VMContext.getConstantStruct(Values);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002294
Daniel Dunbar9dfd3a72009-04-15 02:56:18 +00002295 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Anderson73e7f802009-07-14 23:10:40 +00002296 return VMContext.getConstantExprBitCast(GV,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002297 ObjCTypes.MethodListPtrTy);
Daniel Dunbarace33292008-08-16 03:19:19 +00002298}
2299
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00002300llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00002301 const ObjCContainerDecl *CD) {
Daniel Dunbarace33292008-08-16 03:19:19 +00002302 std::string Name;
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +00002303 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarace33292008-08-16 03:19:19 +00002304
Daniel Dunbar34bda882009-02-02 23:23:47 +00002305 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00002306 const llvm::FunctionType *MethodTy =
Daniel Dunbar34bda882009-02-02 23:23:47 +00002307 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarace33292008-08-16 03:19:19 +00002308 llvm::Function *Method =
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00002309 llvm::Function::Create(MethodTy,
Daniel Dunbarace33292008-08-16 03:19:19 +00002310 llvm::GlobalValue::InternalLinkage,
2311 Name,
2312 &CGM.getModule());
Daniel Dunbar12996f52008-08-26 21:51:14 +00002313 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarace33292008-08-16 03:19:19 +00002314
Daniel Dunbarace33292008-08-16 03:19:19 +00002315 return Method;
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00002316}
2317
Daniel Dunbarc4594f22009-03-09 20:09:19 +00002318llvm::GlobalVariable *
2319CGObjCCommonMac::CreateMetadataVar(const std::string &Name,
2320 llvm::Constant *Init,
2321 const char *Section,
Daniel Dunbareddddd22009-03-09 20:50:13 +00002322 unsigned Align,
2323 bool AddToUsed) {
Daniel Dunbarc4594f22009-03-09 20:09:19 +00002324 const llvm::Type *Ty = Init->getType();
2325 llvm::GlobalVariable *GV =
Owen Anderson94148482009-07-08 19:05:04 +00002326 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattner5f38e5d2009-07-17 23:57:13 +00002327 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarc4594f22009-03-09 20:09:19 +00002328 if (Section)
2329 GV->setSection(Section);
Daniel Dunbareddddd22009-03-09 20:50:13 +00002330 if (Align)
2331 GV->setAlignment(Align);
2332 if (AddToUsed)
Chris Lattner5f38e5d2009-07-17 23:57:13 +00002333 CGM.AddUsedGlobal(GV);
Daniel Dunbarc4594f22009-03-09 20:09:19 +00002334 return GV;
2335}
2336
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00002337llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002338 // Abuse this interface function as a place to finalize.
2339 FinishModule();
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00002340 return NULL;
2341}
2342
Chris Lattneraea1aee2009-03-22 21:03:39 +00002343llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattnera7ecda42009-04-22 02:44:54 +00002344 return ObjCTypes.getGetPropertyFn();
Daniel Dunbarf7103722008-09-24 03:38:44 +00002345}
2346
Chris Lattneraea1aee2009-03-22 21:03:39 +00002347llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattnera7ecda42009-04-22 02:44:54 +00002348 return ObjCTypes.getSetPropertyFn();
Daniel Dunbarf7103722008-09-24 03:38:44 +00002349}
2350
Chris Lattneraea1aee2009-03-22 21:03:39 +00002351llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattnera7ecda42009-04-22 02:44:54 +00002352 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson58d16242008-08-31 04:05:03 +00002353}
2354
Daniel Dunbar83544842008-09-28 01:03:14 +00002355/*
2356
2357Objective-C setjmp-longjmp (sjlj) Exception Handling
2358--
2359
2360The basic framework for a @try-catch-finally is as follows:
2361{
2362 objc_exception_data d;
2363 id _rethrow = null;
Anders Carlsson8559de12009-02-07 21:26:04 +00002364 bool _call_try_exit = true;
2365
Daniel Dunbar83544842008-09-28 01:03:14 +00002366 objc_exception_try_enter(&d);
2367 if (!setjmp(d.jmp_buf)) {
2368 ... try body ...
2369 } else {
2370 // exception path
2371 id _caught = objc_exception_extract(&d);
2372
2373 // enter new try scope for handlers
2374 if (!setjmp(d.jmp_buf)) {
2375 ... match exception and execute catch blocks ...
2376
2377 // fell off end, rethrow.
2378 _rethrow = _caught;
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002379 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar83544842008-09-28 01:03:14 +00002380 } else {
2381 // exception in catch block
2382 _rethrow = objc_exception_extract(&d);
Anders Carlsson8559de12009-02-07 21:26:04 +00002383 _call_try_exit = false;
2384 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar83544842008-09-28 01:03:14 +00002385 }
2386 }
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002387 ... jump-through-finally to finally_end ...
Daniel Dunbar83544842008-09-28 01:03:14 +00002388
2389finally:
Anders Carlsson8559de12009-02-07 21:26:04 +00002390 if (_call_try_exit)
2391 objc_exception_try_exit(&d);
2392
Daniel Dunbar83544842008-09-28 01:03:14 +00002393 ... finally block ....
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002394 ... dispatch to finally destination ...
2395
2396finally_rethrow:
2397 objc_exception_throw(_rethrow);
2398
2399finally_end:
Daniel Dunbar83544842008-09-28 01:03:14 +00002400}
2401
2402This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002403uses _rethrow to determine if objc_exception_try_exit should be called
2404and if the object should be rethrown. This breaks in the face of
2405throwing nil and introduces unnecessary branches.
Daniel Dunbar83544842008-09-28 01:03:14 +00002406
2407We specialize this framework for a few particular circumstances:
2408
2409 - If there are no catch blocks, then we avoid emitting the second
2410 exception handling context.
2411
2412 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2413 e)) we avoid emitting the code to rethrow an uncaught exception.
2414
2415 - FIXME: If there is no @finally block we can do a few more
2416 simplifications.
2417
2418Rethrows and Jumps-Through-Finally
2419--
2420
2421Support for implicit rethrows and jumping through the finally block is
2422handled by storing the current exception-handling context in
2423ObjCEHStack.
2424
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002425In order to implement proper @finally semantics, we support one basic
2426mechanism for jumping through the finally block to an arbitrary
2427destination. Constructs which generate exits from a @try or @catch
2428block use this mechanism to implement the proper semantics by chaining
2429jumps, as necessary.
2430
2431This mechanism works like the one used for indirect goto: we
2432arbitrarily assign an ID to each destination and store the ID for the
2433destination in a variable prior to entering the finally block. At the
2434end of the finally block we simply create a switch to the proper
2435destination.
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00002436
2437Code gen for @synchronized(expr) stmt;
2438Effectively generating code for:
2439objc_sync_enter(expr);
2440@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar83544842008-09-28 01:03:14 +00002441*/
2442
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00002443void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2444 const Stmt &S) {
2445 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002446 // Create various blocks we refer to for handling @finally.
Daniel Dunbar72f96552008-11-11 02:29:29 +00002447 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson8559de12009-02-07 21:26:04 +00002448 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar72f96552008-11-11 02:29:29 +00002449 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
2450 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
2451 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar34416d62009-02-24 01:43:46 +00002452
2453 // For @synchronized, call objc_sync_enter(sync.expr). The
2454 // evaluation of the expression must occur before we enter the
2455 // @synchronized. We can safely avoid a temp here because jumps into
2456 // @synchronized are illegal & this will dominate uses.
2457 llvm::Value *SyncArg = 0;
2458 if (!isTry) {
2459 SyncArg =
2460 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2461 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattner23e24652009-04-06 16:53:45 +00002462 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar34416d62009-02-24 01:43:46 +00002463 }
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002464
2465 // Push an EH context entry, used for handling rethrows and jumps
2466 // through finally.
Anders Carlsson00ffb962009-02-09 20:38:58 +00002467 CGF.PushCleanupBlock(FinallyBlock);
2468
Anders Carlssonecd81832009-02-07 21:37:21 +00002469 CGF.ObjCEHValueStack.push_back(0);
2470
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002471 // Allocate memory for the exception data and rethrow pointer.
Anders Carlssonfca6c292008-09-09 17:59:25 +00002472 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2473 "exceptiondata.ptr");
Daniel Dunbar35b777f2008-10-29 22:36:39 +00002474 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
2475 "_rethrow");
Anders Carlsson8559de12009-02-07 21:26:04 +00002476 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
2477 "_call_try_exit");
Owen Anderson0ce92b42009-07-21 18:06:41 +00002478 CGF.Builder.CreateStore(VMContext.getTrue(), CallTryExitPtr);
Anders Carlsson8559de12009-02-07 21:26:04 +00002479
Anders Carlssonfca6c292008-09-09 17:59:25 +00002480 // Enter a new try block and call setjmp.
Chris Lattnere05d4cb2009-04-22 02:26:14 +00002481 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Anders Carlssonfca6c292008-09-09 17:59:25 +00002482 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
2483 "jmpbufarray");
2484 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
Chris Lattnere05d4cb2009-04-22 02:26:14 +00002485 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlssonfca6c292008-09-09 17:59:25 +00002486 JmpBufPtr, "result");
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002487
Daniel Dunbar72f96552008-11-11 02:29:29 +00002488 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2489 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbarbe56f012008-10-02 17:05:36 +00002490 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbar0ac66f72008-09-27 23:30:04 +00002491 TryHandler, TryBlock);
Anders Carlssonfca6c292008-09-09 17:59:25 +00002492
2493 // Emit the @try block.
2494 CGF.EmitBlock(TryBlock);
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00002495 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
2496 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlsson00ffb962009-02-09 20:38:58 +00002497 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlssonfca6c292008-09-09 17:59:25 +00002498
2499 // Emit the "exception in @try" block.
Daniel Dunbar0ac66f72008-09-27 23:30:04 +00002500 CGF.EmitBlock(TryHandler);
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002501
2502 // Retrieve the exception object. We may emit multiple blocks but
2503 // nothing can cross this so the value is already in SSA form.
Chris Lattnere05d4cb2009-04-22 02:26:14 +00002504 llvm::Value *Caught =
2505 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2506 ExceptionData, "caught");
Anders Carlssonecd81832009-02-07 21:37:21 +00002507 CGF.ObjCEHValueStack.back() = Caught;
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00002508 if (!isTry)
2509 {
2510 CGF.Builder.CreateStore(Caught, RethrowPtr);
Owen Anderson0ce92b42009-07-21 18:06:41 +00002511 CGF.Builder.CreateStore(VMContext.getFalse(), CallTryExitPtr);
Anders Carlsson00ffb962009-02-09 20:38:58 +00002512 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00002513 }
2514 else if (const ObjCAtCatchStmt* CatchStmt =
2515 cast<ObjCAtTryStmt>(S).getCatchStmts())
2516 {
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002517 // Enter a new exception try block (in case a @catch block throws
2518 // an exception).
Chris Lattnere05d4cb2009-04-22 02:26:14 +00002519 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002520
Chris Lattnere05d4cb2009-04-22 02:26:14 +00002521 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlssonfca6c292008-09-09 17:59:25 +00002522 JmpBufPtr, "result");
Daniel Dunbarbe56f012008-10-02 17:05:36 +00002523 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlssonfca6c292008-09-09 17:59:25 +00002524
Daniel Dunbar72f96552008-11-11 02:29:29 +00002525 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
2526 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbar0ac66f72008-09-27 23:30:04 +00002527 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlssonfca6c292008-09-09 17:59:25 +00002528
2529 CGF.EmitBlock(CatchBlock);
2530
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002531 // Handle catch list. As a special case we check if everything is
2532 // matched and avoid generating code for falling off the end if
2533 // so.
2534 bool AllMatched = false;
Anders Carlssonfca6c292008-09-09 17:59:25 +00002535 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar72f96552008-11-11 02:29:29 +00002536 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlssonfca6c292008-09-09 17:59:25 +00002537
Steve Naroff0e8b96a2009-03-03 19:52:17 +00002538 const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff329ec222009-07-10 23:34:53 +00002539 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar7a68b452008-09-27 07:36:24 +00002540
Anders Carlssonfca6c292008-09-09 17:59:25 +00002541 // catch(...) always matches.
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002542 if (!CatchParam) {
2543 AllMatched = true;
2544 } else {
Steve Naroff329ec222009-07-10 23:34:53 +00002545 OPT = CatchParam->getType()->getAsObjCObjectPointerType();
Anders Carlssonfca6c292008-09-09 17:59:25 +00002546
Daniel Dunbard04c9352008-09-27 22:21:14 +00002547 // catch(id e) always matches.
2548 // FIXME: For the time being we also match id<X>; this should
2549 // be rejected by Sema instead.
Eli Friedmanfb6831a2009-07-11 00:57:02 +00002550 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002551 AllMatched = true;
Anders Carlssonfca6c292008-09-09 17:59:25 +00002552 }
2553
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002554 if (AllMatched) {
Anders Carlsson75d86732008-09-11 09:15:33 +00002555 if (CatchParam) {
Steve Naroff0e8b96a2009-03-03 19:52:17 +00002556 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbar5aa22bc2008-11-11 23:11:34 +00002557 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Steve Naroff0e8b96a2009-03-03 19:52:17 +00002558 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson75d86732008-09-11 09:15:33 +00002559 }
Anders Carlsson1f4acc32008-09-11 08:21:54 +00002560
Anders Carlsson75d86732008-09-11 09:15:33 +00002561 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlsson00ffb962009-02-09 20:38:58 +00002562 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlssonfca6c292008-09-09 17:59:25 +00002563 break;
2564 }
2565
Steve Naroff329ec222009-07-10 23:34:53 +00002566 assert(OPT && "Unexpected non-object pointer type in @catch");
2567 QualType T = OPT->getPointeeType();
Anders Carlssona4519172008-09-11 06:35:14 +00002568 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlssonfca6c292008-09-09 17:59:25 +00002569 assert(ObjCType && "Catch parameter must have Objective-C type!");
2570
2571 // Check if the @catch block matches the exception object.
2572 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
2573
Chris Lattnere05d4cb2009-04-22 02:26:14 +00002574 llvm::Value *Match =
2575 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
2576 Class, Caught, "match");
Anders Carlssonfca6c292008-09-09 17:59:25 +00002577
Daniel Dunbar72f96552008-11-11 02:29:29 +00002578 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlssonfca6c292008-09-09 17:59:25 +00002579
Daniel Dunbarbe56f012008-10-02 17:05:36 +00002580 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar0ac66f72008-09-27 23:30:04 +00002581 MatchedBlock, NextCatchBlock);
Anders Carlssonfca6c292008-09-09 17:59:25 +00002582
2583 // Emit the @catch block.
2584 CGF.EmitBlock(MatchedBlock);
Steve Naroff0e8b96a2009-03-03 19:52:17 +00002585 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbar5aa22bc2008-11-11 23:11:34 +00002586 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar83544842008-09-28 01:03:14 +00002587
2588 llvm::Value *Tmp =
Steve Naroff0e8b96a2009-03-03 19:52:17 +00002589 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar83544842008-09-28 01:03:14 +00002590 "tmp");
Steve Naroff0e8b96a2009-03-03 19:52:17 +00002591 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson75d86732008-09-11 09:15:33 +00002592
2593 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlsson00ffb962009-02-09 20:38:58 +00002594 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlssonfca6c292008-09-09 17:59:25 +00002595
2596 CGF.EmitBlock(NextCatchBlock);
2597 }
2598
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002599 if (!AllMatched) {
2600 // None of the handlers caught the exception, so store it to be
2601 // rethrown at the end of the @finally block.
2602 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson00ffb962009-02-09 20:38:58 +00002603 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002604 }
2605
2606 // Emit the exception handler for the @catch blocks.
Daniel Dunbar0ac66f72008-09-27 23:30:04 +00002607 CGF.EmitBlock(CatchHandler);
Chris Lattnere05d4cb2009-04-22 02:26:14 +00002608 CGF.Builder.CreateStore(
2609 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2610 ExceptionData),
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002611 RethrowPtr);
Owen Anderson0ce92b42009-07-21 18:06:41 +00002612 CGF.Builder.CreateStore(VMContext.getFalse(), CallTryExitPtr);
Anders Carlsson00ffb962009-02-09 20:38:58 +00002613 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002614 } else {
Anders Carlssonfca6c292008-09-09 17:59:25 +00002615 CGF.Builder.CreateStore(Caught, RethrowPtr);
Owen Anderson0ce92b42009-07-21 18:06:41 +00002616 CGF.Builder.CreateStore(VMContext.getFalse(), CallTryExitPtr);
Anders Carlsson00ffb962009-02-09 20:38:58 +00002617 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlssonfca6c292008-09-09 17:59:25 +00002618 }
2619
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002620 // Pop the exception-handling stack entry. It is important to do
2621 // this now, because the code in the @finally block is not in this
2622 // context.
Anders Carlsson00ffb962009-02-09 20:38:58 +00002623 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2624
Anders Carlssonecd81832009-02-07 21:37:21 +00002625 CGF.ObjCEHValueStack.pop_back();
2626
Anders Carlssonfca6c292008-09-09 17:59:25 +00002627 // Emit the @finally block.
2628 CGF.EmitBlock(FinallyBlock);
Anders Carlsson8559de12009-02-07 21:26:04 +00002629 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2630
2631 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2632
2633 CGF.EmitBlock(FinallyExit);
Chris Lattnere05d4cb2009-04-22 02:26:14 +00002634 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData);
Daniel Dunbar7a68b452008-09-27 07:36:24 +00002635
2636 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00002637 if (isTry) {
2638 if (const ObjCAtFinallyStmt* FinallyStmt =
2639 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2640 CGF.EmitStmt(FinallyStmt->getFinallyBody());
Daniel Dunbar34416d62009-02-24 01:43:46 +00002641 } else {
2642 // Emit objc_sync_exit(expr); as finally's sole statement for
2643 // @synchronized.
Chris Lattnerf6ec7e42009-04-22 02:38:11 +00002644 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Fariborz Jahanian3895d922008-11-21 19:21:53 +00002645 }
Anders Carlssonfca6c292008-09-09 17:59:25 +00002646
Anders Carlsson00ffb962009-02-09 20:38:58 +00002647 // Emit the switch block
2648 if (Info.SwitchBlock)
2649 CGF.EmitBlock(Info.SwitchBlock);
2650 if (Info.EndBlock)
2651 CGF.EmitBlock(Info.EndBlock);
2652
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002653 CGF.EmitBlock(FinallyRethrow);
Chris Lattnerf6ec7e42009-04-22 02:38:11 +00002654 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002655 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbar0ac66f72008-09-27 23:30:04 +00002656 CGF.Builder.CreateUnreachable();
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002657
2658 CGF.EmitBlock(FinallyEnd);
Anders Carlssonb01a2112008-09-09 10:04:29 +00002659}
2660
2661void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002662 const ObjCAtThrowStmt &S) {
Anders Carlsson05d7be72008-09-09 16:16:55 +00002663 llvm::Value *ExceptionAsObject;
2664
2665 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2666 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2667 ExceptionAsObject =
2668 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2669 } else {
Anders Carlssonecd81832009-02-07 21:37:21 +00002670 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar83544842008-09-28 01:03:14 +00002671 "Unexpected rethrow outside @catch block.");
Anders Carlssonecd81832009-02-07 21:37:21 +00002672 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson05d7be72008-09-09 16:16:55 +00002673 }
2674
Chris Lattnerf6ec7e42009-04-22 02:38:11 +00002675 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Anders Carlssonfca6c292008-09-09 17:59:25 +00002676 CGF.Builder.CreateUnreachable();
Daniel Dunbar5aa22bc2008-11-11 23:11:34 +00002677
2678 // Clear the insertion point to indicate we are in unreachable code.
2679 CGF.Builder.ClearInsertionPoint();
Anders Carlssonb01a2112008-09-09 10:04:29 +00002680}
2681
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00002682/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian3305ad32008-11-18 21:45:40 +00002683/// object: objc_read_weak (id *src)
2684///
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00002685llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian3305ad32008-11-18 21:45:40 +00002686 llvm::Value *AddrWeakObj)
2687{
Eli Friedmanf8466232009-03-07 03:57:15 +00002688 const llvm::Type* DestTy =
2689 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +00002690 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattnera7ecda42009-04-22 02:44:54 +00002691 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00002692 AddrWeakObj, "weakread");
Eli Friedmanf8466232009-03-07 03:57:15 +00002693 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian3305ad32008-11-18 21:45:40 +00002694 return read_weak;
2695}
2696
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00002697/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2698/// objc_assign_weak (id src, id *dst)
2699///
2700void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2701 llvm::Value *src, llvm::Value *dst)
2702{
Fariborz Jahanianad51ca02009-03-23 19:10:40 +00002703 const llvm::Type * SrcTy = src->getType();
2704 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsee6f6f82009-05-09 07:08:47 +00002705 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianad51ca02009-03-23 19:10:40 +00002706 assert(Size <= 8 && "does not support size > 8");
2707 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2708 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian664da982009-03-13 00:42:52 +00002709 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2710 }
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +00002711 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2712 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner293c1d32009-04-17 22:12:36 +00002713 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00002714 src, dst, "weakassign");
2715 return;
2716}
2717
Fariborz Jahanian17958902008-11-19 00:59:10 +00002718/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2719/// objc_assign_global (id src, id *dst)
2720///
2721void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2722 llvm::Value *src, llvm::Value *dst)
2723{
Fariborz Jahanianad51ca02009-03-23 19:10:40 +00002724 const llvm::Type * SrcTy = src->getType();
2725 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsee6f6f82009-05-09 07:08:47 +00002726 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianad51ca02009-03-23 19:10:40 +00002727 assert(Size <= 8 && "does not support size > 8");
2728 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2729 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian664da982009-03-13 00:42:52 +00002730 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2731 }
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +00002732 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2733 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerf6ec7e42009-04-22 02:38:11 +00002734 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian17958902008-11-19 00:59:10 +00002735 src, dst, "globalassign");
2736 return;
2737}
2738
Fariborz Jahanianf310b592008-11-20 19:23:36 +00002739/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2740/// objc_assign_ivar (id src, id *dst)
2741///
2742void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2743 llvm::Value *src, llvm::Value *dst)
2744{
Fariborz Jahanianad51ca02009-03-23 19:10:40 +00002745 const llvm::Type * SrcTy = src->getType();
2746 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsee6f6f82009-05-09 07:08:47 +00002747 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianad51ca02009-03-23 19:10:40 +00002748 assert(Size <= 8 && "does not support size > 8");
2749 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2750 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian664da982009-03-13 00:42:52 +00002751 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2752 }
Fariborz Jahanianf310b592008-11-20 19:23:36 +00002753 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2754 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerf6ec7e42009-04-22 02:38:11 +00002755 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(),
Fariborz Jahanianf310b592008-11-20 19:23:36 +00002756 src, dst, "assignivar");
2757 return;
2758}
2759
Fariborz Jahanian17958902008-11-19 00:59:10 +00002760/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2761/// objc_assign_strongCast (id src, id *dst)
2762///
2763void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2764 llvm::Value *src, llvm::Value *dst)
2765{
Fariborz Jahanianad51ca02009-03-23 19:10:40 +00002766 const llvm::Type * SrcTy = src->getType();
2767 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsee6f6f82009-05-09 07:08:47 +00002768 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianad51ca02009-03-23 19:10:40 +00002769 assert(Size <= 8 && "does not support size > 8");
2770 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2771 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian664da982009-03-13 00:42:52 +00002772 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2773 }
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +00002774 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2775 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerf6ec7e42009-04-22 02:38:11 +00002776 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian17958902008-11-19 00:59:10 +00002777 src, dst, "weakassign");
2778 return;
2779}
2780
Fariborz Jahanian614e8f02009-07-08 01:18:33 +00002781void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
2782 llvm::Value *DestPtr,
2783 llvm::Value *SrcPtr,
2784 unsigned long size) {
2785 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
2786 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Owen Anderson73e7f802009-07-14 23:10:40 +00002787 llvm::Value *N = VMContext.getConstantInt(ObjCTypes.LongTy, size);
Fariborz Jahanian614e8f02009-07-08 01:18:33 +00002788 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
2789 DestPtr, SrcPtr, N);
2790 return;
2791}
2792
Fariborz Jahanian4337afe2009-02-02 20:02:29 +00002793/// EmitObjCValueForIvar - Code Gen for ivar reference.
2794///
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00002795LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2796 QualType ObjectTy,
2797 llvm::Value *BaseValue,
2798 const ObjCIvarDecl *Ivar,
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00002799 unsigned CVRQualifiers) {
Daniel Dunbarf5254bd2009-04-21 01:19:28 +00002800 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar85d37542009-04-22 07:32:20 +00002801 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
2802 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian4337afe2009-02-02 20:02:29 +00002803}
2804
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00002805llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar61e14a62009-04-22 05:08:15 +00002806 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00002807 const ObjCIvarDecl *Ivar) {
Daniel Dunbar85d37542009-04-22 07:32:20 +00002808 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson73e7f802009-07-14 23:10:40 +00002809 return VMContext.getConstantInt(
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00002810 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2811 Offset);
2812}
2813
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002814/* *** Private Interface *** */
2815
2816/// EmitImageInfo - Emit the image info marker used to encode some module
2817/// level information.
2818///
2819/// See: <rdr://4810609&4810587&4810587>
2820/// struct IMAGE_INFO {
2821/// unsigned version;
2822/// unsigned flags;
2823/// };
2824enum ImageInfoFlags {
Daniel Dunbarb79f5a92009-04-20 07:11:47 +00002825 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what
2826 // this implies.
2827 eImageInfo_GarbageCollected = (1 << 1),
2828 eImageInfo_GCOnly = (1 << 2),
2829 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
2830
2831 // A flag indicating that the module has no instances of an
2832 // @synthesize of a superclass variable. <rdar://problem/6803242>
2833 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002834};
2835
2836void CGObjCMac::EmitImageInfo() {
2837 unsigned version = 0; // Version is unused?
2838 unsigned flags = 0;
2839
2840 // FIXME: Fix and continue?
2841 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2842 flags |= eImageInfo_GarbageCollected;
2843 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2844 flags |= eImageInfo_GCOnly;
Daniel Dunbarb79f5a92009-04-20 07:11:47 +00002845
2846 // We never allow @synthesize of a superclass property.
2847 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002848
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002849 // Emitted as int[2];
2850 llvm::Constant *values[2] = {
Owen Anderson73e7f802009-07-14 23:10:40 +00002851 VMContext.getConstantInt(llvm::Type::Int32Ty, version),
2852 VMContext.getConstantInt(llvm::Type::Int32Ty, flags)
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002853 };
Owen Anderson73e7f802009-07-14 23:10:40 +00002854 llvm::ArrayType *AT = VMContext.getArrayType(llvm::Type::Int32Ty, 2);
Daniel Dunbar90d88f92009-03-09 21:49:58 +00002855
2856 const char *Section;
2857 if (ObjCABI == 1)
2858 Section = "__OBJC, __image_info,regular";
2859 else
2860 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002861 llvm::GlobalVariable *GV =
Daniel Dunbar90d88f92009-03-09 21:49:58 +00002862 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson73e7f802009-07-14 23:10:40 +00002863 VMContext.getConstantArray(AT, values, 2),
Daniel Dunbar90d88f92009-03-09 21:49:58 +00002864 Section,
2865 0,
2866 true);
2867 GV->setConstant(true);
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002868}
2869
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002870
2871// struct objc_module {
2872// unsigned long version;
2873// unsigned long size;
2874// const char *name;
2875// Symtab symtab;
2876// };
2877
2878// FIXME: Get from somewhere
2879static const int ModuleVersion = 7;
2880
2881void CGObjCMac::EmitModuleInfo() {
Duncan Sandsee6f6f82009-05-09 07:08:47 +00002882 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002883
2884 std::vector<llvm::Constant*> Values(4);
Owen Anderson73e7f802009-07-14 23:10:40 +00002885 Values[0] = VMContext.getConstantInt(ObjCTypes.LongTy, ModuleVersion);
2886 Values[1] = VMContext.getConstantInt(ObjCTypes.LongTy, Size);
Daniel Dunbarac93e472008-08-15 22:20:32 +00002887 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002888 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002889 Values[3] = EmitModuleSymbols();
Daniel Dunbar90d88f92009-03-09 21:49:58 +00002890 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson73e7f802009-07-14 23:10:40 +00002891 VMContext.getConstantStruct(ObjCTypes.ModuleTy, Values),
Daniel Dunbar90d88f92009-03-09 21:49:58 +00002892 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar56756c32009-03-09 22:18:41 +00002893 4, true);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002894}
2895
2896llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002897 unsigned NumClasses = DefinedClasses.size();
2898 unsigned NumCategories = DefinedCategories.size();
2899
Daniel Dunbar8ede0052008-08-25 06:02:07 +00002900 // Return null if no symbols were defined.
2901 if (!NumClasses && !NumCategories)
Owen Anderson5f1adc22009-07-13 04:10:07 +00002902 return VMContext.getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar8ede0052008-08-25 06:02:07 +00002903
2904 std::vector<llvm::Constant*> Values(5);
Owen Anderson73e7f802009-07-14 23:10:40 +00002905 Values[0] = VMContext.getConstantInt(ObjCTypes.LongTy, 0);
Owen Anderson5f1adc22009-07-13 04:10:07 +00002906 Values[1] = VMContext.getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson73e7f802009-07-14 23:10:40 +00002907 Values[2] = VMContext.getConstantInt(ObjCTypes.ShortTy, NumClasses);
2908 Values[3] = VMContext.getConstantInt(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002909
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00002910 // The runtime expects exactly the list of defined classes followed
2911 // by the list of defined categories, in a single array.
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002912 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00002913 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson73e7f802009-07-14 23:10:40 +00002914 Symbols[i] = VMContext.getConstantExprBitCast(DefinedClasses[i],
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00002915 ObjCTypes.Int8PtrTy);
2916 for (unsigned i=0; i<NumCategories; i++)
2917 Symbols[NumClasses + i] =
Owen Anderson73e7f802009-07-14 23:10:40 +00002918 VMContext.getConstantExprBitCast(DefinedCategories[i],
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00002919 ObjCTypes.Int8PtrTy);
2920
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002921 Values[4] =
Owen Anderson73e7f802009-07-14 23:10:40 +00002922 VMContext.getConstantArray(VMContext.getArrayType(ObjCTypes.Int8PtrTy,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002923 NumClasses + NumCategories),
2924 Symbols);
2925
Owen Anderson73e7f802009-07-14 23:10:40 +00002926 llvm::Constant *Init = VMContext.getConstantStruct(Values);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002927
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002928 llvm::GlobalVariable *GV =
Daniel Dunbar90d88f92009-03-09 21:49:58 +00002929 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
2930 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar9dfd3a72009-04-15 02:56:18 +00002931 4, true);
Owen Anderson73e7f802009-07-14 23:10:40 +00002932 return VMContext.getConstantExprBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002933}
2934
Daniel Dunbard916e6e2008-11-01 01:53:16 +00002935llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002936 const ObjCInterfaceDecl *ID) {
Daniel Dunbar8ede0052008-08-25 06:02:07 +00002937 LazySymbols.insert(ID->getIdentifier());
2938
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002939 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2940
2941 if (!Entry) {
2942 llvm::Constant *Casted =
Owen Anderson73e7f802009-07-14 23:10:40 +00002943 VMContext.getConstantExprBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002944 ObjCTypes.ClassPtrTy);
2945 Entry =
Daniel Dunbar90d88f92009-03-09 21:49:58 +00002946 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
2947 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar9dfd3a72009-04-15 02:56:18 +00002948 4, true);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002949 }
2950
2951 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002952}
2953
Daniel Dunbard916e6e2008-11-01 01:53:16 +00002954llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar5eec6142008-08-12 03:39:23 +00002955 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2956
2957 if (!Entry) {
2958 llvm::Constant *Casted =
Owen Anderson73e7f802009-07-14 23:10:40 +00002959 VMContext.getConstantExprBitCast(GetMethodVarName(Sel),
Daniel Dunbar5eec6142008-08-12 03:39:23 +00002960 ObjCTypes.SelectorPtrTy);
2961 Entry =
Daniel Dunbar90d88f92009-03-09 21:49:58 +00002962 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
2963 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar9dfd3a72009-04-15 02:56:18 +00002964 4, true);
Daniel Dunbar5eec6142008-08-12 03:39:23 +00002965 }
2966
2967 return Builder.CreateLoad(Entry, false, "tmp");
2968}
2969
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00002970llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002971 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002972
Daniel Dunbar90d88f92009-03-09 21:49:58 +00002973 if (!Entry)
2974 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Owen Anderson73e7f802009-07-14 23:10:40 +00002975 VMContext.getConstantArray(Ident->getName()),
Daniel Dunbar90d88f92009-03-09 21:49:58 +00002976 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarfbfd92a2009-04-14 23:14:47 +00002977 1, true);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002978
Owen Anderson73e7f802009-07-14 23:10:40 +00002979 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002980}
2981
Fariborz Jahanian7345eba2009-03-05 19:17:31 +00002982/// GetIvarLayoutName - Returns a unique constant for the given
2983/// ivar layout bitmap.
2984llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
2985 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson5f1adc22009-07-13 04:10:07 +00002986 return VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian7345eba2009-03-05 19:17:31 +00002987}
2988
Daniel Dunbar7b1da722009-05-03 14:10:34 +00002989static QualType::GCAttrTypes GetGCAttrTypeForType(ASTContext &Ctx,
2990 QualType FQT) {
Daniel Dunbar25acf212009-05-03 13:55:09 +00002991 if (FQT.isObjCGCStrong())
2992 return QualType::Strong;
2993
2994 if (FQT.isObjCGCWeak())
2995 return QualType::Weak;
2996
Steve Naroffad75bd22009-07-16 15:41:00 +00002997 if (FQT->isObjCObjectPointerType())
Daniel Dunbar25acf212009-05-03 13:55:09 +00002998 return QualType::Strong;
2999
Ted Kremenekd9b39bf2009-07-17 17:50:17 +00003000 if (const PointerType *PT = FQT->getAsPointerType())
Daniel Dunbar7b1da722009-05-03 14:10:34 +00003001 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
Daniel Dunbar25acf212009-05-03 13:55:09 +00003002
3003 return QualType::GCNone;
3004}
3005
Daniel Dunbar7b1da722009-05-03 14:10:34 +00003006void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
3007 unsigned int BytePos,
3008 bool ForStrongLayout,
3009 bool &HasUnion) {
3010 const RecordDecl *RD = RT->getDecl();
3011 // FIXME - Use iterator.
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00003012 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbar7b1da722009-05-03 14:10:34 +00003013 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
3014 const llvm::StructLayout *RecLayout =
3015 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
3016
3017 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3018 ForStrongLayout, HasUnion);
3019}
3020
Daniel Dunbarb1d3b8e2009-05-03 21:05:10 +00003021void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003022 const llvm::StructLayout *Layout,
Fariborz Jahanian37931062009-03-10 16:22:08 +00003023 const RecordDecl *RD,
Chris Lattner9329cf52009-03-31 08:48:01 +00003024 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahanian01b3e342009-03-05 22:39:55 +00003025 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003026 bool &HasUnion) {
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00003027 bool IsUnion = (RD && RD->isUnion());
3028 uint64_t MaxUnionIvarSize = 0;
3029 uint64_t MaxSkippedUnionIvarSize = 0;
3030 FieldDecl *MaxField = 0;
3031 FieldDecl *MaxSkippedField = 0;
Fariborz Jahanian7e052812009-04-21 18:33:06 +00003032 FieldDecl *LastFieldBitfield = 0;
Daniel Dunbar56bdd7b2009-05-03 23:31:46 +00003033 uint64_t MaxFieldOffset = 0;
3034 uint64_t MaxSkippedFieldOffset = 0;
3035 uint64_t LastBitfieldOffset = 0;
Fariborz Jahanian7e052812009-04-21 18:33:06 +00003036
Fariborz Jahanian37931062009-03-10 16:22:08 +00003037 if (RecFields.empty())
3038 return;
Chris Lattner9329cf52009-03-31 08:48:01 +00003039 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3040 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3041
Chris Lattner9329cf52009-03-31 08:48:01 +00003042 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian37931062009-03-10 16:22:08 +00003043 FieldDecl *Field = RecFields[i];
Daniel Dunbara15c71e2009-05-03 23:35:23 +00003044 uint64_t FieldOffset;
3045 if (RD)
3046 FieldOffset =
3047 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
3048 else
3049 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar9638d362009-05-03 14:17:18 +00003050
Fariborz Jahanian37931062009-03-10 16:22:08 +00003051 // Skip over unnamed or bitfields
Fariborz Jahanian7e052812009-04-21 18:33:06 +00003052 if (!Field->getIdentifier() || Field->isBitField()) {
3053 LastFieldBitfield = Field;
Daniel Dunbar56bdd7b2009-05-03 23:31:46 +00003054 LastBitfieldOffset = FieldOffset;
Fariborz Jahanian37931062009-03-10 16:22:08 +00003055 continue;
Fariborz Jahanian7e052812009-04-21 18:33:06 +00003056 }
Daniel Dunbar9638d362009-05-03 14:17:18 +00003057
Fariborz Jahanian7e052812009-04-21 18:33:06 +00003058 LastFieldBitfield = 0;
Fariborz Jahanian37931062009-03-10 16:22:08 +00003059 QualType FQT = Field->getType();
Fariborz Jahanian738ee712009-03-25 22:36:49 +00003060 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahanian37931062009-03-10 16:22:08 +00003061 if (FQT->isUnionType())
3062 HasUnion = true;
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00003063
Ted Kremenekd9b39bf2009-07-17 17:50:17 +00003064 BuildAggrIvarRecordLayout(FQT->getAsRecordType(),
Daniel Dunbar9638d362009-05-03 14:17:18 +00003065 BytePos + FieldOffset,
Daniel Dunbar7b1da722009-05-03 14:10:34 +00003066 ForStrongLayout, HasUnion);
Fariborz Jahanian37931062009-03-10 16:22:08 +00003067 continue;
3068 }
Chris Lattner9329cf52009-03-31 08:48:01 +00003069
3070 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00003071 const ConstantArrayType *CArray =
Daniel Dunbar25acf212009-05-03 13:55:09 +00003072 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7e052812009-04-21 18:33:06 +00003073 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar25acf212009-05-03 13:55:09 +00003074 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00003075 FQT = CArray->getElementType();
Fariborz Jahanian738ee712009-03-25 22:36:49 +00003076 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3077 const ConstantArrayType *CArray =
Daniel Dunbar25acf212009-05-03 13:55:09 +00003078 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7e052812009-04-21 18:33:06 +00003079 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian738ee712009-03-25 22:36:49 +00003080 FQT = CArray->getElementType();
3081 }
3082
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00003083 assert(!FQT->isUnionType() &&
3084 "layout for array of unions not supported");
3085 if (FQT->isRecordType()) {
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003086 int OldIndex = IvarsInfo.size() - 1;
3087 int OldSkIndex = SkipIvars.size() -1;
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00003088
Ted Kremenekd9b39bf2009-07-17 17:50:17 +00003089 const RecordType *RT = FQT->getAsRecordType();
Daniel Dunbar9638d362009-05-03 14:17:18 +00003090 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbar7b1da722009-05-03 14:10:34 +00003091 ForStrongLayout, HasUnion);
3092
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00003093 // Replicate layout information for each array element. Note that
3094 // one element is already done.
3095 uint64_t ElIx = 1;
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003096 for (int FirstIndex = IvarsInfo.size() - 1,
3097 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003098 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar59df30d2009-05-03 13:44:42 +00003099 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3100 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3101 IvarsInfo[i].ivar_size));
3102 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3103 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3104 SkipIvars[i].ivar_size));
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00003105 }
3106 continue;
3107 }
Fariborz Jahanian37931062009-03-10 16:22:08 +00003108 }
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00003109 // At this point, we are done with Record/Union and array there of.
3110 // For other arrays we are down to its element type.
Daniel Dunbar7b1da722009-05-03 14:10:34 +00003111 QualType::GCAttrTypes GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar25acf212009-05-03 13:55:09 +00003112
Daniel Dunbar59df30d2009-05-03 13:44:42 +00003113 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00003114 if ((ForStrongLayout && GCAttr == QualType::Strong)
3115 || (!ForStrongLayout && GCAttr == QualType::Weak)) {
Daniel Dunbar736f47f2009-05-03 13:32:01 +00003116 if (IsUnion) {
Daniel Dunbar59df30d2009-05-03 13:44:42 +00003117 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar736f47f2009-05-03 13:32:01 +00003118 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00003119 MaxUnionIvarSize = UnionIvarSize;
3120 MaxField = Field;
Daniel Dunbar56bdd7b2009-05-03 23:31:46 +00003121 MaxFieldOffset = FieldOffset;
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00003122 }
Daniel Dunbar736f47f2009-05-03 13:32:01 +00003123 } else {
Daniel Dunbar9638d362009-05-03 14:17:18 +00003124 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar59df30d2009-05-03 13:44:42 +00003125 FieldSize / WordSizeInBits));
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00003126 }
Daniel Dunbar736f47f2009-05-03 13:32:01 +00003127 } else if ((ForStrongLayout &&
3128 (GCAttr == QualType::GCNone || GCAttr == QualType::Weak))
3129 || (!ForStrongLayout && GCAttr != QualType::Weak)) {
3130 if (IsUnion) {
Mike Stumpba2cb0e2009-05-16 07:57:57 +00003131 // FIXME: Why the asymmetry? We divide by word size in bits on other
3132 // side.
Daniel Dunbar59df30d2009-05-03 13:44:42 +00003133 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar736f47f2009-05-03 13:32:01 +00003134 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00003135 MaxSkippedUnionIvarSize = UnionIvarSize;
3136 MaxSkippedField = Field;
Daniel Dunbar56bdd7b2009-05-03 23:31:46 +00003137 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00003138 }
Daniel Dunbar736f47f2009-05-03 13:32:01 +00003139 } else {
Daniel Dunbar59df30d2009-05-03 13:44:42 +00003140 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar9638d362009-05-03 14:17:18 +00003141 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar59df30d2009-05-03 13:44:42 +00003142 FieldSize / ByteSizeInBits));
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00003143 }
3144 }
3145 }
Daniel Dunbar7b1da722009-05-03 14:10:34 +00003146
Fariborz Jahanian7e052812009-04-21 18:33:06 +00003147 if (LastFieldBitfield) {
3148 // Last field was a bitfield. Must update skip info.
Fariborz Jahanian7e052812009-04-21 18:33:06 +00003149 Expr *BitWidth = LastFieldBitfield->getBitWidth();
3150 uint64_t BitFieldSize =
Eli Friedman5255e7a2009-04-26 19:19:15 +00003151 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Daniel Dunbar736f47f2009-05-03 13:32:01 +00003152 GC_IVAR skivar;
Daniel Dunbar56bdd7b2009-05-03 23:31:46 +00003153 skivar.ivar_bytepos = BytePos + LastBitfieldOffset;
Fariborz Jahanian7e052812009-04-21 18:33:06 +00003154 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3155 + ((BitFieldSize % ByteSizeInBits) != 0);
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003156 SkipIvars.push_back(skivar);
Fariborz Jahanian7e052812009-04-21 18:33:06 +00003157 }
3158
Daniel Dunbar59df30d2009-05-03 13:44:42 +00003159 if (MaxField)
Daniel Dunbar56bdd7b2009-05-03 23:31:46 +00003160 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar59df30d2009-05-03 13:44:42 +00003161 MaxUnionIvarSize));
3162 if (MaxSkippedField)
Daniel Dunbar56bdd7b2009-05-03 23:31:46 +00003163 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar59df30d2009-05-03 13:44:42 +00003164 MaxSkippedUnionIvarSize));
Fariborz Jahanian01b3e342009-03-05 22:39:55 +00003165}
3166
3167/// BuildIvarLayout - Builds ivar layout bitmap for the class
3168/// implementation for the __strong or __weak case.
3169/// The layout map displays which words in ivar list must be skipped
3170/// and which must be scanned by GC (see below). String is built of bytes.
3171/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3172/// of words to skip and right nibble is count of words to scan. So, each
3173/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3174/// represented by a 0x00 byte which also ends the string.
3175/// 1. when ForStrongLayout is true, following ivars are scanned:
3176/// - id, Class
3177/// - object *
3178/// - __strong anything
3179///
3180/// 2. When ForStrongLayout is false, following ivars are scanned:
3181/// - __weak anything
3182///
Fariborz Jahanian37931062009-03-10 16:22:08 +00003183llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003184 const ObjCImplementationDecl *OMD,
3185 bool ForStrongLayout) {
Fariborz Jahanian01b3e342009-03-05 22:39:55 +00003186 bool hasUnion = false;
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003187
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003188 unsigned int WordsToScan, WordsToSkip;
Owen Anderson73e7f802009-07-14 23:10:40 +00003189 const llvm::Type *PtrTy = VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty);
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003190 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
Owen Anderson5f1adc22009-07-13 04:10:07 +00003191 return VMContext.getNullValue(PtrTy);
Fariborz Jahanian01b3e342009-03-05 22:39:55 +00003192
Chris Lattner9329cf52009-03-31 08:48:01 +00003193 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003194 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahanian01b3e342009-03-05 22:39:55 +00003195 CGM.getContext().CollectObjCIvars(OI, RecFields);
Fariborz Jahanian02ebfa82009-05-12 18:14:29 +00003196
Daniel Dunbar3d091d62009-05-04 04:10:48 +00003197 // Add this implementations synthesized ivars.
Fariborz Jahanian02ebfa82009-05-12 18:14:29 +00003198 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
3199 CGM.getContext().CollectSynthesizedIvars(OI, Ivars);
3200 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3201 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3202
Fariborz Jahanian01b3e342009-03-05 22:39:55 +00003203 if (RecFields.empty())
Owen Anderson5f1adc22009-07-13 04:10:07 +00003204 return VMContext.getNullValue(PtrTy);
Chris Lattner9329cf52009-03-31 08:48:01 +00003205
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003206 SkipIvars.clear();
3207 IvarsInfo.clear();
Fariborz Jahanian6d49ab62009-03-11 21:42:00 +00003208
Daniel Dunbarb1d3b8e2009-05-03 21:05:10 +00003209 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003210 if (IvarsInfo.empty())
Owen Anderson5f1adc22009-07-13 04:10:07 +00003211 return VMContext.getNullValue(PtrTy);
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003212
3213 // Sort on byte position in case we encounterred a union nested in
3214 // the ivar list.
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003215 if (hasUnion && !IvarsInfo.empty())
Daniel Dunbar48445182009-04-23 01:29:05 +00003216 std::sort(IvarsInfo.begin(), IvarsInfo.end());
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003217 if (hasUnion && !SkipIvars.empty())
Daniel Dunbar48445182009-04-23 01:29:05 +00003218 std::sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003219
3220 // Build the string of skip/scan nibbles
Fariborz Jahanian1fb3c2d2009-04-24 17:15:27 +00003221 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003222 unsigned int WordSize =
Duncan Sandsee6f6f82009-05-09 07:08:47 +00003223 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003224 if (IvarsInfo[0].ivar_bytepos == 0) {
3225 WordsToSkip = 0;
3226 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar5e773be2009-05-03 23:21:22 +00003227 } else {
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003228 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3229 WordsToScan = IvarsInfo[0].ivar_size;
3230 }
Daniel Dunbar5e773be2009-05-03 23:21:22 +00003231 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003232 unsigned int TailPrevGCObjC =
3233 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar5e773be2009-05-03 23:21:22 +00003234 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003235 // consecutive 'scanned' object pointers.
3236 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar5e773be2009-05-03 23:21:22 +00003237 } else {
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003238 // Skip over 'gc'able object pointer which lay over each other.
3239 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3240 continue;
3241 // Must skip over 1 or more words. We save current skip/scan values
3242 // and start a new pair.
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003243 SKIP_SCAN SkScan;
3244 SkScan.skip = WordsToSkip;
3245 SkScan.scan = WordsToScan;
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003246 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003247
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003248 // Skip the hole.
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003249 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3250 SkScan.scan = 0;
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003251 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003252 WordsToSkip = 0;
3253 WordsToScan = IvarsInfo[i].ivar_size;
3254 }
3255 }
Daniel Dunbar5e773be2009-05-03 23:21:22 +00003256 if (WordsToScan > 0) {
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003257 SKIP_SCAN SkScan;
3258 SkScan.skip = WordsToSkip;
3259 SkScan.scan = WordsToScan;
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003260 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003261 }
3262
3263 bool BytesSkipped = false;
Daniel Dunbar5e773be2009-05-03 23:21:22 +00003264 if (!SkipIvars.empty()) {
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003265 unsigned int LastIndex = SkipIvars.size()-1;
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003266 int LastByteSkipped =
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003267 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
3268 LastIndex = IvarsInfo.size()-1;
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003269 int LastByteScanned =
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003270 IvarsInfo[LastIndex].ivar_bytepos +
3271 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003272 BytesSkipped = (LastByteSkipped > LastByteScanned);
3273 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Daniel Dunbar5e773be2009-05-03 23:21:22 +00003274 if (BytesSkipped) {
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003275 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003276 SKIP_SCAN SkScan;
3277 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3278 SkScan.scan = 0;
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003279 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003280 }
3281 }
3282 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3283 // as 0xMN.
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003284 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar5e773be2009-05-03 23:21:22 +00003285 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003286 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3287 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3288 // 0xM0 followed by 0x0N detected.
3289 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3290 for (int j = i+1; j < SkipScan; j++)
3291 SkipScanIvars[j] = SkipScanIvars[j+1];
3292 --SkipScan;
3293 }
3294 }
3295
3296 // Generate the string.
3297 std::string BitMap;
Daniel Dunbar5e773be2009-05-03 23:21:22 +00003298 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003299 unsigned char byte;
3300 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3301 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3302 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3303 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
3304
3305 if (skip_small > 0 || skip_big > 0)
3306 BytesSkipped = true;
3307 // first skip big.
3308 for (unsigned int ix = 0; ix < skip_big; ix++)
3309 BitMap += (unsigned char)(0xf0);
3310
3311 // next (skip small, scan)
Daniel Dunbar5e773be2009-05-03 23:21:22 +00003312 if (skip_small) {
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003313 byte = skip_small << 4;
Daniel Dunbar5e773be2009-05-03 23:21:22 +00003314 if (scan_big > 0) {
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003315 byte |= 0xf;
3316 --scan_big;
Daniel Dunbar5e773be2009-05-03 23:21:22 +00003317 } else if (scan_small) {
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003318 byte |= scan_small;
3319 scan_small = 0;
3320 }
3321 BitMap += byte;
3322 }
3323 // next scan big
3324 for (unsigned int ix = 0; ix < scan_big; ix++)
3325 BitMap += (unsigned char)(0x0f);
3326 // last scan small
Daniel Dunbar5e773be2009-05-03 23:21:22 +00003327 if (scan_small) {
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003328 byte = scan_small;
3329 BitMap += byte;
3330 }
3331 }
3332 // null terminate string.
Fariborz Jahanian738ee712009-03-25 22:36:49 +00003333 unsigned char zero = 0;
3334 BitMap += zero;
Fariborz Jahanian31614742009-04-20 22:03:45 +00003335
3336 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
3337 printf("\n%s ivar layout for class '%s': ",
3338 ForStrongLayout ? "strong" : "weak",
3339 OMD->getClassInterface()->getNameAsCString());
3340 const unsigned char *s = (unsigned char*)BitMap.c_str();
3341 for (unsigned i = 0; i < BitMap.size(); i++)
3342 if (!(s[i] & 0xf0))
3343 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3344 else
3345 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3346 printf("\n");
3347 }
3348
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003349 // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as
3350 // final layout.
3351 if (ForStrongLayout && !BytesSkipped)
Owen Anderson5f1adc22009-07-13 04:10:07 +00003352 return VMContext.getNullValue(PtrTy);
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003353 llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Owen Anderson73e7f802009-07-14 23:10:40 +00003354 VMContext.getConstantArray(BitMap.c_str()),
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003355 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarfbfd92a2009-04-14 23:14:47 +00003356 1, true);
Owen Anderson73e7f802009-07-14 23:10:40 +00003357 return getConstantGEP(VMContext, Entry, 0, 0);
Fariborz Jahanian01b3e342009-03-05 22:39:55 +00003358}
3359
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00003360llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar5eec6142008-08-12 03:39:23 +00003361 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3362
Daniel Dunbar90d88f92009-03-09 21:49:58 +00003363 // FIXME: Avoid std::string copying.
3364 if (!Entry)
3365 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson73e7f802009-07-14 23:10:40 +00003366 VMContext.getConstantArray(Sel.getAsString()),
Daniel Dunbar90d88f92009-03-09 21:49:58 +00003367 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarfbfd92a2009-04-14 23:14:47 +00003368 1, true);
Daniel Dunbar5eec6142008-08-12 03:39:23 +00003369
Owen Anderson73e7f802009-07-14 23:10:40 +00003370 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003371}
3372
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003373// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00003374llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003375 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3376}
3377
3378// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00003379llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003380 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
3381}
3382
Daniel Dunbar356f0742009-04-20 06:54:31 +00003383llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel593a07a2009-03-04 18:21:39 +00003384 std::string TypeStr;
3385 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3386
3387 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003388
Daniel Dunbar90d88f92009-03-09 21:49:58 +00003389 if (!Entry)
3390 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson73e7f802009-07-14 23:10:40 +00003391 VMContext.getConstantArray(TypeStr),
Daniel Dunbar90d88f92009-03-09 21:49:58 +00003392 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarfbfd92a2009-04-14 23:14:47 +00003393 1, true);
Daniel Dunbar90d88f92009-03-09 21:49:58 +00003394
Owen Anderson73e7f802009-07-14 23:10:40 +00003395 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar5eec6142008-08-12 03:39:23 +00003396}
3397
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00003398llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003399 std::string TypeStr;
Daniel Dunbar12996f52008-08-26 21:51:14 +00003400 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3401 TypeStr);
Devang Patel593a07a2009-03-04 18:21:39 +00003402
3403 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3404
Daniel Dunbarfbfd92a2009-04-14 23:14:47 +00003405 if (!Entry)
3406 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson73e7f802009-07-14 23:10:40 +00003407 VMContext.getConstantArray(TypeStr),
Daniel Dunbarfbfd92a2009-04-14 23:14:47 +00003408 "__TEXT,__cstring,cstring_literals",
3409 1, true);
Devang Patel593a07a2009-03-04 18:21:39 +00003410
Owen Anderson73e7f802009-07-14 23:10:40 +00003411 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003412}
3413
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00003414// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00003415llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00003416 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
3417
Daniel Dunbar90d88f92009-03-09 21:49:58 +00003418 if (!Entry)
3419 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Owen Anderson73e7f802009-07-14 23:10:40 +00003420 VMContext.getConstantArray(Ident->getName()),
Daniel Dunbar90d88f92009-03-09 21:49:58 +00003421 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarfbfd92a2009-04-14 23:14:47 +00003422 1, true);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00003423
Owen Anderson73e7f802009-07-14 23:10:40 +00003424 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00003425}
3426
3427// FIXME: Merge into a single cstring creation function.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00003428// FIXME: This Decl should be more precise.
Daniel Dunbar90d88f92009-03-09 21:49:58 +00003429llvm::Constant *
3430 CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3431 const Decl *Container) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00003432 std::string TypeStr;
3433 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00003434 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3435}
3436
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00003437void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
3438 const ObjCContainerDecl *CD,
3439 std::string &NameOut) {
Daniel Dunbara2d275d2009-04-07 05:48:37 +00003440 NameOut = '\01';
3441 NameOut += (D->isInstanceMethod() ? '-' : '+');
Chris Lattner3a8f2942008-11-24 03:33:13 +00003442 NameOut += '[';
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +00003443 assert (CD && "Missing container decl in GetNameForMethod");
3444 NameOut += CD->getNameAsString();
Fariborz Jahanian6e4b7372009-04-16 18:34:20 +00003445 if (const ObjCCategoryImplDecl *CID =
3446 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) {
3447 NameOut += '(';
3448 NameOut += CID->getNameAsString();
3449 NameOut+= ')';
3450 }
Chris Lattner3a8f2942008-11-24 03:33:13 +00003451 NameOut += ' ';
3452 NameOut += D->getSelector().getAsString();
3453 NameOut += ']';
Daniel Dunbarace33292008-08-16 03:19:19 +00003454}
3455
Daniel Dunbar1be1df32008-08-11 21:35:06 +00003456void CGObjCMac::FinishModule() {
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00003457 EmitModuleInfo();
3458
Daniel Dunbar35b777f2008-10-29 22:36:39 +00003459 // Emit the dummy bodies for any protocols which were referenced but
3460 // never defined.
3461 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattner5f38e5d2009-07-17 23:57:13 +00003462 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
3463 if (I->second->hasInitializer())
Daniel Dunbar35b777f2008-10-29 22:36:39 +00003464 continue;
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00003465
Daniel Dunbar35b777f2008-10-29 22:36:39 +00003466 std::vector<llvm::Constant*> Values(5);
Owen Anderson5f1adc22009-07-13 04:10:07 +00003467 Values[0] = VMContext.getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattner5f38e5d2009-07-17 23:57:13 +00003468 Values[1] = GetClassName(I->first);
Owen Anderson5f1adc22009-07-13 04:10:07 +00003469 Values[2] = VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar35b777f2008-10-29 22:36:39 +00003470 Values[3] = Values[4] =
Owen Anderson5f1adc22009-07-13 04:10:07 +00003471 VMContext.getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattner5f38e5d2009-07-17 23:57:13 +00003472 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
3473 I->second->setInitializer(VMContext.getConstantStruct(ObjCTypes.ProtocolTy,
Daniel Dunbar35b777f2008-10-29 22:36:39 +00003474 Values));
Chris Lattner5f38e5d2009-07-17 23:57:13 +00003475 CGM.AddUsedGlobal(I->second);
Daniel Dunbar35b777f2008-10-29 22:36:39 +00003476 }
3477
Daniel Dunbar8ede0052008-08-25 06:02:07 +00003478 // Add assembler directives to add lazy undefined symbol references
3479 // for classes which are referenced but not defined. This is
3480 // important for correct linker interaction.
3481
3482 // FIXME: Uh, this isn't particularly portable.
3483 std::stringstream s;
Anders Carlsson63f98352008-12-10 02:21:04 +00003484
3485 if (!CGM.getModule().getModuleInlineAsm().empty())
3486 s << "\n";
3487
Chris Lattner5f38e5d2009-07-17 23:57:13 +00003488 for (std::set<IdentifierInfo*>::iterator I = LazySymbols.begin(),
3489 e = LazySymbols.end(); I != e; ++I) {
3490 s << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Daniel Dunbar8ede0052008-08-25 06:02:07 +00003491 }
Chris Lattner5f38e5d2009-07-17 23:57:13 +00003492 for (std::set<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
3493 e = DefinedSymbols.end(); I != e; ++I) {
3494 s << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
3495 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Daniel Dunbar8ede0052008-08-25 06:02:07 +00003496 }
Anders Carlsson63f98352008-12-10 02:21:04 +00003497
Daniel Dunbar8ede0052008-08-25 06:02:07 +00003498 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbar1be1df32008-08-11 21:35:06 +00003499}
3500
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003501CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian48543f52009-01-21 22:04:16 +00003502 : CGObjCCommonMac(cgm),
3503 ObjCTypes(cgm)
3504{
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003505 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian48543f52009-01-21 22:04:16 +00003506 ObjCABI = 2;
3507}
3508
Daniel Dunbar1be1df32008-08-11 21:35:06 +00003509/* *** */
3510
Fariborz Jahanian48543f52009-01-21 22:04:16 +00003511ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Owen Anderson73e7f802009-07-14 23:10:40 +00003512: VMContext(cgm.getLLVMContext()), CGM(cgm)
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00003513{
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00003514 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3515 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian48543f52009-01-21 22:04:16 +00003516
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003517 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003518 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00003519 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanianad51ca02009-03-23 19:10:40 +00003520 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Owen Anderson73e7f802009-07-14 23:10:40 +00003521 Int8PtrTy = VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003522
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00003523 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson73e7f802009-07-14 23:10:40 +00003524 PtrObjectPtrTy = VMContext.getPointerTypeUnqual(ObjectPtrTy);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00003525 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003526
Mike Stumpba2cb0e2009-05-16 07:57:57 +00003527 // FIXME: It would be nice to unify this with the opaque type, so that the IR
3528 // comes out a bit cleaner.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003529 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson73e7f802009-07-14 23:10:40 +00003530 ExternalProtocolPtrTy = VMContext.getPointerTypeUnqual(T);
Fariborz Jahanian48543f52009-01-21 22:04:16 +00003531
3532 // I'm not sure I like this. The implicit coordination is a bit
3533 // gross. We should solve this in a reasonable fashion because this
3534 // is a pretty common task (match some runtime data structure with
3535 // an LLVM data structure).
3536
3537 // FIXME: This is leaked.
3538 // FIXME: Merge with rewriter code?
3539
3540 // struct _objc_super {
3541 // id self;
3542 // Class cls;
3543 // }
3544 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3545 SourceLocation(),
3546 &Ctx.Idents.get("_objc_super"));
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00003547 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Douglas Gregorc55b0b02009-04-09 21:40:53 +00003548 Ctx.getObjCIdType(), 0, false));
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00003549 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Douglas Gregorc55b0b02009-04-09 21:40:53 +00003550 Ctx.getObjCClassType(), 0, false));
Fariborz Jahanian48543f52009-01-21 22:04:16 +00003551 RD->completeDefinition(Ctx);
3552
3553 SuperCTy = Ctx.getTagDeclType(RD);
3554 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
3555
3556 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Owen Anderson73e7f802009-07-14 23:10:40 +00003557 SuperPtrTy = VMContext.getPointerTypeUnqual(SuperTy);
Fariborz Jahanian4b161702009-01-22 00:37:21 +00003558
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003559 // struct _prop_t {
3560 // char *name;
3561 // char *attributes;
3562 // }
Owen Anderson73e7f802009-07-14 23:10:40 +00003563 PropertyTy = VMContext.getStructType(Int8PtrTy, Int8PtrTy, NULL);
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003564 CGM.getModule().addTypeName("struct._prop_t",
3565 PropertyTy);
3566
3567 // struct _prop_list_t {
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003568 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003569 // uint32_t count_of_properties;
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003570 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003571 // }
Owen Anderson73e7f802009-07-14 23:10:40 +00003572 PropertyListTy = VMContext.getStructType(IntTy,
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003573 IntTy,
Owen Anderson73e7f802009-07-14 23:10:40 +00003574 VMContext.getArrayType(PropertyTy, 0),
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003575 NULL);
3576 CGM.getModule().addTypeName("struct._prop_list_t",
3577 PropertyListTy);
3578 // struct _prop_list_t *
Owen Anderson73e7f802009-07-14 23:10:40 +00003579 PropertyListPtrTy = VMContext.getPointerTypeUnqual(PropertyListTy);
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003580
3581 // struct _objc_method {
3582 // SEL _cmd;
3583 // char *method_type;
3584 // char *_imp;
3585 // }
Owen Anderson73e7f802009-07-14 23:10:40 +00003586 MethodTy = VMContext.getStructType(SelectorPtrTy,
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003587 Int8PtrTy,
3588 Int8PtrTy,
3589 NULL);
3590 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003591
3592 // struct _objc_cache *
Owen Anderson73e7f802009-07-14 23:10:40 +00003593 CacheTy = VMContext.getOpaqueType();
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003594 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson73e7f802009-07-14 23:10:40 +00003595 CachePtrTy = VMContext.getPointerTypeUnqual(CacheTy);
Fariborz Jahanian48543f52009-01-21 22:04:16 +00003596}
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00003597
Fariborz Jahanian48543f52009-01-21 22:04:16 +00003598ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
3599 : ObjCCommonTypesHelper(cgm)
3600{
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003601 // struct _objc_method_description {
3602 // SEL name;
3603 // char *types;
3604 // }
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003605 MethodDescriptionTy =
Owen Anderson73e7f802009-07-14 23:10:40 +00003606 VMContext.getStructType(SelectorPtrTy,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003607 Int8PtrTy,
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003608 NULL);
3609 CGM.getModule().addTypeName("struct._objc_method_description",
3610 MethodDescriptionTy);
3611
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003612 // struct _objc_method_description_list {
3613 // int count;
3614 // struct _objc_method_description[1];
3615 // }
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003616 MethodDescriptionListTy =
Owen Anderson73e7f802009-07-14 23:10:40 +00003617 VMContext.getStructType(IntTy,
3618 VMContext.getArrayType(MethodDescriptionTy, 0),
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003619 NULL);
3620 CGM.getModule().addTypeName("struct._objc_method_description_list",
3621 MethodDescriptionListTy);
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003622
3623 // struct _objc_method_description_list *
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003624 MethodDescriptionListPtrTy =
Owen Anderson73e7f802009-07-14 23:10:40 +00003625 VMContext.getPointerTypeUnqual(MethodDescriptionListTy);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003626
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003627 // Protocol description structures
3628
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003629 // struct _objc_protocol_extension {
3630 // uint32_t size; // sizeof(struct _objc_protocol_extension)
3631 // struct _objc_method_description_list *optional_instance_methods;
3632 // struct _objc_method_description_list *optional_class_methods;
3633 // struct _objc_property_list *instance_properties;
3634 // }
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003635 ProtocolExtensionTy =
Owen Anderson73e7f802009-07-14 23:10:40 +00003636 VMContext.getStructType(IntTy,
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003637 MethodDescriptionListPtrTy,
3638 MethodDescriptionListPtrTy,
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003639 PropertyListPtrTy,
3640 NULL);
3641 CGM.getModule().addTypeName("struct._objc_protocol_extension",
3642 ProtocolExtensionTy);
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003643
3644 // struct _objc_protocol_extension *
Owen Anderson73e7f802009-07-14 23:10:40 +00003645 ProtocolExtensionPtrTy = VMContext.getPointerTypeUnqual(ProtocolExtensionTy);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003646
Daniel Dunbar35b777f2008-10-29 22:36:39 +00003647 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003648
Owen Anderson73e7f802009-07-14 23:10:40 +00003649 llvm::PATypeHolder ProtocolTyHolder = VMContext.getOpaqueType();
3650 llvm::PATypeHolder ProtocolListTyHolder = VMContext.getOpaqueType();
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003651
Fariborz Jahanian48543f52009-01-21 22:04:16 +00003652 const llvm::Type *T =
Owen Anderson73e7f802009-07-14 23:10:40 +00003653 VMContext.getStructType(VMContext.getPointerTypeUnqual(ProtocolListTyHolder),
Fariborz Jahanian48543f52009-01-21 22:04:16 +00003654 LongTy,
Owen Anderson73e7f802009-07-14 23:10:40 +00003655 VMContext.getArrayType(ProtocolTyHolder, 0),
Fariborz Jahanian48543f52009-01-21 22:04:16 +00003656 NULL);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003657 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
3658
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003659 // struct _objc_protocol {
3660 // struct _objc_protocol_extension *isa;
3661 // char *protocol_name;
3662 // struct _objc_protocol **_objc_protocol_list;
3663 // struct _objc_method_description_list *instance_methods;
3664 // struct _objc_method_description_list *class_methods;
3665 // }
Owen Anderson73e7f802009-07-14 23:10:40 +00003666 T = VMContext.getStructType(ProtocolExtensionPtrTy,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003667 Int8PtrTy,
Owen Anderson73e7f802009-07-14 23:10:40 +00003668 VMContext.getPointerTypeUnqual(ProtocolListTyHolder),
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003669 MethodDescriptionListPtrTy,
3670 MethodDescriptionListPtrTy,
3671 NULL);
3672 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
3673
3674 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
3675 CGM.getModule().addTypeName("struct._objc_protocol_list",
3676 ProtocolListTy);
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003677 // struct _objc_protocol_list *
Owen Anderson73e7f802009-07-14 23:10:40 +00003678 ProtocolListPtrTy = VMContext.getPointerTypeUnqual(ProtocolListTy);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003679
3680 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003681 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson73e7f802009-07-14 23:10:40 +00003682 ProtocolPtrTy = VMContext.getPointerTypeUnqual(ProtocolTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003683
3684 // Class description structures
3685
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003686 // struct _objc_ivar {
3687 // char *ivar_name;
3688 // char *ivar_type;
3689 // int ivar_offset;
3690 // }
Owen Anderson73e7f802009-07-14 23:10:40 +00003691 IvarTy = VMContext.getStructType(Int8PtrTy,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003692 Int8PtrTy,
3693 IntTy,
3694 NULL);
3695 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
3696
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003697 // struct _objc_ivar_list *
Owen Anderson73e7f802009-07-14 23:10:40 +00003698 IvarListTy = VMContext.getOpaqueType();
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003699 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson73e7f802009-07-14 23:10:40 +00003700 IvarListPtrTy = VMContext.getPointerTypeUnqual(IvarListTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003701
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003702 // struct _objc_method_list *
Owen Anderson73e7f802009-07-14 23:10:40 +00003703 MethodListTy = VMContext.getOpaqueType();
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003704 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson73e7f802009-07-14 23:10:40 +00003705 MethodListPtrTy = VMContext.getPointerTypeUnqual(MethodListTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003706
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003707 // struct _objc_class_extension *
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003708 ClassExtensionTy =
Owen Anderson73e7f802009-07-14 23:10:40 +00003709 VMContext.getStructType(IntTy,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003710 Int8PtrTy,
3711 PropertyListPtrTy,
3712 NULL);
3713 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson73e7f802009-07-14 23:10:40 +00003714 ClassExtensionPtrTy = VMContext.getPointerTypeUnqual(ClassExtensionTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003715
Owen Anderson73e7f802009-07-14 23:10:40 +00003716 llvm::PATypeHolder ClassTyHolder = VMContext.getOpaqueType();
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003717
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003718 // struct _objc_class {
3719 // Class isa;
3720 // Class super_class;
3721 // char *name;
3722 // long version;
3723 // long info;
3724 // long instance_size;
3725 // struct _objc_ivar_list *ivars;
3726 // struct _objc_method_list *methods;
3727 // struct _objc_cache *cache;
3728 // struct _objc_protocol_list *protocols;
3729 // char *ivar_layout;
3730 // struct _objc_class_ext *ext;
3731 // };
Owen Anderson73e7f802009-07-14 23:10:40 +00003732 T = VMContext.getStructType(VMContext.getPointerTypeUnqual(ClassTyHolder),
3733 VMContext.getPointerTypeUnqual(ClassTyHolder),
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003734 Int8PtrTy,
3735 LongTy,
3736 LongTy,
3737 LongTy,
3738 IvarListPtrTy,
3739 MethodListPtrTy,
3740 CachePtrTy,
3741 ProtocolListPtrTy,
3742 Int8PtrTy,
3743 ClassExtensionPtrTy,
3744 NULL);
3745 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
3746
3747 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
3748 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson73e7f802009-07-14 23:10:40 +00003749 ClassPtrTy = VMContext.getPointerTypeUnqual(ClassTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003750
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003751 // struct _objc_category {
3752 // char *category_name;
3753 // char *class_name;
3754 // struct _objc_method_list *instance_method;
3755 // struct _objc_method_list *class_method;
3756 // uint32_t size; // sizeof(struct _objc_category)
3757 // struct _objc_property_list *instance_properties;// category's @property
3758 // }
Owen Anderson73e7f802009-07-14 23:10:40 +00003759 CategoryTy = VMContext.getStructType(Int8PtrTy,
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00003760 Int8PtrTy,
3761 MethodListPtrTy,
3762 MethodListPtrTy,
3763 ProtocolListPtrTy,
3764 IntTy,
3765 PropertyListPtrTy,
3766 NULL);
3767 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
3768
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003769 // Global metadata structures
3770
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003771 // struct _objc_symtab {
3772 // long sel_ref_cnt;
3773 // SEL *refs;
3774 // short cls_def_cnt;
3775 // short cat_def_cnt;
3776 // char *defs[cls_def_cnt + cat_def_cnt];
3777 // }
Owen Anderson73e7f802009-07-14 23:10:40 +00003778 SymtabTy = VMContext.getStructType(LongTy,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003779 SelectorPtrTy,
3780 ShortTy,
3781 ShortTy,
Owen Anderson73e7f802009-07-14 23:10:40 +00003782 VMContext.getArrayType(Int8PtrTy, 0),
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003783 NULL);
3784 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson73e7f802009-07-14 23:10:40 +00003785 SymtabPtrTy = VMContext.getPointerTypeUnqual(SymtabTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003786
Fariborz Jahanian4b161702009-01-22 00:37:21 +00003787 // struct _objc_module {
3788 // long version;
3789 // long size; // sizeof(struct _objc_module)
3790 // char *name;
3791 // struct _objc_symtab* symtab;
3792 // }
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003793 ModuleTy =
Owen Anderson73e7f802009-07-14 23:10:40 +00003794 VMContext.getStructType(LongTy,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003795 LongTy,
3796 Int8PtrTy,
3797 SymtabPtrTy,
3798 NULL);
3799 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar87062ff2008-08-23 09:25:55 +00003800
Anders Carlsson58d16242008-08-31 04:05:03 +00003801
Mike Stumpba2cb0e2009-05-16 07:57:57 +00003802 // FIXME: This is the size of the setjmp buffer and should be target
3803 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson9acb0a42008-09-09 10:10:21 +00003804 uint64_t SetJmpBufferSize = 18;
3805
3806 // Exceptions
Owen Anderson73e7f802009-07-14 23:10:40 +00003807 const llvm::Type *StackPtrTy = VMContext.getArrayType(
3808 VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson9acb0a42008-09-09 10:10:21 +00003809
3810 ExceptionDataTy =
Owen Anderson73e7f802009-07-14 23:10:40 +00003811 VMContext.getStructType(VMContext.getArrayType(llvm::Type::Int32Ty,
Anders Carlsson9acb0a42008-09-09 10:10:21 +00003812 SetJmpBufferSize),
3813 StackPtrTy, NULL);
3814 CGM.getModule().addTypeName("struct._objc_exception_data",
3815 ExceptionDataTy);
3816
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00003817}
3818
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003819ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian48543f52009-01-21 22:04:16 +00003820: ObjCCommonTypesHelper(cgm)
3821{
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003822 // struct _method_list_t {
3823 // uint32_t entsize; // sizeof(struct _objc_method)
3824 // uint32_t method_count;
3825 // struct _objc_method method_list[method_count];
3826 // }
Owen Anderson73e7f802009-07-14 23:10:40 +00003827 MethodListnfABITy = VMContext.getStructType(IntTy,
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003828 IntTy,
Owen Anderson73e7f802009-07-14 23:10:40 +00003829 VMContext.getArrayType(MethodTy, 0),
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003830 NULL);
3831 CGM.getModule().addTypeName("struct.__method_list_t",
3832 MethodListnfABITy);
3833 // struct method_list_t *
Owen Anderson73e7f802009-07-14 23:10:40 +00003834 MethodListnfABIPtrTy = VMContext.getPointerTypeUnqual(MethodListnfABITy);
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003835
3836 // struct _protocol_t {
3837 // id isa; // NULL
3838 // const char * const protocol_name;
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003839 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003840 // const struct method_list_t * const instance_methods;
3841 // const struct method_list_t * const class_methods;
3842 // const struct method_list_t *optionalInstanceMethods;
3843 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003844 // const struct _prop_list_t * properties;
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003845 // const uint32_t size; // sizeof(struct _protocol_t)
3846 // const uint32_t flags; // = 0
3847 // }
3848
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003849 // Holder for struct _protocol_list_t *
Owen Anderson73e7f802009-07-14 23:10:40 +00003850 llvm::PATypeHolder ProtocolListTyHolder = VMContext.getOpaqueType();
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003851
Owen Anderson73e7f802009-07-14 23:10:40 +00003852 ProtocolnfABITy = VMContext.getStructType(ObjectPtrTy,
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003853 Int8PtrTy,
Owen Anderson73e7f802009-07-14 23:10:40 +00003854 VMContext.getPointerTypeUnqual(
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003855 ProtocolListTyHolder),
3856 MethodListnfABIPtrTy,
3857 MethodListnfABIPtrTy,
3858 MethodListnfABIPtrTy,
3859 MethodListnfABIPtrTy,
3860 PropertyListPtrTy,
3861 IntTy,
3862 IntTy,
3863 NULL);
3864 CGM.getModule().addTypeName("struct._protocol_t",
3865 ProtocolnfABITy);
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00003866
3867 // struct _protocol_t*
Owen Anderson73e7f802009-07-14 23:10:40 +00003868 ProtocolnfABIPtrTy = VMContext.getPointerTypeUnqual(ProtocolnfABITy);
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003869
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00003870 // struct _protocol_list_t {
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003871 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00003872 // struct _protocol_t *[protocol_count];
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003873 // }
Owen Anderson73e7f802009-07-14 23:10:40 +00003874 ProtocolListnfABITy = VMContext.getStructType(LongTy,
3875 VMContext.getArrayType(
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00003876 ProtocolnfABIPtrTy, 0),
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003877 NULL);
3878 CGM.getModule().addTypeName("struct._objc_protocol_list",
3879 ProtocolListnfABITy);
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00003880 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3881 ProtocolListnfABITy);
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003882
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003883 // struct _objc_protocol_list*
Owen Anderson73e7f802009-07-14 23:10:40 +00003884 ProtocolListnfABIPtrTy = VMContext.getPointerTypeUnqual(ProtocolListnfABITy);
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003885
3886 // struct _ivar_t {
3887 // unsigned long int *offset; // pointer to ivar offset location
3888 // char *name;
3889 // char *type;
3890 // uint32_t alignment;
3891 // uint32_t size;
3892 // }
Owen Anderson73e7f802009-07-14 23:10:40 +00003893 IvarnfABITy = VMContext.getStructType(VMContext.getPointerTypeUnqual(LongTy),
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003894 Int8PtrTy,
3895 Int8PtrTy,
3896 IntTy,
3897 IntTy,
3898 NULL);
3899 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3900
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003901 // struct _ivar_list_t {
3902 // uint32 entsize; // sizeof(struct _ivar_t)
3903 // uint32 count;
3904 // struct _iver_t list[count];
3905 // }
Owen Anderson73e7f802009-07-14 23:10:40 +00003906 IvarListnfABITy = VMContext.getStructType(IntTy,
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003907 IntTy,
Owen Anderson73e7f802009-07-14 23:10:40 +00003908 VMContext.getArrayType(
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003909 IvarnfABITy, 0),
3910 NULL);
3911 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3912
Owen Anderson73e7f802009-07-14 23:10:40 +00003913 IvarListnfABIPtrTy = VMContext.getPointerTypeUnqual(IvarListnfABITy);
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003914
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003915 // struct _class_ro_t {
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003916 // uint32_t const flags;
3917 // uint32_t const instanceStart;
3918 // uint32_t const instanceSize;
3919 // uint32_t const reserved; // only when building for 64bit targets
3920 // const uint8_t * const ivarLayout;
3921 // const char *const name;
3922 // const struct _method_list_t * const baseMethods;
3923 // const struct _objc_protocol_list *const baseProtocols;
3924 // const struct _ivar_list_t *const ivars;
3925 // const uint8_t * const weakIvarLayout;
3926 // const struct _prop_list_t * const properties;
3927 // }
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003928
3929 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson73e7f802009-07-14 23:10:40 +00003930 ClassRonfABITy = VMContext.getStructType(IntTy,
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003931 IntTy,
3932 IntTy,
3933 Int8PtrTy,
3934 Int8PtrTy,
3935 MethodListnfABIPtrTy,
3936 ProtocolListnfABIPtrTy,
3937 IvarListnfABIPtrTy,
3938 Int8PtrTy,
3939 PropertyListPtrTy,
3940 NULL);
3941 CGM.getModule().addTypeName("struct._class_ro_t",
3942 ClassRonfABITy);
3943
3944 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3945 std::vector<const llvm::Type*> Params;
3946 Params.push_back(ObjectPtrTy);
3947 Params.push_back(SelectorPtrTy);
Owen Anderson73e7f802009-07-14 23:10:40 +00003948 ImpnfABITy = VMContext.getPointerTypeUnqual(
3949 VMContext.getFunctionType(ObjectPtrTy, Params, false));
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003950
3951 // struct _class_t {
3952 // struct _class_t *isa;
3953 // struct _class_t * const superclass;
3954 // void *cache;
3955 // IMP *vtable;
3956 // struct class_ro_t *ro;
3957 // }
3958
Owen Anderson73e7f802009-07-14 23:10:40 +00003959 llvm::PATypeHolder ClassTyHolder = VMContext.getOpaqueType();
3960 ClassnfABITy =
3961 VMContext.getStructType(VMContext.getPointerTypeUnqual(ClassTyHolder),
3962 VMContext.getPointerTypeUnqual(ClassTyHolder),
3963 CachePtrTy,
3964 VMContext.getPointerTypeUnqual(ImpnfABITy),
3965 VMContext.getPointerTypeUnqual(ClassRonfABITy),
3966 NULL);
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003967 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3968
3969 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3970 ClassnfABITy);
3971
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003972 // LLVM for struct _class_t *
Owen Anderson73e7f802009-07-14 23:10:40 +00003973 ClassnfABIPtrTy = VMContext.getPointerTypeUnqual(ClassnfABITy);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003974
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003975 // struct _category_t {
3976 // const char * const name;
3977 // struct _class_t *const cls;
3978 // const struct _method_list_t * const instance_methods;
3979 // const struct _method_list_t * const class_methods;
3980 // const struct _protocol_list_t * const protocols;
3981 // const struct _prop_list_t * const properties;
Fariborz Jahanianb9459b72009-01-23 17:41:22 +00003982 // }
Owen Anderson73e7f802009-07-14 23:10:40 +00003983 CategorynfABITy = VMContext.getStructType(Int8PtrTy,
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003984 ClassnfABIPtrTy,
Fariborz Jahanianb9459b72009-01-23 17:41:22 +00003985 MethodListnfABIPtrTy,
3986 MethodListnfABIPtrTy,
3987 ProtocolListnfABIPtrTy,
3988 PropertyListPtrTy,
3989 NULL);
3990 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +00003991
3992 // New types for nonfragile abi messaging.
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00003993 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3994 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +00003995
3996 // MessageRefTy - LLVM for:
3997 // struct _message_ref_t {
3998 // IMP messenger;
3999 // SEL name;
4000 // };
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004001
4002 // First the clang type for struct _message_ref_t
4003 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
4004 SourceLocation(),
4005 &Ctx.Idents.get("_message_ref_t"));
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00004006 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Douglas Gregorc55b0b02009-04-09 21:40:53 +00004007 Ctx.VoidPtrTy, 0, false));
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00004008 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Douglas Gregorc55b0b02009-04-09 21:40:53 +00004009 Ctx.getObjCSelType(), 0, false));
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004010 RD->completeDefinition(Ctx);
4011
4012 MessageRefCTy = Ctx.getTagDeclType(RD);
4013 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4014 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +00004015
4016 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson73e7f802009-07-14 23:10:40 +00004017 MessageRefPtrTy = VMContext.getPointerTypeUnqual(MessageRefTy);
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +00004018
4019 // SuperMessageRefTy - LLVM for:
4020 // struct _super_message_ref_t {
4021 // SUPER_IMP messenger;
4022 // SEL name;
4023 // };
Owen Anderson73e7f802009-07-14 23:10:40 +00004024 SuperMessageRefTy = VMContext.getStructType(ImpnfABITy,
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +00004025 SelectorPtrTy,
4026 NULL);
4027 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
4028
4029 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Owen Anderson73e7f802009-07-14 23:10:40 +00004030 SuperMessageRefPtrTy = VMContext.getPointerTypeUnqual(SuperMessageRefTy);
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +00004031
Daniel Dunbar9c285e72009-03-01 04:46:24 +00004032
4033 // struct objc_typeinfo {
4034 // const void** vtable; // objc_ehtype_vtable + 2
4035 // const char* name; // c++ typeinfo string
4036 // Class cls;
4037 // };
Owen Anderson73e7f802009-07-14 23:10:40 +00004038 EHTypeTy = VMContext.getStructType(VMContext.getPointerTypeUnqual(Int8PtrTy),
Daniel Dunbar9c285e72009-03-01 04:46:24 +00004039 Int8PtrTy,
4040 ClassnfABIPtrTy,
4041 NULL);
Daniel Dunbarc0318b22009-03-02 06:08:11 +00004042 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson73e7f802009-07-14 23:10:40 +00004043 EHTypePtrTy = VMContext.getPointerTypeUnqual(EHTypeTy);
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00004044}
4045
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00004046llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
4047 FinishNonFragileABIModule();
4048
4049 return NULL;
4050}
4051
Daniel Dunbar57d4e112009-05-15 21:48:48 +00004052void CGObjCNonFragileABIMac::AddModuleClassList(const
4053 std::vector<llvm::GlobalValue*>
4054 &Container,
4055 const char *SymbolName,
4056 const char *SectionName) {
4057 unsigned NumClasses = Container.size();
4058
4059 if (!NumClasses)
4060 return;
4061
4062 std::vector<llvm::Constant*> Symbols(NumClasses);
4063 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson73e7f802009-07-14 23:10:40 +00004064 Symbols[i] = VMContext.getConstantExprBitCast(Container[i],
Daniel Dunbar57d4e112009-05-15 21:48:48 +00004065 ObjCTypes.Int8PtrTy);
4066 llvm::Constant* Init =
Owen Anderson73e7f802009-07-14 23:10:40 +00004067 VMContext.getConstantArray(VMContext.getArrayType(ObjCTypes.Int8PtrTy,
Daniel Dunbar57d4e112009-05-15 21:48:48 +00004068 NumClasses),
4069 Symbols);
4070
4071 llvm::GlobalVariable *GV =
Owen Anderson94148482009-07-08 19:05:04 +00004072 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar57d4e112009-05-15 21:48:48 +00004073 llvm::GlobalValue::InternalLinkage,
4074 Init,
Owen Anderson94148482009-07-08 19:05:04 +00004075 SymbolName);
Daniel Dunbar57d4e112009-05-15 21:48:48 +00004076 GV->setAlignment(8);
4077 GV->setSection(SectionName);
Chris Lattner5f38e5d2009-07-17 23:57:13 +00004078 CGM.AddUsedGlobal(GV);
Daniel Dunbar57d4e112009-05-15 21:48:48 +00004079}
4080
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00004081void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4082 // nonfragile abi has no module definition.
Fariborz Jahanian11c93dd2009-01-30 20:55:31 +00004083
Daniel Dunbar57d4e112009-05-15 21:48:48 +00004084 // Build list of all implemented class addresses in array
Fariborz Jahanian11c93dd2009-01-30 20:55:31 +00004085 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar57d4e112009-05-15 21:48:48 +00004086 AddModuleClassList(DefinedClasses,
4087 "\01L_OBJC_LABEL_CLASS_$",
4088 "__DATA, __objc_classlist, regular, no_dead_strip");
Daniel Dunbar1dd092b2009-05-15 22:33:15 +00004089 AddModuleClassList(DefinedNonLazyClasses,
4090 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4091 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Fariborz Jahanian11c93dd2009-01-30 20:55:31 +00004092
4093 // Build list of all implemented category addresses in array
4094 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar57d4e112009-05-15 21:48:48 +00004095 AddModuleClassList(DefinedCategories,
4096 "\01L_OBJC_LABEL_CATEGORY_$",
4097 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar1dd092b2009-05-15 22:33:15 +00004098 AddModuleClassList(DefinedNonLazyCategories,
4099 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4100 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Fariborz Jahanian11c93dd2009-01-30 20:55:31 +00004101
Fariborz Jahanian5b2f5502009-01-30 22:07:48 +00004102 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
4103 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
4104 std::vector<llvm::Constant*> Values(2);
Owen Anderson73e7f802009-07-14 23:10:40 +00004105 Values[0] = VMContext.getConstantInt(ObjCTypes.IntTy, 0);
Fariborz Jahanian4d7933a2009-02-24 21:08:09 +00004106 unsigned int flags = 0;
Fariborz Jahanian27f58962009-02-24 23:34:44 +00004107 // FIXME: Fix and continue?
4108 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
4109 flags |= eImageInfo_GarbageCollected;
4110 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
4111 flags |= eImageInfo_GCOnly;
Owen Anderson73e7f802009-07-14 23:10:40 +00004112 Values[1] = VMContext.getConstantInt(ObjCTypes.IntTy, flags);
4113 llvm::Constant* Init = VMContext.getConstantArray(
4114 VMContext.getArrayType(ObjCTypes.IntTy, 2),
Fariborz Jahanian5b2f5502009-01-30 22:07:48 +00004115 Values);
4116 llvm::GlobalVariable *IMGV =
Owen Anderson94148482009-07-08 19:05:04 +00004117 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian5b2f5502009-01-30 22:07:48 +00004118 llvm::GlobalValue::InternalLinkage,
4119 Init,
Owen Anderson94148482009-07-08 19:05:04 +00004120 "\01L_OBJC_IMAGE_INFO");
Fariborz Jahanian5b2f5502009-01-30 22:07:48 +00004121 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
Daniel Dunbarac277992009-04-23 08:03:21 +00004122 IMGV->setConstant(true);
Chris Lattner5f38e5d2009-07-17 23:57:13 +00004123 CGM.AddUsedGlobal(IMGV);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00004124}
4125
Fariborz Jahanian5c76fd32009-05-11 19:25:47 +00004126/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahaniane142bf42009-05-12 20:06:41 +00004127/// NonLegacyDispatchMethods; false otherwise. What this means is that
Fariborz Jahanian5c76fd32009-05-11 19:25:47 +00004128/// except for the 19 selectors in the list, we generate 32bit-style
4129/// message dispatch call for all the rest.
4130///
4131bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Fariborz Jahaniane142bf42009-05-12 20:06:41 +00004132 if (NonLegacyDispatchMethods.empty()) {
4133 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4134 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4135 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4136 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4137 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4138 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4139 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4140 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4141 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4142 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
4143
4144 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4145 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4146 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4147 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4148 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4149 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4150 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4151 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Fariborz Jahanian4ec484b2009-05-13 16:19:02 +00004152 // "countByEnumeratingWithState:objects:count"
4153 IdentifierInfo *KeyIdents[] = {
4154 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4155 &CGM.getContext().Idents.get("objects"),
4156 &CGM.getContext().Idents.get("count")
4157 };
4158 NonLegacyDispatchMethods.insert(
4159 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahanian5c76fd32009-05-11 19:25:47 +00004160 }
Fariborz Jahaniane142bf42009-05-12 20:06:41 +00004161 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahanian5c76fd32009-05-11 19:25:47 +00004162}
4163
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004164// Metadata flags
4165enum MetaDataDlags {
4166 CLS = 0x0,
4167 CLS_META = 0x1,
4168 CLS_ROOT = 0x2,
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004169 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004170 CLS_EXCEPTION = 0x20
4171};
4172/// BuildClassRoTInitializer - generate meta-data for:
4173/// struct _class_ro_t {
4174/// uint32_t const flags;
4175/// uint32_t const instanceStart;
4176/// uint32_t const instanceSize;
4177/// uint32_t const reserved; // only when building for 64bit targets
4178/// const uint8_t * const ivarLayout;
4179/// const char *const name;
4180/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004181/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004182/// const struct _ivar_list_t *const ivars;
4183/// const uint8_t * const weakIvarLayout;
4184/// const struct _prop_list_t * const properties;
4185/// }
4186///
4187llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
4188 unsigned flags,
4189 unsigned InstanceStart,
4190 unsigned InstanceSize,
4191 const ObjCImplementationDecl *ID) {
4192 std::string ClassName = ID->getNameAsString();
4193 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Anderson73e7f802009-07-14 23:10:40 +00004194 Values[ 0] = VMContext.getConstantInt(ObjCTypes.IntTy, flags);
4195 Values[ 1] = VMContext.getConstantInt(ObjCTypes.IntTy, InstanceStart);
4196 Values[ 2] = VMContext.getConstantInt(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004197 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanian31b96492009-04-22 23:00:43 +00004198 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4199 : BuildIvarLayout(ID, true);
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004200 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004201 // const struct _method_list_t * const baseMethods;
4202 std::vector<llvm::Constant*> Methods;
4203 std::string MethodListName("\01l_OBJC_$_");
4204 if (flags & CLS_META) {
4205 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Douglas Gregorcd19b572009-04-23 01:02:12 +00004206 for (ObjCImplementationDecl::classmeth_iterator
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00004207 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004208 // Class methods should always be defined.
4209 Methods.push_back(GetMethodConstant(*i));
4210 }
4211 } else {
4212 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Douglas Gregorcd19b572009-04-23 01:02:12 +00004213 for (ObjCImplementationDecl::instmeth_iterator
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00004214 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004215 // Instance methods should always be defined.
4216 Methods.push_back(GetMethodConstant(*i));
4217 }
Douglas Gregorcd19b572009-04-23 01:02:12 +00004218 for (ObjCImplementationDecl::propimpl_iterator
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00004219 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian78355ec2009-01-28 22:46:49 +00004220 ObjCPropertyImplDecl *PID = *i;
4221
4222 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4223 ObjCPropertyDecl *PD = PID->getPropertyDecl();
4224
4225 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4226 if (llvm::Constant *C = GetMethodConstant(MD))
4227 Methods.push_back(C);
4228 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4229 if (llvm::Constant *C = GetMethodConstant(MD))
4230 Methods.push_back(C);
4231 }
4232 }
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004233 }
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004234 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004235 "__DATA, __objc_const", Methods);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004236
4237 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4238 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
4239 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
4240 + OID->getNameAsString(),
4241 OID->protocol_begin(),
4242 OID->protocol_end());
4243
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004244 if (flags & CLS_META)
Owen Anderson5f1adc22009-07-13 04:10:07 +00004245 Values[ 7] = VMContext.getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004246 else
4247 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanian31b96492009-04-22 23:00:43 +00004248 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4249 : BuildIvarLayout(ID, false);
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00004250 if (flags & CLS_META)
Owen Anderson5f1adc22009-07-13 04:10:07 +00004251 Values[ 9] = VMContext.getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00004252 else
4253 Values[ 9] =
4254 EmitPropertyList(
4255 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
4256 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson73e7f802009-07-14 23:10:40 +00004257 llvm::Constant *Init = VMContext.getConstantStruct(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004258 Values);
4259 llvm::GlobalVariable *CLASS_RO_GV =
Owen Anderson94148482009-07-08 19:05:04 +00004260 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004261 llvm::GlobalValue::InternalLinkage,
4262 Init,
4263 (flags & CLS_META) ?
4264 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
Owen Anderson94148482009-07-08 19:05:04 +00004265 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00004266 CLASS_RO_GV->setAlignment(
4267 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004268 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004269 return CLASS_RO_GV;
Fariborz Jahanianc98c87b2009-01-26 22:58:07 +00004270
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004271}
4272
4273/// BuildClassMetaData - This routine defines that to-level meta-data
4274/// for the given ClassName for:
4275/// struct _class_t {
4276/// struct _class_t *isa;
4277/// struct _class_t * const superclass;
4278/// void *cache;
4279/// IMP *vtable;
4280/// struct class_ro_t *ro;
4281/// }
4282///
Fariborz Jahanian06726462009-01-24 21:21:53 +00004283llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
4284 std::string &ClassName,
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004285 llvm::Constant *IsAGV,
4286 llvm::Constant *SuperClassGV,
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +00004287 llvm::Constant *ClassRoGV,
4288 bool HiddenVisibility) {
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004289 std::vector<llvm::Constant*> Values(5);
4290 Values[0] = IsAGV;
Fariborz Jahanian06726462009-01-24 21:21:53 +00004291 Values[1] = SuperClassGV
4292 ? SuperClassGV
Owen Anderson5f1adc22009-07-13 04:10:07 +00004293 : VMContext.getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004294 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4295 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4296 Values[4] = ClassRoGV; // &CLASS_RO_GV
Owen Anderson73e7f802009-07-14 23:10:40 +00004297 llvm::Constant *Init = VMContext.getConstantStruct(ObjCTypes.ClassnfABITy,
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004298 Values);
Daniel Dunbarabbda222009-03-01 04:40:10 +00004299 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4300 GV->setInitializer(Init);
Fariborz Jahanian7c891592009-01-31 01:07:39 +00004301 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00004302 GV->setAlignment(
4303 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +00004304 if (HiddenVisibility)
4305 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian06726462009-01-24 21:21:53 +00004306 return GV;
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004307}
4308
Daniel Dunbar1dd092b2009-05-15 22:33:15 +00004309bool
Fariborz Jahanian86503952009-05-21 01:03:45 +00004310CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00004311 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar1dd092b2009-05-15 22:33:15 +00004312}
4313
Daniel Dunbared4d5962009-05-03 12:57:56 +00004314void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarecb5d402009-04-19 23:41:48 +00004315 uint32_t &InstanceStart,
4316 uint32_t &InstanceSize) {
Daniel Dunbar78387522009-05-04 21:26:30 +00004317 const ASTRecordLayout &RL =
4318 CGM.getContext().getASTObjCImplementationLayout(OID);
4319
Daniel Dunbarc40d6852009-05-04 23:23:09 +00004320 // InstanceSize is really instance end.
Anders Carlssona0359942009-07-18 21:26:44 +00004321 InstanceSize = llvm::RoundUpToAlignment(RL.getDataSize(), 8) / 8;
Daniel Dunbarc40d6852009-05-04 23:23:09 +00004322
4323 // If there are no fields, the start is the same as the end.
4324 if (!RL.getFieldCount())
4325 InstanceStart = InstanceSize;
4326 else
4327 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbarecb5d402009-04-19 23:41:48 +00004328}
4329
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00004330void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4331 std::string ClassName = ID->getNameAsString();
4332 if (!ObjCEmptyCacheVar) {
4333 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Owen Anderson94148482009-07-08 19:05:04 +00004334 CGM.getModule(),
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004335 ObjCTypes.CacheTy,
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00004336 false,
4337 llvm::GlobalValue::ExternalLinkage,
4338 0,
Owen Anderson94148482009-07-08 19:05:04 +00004339 "_objc_empty_cache");
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00004340
4341 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Owen Anderson94148482009-07-08 19:05:04 +00004342 CGM.getModule(),
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004343 ObjCTypes.ImpnfABITy,
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00004344 false,
4345 llvm::GlobalValue::ExternalLinkage,
4346 0,
Owen Anderson94148482009-07-08 19:05:04 +00004347 "_objc_empty_vtable");
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00004348 }
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004349 assert(ID->getClassInterface() &&
4350 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar72878722009-04-20 20:18:54 +00004351 // FIXME: Is this correct (that meta class size is never computed)?
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004352 uint32_t InstanceStart =
Duncan Sandsee6f6f82009-05-09 07:08:47 +00004353 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004354 uint32_t InstanceSize = InstanceStart;
4355 uint32_t flags = CLS_META;
Daniel Dunbara2d275d2009-04-07 05:48:37 +00004356 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4357 std::string ObjCClassName(getClassSymbolPrefix());
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00004358
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004359 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00004360
Daniel Dunbar8394fda2009-04-14 06:00:08 +00004361 bool classIsHidden =
4362 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +00004363 if (classIsHidden)
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004364 flags |= OBJC2_CLS_HIDDEN;
4365 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004366 // class is root
4367 flags |= CLS_ROOT;
Daniel Dunbarabbda222009-03-01 04:40:10 +00004368 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanianab438842009-04-14 18:41:56 +00004369 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004370 } else {
Fariborz Jahanian06726462009-01-24 21:21:53 +00004371 // Has a root. Current class is not a root.
Fariborz Jahanian514c63b2009-02-26 18:23:47 +00004372 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4373 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4374 Root = Super;
Fariborz Jahanianab438842009-04-14 18:41:56 +00004375 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanian514c63b2009-02-26 18:23:47 +00004376 // work on super class metadata symbol.
4377 std::string SuperClassName =
4378 ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanianab438842009-04-14 18:41:56 +00004379 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004380 }
4381 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4382 InstanceStart,
4383 InstanceSize,ID);
Fariborz Jahanian06726462009-01-24 21:21:53 +00004384 std::string TClassName = ObjCMetaClassName + ClassName;
4385 llvm::GlobalVariable *MetaTClass =
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +00004386 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4387 classIsHidden);
Daniel Dunbara2d275d2009-04-07 05:48:37 +00004388
Fariborz Jahanian06726462009-01-24 21:21:53 +00004389 // Metadata for the class
4390 flags = CLS;
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +00004391 if (classIsHidden)
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004392 flags |= OBJC2_CLS_HIDDEN;
Daniel Dunbarc2129532009-04-08 04:21:03 +00004393
Douglas Gregor98da6ae2009-06-18 16:11:24 +00004394 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbarc2129532009-04-08 04:21:03 +00004395 flags |= CLS_EXCEPTION;
4396
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004397 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian06726462009-01-24 21:21:53 +00004398 flags |= CLS_ROOT;
4399 SuperClassGV = 0;
Chris Lattner9fe470d2009-04-19 06:02:28 +00004400 } else {
Fariborz Jahanian06726462009-01-24 21:21:53 +00004401 // Has a root. Current class is not a root.
Fariborz Jahanian514c63b2009-02-26 18:23:47 +00004402 std::string RootClassName =
Fariborz Jahanian06726462009-01-24 21:21:53 +00004403 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbarabbda222009-03-01 04:40:10 +00004404 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahanian06726462009-01-24 21:21:53 +00004405 }
Daniel Dunbared4d5962009-05-03 12:57:56 +00004406 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian06726462009-01-24 21:21:53 +00004407 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianddd2fdd2009-01-24 23:43:01 +00004408 InstanceStart,
4409 InstanceSize,
4410 ID);
Fariborz Jahanian06726462009-01-24 21:21:53 +00004411
4412 TClassName = ObjCClassName + ClassName;
Fariborz Jahanian11c93dd2009-01-30 20:55:31 +00004413 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +00004414 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4415 classIsHidden);
Fariborz Jahanian11c93dd2009-01-30 20:55:31 +00004416 DefinedClasses.push_back(ClassMD);
Daniel Dunbarc2129532009-04-08 04:21:03 +00004417
Daniel Dunbar1dd092b2009-05-15 22:33:15 +00004418 // Determine if this class is also "non-lazy".
4419 if (ImplementationIsNonLazy(ID))
4420 DefinedNonLazyClasses.push_back(ClassMD);
4421
Daniel Dunbarc2129532009-04-08 04:21:03 +00004422 // Force the definition of the EHType if necessary.
4423 if (flags & CLS_EXCEPTION)
4424 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00004425}
4426
Fariborz Jahanian5d13ab12009-01-30 18:58:59 +00004427/// GenerateProtocolRef - This routine is called to generate code for
4428/// a protocol reference expression; as in:
4429/// @code
4430/// @protocol(Proto1);
4431/// @endcode
4432/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4433/// which will hold address of the protocol meta-data.
4434///
4435llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
4436 const ObjCProtocolDecl *PD) {
4437
Fariborz Jahaniand3243322009-04-10 18:47:34 +00004438 // This routine is called for @protocol only. So, we must build definition
4439 // of protocol's meta-data (not a reference to it!)
4440 //
Owen Anderson73e7f802009-07-14 23:10:40 +00004441 llvm::Constant *Init =
4442 VMContext.getConstantExprBitCast(GetOrEmitProtocol(PD),
Fariborz Jahanian5d13ab12009-01-30 18:58:59 +00004443 ObjCTypes.ExternalProtocolPtrTy);
4444
4445 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
4446 ProtocolName += PD->getNameAsCString();
4447
4448 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
4449 if (PTGV)
4450 return Builder.CreateLoad(PTGV, false, "tmp");
4451 PTGV = new llvm::GlobalVariable(
Owen Anderson94148482009-07-08 19:05:04 +00004452 CGM.getModule(),
Fariborz Jahanian5d13ab12009-01-30 18:58:59 +00004453 Init->getType(), false,
Mike Stump36dbf222009-03-07 16:33:28 +00004454 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian5d13ab12009-01-30 18:58:59 +00004455 Init,
Owen Anderson94148482009-07-08 19:05:04 +00004456 ProtocolName);
Fariborz Jahanian5d13ab12009-01-30 18:58:59 +00004457 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
4458 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattner5f38e5d2009-07-17 23:57:13 +00004459 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian5d13ab12009-01-30 18:58:59 +00004460 return Builder.CreateLoad(PTGV, false, "tmp");
4461}
4462
Fariborz Jahanianfe49a092009-01-26 18:32:24 +00004463/// GenerateCategory - Build metadata for a category implementation.
4464/// struct _category_t {
4465/// const char * const name;
4466/// struct _class_t *const cls;
4467/// const struct _method_list_t * const instance_methods;
4468/// const struct _method_list_t * const class_methods;
4469/// const struct _protocol_list_t * const protocols;
4470/// const struct _prop_list_t * const properties;
4471/// }
4472///
Daniel Dunbar1dd092b2009-05-15 22:33:15 +00004473void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianfe49a092009-01-26 18:32:24 +00004474 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianc98c87b2009-01-26 22:58:07 +00004475 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
4476 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianfe49a092009-01-26 18:32:24 +00004477 "_$_" + OCD->getNameAsString());
Daniel Dunbara2d275d2009-04-07 05:48:37 +00004478 std::string ExtClassName(getClassSymbolPrefix() +
4479 Interface->getNameAsString());
Fariborz Jahanianfe49a092009-01-26 18:32:24 +00004480
4481 std::vector<llvm::Constant*> Values(6);
4482 Values[0] = GetClassName(OCD->getIdentifier());
4483 // meta-class entry symbol
Daniel Dunbarabbda222009-03-01 04:40:10 +00004484 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanianfe49a092009-01-26 18:32:24 +00004485 Values[1] = ClassGV;
Fariborz Jahanianc98c87b2009-01-26 22:58:07 +00004486 std::vector<llvm::Constant*> Methods;
4487 std::string MethodListName(Prefix);
4488 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
4489 "_$_" + OCD->getNameAsString();
4490
Douglas Gregorcd19b572009-04-23 01:02:12 +00004491 for (ObjCCategoryImplDecl::instmeth_iterator
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00004492 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianc98c87b2009-01-26 22:58:07 +00004493 // Instance methods should always be defined.
4494 Methods.push_back(GetMethodConstant(*i));
4495 }
4496
4497 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004498 "__DATA, __objc_const",
Fariborz Jahanianc98c87b2009-01-26 22:58:07 +00004499 Methods);
4500
4501 MethodListName = Prefix;
4502 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
4503 OCD->getNameAsString();
4504 Methods.clear();
Douglas Gregorcd19b572009-04-23 01:02:12 +00004505 for (ObjCCategoryImplDecl::classmeth_iterator
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00004506 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianc98c87b2009-01-26 22:58:07 +00004507 // Class methods should always be defined.
4508 Methods.push_back(GetMethodConstant(*i));
4509 }
4510
4511 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004512 "__DATA, __objc_const",
Fariborz Jahanianc98c87b2009-01-26 22:58:07 +00004513 Methods);
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00004514 const ObjCCategoryDecl *Category =
4515 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian8c7904b2009-02-13 17:52:22 +00004516 if (Category) {
4517 std::string ExtName(Interface->getNameAsString() + "_$_" +
4518 OCD->getNameAsString());
4519 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
4520 + Interface->getNameAsString() + "_$_"
4521 + Category->getNameAsString(),
4522 Category->protocol_begin(),
4523 Category->protocol_end());
4524 Values[5] =
4525 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
4526 OCD, Category, ObjCTypes);
4527 }
4528 else {
Owen Anderson5f1adc22009-07-13 04:10:07 +00004529 Values[4] = VMContext.getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4530 Values[5] = VMContext.getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian8c7904b2009-02-13 17:52:22 +00004531 }
4532
Fariborz Jahanianfe49a092009-01-26 18:32:24 +00004533 llvm::Constant *Init =
Owen Anderson73e7f802009-07-14 23:10:40 +00004534 VMContext.getConstantStruct(ObjCTypes.CategorynfABITy,
Fariborz Jahanianfe49a092009-01-26 18:32:24 +00004535 Values);
4536 llvm::GlobalVariable *GCATV
Owen Anderson94148482009-07-08 19:05:04 +00004537 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianfe49a092009-01-26 18:32:24 +00004538 false,
4539 llvm::GlobalValue::InternalLinkage,
4540 Init,
Owen Anderson94148482009-07-08 19:05:04 +00004541 ExtCatName);
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00004542 GCATV->setAlignment(
4543 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004544 GCATV->setSection("__DATA, __objc_const");
Chris Lattner5f38e5d2009-07-17 23:57:13 +00004545 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianfe49a092009-01-26 18:32:24 +00004546 DefinedCategories.push_back(GCATV);
Daniel Dunbar1dd092b2009-05-15 22:33:15 +00004547
4548 // Determine if this category is also "non-lazy".
4549 if (ImplementationIsNonLazy(OCD))
4550 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianfe49a092009-01-26 18:32:24 +00004551}
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004552
4553/// GetMethodConstant - Return a struct objc_method constant for the
4554/// given method if it has been defined. The result is null if the
4555/// method has not been defined. The return value has type MethodPtrTy.
4556llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
4557 const ObjCMethodDecl *MD) {
4558 // FIXME: Use DenseMap::lookup
4559 llvm::Function *Fn = MethodDefinitions[MD];
4560 if (!Fn)
4561 return 0;
4562
4563 std::vector<llvm::Constant*> Method(3);
4564 Method[0] =
Owen Anderson73e7f802009-07-14 23:10:40 +00004565 VMContext.getConstantExprBitCast(GetMethodVarName(MD->getSelector()),
4566 ObjCTypes.SelectorPtrTy);
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004567 Method[1] = GetMethodVarType(MD);
Owen Anderson73e7f802009-07-14 23:10:40 +00004568 Method[2] = VMContext.getConstantExprBitCast(Fn, ObjCTypes.Int8PtrTy);
4569 return VMContext.getConstantStruct(ObjCTypes.MethodTy, Method);
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004570}
4571
4572/// EmitMethodList - Build meta-data for method declarations
4573/// struct _method_list_t {
4574/// uint32_t entsize; // sizeof(struct _objc_method)
4575/// uint32_t method_count;
4576/// struct _objc_method method_list[method_count];
4577/// }
4578///
4579llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
4580 const std::string &Name,
4581 const char *Section,
4582 const ConstantVector &Methods) {
4583 // Return null for empty list.
4584 if (Methods.empty())
Owen Anderson5f1adc22009-07-13 04:10:07 +00004585 return VMContext.getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004586
4587 std::vector<llvm::Constant*> Values(3);
4588 // sizeof(struct _objc_method)
Duncan Sandsee6f6f82009-05-09 07:08:47 +00004589 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson73e7f802009-07-14 23:10:40 +00004590 Values[0] = VMContext.getConstantInt(ObjCTypes.IntTy, Size);
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004591 // method_count
Owen Anderson73e7f802009-07-14 23:10:40 +00004592 Values[1] = VMContext.getConstantInt(ObjCTypes.IntTy, Methods.size());
4593 llvm::ArrayType *AT = VMContext.getArrayType(ObjCTypes.MethodTy,
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004594 Methods.size());
Owen Anderson73e7f802009-07-14 23:10:40 +00004595 Values[2] = VMContext.getConstantArray(AT, Methods);
4596 llvm::Constant *Init = VMContext.getConstantStruct(Values);
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004597
4598 llvm::GlobalVariable *GV =
Owen Anderson94148482009-07-08 19:05:04 +00004599 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004600 llvm::GlobalValue::InternalLinkage,
4601 Init,
Owen Anderson94148482009-07-08 19:05:04 +00004602 Name);
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00004603 GV->setAlignment(
4604 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004605 GV->setSection(Section);
Chris Lattner5f38e5d2009-07-17 23:57:13 +00004606 CGM.AddUsedGlobal(GV);
Owen Anderson73e7f802009-07-14 23:10:40 +00004607 return VMContext.getConstantExprBitCast(GV,
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004608 ObjCTypes.MethodListnfABIPtrTy);
4609}
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004610
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00004611/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
4612/// the given ivar.
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00004613llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(
Fariborz Jahaniana09a5142009-02-12 18:51:23 +00004614 const ObjCInterfaceDecl *ID,
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00004615 const ObjCIvarDecl *Ivar) {
Daniel Dunbar671e8a22009-05-05 00:36:57 +00004616 // FIXME: We shouldn't need to do this lookup.
4617 unsigned Index;
4618 const ObjCInterfaceDecl *Container =
4619 FindIvarInterface(CGM.getContext(), ID, Ivar, Index);
4620 assert(Container && "Unable to find ivar container!");
4621 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregorc55b0b02009-04-09 21:40:53 +00004622 '.' + Ivar->getNameAsString();
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00004623 llvm::GlobalVariable *IvarOffsetGV =
4624 CGM.getModule().getGlobalVariable(Name);
4625 if (!IvarOffsetGV)
4626 IvarOffsetGV =
Owen Anderson94148482009-07-08 19:05:04 +00004627 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00004628 false,
4629 llvm::GlobalValue::ExternalLinkage,
4630 0,
Owen Anderson94148482009-07-08 19:05:04 +00004631 Name);
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00004632 return IvarOffsetGV;
4633}
4634
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004635llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00004636 const ObjCInterfaceDecl *ID,
Fariborz Jahanian150f7732009-01-28 01:36:42 +00004637 const ObjCIvarDecl *Ivar,
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004638 unsigned long int Offset) {
Daniel Dunbar0438ff42009-04-19 00:44:02 +00004639 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Owen Anderson73e7f802009-07-14 23:10:40 +00004640 IvarOffsetGV->setInitializer(VMContext.getConstantInt(ObjCTypes.LongTy,
Daniel Dunbar0438ff42009-04-19 00:44:02 +00004641 Offset));
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00004642 IvarOffsetGV->setAlignment(
Fariborz Jahanian55343922009-02-03 00:09:52 +00004643 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar0438ff42009-04-19 00:44:02 +00004644
Mike Stumpba2cb0e2009-05-16 07:57:57 +00004645 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
4646 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar0438ff42009-04-19 00:44:02 +00004647 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
4648 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
4649 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian150f7732009-01-28 01:36:42 +00004650 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar8394fda2009-04-14 06:00:08 +00004651 else
Fariborz Jahanian745fd892009-04-06 18:30:00 +00004652 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004653 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian55343922009-02-03 00:09:52 +00004654 return IvarOffsetGV;
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004655}
4656
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004657/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar3c190812009-04-18 08:51:00 +00004658/// implementation. The return value has type
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004659/// IvarListnfABIPtrTy.
4660/// struct _ivar_t {
4661/// unsigned long int *offset; // pointer to ivar offset location
4662/// char *name;
4663/// char *type;
4664/// uint32_t alignment;
4665/// uint32_t size;
4666/// }
4667/// struct _ivar_list_t {
4668/// uint32 entsize; // sizeof(struct _ivar_t)
4669/// uint32 count;
4670/// struct _iver_t list[count];
4671/// }
4672///
Daniel Dunbar356f0742009-04-20 06:54:31 +00004673
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004674llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
4675 const ObjCImplementationDecl *ID) {
4676
4677 std::vector<llvm::Constant*> Ivars, Ivar(5);
4678
4679 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4680 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
4681
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004682 // FIXME. Consolidate this with similar code in GenerateClass.
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00004683
Daniel Dunbar1748ac32009-04-20 00:33:43 +00004684 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanianfbf44642009-03-31 18:11:23 +00004685 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanianb290be02009-06-04 01:19:09 +00004686 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Fariborz Jahanian84c45692009-04-01 19:37:34 +00004687
Daniel Dunbar356f0742009-04-20 06:54:31 +00004688 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
4689 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanianb290be02009-06-04 01:19:09 +00004690 // Ignore unnamed bit-fields.
4691 if (!IVD->getDeclName())
4692 continue;
Daniel Dunbard73f5f22009-04-20 05:53:40 +00004693 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbared4d5962009-05-03 12:57:56 +00004694 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbare42aede2009-04-22 08:22:17 +00004695 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
4696 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004697 const llvm::Type *FieldTy =
Daniel Dunbare42aede2009-04-22 08:22:17 +00004698 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sandsee6f6f82009-05-09 07:08:47 +00004699 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004700 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbare42aede2009-04-22 08:22:17 +00004701 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004702 Align = llvm::Log2_32(Align);
Owen Anderson73e7f802009-07-14 23:10:40 +00004703 Ivar[3] = VMContext.getConstantInt(ObjCTypes.IntTy, Align);
Daniel Dunbar1748ac32009-04-20 00:33:43 +00004704 // NOTE. Size of a bitfield does not match gcc's, because of the
4705 // way bitfields are treated special in each. But I am told that
4706 // 'size' for bitfield ivars is ignored by the runtime so it does
4707 // not matter. If it matters, there is enough info to get the
4708 // bitfield right!
Owen Anderson73e7f802009-07-14 23:10:40 +00004709 Ivar[4] = VMContext.getConstantInt(ObjCTypes.IntTy, Size);
4710 Ivars.push_back(VMContext.getConstantStruct(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004711 }
4712 // Return null for empty list.
4713 if (Ivars.empty())
Owen Anderson5f1adc22009-07-13 04:10:07 +00004714 return VMContext.getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004715 std::vector<llvm::Constant*> Values(3);
Duncan Sandsee6f6f82009-05-09 07:08:47 +00004716 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson73e7f802009-07-14 23:10:40 +00004717 Values[0] = VMContext.getConstantInt(ObjCTypes.IntTy, Size);
4718 Values[1] = VMContext.getConstantInt(ObjCTypes.IntTy, Ivars.size());
4719 llvm::ArrayType *AT = VMContext.getArrayType(ObjCTypes.IvarnfABITy,
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004720 Ivars.size());
Owen Anderson73e7f802009-07-14 23:10:40 +00004721 Values[2] = VMContext.getConstantArray(AT, Ivars);
4722 llvm::Constant *Init = VMContext.getConstantStruct(Values);
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004723 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4724 llvm::GlobalVariable *GV =
Owen Anderson94148482009-07-08 19:05:04 +00004725 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004726 llvm::GlobalValue::InternalLinkage,
4727 Init,
Owen Anderson94148482009-07-08 19:05:04 +00004728 Prefix + OID->getNameAsString());
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00004729 GV->setAlignment(
4730 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004731 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004732
Chris Lattner5f38e5d2009-07-17 23:57:13 +00004733 CGM.AddUsedGlobal(GV);
Owen Anderson73e7f802009-07-14 23:10:40 +00004734 return VMContext.getConstantExprBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004735}
4736
4737llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4738 const ObjCProtocolDecl *PD) {
4739 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4740
4741 if (!Entry) {
4742 // We use the initializer as a marker of whether this is a forward
4743 // reference or not. At module finalization we add the empty
4744 // contents for protocols which were referenced but never defined.
4745 Entry =
Owen Anderson94148482009-07-08 19:05:04 +00004746 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004747 llvm::GlobalValue::ExternalLinkage,
4748 0,
Owen Anderson94148482009-07-08 19:05:04 +00004749 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString());
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004750 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004751 }
4752
4753 return Entry;
4754}
4755
4756/// GetOrEmitProtocol - Generate the protocol meta-data:
4757/// @code
4758/// struct _protocol_t {
4759/// id isa; // NULL
4760/// const char * const protocol_name;
4761/// const struct _protocol_list_t * protocol_list; // super protocols
4762/// const struct method_list_t * const instance_methods;
4763/// const struct method_list_t * const class_methods;
4764/// const struct method_list_t *optionalInstanceMethods;
4765/// const struct method_list_t *optionalClassMethods;
4766/// const struct _prop_list_t * properties;
4767/// const uint32_t size; // sizeof(struct _protocol_t)
4768/// const uint32_t flags; // = 0
4769/// }
4770/// @endcode
4771///
4772
4773llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4774 const ObjCProtocolDecl *PD) {
4775 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4776
4777 // Early exit if a defining object has already been generated.
4778 if (Entry && Entry->hasInitializer())
4779 return Entry;
4780
4781 const char *ProtocolName = PD->getNameAsCString();
4782
4783 // Construct method lists.
4784 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4785 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregorc55b0b02009-04-09 21:40:53 +00004786 for (ObjCProtocolDecl::instmeth_iterator
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00004787 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004788 ObjCMethodDecl *MD = *i;
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004789 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004790 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4791 OptInstanceMethods.push_back(C);
4792 } else {
4793 InstanceMethods.push_back(C);
4794 }
4795 }
4796
Douglas Gregorc55b0b02009-04-09 21:40:53 +00004797 for (ObjCProtocolDecl::classmeth_iterator
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00004798 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004799 ObjCMethodDecl *MD = *i;
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004800 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004801 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4802 OptClassMethods.push_back(C);
4803 } else {
4804 ClassMethods.push_back(C);
4805 }
4806 }
4807
4808 std::vector<llvm::Constant*> Values(10);
4809 // isa is NULL
Owen Anderson5f1adc22009-07-13 04:10:07 +00004810 Values[0] = VMContext.getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004811 Values[1] = GetClassName(PD->getIdentifier());
4812 Values[2] = EmitProtocolList(
4813 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4814 PD->protocol_begin(),
4815 PD->protocol_end());
4816
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004817 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004818 + PD->getNameAsString(),
4819 "__DATA, __objc_const",
4820 InstanceMethods);
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004821 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004822 + PD->getNameAsString(),
4823 "__DATA, __objc_const",
4824 ClassMethods);
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004825 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004826 + PD->getNameAsString(),
4827 "__DATA, __objc_const",
4828 OptInstanceMethods);
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004829 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004830 + PD->getNameAsString(),
4831 "__DATA, __objc_const",
4832 OptClassMethods);
4833 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4834 0, PD, ObjCTypes);
4835 uint32_t Size =
Duncan Sandsee6f6f82009-05-09 07:08:47 +00004836 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson73e7f802009-07-14 23:10:40 +00004837 Values[8] = VMContext.getConstantInt(ObjCTypes.IntTy, Size);
Owen Anderson5f1adc22009-07-13 04:10:07 +00004838 Values[9] = VMContext.getNullValue(ObjCTypes.IntTy);
Owen Anderson73e7f802009-07-14 23:10:40 +00004839 llvm::Constant *Init = VMContext.getConstantStruct(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004840 Values);
4841
4842 if (Entry) {
4843 // Already created, fix the linkage and update the initializer.
Mike Stump36dbf222009-03-07 16:33:28 +00004844 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004845 Entry->setInitializer(Init);
4846 } else {
4847 Entry =
Owen Anderson94148482009-07-08 19:05:04 +00004848 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
Mike Stump36dbf222009-03-07 16:33:28 +00004849 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004850 Init,
Owen Anderson94148482009-07-08 19:05:04 +00004851 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName);
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00004852 Entry->setAlignment(
4853 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004854 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004855 }
Fariborz Jahanianfd02a662009-01-29 20:10:59 +00004856 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattner5f38e5d2009-07-17 23:57:13 +00004857 CGM.AddUsedGlobal(Entry);
4858
Fariborz Jahanianfd02a662009-01-29 20:10:59 +00004859 // Use this protocol meta-data to build protocol list table in section
4860 // __DATA, __objc_protolist
Fariborz Jahanianfd02a662009-01-29 20:10:59 +00004861 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
Owen Anderson94148482009-07-08 19:05:04 +00004862 CGM.getModule(),
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004863 ObjCTypes.ProtocolnfABIPtrTy, false,
Mike Stump36dbf222009-03-07 16:33:28 +00004864 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanianfd02a662009-01-29 20:10:59 +00004865 Entry,
4866 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
Owen Anderson94148482009-07-08 19:05:04 +00004867 +ProtocolName);
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00004868 PTGV->setAlignment(
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004869 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar9dfd3a72009-04-15 02:56:18 +00004870 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanianfd02a662009-01-29 20:10:59 +00004871 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattner5f38e5d2009-07-17 23:57:13 +00004872 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004873 return Entry;
4874}
4875
4876/// EmitProtocolList - Generate protocol list meta-data:
4877/// @code
4878/// struct _protocol_list_t {
4879/// long protocol_count; // Note, this is 32/64 bit
4880/// struct _protocol_t[protocol_count];
4881/// }
4882/// @endcode
4883///
4884llvm::Constant *
4885CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4886 ObjCProtocolDecl::protocol_iterator begin,
4887 ObjCProtocolDecl::protocol_iterator end) {
4888 std::vector<llvm::Constant*> ProtocolRefs;
4889
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004890 // Just return null for empty protocol lists
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004891 if (begin == end)
Owen Anderson5f1adc22009-07-13 04:10:07 +00004892 return VMContext.getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004893
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004894 // FIXME: We shouldn't need to do this lookup here, should we?
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004895 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4896 if (GV)
Owen Anderson73e7f802009-07-14 23:10:40 +00004897 return VMContext.getConstantExprBitCast(GV,
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004898 ObjCTypes.ProtocolListnfABIPtrTy);
4899
4900 for (; begin != end; ++begin)
4901 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4902
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004903 // This list is null terminated.
Owen Anderson5f1adc22009-07-13 04:10:07 +00004904 ProtocolRefs.push_back(VMContext.getNullValue(
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004905 ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004906
4907 std::vector<llvm::Constant*> Values(2);
Owen Anderson73e7f802009-07-14 23:10:40 +00004908 Values[0] =
4909 VMContext.getConstantInt(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004910 Values[1] =
Owen Anderson73e7f802009-07-14 23:10:40 +00004911 VMContext.getConstantArray(
4912 VMContext.getArrayType(ObjCTypes.ProtocolnfABIPtrTy,
4913 ProtocolRefs.size()),
4914 ProtocolRefs);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004915
Owen Anderson73e7f802009-07-14 23:10:40 +00004916 llvm::Constant *Init = VMContext.getConstantStruct(Values);
Owen Anderson94148482009-07-08 19:05:04 +00004917 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004918 llvm::GlobalValue::InternalLinkage,
4919 Init,
Owen Anderson94148482009-07-08 19:05:04 +00004920 Name);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004921 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00004922 GV->setAlignment(
4923 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Chris Lattner5f38e5d2009-07-17 23:57:13 +00004924 CGM.AddUsedGlobal(GV);
Owen Anderson73e7f802009-07-14 23:10:40 +00004925 return VMContext.getConstantExprBitCast(GV,
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004926 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004927}
4928
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004929/// GetMethodDescriptionConstant - This routine build following meta-data:
4930/// struct _objc_method {
4931/// SEL _cmd;
4932/// char *method_type;
4933/// char *_imp;
4934/// }
4935
4936llvm::Constant *
4937CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4938 std::vector<llvm::Constant*> Desc(3);
Owen Anderson73e7f802009-07-14 23:10:40 +00004939 Desc[0] =
4940 VMContext.getConstantExprBitCast(GetMethodVarName(MD->getSelector()),
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004941 ObjCTypes.SelectorPtrTy);
4942 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian5d13ab12009-01-30 18:58:59 +00004943 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson5f1adc22009-07-13 04:10:07 +00004944 Desc[2] = VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson73e7f802009-07-14 23:10:40 +00004945 return VMContext.getConstantStruct(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004946}
Fariborz Jahanian55343922009-02-03 00:09:52 +00004947
4948/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4949/// This code gen. amounts to generating code for:
4950/// @code
4951/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4952/// @encode
4953///
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00004954LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian55343922009-02-03 00:09:52 +00004955 CodeGen::CodeGenFunction &CGF,
4956 QualType ObjectTy,
4957 llvm::Value *BaseValue,
4958 const ObjCIvarDecl *Ivar,
Fariborz Jahanian55343922009-02-03 00:09:52 +00004959 unsigned CVRQualifiers) {
Daniel Dunbarf5254bd2009-04-21 01:19:28 +00004960 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar85d37542009-04-22 07:32:20 +00004961 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
4962 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian55343922009-02-03 00:09:52 +00004963}
4964
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00004965llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
4966 CodeGen::CodeGenFunction &CGF,
Daniel Dunbar61e14a62009-04-22 05:08:15 +00004967 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00004968 const ObjCIvarDecl *Ivar) {
Daniel Dunbar07d204a2009-04-19 00:31:15 +00004969 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),
4970 false, "ivar");
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00004971}
4972
Fariborz Jahanian7e881162009-02-04 00:22:57 +00004973CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4974 CodeGen::CodeGenFunction &CGF,
4975 QualType ResultType,
4976 Selector Sel,
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004977 llvm::Value *Receiver,
Fariborz Jahanian7e881162009-02-04 00:22:57 +00004978 QualType Arg0Ty,
4979 bool IsSuper,
4980 const CallArgList &CallArgs) {
Mike Stumpba2cb0e2009-05-16 07:57:57 +00004981 // FIXME. Even though IsSuper is passes. This function doese not handle calls
4982 // to 'super' receivers.
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004983 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00004984 llvm::Value *Arg0 = Receiver;
4985 if (!IsSuper)
4986 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004987
4988 // Find the message function name.
Mike Stumpba2cb0e2009-05-16 07:57:57 +00004989 // FIXME. This is too much work to get the ABI-specific result type needed to
4990 // find the message name.
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004991 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4992 llvm::SmallVector<QualType, 16>());
Fariborz Jahanian9d7167d2009-04-30 23:08:58 +00004993 llvm::Constant *Fn = 0;
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004994 std::string Name("\01l_");
4995 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanian13ab25e2009-02-05 18:00:27 +00004996#if 0
4997 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004998 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattnerada416b2009-04-22 02:53:24 +00004999 Fn = ObjCTypes.getMessageSendIdStretFixupFn();
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00005000 // FIXME. Is there a better way of getting these names.
5001 // They are available in RuntimeFunctions vector pair.
5002 Name += "objc_msgSendId_stret_fixup";
5003 }
Fariborz Jahanian13ab25e2009-02-05 18:00:27 +00005004 else
5005#endif
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00005006 if (IsSuper) {
Chris Lattnerada416b2009-04-22 02:53:24 +00005007 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00005008 Name += "objc_msgSendSuper2_stret_fixup";
5009 }
5010 else
Fariborz Jahanian13ab25e2009-02-05 18:00:27 +00005011 {
Chris Lattnerada416b2009-04-22 02:53:24 +00005012 Fn = ObjCTypes.getMessageSendStretFixupFn();
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00005013 Name += "objc_msgSend_stret_fixup";
5014 }
5015 }
Fariborz Jahanian865260c2009-04-30 16:31:11 +00005016 else if (!IsSuper && ResultType->isFloatingType()) {
Daniel Dunbar9e6277f2009-06-26 18:32:06 +00005017 if (ResultType->isSpecificBuiltinType(BuiltinType::LongDouble)) {
5018 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5019 Name += "objc_msgSend_fpret_fixup";
5020 }
5021 else {
5022 Fn = ObjCTypes.getMessageSendFixupFn();
5023 Name += "objc_msgSend_fixup";
Fariborz Jahanian865260c2009-04-30 16:31:11 +00005024 }
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00005025 }
5026 else {
Fariborz Jahanian13ab25e2009-02-05 18:00:27 +00005027#if 0
5028// unlike what is documented. gcc never generates this API!!
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00005029 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattnerada416b2009-04-22 02:53:24 +00005030 Fn = ObjCTypes.getMessageSendIdFixupFn();
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00005031 Name += "objc_msgSendId_fixup";
5032 }
Fariborz Jahanian13ab25e2009-02-05 18:00:27 +00005033 else
5034#endif
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00005035 if (IsSuper) {
Chris Lattnerada416b2009-04-22 02:53:24 +00005036 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00005037 Name += "objc_msgSendSuper2_fixup";
5038 }
5039 else
Fariborz Jahanian13ab25e2009-02-05 18:00:27 +00005040 {
Chris Lattnerada416b2009-04-22 02:53:24 +00005041 Fn = ObjCTypes.getMessageSendFixupFn();
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00005042 Name += "objc_msgSend_fixup";
5043 }
5044 }
Fariborz Jahanian9d7167d2009-04-30 23:08:58 +00005045 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00005046 Name += '_';
5047 std::string SelName(Sel.getAsString());
5048 // Replace all ':' in selector name with '_' ouch!
5049 for(unsigned i = 0; i < SelName.size(); i++)
5050 if (SelName[i] == ':')
5051 SelName[i] = '_';
5052 Name += SelName;
5053 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5054 if (!GV) {
Daniel Dunbar4993e292009-04-15 19:03:14 +00005055 // Build message ref table entry.
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00005056 std::vector<llvm::Constant*> Values(2);
5057 Values[0] = Fn;
5058 Values[1] = GetMethodVarName(Sel);
Owen Anderson73e7f802009-07-14 23:10:40 +00005059 llvm::Constant *Init = VMContext.getConstantStruct(Values);
Owen Anderson94148482009-07-08 19:05:04 +00005060 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stump36dbf222009-03-07 16:33:28 +00005061 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00005062 Init,
Owen Anderson94148482009-07-08 19:05:04 +00005063 Name);
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00005064 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbara405f782009-04-15 19:04:46 +00005065 GV->setAlignment(16);
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00005066 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00005067 }
5068 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanian10d69ea2009-02-05 01:13:09 +00005069
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00005070 CallArgList ActualArgs;
5071 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
5072 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
5073 ObjCTypes.MessageRefCPtrTy));
5074 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanian10d69ea2009-02-05 01:13:09 +00005075 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
5076 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5077 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanianf3c17752009-02-14 21:25:36 +00005078 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanian10d69ea2009-02-05 01:13:09 +00005079 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Anderson73e7f802009-07-14 23:10:40 +00005080 VMContext.getPointerTypeUnqual(FTy));
Fariborz Jahanian10d69ea2009-02-05 01:13:09 +00005081 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian7e881162009-02-04 00:22:57 +00005082}
5083
5084/// Generate code for a message send expression in the nonfragile abi.
5085CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
5086 CodeGen::CodeGenFunction &CGF,
5087 QualType ResultType,
5088 Selector Sel,
5089 llvm::Value *Receiver,
5090 bool IsClassMessage,
Fariborz Jahanianc8007152009-05-05 21:36:57 +00005091 const CallArgList &CallArgs,
5092 const ObjCMethodDecl *Method) {
Fariborz Jahanian5c76fd32009-05-11 19:25:47 +00005093 return LegacyDispatchedSelector(Sel)
5094 ? EmitLegacyMessageSend(CGF, ResultType, EmitSelector(CGF.Builder, Sel),
5095 Receiver, CGF.getContext().getObjCIdType(),
5096 false, CallArgs, ObjCTypes)
5097 : EmitMessageSend(CGF, ResultType, Sel,
5098 Receiver, CGF.getContext().getObjCIdType(),
5099 false, CallArgs);
Fariborz Jahanian7e881162009-02-04 00:22:57 +00005100}
5101
Daniel Dunbarabbda222009-03-01 04:40:10 +00005102llvm::GlobalVariable *
Fariborz Jahanianab438842009-04-14 18:41:56 +00005103CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbarabbda222009-03-01 04:40:10 +00005104 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5105
Daniel Dunbar66b47512009-03-02 05:18:14 +00005106 if (!GV) {
Owen Anderson94148482009-07-08 19:05:04 +00005107 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
5108 false, llvm::GlobalValue::ExternalLinkage,
5109 0, Name);
Daniel Dunbarabbda222009-03-01 04:40:10 +00005110 }
5111
5112 return GV;
5113}
5114
Fariborz Jahanian917c0402009-02-05 20:41:40 +00005115llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar3c190812009-04-18 08:51:00 +00005116 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian917c0402009-02-05 20:41:40 +00005117 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
5118
5119 if (!Entry) {
Daniel Dunbara2d275d2009-04-07 05:48:37 +00005120 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarabbda222009-03-01 04:40:10 +00005121 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Fariborz Jahanian917c0402009-02-05 20:41:40 +00005122 Entry =
Owen Anderson94148482009-07-08 19:05:04 +00005123 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5124 false, llvm::GlobalValue::InternalLinkage,
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00005125 ClassGV,
Owen Anderson94148482009-07-08 19:05:04 +00005126 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian917c0402009-02-05 20:41:40 +00005127 Entry->setAlignment(
5128 CGM.getTargetData().getPrefTypeAlignment(
5129 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar3c190812009-04-18 08:51:00 +00005130 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattner5f38e5d2009-07-17 23:57:13 +00005131 CGM.AddUsedGlobal(Entry);
Daniel Dunbar3c190812009-04-18 08:51:00 +00005132 }
5133
5134 return Builder.CreateLoad(Entry, false, "tmp");
5135}
Fariborz Jahanian917c0402009-02-05 20:41:40 +00005136
Daniel Dunbar3c190812009-04-18 08:51:00 +00005137llvm::Value *
5138CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
5139 const ObjCInterfaceDecl *ID) {
5140 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
5141
5142 if (!Entry) {
5143 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5144 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5145 Entry =
Owen Anderson94148482009-07-08 19:05:04 +00005146 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5147 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar3c190812009-04-18 08:51:00 +00005148 ClassGV,
Owen Anderson94148482009-07-08 19:05:04 +00005149 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar3c190812009-04-18 08:51:00 +00005150 Entry->setAlignment(
5151 CGM.getTargetData().getPrefTypeAlignment(
5152 ObjCTypes.ClassnfABIPtrTy));
5153 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattner5f38e5d2009-07-17 23:57:13 +00005154 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian917c0402009-02-05 20:41:40 +00005155 }
5156
5157 return Builder.CreateLoad(Entry, false, "tmp");
5158}
5159
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00005160/// EmitMetaClassRef - Return a Value * of the address of _class_t
5161/// meta-data
5162///
5163llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5164 const ObjCInterfaceDecl *ID) {
5165 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5166 if (Entry)
5167 return Builder.CreateLoad(Entry, false, "tmp");
5168
Daniel Dunbara2d275d2009-04-07 05:48:37 +00005169 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanianab438842009-04-14 18:41:56 +00005170 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00005171 Entry =
Owen Anderson94148482009-07-08 19:05:04 +00005172 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00005173 llvm::GlobalValue::InternalLinkage,
5174 MetaClassGV,
Owen Anderson94148482009-07-08 19:05:04 +00005175 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00005176 Entry->setAlignment(
5177 CGM.getTargetData().getPrefTypeAlignment(
5178 ObjCTypes.ClassnfABIPtrTy));
5179
Daniel Dunbar4993e292009-04-15 19:03:14 +00005180 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattner5f38e5d2009-07-17 23:57:13 +00005181 CGM.AddUsedGlobal(Entry);
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00005182
5183 return Builder.CreateLoad(Entry, false, "tmp");
5184}
5185
Fariborz Jahanian917c0402009-02-05 20:41:40 +00005186/// GetClass - Return a reference to the class for the given interface
5187/// decl.
5188llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5189 const ObjCInterfaceDecl *ID) {
5190 return EmitClassRef(Builder, ID);
5191}
5192
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00005193/// Generates a message send where the super is the receiver. This is
5194/// a message send to self with special delivery semantics indicating
5195/// which class's method should be called.
5196CodeGen::RValue
5197CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
5198 QualType ResultType,
5199 Selector Sel,
5200 const ObjCInterfaceDecl *Class,
Fariborz Jahanian17636fa2009-02-28 20:07:56 +00005201 bool isCategoryImpl,
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00005202 llvm::Value *Receiver,
5203 bool IsClassMessage,
5204 const CodeGen::CallArgList &CallArgs) {
5205 // ...
5206 // Create and init a super structure; this is a (receiver, class)
5207 // pair we will pass to objc_msgSendSuper.
5208 llvm::Value *ObjCSuper =
5209 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
5210
5211 llvm::Value *ReceiverAsObject =
5212 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5213 CGF.Builder.CreateStore(ReceiverAsObject,
5214 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
5215
5216 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian17636fa2009-02-28 20:07:56 +00005217 llvm::Value *Target;
5218 if (IsClassMessage) {
5219 if (isCategoryImpl) {
5220 // Message sent to "super' in a class method defined in
5221 // a category implementation.
Daniel Dunbar3c190812009-04-18 08:51:00 +00005222 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian17636fa2009-02-28 20:07:56 +00005223 Target = CGF.Builder.CreateStructGEP(Target, 0);
5224 Target = CGF.Builder.CreateLoad(Target);
5225 }
5226 else
5227 Target = EmitMetaClassRef(CGF.Builder, Class);
5228 }
5229 else
Daniel Dunbar3c190812009-04-18 08:51:00 +00005230 Target = EmitSuperClassRef(CGF.Builder, Class);
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00005231
Mike Stumpba2cb0e2009-05-16 07:57:57 +00005232 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5233 // ObjCTypes types.
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00005234 const llvm::Type *ClassTy =
5235 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5236 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5237 CGF.Builder.CreateStore(Target,
5238 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
5239
Fariborz Jahanian5c76fd32009-05-11 19:25:47 +00005240 return (LegacyDispatchedSelector(Sel))
5241 ? EmitLegacyMessageSend(CGF, ResultType,EmitSelector(CGF.Builder, Sel),
5242 ObjCSuper, ObjCTypes.SuperPtrCTy,
5243 true, CallArgs,
5244 ObjCTypes)
5245 : EmitMessageSend(CGF, ResultType, Sel,
5246 ObjCSuper, ObjCTypes.SuperPtrCTy,
5247 true, CallArgs);
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00005248}
Fariborz Jahanianebb82c62009-02-11 20:51:17 +00005249
5250llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
5251 Selector Sel) {
5252 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
5253
5254 if (!Entry) {
5255 llvm::Constant *Casted =
Owen Anderson73e7f802009-07-14 23:10:40 +00005256 VMContext.getConstantExprBitCast(GetMethodVarName(Sel),
Fariborz Jahanianebb82c62009-02-11 20:51:17 +00005257 ObjCTypes.SelectorPtrTy);
5258 Entry =
Owen Anderson94148482009-07-08 19:05:04 +00005259 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
Fariborz Jahanianebb82c62009-02-11 20:51:17 +00005260 llvm::GlobalValue::InternalLinkage,
Owen Anderson94148482009-07-08 19:05:04 +00005261 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahanian5c76fd32009-05-11 19:25:47 +00005262 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattner5f38e5d2009-07-17 23:57:13 +00005263 CGM.AddUsedGlobal(Entry);
Fariborz Jahanianebb82c62009-02-11 20:51:17 +00005264 }
5265
5266 return Builder.CreateLoad(Entry, false, "tmp");
5267}
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00005268/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
5269/// objc_assign_ivar (id src, id *dst)
5270///
5271void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
5272 llvm::Value *src, llvm::Value *dst)
5273{
Fariborz Jahanianad51ca02009-03-23 19:10:40 +00005274 const llvm::Type * SrcTy = src->getType();
5275 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsee6f6f82009-05-09 07:08:47 +00005276 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianad51ca02009-03-23 19:10:40 +00005277 assert(Size <= 8 && "does not support size > 8");
5278 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5279 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian664da982009-03-13 00:42:52 +00005280 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5281 }
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00005282 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5283 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerf6ec7e42009-04-22 02:38:11 +00005284 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(),
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00005285 src, dst, "assignivar");
5286 return;
5287}
5288
5289/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5290/// objc_assign_strongCast (id src, id *dst)
5291///
5292void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
5293 CodeGen::CodeGenFunction &CGF,
5294 llvm::Value *src, llvm::Value *dst)
5295{
Fariborz Jahanianad51ca02009-03-23 19:10:40 +00005296 const llvm::Type * SrcTy = src->getType();
5297 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsee6f6f82009-05-09 07:08:47 +00005298 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianad51ca02009-03-23 19:10:40 +00005299 assert(Size <= 8 && "does not support size > 8");
5300 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5301 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian664da982009-03-13 00:42:52 +00005302 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5303 }
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00005304 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5305 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerf6ec7e42009-04-22 02:38:11 +00005306 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00005307 src, dst, "weakassign");
5308 return;
5309}
5310
Fariborz Jahanian614e8f02009-07-08 01:18:33 +00005311void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
5312 CodeGen::CodeGenFunction &CGF,
5313 llvm::Value *DestPtr,
5314 llvm::Value *SrcPtr,
5315 unsigned long size) {
5316 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5317 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Owen Anderson73e7f802009-07-14 23:10:40 +00005318 llvm::Value *N = VMContext.getConstantInt(ObjCTypes.LongTy, size);
Fariborz Jahanian614e8f02009-07-08 01:18:33 +00005319 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
5320 DestPtr, SrcPtr, N);
5321 return;
5322}
5323
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00005324/// EmitObjCWeakRead - Code gen for loading value of a __weak
5325/// object: objc_read_weak (id *src)
5326///
5327llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
5328 CodeGen::CodeGenFunction &CGF,
5329 llvm::Value *AddrWeakObj)
5330{
Eli Friedmanf8466232009-03-07 03:57:15 +00005331 const llvm::Type* DestTy =
5332 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00005333 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattnera7ecda42009-04-22 02:44:54 +00005334 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00005335 AddrWeakObj, "weakread");
Eli Friedmanf8466232009-03-07 03:57:15 +00005336 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00005337 return read_weak;
5338}
5339
5340/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5341/// objc_assign_weak (id src, id *dst)
5342///
5343void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
5344 llvm::Value *src, llvm::Value *dst)
5345{
Fariborz Jahanianad51ca02009-03-23 19:10:40 +00005346 const llvm::Type * SrcTy = src->getType();
5347 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsee6f6f82009-05-09 07:08:47 +00005348 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianad51ca02009-03-23 19:10:40 +00005349 assert(Size <= 8 && "does not support size > 8");
5350 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5351 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian664da982009-03-13 00:42:52 +00005352 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5353 }
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00005354 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5355 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner293c1d32009-04-17 22:12:36 +00005356 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00005357 src, dst, "weakassign");
5358 return;
5359}
5360
5361/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5362/// objc_assign_global (id src, id *dst)
5363///
5364void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
5365 llvm::Value *src, llvm::Value *dst)
5366{
Fariborz Jahanianad51ca02009-03-23 19:10:40 +00005367 const llvm::Type * SrcTy = src->getType();
5368 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsee6f6f82009-05-09 07:08:47 +00005369 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianad51ca02009-03-23 19:10:40 +00005370 assert(Size <= 8 && "does not support size > 8");
5371 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5372 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian664da982009-03-13 00:42:52 +00005373 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5374 }
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00005375 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5376 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerf6ec7e42009-04-22 02:38:11 +00005377 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00005378 src, dst, "globalassign");
5379 return;
5380}
Fariborz Jahanianebb82c62009-02-11 20:51:17 +00005381
Daniel Dunbar75de89f2009-02-24 07:47:38 +00005382void
5383CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5384 const Stmt &S) {
Daniel Dunbar75de89f2009-02-24 07:47:38 +00005385 bool isTry = isa<ObjCAtTryStmt>(S);
5386 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
5387 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005388 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar75de89f2009-02-24 07:47:38 +00005389 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005390 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
Daniel Dunbar75de89f2009-02-24 07:47:38 +00005391 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
5392
5393 // For @synchronized, call objc_sync_enter(sync.expr). The
5394 // evaluation of the expression must occur before we enter the
5395 // @synchronized. We can safely avoid a temp here because jumps into
5396 // @synchronized are illegal & this will dominate uses.
5397 llvm::Value *SyncArg = 0;
5398 if (!isTry) {
5399 SyncArg =
5400 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
5401 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattner23e24652009-04-06 16:53:45 +00005402 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar75de89f2009-02-24 07:47:38 +00005403 }
5404
5405 // Push an EH context entry, used for handling rethrows and jumps
5406 // through finally.
5407 CGF.PushCleanupBlock(FinallyBlock);
5408
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005409 CGF.setInvokeDest(TryHandler);
Daniel Dunbar75de89f2009-02-24 07:47:38 +00005410
5411 CGF.EmitBlock(TryBlock);
5412 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
5413 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
5414 CGF.EmitBranchThroughCleanup(FinallyEnd);
5415
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005416 // Emit the exception handler.
5417
5418 CGF.EmitBlock(TryHandler);
5419
5420 llvm::Value *llvm_eh_exception =
5421 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
5422 llvm::Value *llvm_eh_selector_i64 =
5423 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64);
5424 llvm::Value *llvm_eh_typeid_for_i64 =
5425 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64);
5426 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5427 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow");
5428
5429 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
5430 SelectorArgs.push_back(Exc);
Chris Lattner23e24652009-04-06 16:53:45 +00005431 SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005432
5433 // Construct the lists of (type, catch body) to handle.
Daniel Dunbar0098e7a2009-03-06 00:01:21 +00005434 llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers;
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005435 bool HasCatchAll = false;
5436 if (isTry) {
5437 if (const ObjCAtCatchStmt* CatchStmt =
5438 cast<ObjCAtTryStmt>(S).getCatchStmts()) {
5439 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar0098e7a2009-03-06 00:01:21 +00005440 const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Steve Naroff0e8b96a2009-03-03 19:52:17 +00005441 Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody()));
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005442
5443 // catch(...) always matches.
Steve Naroff0e8b96a2009-03-03 19:52:17 +00005444 if (!CatchDecl) {
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005445 // Use i8* null here to signal this is a catch all, not a cleanup.
Owen Anderson5f1adc22009-07-13 04:10:07 +00005446 llvm::Value *Null = VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005447 SelectorArgs.push_back(Null);
5448 HasCatchAll = true;
5449 break;
5450 }
5451
Steve Naroff329ec222009-07-10 23:34:53 +00005452 if (CatchDecl->getType()->isObjCIdType() ||
Daniel Dunbar0098e7a2009-03-06 00:01:21 +00005453 CatchDecl->getType()->isObjCQualifiedIdType()) {
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005454 llvm::Value *IDEHType =
5455 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5456 if (!IDEHType)
5457 IDEHType =
Owen Anderson94148482009-07-08 19:05:04 +00005458 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
5459 false,
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005460 llvm::GlobalValue::ExternalLinkage,
Owen Anderson94148482009-07-08 19:05:04 +00005461 0, "OBJC_EHTYPE_id");
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005462 SelectorArgs.push_back(IDEHType);
5463 HasCatchAll = true;
5464 break;
5465 }
5466
5467 // All other types should be Objective-C interface pointer types.
Steve Naroff329ec222009-07-10 23:34:53 +00005468 const ObjCObjectPointerType *PT =
5469 CatchDecl->getType()->getAsObjCObjectPointerType();
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005470 assert(PT && "Invalid @catch type.");
Steve Naroff329ec222009-07-10 23:34:53 +00005471 const ObjCInterfaceType *IT = PT->getInterfaceType();
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005472 assert(IT && "Invalid @catch type.");
Daniel Dunbarc2129532009-04-08 04:21:03 +00005473 llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false);
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005474 SelectorArgs.push_back(EHType);
5475 }
5476 }
5477 }
5478
5479 // We use a cleanup unless there was already a catch all.
5480 if (!HasCatchAll) {
Owen Anderson73e7f802009-07-14 23:10:40 +00005481 SelectorArgs.push_back(VMContext.getConstantInt(llvm::Type::Int32Ty, 0));
Daniel Dunbar0098e7a2009-03-06 00:01:21 +00005482 Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0));
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005483 }
5484
5485 llvm::Value *Selector =
5486 CGF.Builder.CreateCall(llvm_eh_selector_i64,
5487 SelectorArgs.begin(), SelectorArgs.end(),
5488 "selector");
5489 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
Daniel Dunbar0098e7a2009-03-06 00:01:21 +00005490 const ParmVarDecl *CatchParam = Handlers[i].first;
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005491 const Stmt *CatchBody = Handlers[i].second;
5492
5493 llvm::BasicBlock *Next = 0;
5494
5495 // The last handler always matches.
5496 if (i + 1 != e) {
5497 assert(CatchParam && "Only last handler can be a catch all.");
5498
5499 llvm::BasicBlock *Match = CGF.createBasicBlock("match");
5500 Next = CGF.createBasicBlock("catch.next");
5501 llvm::Value *Id =
5502 CGF.Builder.CreateCall(llvm_eh_typeid_for_i64,
5503 CGF.Builder.CreateBitCast(SelectorArgs[i+2],
5504 ObjCTypes.Int8PtrTy));
5505 CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id),
5506 Match, Next);
5507
5508 CGF.EmitBlock(Match);
5509 }
5510
5511 if (CatchBody) {
5512 llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end");
5513 llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler");
5514
5515 // Cleanups must call objc_end_catch.
5516 //
Mike Stumpba2cb0e2009-05-16 07:57:57 +00005517 // FIXME: It seems incorrect for objc_begin_catch to be inside this
5518 // context, but this matches gcc.
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005519 CGF.PushCleanupBlock(MatchEnd);
5520 CGF.setInvokeDest(MatchHandler);
5521
5522 llvm::Value *ExcObject =
Chris Lattner93dca5b2009-04-22 02:15:23 +00005523 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc);
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005524
5525 // Bind the catch parameter if it exists.
5526 if (CatchParam) {
Daniel Dunbar0098e7a2009-03-06 00:01:21 +00005527 ExcObject =
5528 CGF.Builder.CreateBitCast(ExcObject,
5529 CGF.ConvertType(CatchParam->getType()));
5530 // CatchParam is a ParmVarDecl because of the grammar
5531 // construction used to handle this, but for codegen purposes
5532 // we treat this as a local decl.
5533 CGF.EmitLocalBlockVarDecl(*CatchParam);
5534 CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005535 }
5536
5537 CGF.ObjCEHValueStack.push_back(ExcObject);
5538 CGF.EmitStmt(CatchBody);
5539 CGF.ObjCEHValueStack.pop_back();
5540
5541 CGF.EmitBranchThroughCleanup(FinallyEnd);
5542
5543 CGF.EmitBlock(MatchHandler);
5544
5545 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5546 // We are required to emit this call to satisfy LLVM, even
5547 // though we don't use the result.
5548 llvm::SmallVector<llvm::Value*, 8> Args;
5549 Args.push_back(Exc);
Chris Lattner23e24652009-04-06 16:53:45 +00005550 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Owen Anderson73e7f802009-07-14 23:10:40 +00005551 Args.push_back(VMContext.getConstantInt(llvm::Type::Int32Ty,
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005552 0));
5553 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5554 CGF.Builder.CreateStore(Exc, RethrowPtr);
5555 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5556
5557 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5558
5559 CGF.EmitBlock(MatchEnd);
5560
5561 // Unfortunately, we also have to generate another EH frame here
5562 // in case this throws.
5563 llvm::BasicBlock *MatchEndHandler =
5564 CGF.createBasicBlock("match.end.handler");
5565 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattner93dca5b2009-04-22 02:15:23 +00005566 CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(),
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005567 Cont, MatchEndHandler,
5568 Args.begin(), Args.begin());
5569
5570 CGF.EmitBlock(Cont);
5571 if (Info.SwitchBlock)
5572 CGF.EmitBlock(Info.SwitchBlock);
5573 if (Info.EndBlock)
5574 CGF.EmitBlock(Info.EndBlock);
5575
5576 CGF.EmitBlock(MatchEndHandler);
5577 Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5578 // We are required to emit this call to satisfy LLVM, even
5579 // though we don't use the result.
5580 Args.clear();
5581 Args.push_back(Exc);
Chris Lattner23e24652009-04-06 16:53:45 +00005582 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Owen Anderson73e7f802009-07-14 23:10:40 +00005583 Args.push_back(VMContext.getConstantInt(llvm::Type::Int32Ty,
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005584 0));
5585 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5586 CGF.Builder.CreateStore(Exc, RethrowPtr);
5587 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5588
5589 if (Next)
5590 CGF.EmitBlock(Next);
5591 } else {
5592 assert(!Next && "catchup should be last handler.");
5593
5594 CGF.Builder.CreateStore(Exc, RethrowPtr);
5595 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5596 }
5597 }
5598
Daniel Dunbar75de89f2009-02-24 07:47:38 +00005599 // Pop the cleanup entry, the @finally is outside this cleanup
5600 // scope.
5601 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5602 CGF.setInvokeDest(PrevLandingPad);
5603
5604 CGF.EmitBlock(FinallyBlock);
5605
5606 if (isTry) {
5607 if (const ObjCAtFinallyStmt* FinallyStmt =
5608 cast<ObjCAtTryStmt>(S).getFinallyStmt())
5609 CGF.EmitStmt(FinallyStmt->getFinallyBody());
5610 } else {
5611 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
5612 // @synchronized.
Chris Lattnerf6ec7e42009-04-22 02:38:11 +00005613 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005614 }
Daniel Dunbar75de89f2009-02-24 07:47:38 +00005615
5616 if (Info.SwitchBlock)
5617 CGF.EmitBlock(Info.SwitchBlock);
5618 if (Info.EndBlock)
5619 CGF.EmitBlock(Info.EndBlock);
5620
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005621 // Branch around the rethrow code.
Daniel Dunbar75de89f2009-02-24 07:47:38 +00005622 CGF.EmitBranch(FinallyEnd);
5623
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005624 CGF.EmitBlock(FinallyRethrow);
Chris Lattner93dca5b2009-04-22 02:15:23 +00005625 CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(),
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005626 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbar75de89f2009-02-24 07:47:38 +00005627 CGF.Builder.CreateUnreachable();
5628
5629 CGF.EmitBlock(FinallyEnd);
5630}
5631
Anders Carlsson1cf75362009-02-16 22:59:18 +00005632/// EmitThrowStmt - Generate code for a throw statement.
5633void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
5634 const ObjCAtThrowStmt &S) {
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005635 llvm::Value *Exception;
Anders Carlsson1cf75362009-02-16 22:59:18 +00005636 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005637 Exception = CGF.EmitScalarExpr(ThrowExpr);
Anders Carlsson1cf75362009-02-16 22:59:18 +00005638 } else {
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005639 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
5640 "Unexpected rethrow outside @catch block.");
5641 Exception = CGF.ObjCEHValueStack.back();
Anders Carlsson1cf75362009-02-16 22:59:18 +00005642 }
5643
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005644 llvm::Value *ExceptionAsObject =
5645 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
5646 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
5647 if (InvokeDest) {
5648 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattnerf6ec7e42009-04-22 02:38:11 +00005649 CGF.Builder.CreateInvoke(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005650 Cont, InvokeDest,
5651 &ExceptionAsObject, &ExceptionAsObject + 1);
5652 CGF.EmitBlock(Cont);
5653 } else
Chris Lattnerf6ec7e42009-04-22 02:38:11 +00005654 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005655 CGF.Builder.CreateUnreachable();
5656
Anders Carlsson1cf75362009-02-16 22:59:18 +00005657 // Clear the insertion point to indicate we are in unreachable code.
5658 CGF.Builder.ClearInsertionPoint();
5659}
Daniel Dunbar9c285e72009-03-01 04:46:24 +00005660
5661llvm::Value *
Daniel Dunbarc2129532009-04-08 04:21:03 +00005662CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
5663 bool ForDefinition) {
Daniel Dunbar9c285e72009-03-01 04:46:24 +00005664 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbar9c285e72009-03-01 04:46:24 +00005665
Daniel Dunbarc2129532009-04-08 04:21:03 +00005666 // If we don't need a definition, return the entry if found or check
5667 // if we use an external reference.
5668 if (!ForDefinition) {
5669 if (Entry)
5670 return Entry;
Daniel Dunbarafac9be2009-04-07 06:43:45 +00005671
Daniel Dunbarc2129532009-04-08 04:21:03 +00005672 // If this type (or a super class) has the __objc_exception__
5673 // attribute, emit an external reference.
Douglas Gregor98da6ae2009-06-18 16:11:24 +00005674 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbarc2129532009-04-08 04:21:03 +00005675 return Entry =
Owen Anderson94148482009-07-08 19:05:04 +00005676 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbarc2129532009-04-08 04:21:03 +00005677 llvm::GlobalValue::ExternalLinkage,
5678 0,
5679 (std::string("OBJC_EHTYPE_$_") +
Owen Anderson94148482009-07-08 19:05:04 +00005680 ID->getIdentifier()->getName()));
Daniel Dunbarc2129532009-04-08 04:21:03 +00005681 }
5682
5683 // Otherwise we need to either make a new entry or fill in the
5684 // initializer.
5685 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbara2d275d2009-04-07 05:48:37 +00005686 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar9c285e72009-03-01 04:46:24 +00005687 std::string VTableName = "objc_ehtype_vtable";
5688 llvm::GlobalVariable *VTableGV =
5689 CGM.getModule().getGlobalVariable(VTableName);
5690 if (!VTableGV)
Owen Anderson94148482009-07-08 19:05:04 +00005691 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
5692 false,
Daniel Dunbar9c285e72009-03-01 04:46:24 +00005693 llvm::GlobalValue::ExternalLinkage,
Owen Anderson94148482009-07-08 19:05:04 +00005694 0, VTableName);
Daniel Dunbar9c285e72009-03-01 04:46:24 +00005695
Owen Anderson73e7f802009-07-14 23:10:40 +00005696 llvm::Value *VTableIdx = VMContext.getConstantInt(llvm::Type::Int32Ty, 2);
Daniel Dunbar9c285e72009-03-01 04:46:24 +00005697
5698 std::vector<llvm::Constant*> Values(3);
Owen Anderson73e7f802009-07-14 23:10:40 +00005699 Values[0] = VMContext.getConstantExprGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbar9c285e72009-03-01 04:46:24 +00005700 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanianab438842009-04-14 18:41:56 +00005701 Values[2] = GetClassGlobal(ClassName);
Owen Anderson73e7f802009-07-14 23:10:40 +00005702 llvm::Constant *Init =
5703 VMContext.getConstantStruct(ObjCTypes.EHTypeTy, Values);
Daniel Dunbar9c285e72009-03-01 04:46:24 +00005704
Daniel Dunbarc2129532009-04-08 04:21:03 +00005705 if (Entry) {
5706 Entry->setInitializer(Init);
5707 } else {
Owen Anderson94148482009-07-08 19:05:04 +00005708 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbarc2129532009-04-08 04:21:03 +00005709 llvm::GlobalValue::WeakAnyLinkage,
5710 Init,
5711 (std::string("OBJC_EHTYPE_$_") +
Owen Anderson94148482009-07-08 19:05:04 +00005712 ID->getIdentifier()->getName()));
Daniel Dunbarc2129532009-04-08 04:21:03 +00005713 }
5714
Daniel Dunbar8394fda2009-04-14 06:00:08 +00005715 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbara2d275d2009-04-07 05:48:37 +00005716 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarc2129532009-04-08 04:21:03 +00005717 Entry->setAlignment(8);
5718
5719 if (ForDefinition) {
5720 Entry->setSection("__DATA,__objc_const");
5721 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
5722 } else {
5723 Entry->setSection("__DATA,__datacoal_nt,coalesced");
5724 }
Daniel Dunbar9c285e72009-03-01 04:46:24 +00005725
5726 return Entry;
5727}
Anders Carlsson1cf75362009-02-16 22:59:18 +00005728
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00005729/* *** */
5730
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00005731CodeGen::CGObjCRuntime *
5732CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00005733 return new CGObjCMac(CGM);
5734}
Fariborz Jahanian48543f52009-01-21 22:04:16 +00005735
5736CodeGen::CGObjCRuntime *
Fariborz Jahaniand0374812009-01-22 23:02:58 +00005737CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00005738 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanian48543f52009-01-21 22:04:16 +00005739}