blob: a2ef680e2bb4d607559e771a3fb704d04f386456 [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"
Daniel Dunbar07ddfa92009-05-03 10:46:44 +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"
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000026#include "llvm/Module.h"
Daniel Dunbar35b777f2008-10-29 22:36:39 +000027#include "llvm/ADT/DenseSet.h"
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +000028#include "llvm/Target/TargetData.h"
Daniel Dunbarace33292008-08-16 03:19:19 +000029#include <sstream>
Daniel Dunbar8c85fac2008-08-11 02:45:11 +000030
31using namespace clang;
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +000032using namespace CodeGen;
Daniel Dunbar8c85fac2008-08-11 02:45:11 +000033
Daniel Dunbar85d37542009-04-22 07:32:20 +000034// Common CGObjCRuntime functions, these don't belong here, but they
35// don't belong in CGObjCRuntime either so we will live with it for
36// now.
37
Daniel Dunbar07ddfa92009-05-03 10:46:44 +000038static const llvm::StructType *
39GetConcreteClassStruct(CodeGen::CodeGenModule &CGM,
40 const ObjCInterfaceDecl *OID) {
Daniel Dunbare5bb23c2009-04-22 09:39:34 +000041 assert(!OID->isForwardDecl() && "Invalid interface decl!");
Daniel Dunbarfa6fda42009-04-22 10:28:39 +000042 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
43 return cast<llvm::StructType>(CGM.getTypes().ConvertTagDeclType(RD));
Daniel Dunbare5bb23c2009-04-22 09:39:34 +000044}
45
Daniel Dunbar94d2ede2009-05-03 13:15:50 +000046/// FindIvarInterface - Find the interface containing the ivar.
Daniel Dunbarfb65bfb2009-04-22 12:00:04 +000047///
Daniel Dunbar94d2ede2009-05-03 13:15:50 +000048/// FIXME: We shouldn't need to do this, the containing context should
49/// be fixed.
50static const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context,
51 const ObjCInterfaceDecl *OID,
52 const ObjCIvarDecl *OIVD,
53 unsigned &Index) {
54 const ObjCInterfaceDecl *Super = OID->getSuperClass();
Daniel Dunbar1af336e2009-04-22 17:43:55 +000055
Daniel Dunbar94d2ede2009-05-03 13:15:50 +000056 // FIXME: The index here is closely tied to how
57 // ASTContext::getObjCLayout is implemented. This should be fixed to
58 // get the information from the layout directly.
59 Index = 0;
60 for (ObjCInterfaceDecl::ivar_iterator IVI = OID->ivar_begin(),
61 IVE = OID->ivar_end(); IVI != IVE; ++IVI, ++Index)
62 if (OIVD == *IVI)
63 return OID;
64
65 // Also look in synthesized ivars.
66 for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(Context),
67 E = OID->prop_end(Context); I != E; ++I) {
68 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl()) {
69 if (OIVD == Ivar)
70 return OID;
71 ++Index;
72 }
Daniel Dunbar1af336e2009-04-22 17:43:55 +000073 }
74
Daniel Dunbar94d2ede2009-05-03 13:15:50 +000075 // Otherwise check in the super class.
76 if (Super)
77 return FindIvarInterface(Context, Super, OIVD, Index);
78
79 return 0;
Daniel Dunbarfb65bfb2009-04-22 12:00:04 +000080}
81
Daniel Dunbar35403f92009-05-03 08:55:17 +000082static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM,
83 const ObjCInterfaceDecl *OID,
Daniel Dunbared4d5962009-05-03 12:57:56 +000084 const ObjCImplementationDecl *ID,
Daniel Dunbar35403f92009-05-03 08:55:17 +000085 const ObjCIvarDecl *Ivar) {
Daniel Dunbar94d2ede2009-05-03 13:15:50 +000086 unsigned Index;
87 const ObjCInterfaceDecl *Container =
88 FindIvarInterface(CGM.getContext(), OID, Ivar, Index);
89 assert(Container && "Unable to find ivar container");
90
91 // If we know have an implementation (and the ivar is in it) then
92 // look up in the implementation layout.
93 const ASTRecordLayout *RL;
94 if (ID && ID->getClassInterface() == Container)
95 RL = &CGM.getContext().getASTObjCImplementationLayout(ID);
96 else
97 RL = &CGM.getContext().getASTObjCInterfaceLayout(Container);
98 return RL->getFieldOffset(Index);
Daniel Dunbar35403f92009-05-03 08:55:17 +000099}
100
101uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
102 const ObjCInterfaceDecl *OID,
103 const ObjCIvarDecl *Ivar) {
Daniel Dunbared4d5962009-05-03 12:57:56 +0000104 return LookupFieldBitOffset(CGM, OID, 0, Ivar) / 8;
105}
106
107uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
108 const ObjCImplementationDecl *OID,
109 const ObjCIvarDecl *Ivar) {
110 return LookupFieldBitOffset(CGM, OID->getClassInterface(), OID, Ivar) / 8;
Daniel Dunbar85d37542009-04-22 07:32:20 +0000111}
112
113LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
114 const ObjCInterfaceDecl *OID,
115 llvm::Value *BaseValue,
116 const ObjCIvarDecl *Ivar,
117 unsigned CVRQualifiers,
118 llvm::Value *Offset) {
Daniel Dunbar35403f92009-05-03 08:55:17 +0000119 // Compute (type*) ( (char *) BaseValue + Offset)
Daniel Dunbar85d37542009-04-22 07:32:20 +0000120 llvm::Type *I8Ptr = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Daniel Dunbar35403f92009-05-03 08:55:17 +0000121 QualType IvarTy = Ivar->getType();
122 const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy);
Daniel Dunbar85d37542009-04-22 07:32:20 +0000123 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr);
Daniel Dunbar85d37542009-04-22 07:32:20 +0000124 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
Daniel Dunbar35403f92009-05-03 08:55:17 +0000125 V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar85d37542009-04-22 07:32:20 +0000126
127 if (Ivar->isBitField()) {
Daniel Dunbared4d5962009-05-03 12:57:56 +0000128 // We need to compute the bit offset for the bit-field, the offset
129 // is to the byte. Note, there is a subtle invariant here: we can
130 // only call this routine on non-sythesized ivars but we may be
131 // called for synthesized ivars. However, a synthesized ivar can
132 // never be a bit-field so this is safe.
133 uint64_t BitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar) % 8;
134
Daniel Dunbar35403f92009-05-03 08:55:17 +0000135 uint64_t BitFieldSize =
136 Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue();
137 return LValue::MakeBitfield(V, BitOffset, BitFieldSize,
Daniel Dunbar0e453312009-05-03 07:52:00 +0000138 IvarTy->isSignedIntegerType(),
139 IvarTy.getCVRQualifiers()|CVRQualifiers);
Daniel Dunbar85d37542009-04-22 07:32:20 +0000140 }
141
Daniel Dunbar35403f92009-05-03 08:55:17 +0000142 LValue LV = LValue::MakeAddr(V, IvarTy.getCVRQualifiers()|CVRQualifiers,
143 CGF.CGM.getContext().getObjCGCAttrKind(IvarTy));
Daniel Dunbar85d37542009-04-22 07:32:20 +0000144 LValue::SetObjCIvar(LV, true);
145 return LV;
146}
147
148///
149
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000150namespace {
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000151
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000152 typedef std::vector<llvm::Constant*> ConstantVector;
153
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000154 // FIXME: We should find a nicer way to make the labels for
155 // metadata, string concatenation is lame.
156
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000157class ObjCCommonTypesHelper {
158protected:
159 CodeGen::CodeGenModule &CGM;
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000160
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000161public:
Fariborz Jahanianad51ca02009-03-23 19:10:40 +0000162 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000163 const llvm::Type *Int8PtrTy;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000164
Daniel Dunbar6fa3daf2008-08-12 05:28:47 +0000165 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
166 const llvm::Type *ObjectPtrTy;
Fariborz Jahanianc192d4d2008-11-18 20:18:11 +0000167
168 /// PtrObjectPtrTy - LLVM type for id *
169 const llvm::Type *PtrObjectPtrTy;
170
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000171 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar6fa3daf2008-08-12 05:28:47 +0000172 const llvm::Type *SelectorPtrTy;
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000173 /// ProtocolPtrTy - LLVM type for external protocol handles
174 /// (typeof(Protocol))
175 const llvm::Type *ExternalProtocolPtrTy;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000176
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000177 // SuperCTy - clang type for struct objc_super.
178 QualType SuperCTy;
179 // SuperPtrCTy - clang type for struct objc_super *.
180 QualType SuperPtrCTy;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000181
Daniel Dunbar15245e52008-08-23 04:28:29 +0000182 /// SuperTy - LLVM type for struct objc_super.
183 const llvm::StructType *SuperTy;
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000184 /// SuperPtrTy - LLVM type for struct objc_super *.
185 const llvm::Type *SuperPtrTy;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000186
Fariborz Jahaniand0374812009-01-22 23:02:58 +0000187 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
188 /// in GCC parlance).
189 const llvm::StructType *PropertyTy;
190
191 /// PropertyListTy - LLVM type for struct objc_property_list
192 /// (_prop_list_t in GCC parlance).
193 const llvm::StructType *PropertyListTy;
194 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
195 const llvm::Type *PropertyListPtrTy;
196
197 // MethodTy - LLVM type for struct objc_method.
198 const llvm::StructType *MethodTy;
199
Fariborz Jahanian781f2732009-01-23 01:46:23 +0000200 /// CacheTy - LLVM type for struct objc_cache.
201 const llvm::Type *CacheTy;
202 /// CachePtrTy - LLVM type for struct objc_cache *.
203 const llvm::Type *CachePtrTy;
204
Chris Lattnera7ecda42009-04-22 02:44:54 +0000205 llvm::Constant *getGetPropertyFn() {
206 CodeGen::CodeGenTypes &Types = CGM.getTypes();
207 ASTContext &Ctx = CGM.getContext();
208 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
209 llvm::SmallVector<QualType,16> Params;
210 QualType IdType = Ctx.getObjCIdType();
211 QualType SelType = Ctx.getObjCSelType();
212 Params.push_back(IdType);
213 Params.push_back(SelType);
214 Params.push_back(Ctx.LongTy);
215 Params.push_back(Ctx.BoolTy);
216 const llvm::FunctionType *FTy =
217 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
218 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
219 }
Fariborz Jahanian4b161702009-01-22 00:37:21 +0000220
Chris Lattnera7ecda42009-04-22 02:44:54 +0000221 llvm::Constant *getSetPropertyFn() {
222 CodeGen::CodeGenTypes &Types = CGM.getTypes();
223 ASTContext &Ctx = CGM.getContext();
224 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
225 llvm::SmallVector<QualType,16> Params;
226 QualType IdType = Ctx.getObjCIdType();
227 QualType SelType = Ctx.getObjCSelType();
228 Params.push_back(IdType);
229 Params.push_back(SelType);
230 Params.push_back(Ctx.LongTy);
231 Params.push_back(IdType);
232 Params.push_back(Ctx.BoolTy);
233 Params.push_back(Ctx.BoolTy);
234 const llvm::FunctionType *FTy =
235 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
236 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
237 }
238
239 llvm::Constant *getEnumerationMutationFn() {
240 // void objc_enumerationMutation (id)
241 std::vector<const llvm::Type*> Args;
242 Args.push_back(ObjectPtrTy);
243 llvm::FunctionType *FTy =
244 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
245 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
246 }
Fariborz Jahanian4b161702009-01-22 00:37:21 +0000247
248 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattnera7ecda42009-04-22 02:44:54 +0000249 llvm::Constant *getGcReadWeakFn() {
250 // id objc_read_weak (id *)
251 std::vector<const llvm::Type*> Args;
252 Args.push_back(ObjectPtrTy->getPointerTo());
253 llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false);
254 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
255 }
Fariborz Jahanian4b161702009-01-22 00:37:21 +0000256
257 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner293c1d32009-04-17 22:12:36 +0000258 llvm::Constant *getGcAssignWeakFn() {
259 // id objc_assign_weak (id, id *)
260 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
261 Args.push_back(ObjectPtrTy->getPointerTo());
262 llvm::FunctionType *FTy =
263 llvm::FunctionType::get(ObjectPtrTy, Args, false);
264 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
265 }
Fariborz Jahanian4b161702009-01-22 00:37:21 +0000266
267 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerf6ec7e42009-04-22 02:38:11 +0000268 llvm::Constant *getGcAssignGlobalFn() {
269 // id objc_assign_global(id, id *)
270 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
271 Args.push_back(ObjectPtrTy->getPointerTo());
272 llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false);
273 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
274 }
Fariborz Jahanian4b161702009-01-22 00:37:21 +0000275
276 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerf6ec7e42009-04-22 02:38:11 +0000277 llvm::Constant *getGcAssignIvarFn() {
278 // id objc_assign_ivar(id, id *)
279 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
280 Args.push_back(ObjectPtrTy->getPointerTo());
281 llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false);
282 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
283 }
Fariborz Jahanian4b161702009-01-22 00:37:21 +0000284
285 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerf6ec7e42009-04-22 02:38:11 +0000286 llvm::Constant *getGcAssignStrongCastFn() {
287 // id objc_assign_global(id, id *)
288 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
289 Args.push_back(ObjectPtrTy->getPointerTo());
290 llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false);
291 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
292 }
Anders Carlsson1cf75362009-02-16 22:59:18 +0000293
294 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerf6ec7e42009-04-22 02:38:11 +0000295 llvm::Constant *getExceptionThrowFn() {
296 // void objc_exception_throw(id)
297 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
298 llvm::FunctionType *FTy =
299 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
300 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
301 }
Anders Carlsson1cf75362009-02-16 22:59:18 +0000302
Daniel Dunbar34416d62009-02-24 01:43:46 +0000303 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattner23e24652009-04-06 16:53:45 +0000304 llvm::Constant *getSyncEnterFn() {
305 // void objc_sync_enter (id)
306 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
307 llvm::FunctionType *FTy =
308 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
309 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
310 }
Daniel Dunbar34416d62009-02-24 01:43:46 +0000311
312 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerf6ec7e42009-04-22 02:38:11 +0000313 llvm::Constant *getSyncExitFn() {
314 // void objc_sync_exit (id)
315 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
316 llvm::FunctionType *FTy =
317 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
318 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
319 }
Daniel Dunbar34416d62009-02-24 01:43:46 +0000320
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000321 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
322 ~ObjCCommonTypesHelper(){}
323};
Daniel Dunbar15245e52008-08-23 04:28:29 +0000324
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000325/// ObjCTypesHelper - Helper class that encapsulates lazy
326/// construction of varies types used during ObjC generation.
327class ObjCTypesHelper : public ObjCCommonTypesHelper {
328private:
329
Chris Lattner61114192009-04-22 02:32:31 +0000330 llvm::Constant *getMessageSendFn() {
331 // id objc_msgSend (id, SEL, ...)
332 std::vector<const llvm::Type*> Params;
333 Params.push_back(ObjectPtrTy);
334 Params.push_back(SelectorPtrTy);
335 return
336 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
337 Params, true),
338 "objc_msgSend");
339 }
340
341 llvm::Constant *getMessageSendStretFn() {
342 // id objc_msgSend_stret (id, SEL, ...)
343 std::vector<const llvm::Type*> Params;
344 Params.push_back(ObjectPtrTy);
345 Params.push_back(SelectorPtrTy);
346 return
347 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
348 Params, true),
349 "objc_msgSend_stret");
350
351 }
352
353 llvm::Constant *getMessageSendFpretFn() {
354 // FIXME: This should be long double on x86_64?
355 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
356 std::vector<const llvm::Type*> Params;
357 Params.push_back(ObjectPtrTy);
358 Params.push_back(SelectorPtrTy);
359 return
360 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
361 Params,
362 true),
363 "objc_msgSend_fpret");
364
365 }
366
367 llvm::Constant *getMessageSendSuperFn() {
368 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
369 std::vector<const llvm::Type*> Params;
370 Params.push_back(SuperPtrTy);
371 Params.push_back(SelectorPtrTy);
372 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
373 Params, true),
374 "objc_msgSendSuper");
375 }
376 llvm::Constant *getMessageSendSuperStretFn() {
377 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
378 // SEL op, ...)
379 std::vector<const llvm::Type*> Params;
380 Params.push_back(Int8PtrTy);
381 Params.push_back(SuperPtrTy);
382 Params.push_back(SelectorPtrTy);
383 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
384 Params, true),
385 "objc_msgSendSuper_stret");
386 }
387
388 llvm::Constant *getMessageSendSuperFpretFn() {
389 // There is no objc_msgSendSuper_fpret? How can that work?
390 return getMessageSendSuperFn();
391 }
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000392
393public:
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000394 /// SymtabTy - LLVM type for struct objc_symtab.
395 const llvm::StructType *SymtabTy;
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000396 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
397 const llvm::Type *SymtabPtrTy;
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000398 /// ModuleTy - LLVM type for struct objc_module.
399 const llvm::StructType *ModuleTy;
Daniel Dunbar5eec6142008-08-12 03:39:23 +0000400
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000401 /// ProtocolTy - LLVM type for struct objc_protocol.
402 const llvm::StructType *ProtocolTy;
403 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
404 const llvm::Type *ProtocolPtrTy;
405 /// ProtocolExtensionTy - LLVM type for struct
406 /// objc_protocol_extension.
407 const llvm::StructType *ProtocolExtensionTy;
408 /// ProtocolExtensionTy - LLVM type for struct
409 /// objc_protocol_extension *.
410 const llvm::Type *ProtocolExtensionPtrTy;
411 /// MethodDescriptionTy - LLVM type for struct
412 /// objc_method_description.
413 const llvm::StructType *MethodDescriptionTy;
414 /// MethodDescriptionListTy - LLVM type for struct
415 /// objc_method_description_list.
416 const llvm::StructType *MethodDescriptionListTy;
417 /// MethodDescriptionListPtrTy - LLVM type for struct
418 /// objc_method_description_list *.
419 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000420 /// ProtocolListTy - LLVM type for struct objc_property_list.
421 const llvm::Type *ProtocolListTy;
422 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
423 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar4246a8b2008-08-22 20:34:54 +0000424 /// CategoryTy - LLVM type for struct objc_category.
425 const llvm::StructType *CategoryTy;
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000426 /// ClassTy - LLVM type for struct objc_class.
427 const llvm::StructType *ClassTy;
428 /// ClassPtrTy - LLVM type for struct objc_class *.
429 const llvm::Type *ClassPtrTy;
430 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
431 const llvm::StructType *ClassExtensionTy;
432 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
433 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000434 // IvarTy - LLVM type for struct objc_ivar.
435 const llvm::StructType *IvarTy;
436 /// IvarListTy - LLVM type for struct objc_ivar_list.
437 const llvm::Type *IvarListTy;
438 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
439 const llvm::Type *IvarListPtrTy;
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000440 /// MethodListTy - LLVM type for struct objc_method_list.
441 const llvm::Type *MethodListTy;
442 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
443 const llvm::Type *MethodListPtrTy;
Anders Carlsson9acb0a42008-09-09 10:10:21 +0000444
445 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
446 const llvm::Type *ExceptionDataTy;
447
Anders Carlsson9acb0a42008-09-09 10:10:21 +0000448 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattnere05d4cb2009-04-22 02:26:14 +0000449 llvm::Constant *getExceptionTryEnterFn() {
450 std::vector<const llvm::Type*> Params;
451 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
452 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
453 Params, false),
454 "objc_exception_try_enter");
455 }
Anders Carlsson9acb0a42008-09-09 10:10:21 +0000456
457 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattnere05d4cb2009-04-22 02:26:14 +0000458 llvm::Constant *getExceptionTryExitFn() {
459 std::vector<const llvm::Type*> Params;
460 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
461 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
462 Params, false),
463 "objc_exception_try_exit");
464 }
Anders Carlsson9acb0a42008-09-09 10:10:21 +0000465
466 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattnere05d4cb2009-04-22 02:26:14 +0000467 llvm::Constant *getExceptionExtractFn() {
468 std::vector<const llvm::Type*> Params;
469 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
470 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
471 Params, false),
472 "objc_exception_extract");
473
474 }
Anders Carlsson9acb0a42008-09-09 10:10:21 +0000475
476 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattnere05d4cb2009-04-22 02:26:14 +0000477 llvm::Constant *getExceptionMatchFn() {
478 std::vector<const llvm::Type*> Params;
479 Params.push_back(ClassPtrTy);
480 Params.push_back(ObjectPtrTy);
481 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
482 Params, false),
483 "objc_exception_match");
484
485 }
Anders Carlsson9acb0a42008-09-09 10:10:21 +0000486
487 /// SetJmpFn - LLVM _setjmp function.
Chris Lattnere05d4cb2009-04-22 02:26:14 +0000488 llvm::Constant *getSetJmpFn() {
489 std::vector<const llvm::Type*> Params;
490 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
491 return
492 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
493 Params, false),
494 "_setjmp");
495
496 }
Chris Lattnerdd978702008-11-15 21:26:17 +0000497
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000498public:
499 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000500 ~ObjCTypesHelper() {}
Daniel Dunbaraecef4c2008-10-17 03:24:53 +0000501
502
Chris Lattneraea1aee2009-03-22 21:03:39 +0000503 llvm::Constant *getSendFn(bool IsSuper) {
Chris Lattner61114192009-04-22 02:32:31 +0000504 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
Daniel Dunbaraecef4c2008-10-17 03:24:53 +0000505 }
506
Chris Lattneraea1aee2009-03-22 21:03:39 +0000507 llvm::Constant *getSendStretFn(bool IsSuper) {
Chris Lattner61114192009-04-22 02:32:31 +0000508 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
Daniel Dunbaraecef4c2008-10-17 03:24:53 +0000509 }
510
Chris Lattneraea1aee2009-03-22 21:03:39 +0000511 llvm::Constant *getSendFpretFn(bool IsSuper) {
Chris Lattner61114192009-04-22 02:32:31 +0000512 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
Daniel Dunbaraecef4c2008-10-17 03:24:53 +0000513 }
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000514};
515
Fariborz Jahaniand0374812009-01-22 23:02:58 +0000516/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000517/// modern abi
Fariborz Jahaniand0374812009-01-22 23:02:58 +0000518class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianf52110f2009-02-04 20:42:28 +0000519public:
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +0000520
Fariborz Jahanian781f2732009-01-23 01:46:23 +0000521 // MethodListnfABITy - LLVM for struct _method_list_t
522 const llvm::StructType *MethodListnfABITy;
523
524 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
525 const llvm::Type *MethodListnfABIPtrTy;
526
527 // ProtocolnfABITy = LLVM for struct _protocol_t
528 const llvm::StructType *ProtocolnfABITy;
529
Daniel Dunbar1f42bb02009-02-15 07:36:20 +0000530 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
531 const llvm::Type *ProtocolnfABIPtrTy;
532
Fariborz Jahanian781f2732009-01-23 01:46:23 +0000533 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
534 const llvm::StructType *ProtocolListnfABITy;
535
536 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
537 const llvm::Type *ProtocolListnfABIPtrTy;
538
539 // ClassnfABITy - LLVM for struct _class_t
540 const llvm::StructType *ClassnfABITy;
541
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000542 // ClassnfABIPtrTy - LLVM for struct _class_t*
543 const llvm::Type *ClassnfABIPtrTy;
544
Fariborz Jahanian781f2732009-01-23 01:46:23 +0000545 // IvarnfABITy - LLVM for struct _ivar_t
546 const llvm::StructType *IvarnfABITy;
547
548 // IvarListnfABITy - LLVM for struct _ivar_list_t
549 const llvm::StructType *IvarListnfABITy;
550
551 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
552 const llvm::Type *IvarListnfABIPtrTy;
553
554 // ClassRonfABITy - LLVM for struct _class_ro_t
555 const llvm::StructType *ClassRonfABITy;
556
557 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
558 const llvm::Type *ImpnfABITy;
559
560 // CategorynfABITy - LLVM for struct _category_t
561 const llvm::StructType *CategorynfABITy;
562
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +0000563 // New types for nonfragile abi messaging.
564
565 // MessageRefTy - LLVM for:
566 // struct _message_ref_t {
567 // IMP messenger;
568 // SEL name;
569 // };
570 const llvm::StructType *MessageRefTy;
Fariborz Jahanianf52110f2009-02-04 20:42:28 +0000571 // MessageRefCTy - clang type for struct _message_ref_t
572 QualType MessageRefCTy;
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +0000573
574 // MessageRefPtrTy - LLVM for struct _message_ref_t*
575 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanianf52110f2009-02-04 20:42:28 +0000576 // MessageRefCPtrTy - clang type for struct _message_ref_t*
577 QualType MessageRefCPtrTy;
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +0000578
Fariborz Jahanian10d69ea2009-02-05 01:13:09 +0000579 // MessengerTy - Type of the messenger (shown as IMP above)
580 const llvm::FunctionType *MessengerTy;
581
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +0000582 // SuperMessageRefTy - LLVM for:
583 // struct _super_message_ref_t {
584 // SUPER_IMP messenger;
585 // SEL name;
586 // };
587 const llvm::StructType *SuperMessageRefTy;
588
589 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
590 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar75de89f2009-02-24 07:47:38 +0000591
Chris Lattnerada416b2009-04-22 02:53:24 +0000592 llvm::Constant *getMessageSendFixupFn() {
593 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
594 std::vector<const llvm::Type*> Params;
595 Params.push_back(ObjectPtrTy);
596 Params.push_back(MessageRefPtrTy);
597 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
598 Params, true),
599 "objc_msgSend_fixup");
600 }
601
602 llvm::Constant *getMessageSendFpretFixupFn() {
603 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
604 std::vector<const llvm::Type*> Params;
605 Params.push_back(ObjectPtrTy);
606 Params.push_back(MessageRefPtrTy);
607 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
608 Params, true),
609 "objc_msgSend_fpret_fixup");
610 }
611
612 llvm::Constant *getMessageSendStretFixupFn() {
613 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
614 std::vector<const llvm::Type*> Params;
615 Params.push_back(ObjectPtrTy);
616 Params.push_back(MessageRefPtrTy);
617 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
618 Params, true),
619 "objc_msgSend_stret_fixup");
620 }
621
622 llvm::Constant *getMessageSendIdFixupFn() {
623 // id objc_msgSendId_fixup(id, struct message_ref_t*, ...)
624 std::vector<const llvm::Type*> Params;
625 Params.push_back(ObjectPtrTy);
626 Params.push_back(MessageRefPtrTy);
627 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
628 Params, true),
629 "objc_msgSendId_fixup");
630 }
631
632 llvm::Constant *getMessageSendIdStretFixupFn() {
633 // id objc_msgSendId_stret_fixup(id, struct message_ref_t*, ...)
634 std::vector<const llvm::Type*> Params;
635 Params.push_back(ObjectPtrTy);
636 Params.push_back(MessageRefPtrTy);
637 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
638 Params, true),
639 "objc_msgSendId_stret_fixup");
640 }
641 llvm::Constant *getMessageSendSuper2FixupFn() {
642 // id objc_msgSendSuper2_fixup (struct objc_super *,
643 // struct _super_message_ref_t*, ...)
644 std::vector<const llvm::Type*> Params;
645 Params.push_back(SuperPtrTy);
646 Params.push_back(SuperMessageRefPtrTy);
647 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
648 Params, true),
649 "objc_msgSendSuper2_fixup");
650 }
651
652 llvm::Constant *getMessageSendSuper2StretFixupFn() {
653 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
654 // struct _super_message_ref_t*, ...)
655 std::vector<const llvm::Type*> Params;
656 Params.push_back(SuperPtrTy);
657 Params.push_back(SuperMessageRefPtrTy);
658 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
659 Params, true),
660 "objc_msgSendSuper2_stret_fixup");
661 }
662
663
664
Daniel Dunbar75de89f2009-02-24 07:47:38 +0000665 /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C
666 /// exception personality function.
Chris Lattner23e24652009-04-06 16:53:45 +0000667 llvm::Value *getEHPersonalityPtr() {
668 llvm::Constant *Personality =
669 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
670 std::vector<const llvm::Type*>(),
671 true),
672 "__objc_personality_v0");
673 return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy);
674 }
Daniel Dunbar75de89f2009-02-24 07:47:38 +0000675
Chris Lattner93dca5b2009-04-22 02:15:23 +0000676 llvm::Constant *getUnwindResumeOrRethrowFn() {
677 std::vector<const llvm::Type*> Params;
678 Params.push_back(Int8PtrTy);
679 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
680 Params, false),
681 "_Unwind_Resume_or_Rethrow");
682 }
683
684 llvm::Constant *getObjCEndCatchFn() {
685 std::vector<const llvm::Type*> Params;
686 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
687 Params, false),
688 "objc_end_catch");
689
690 }
691
692 llvm::Constant *getObjCBeginCatchFn() {
693 std::vector<const llvm::Type*> Params;
694 Params.push_back(Int8PtrTy);
695 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
696 Params, false),
697 "objc_begin_catch");
698 }
Daniel Dunbar9c285e72009-03-01 04:46:24 +0000699
700 const llvm::StructType *EHTypeTy;
701 const llvm::Type *EHTypePtrTy;
Daniel Dunbar75de89f2009-02-24 07:47:38 +0000702
Fariborz Jahaniand0374812009-01-22 23:02:58 +0000703 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
704 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000705};
706
707class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +0000708public:
709 // FIXME - accessibility
Fariborz Jahanian37931062009-03-10 16:22:08 +0000710 class GC_IVAR {
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +0000711 public:
Fariborz Jahanian37931062009-03-10 16:22:08 +0000712 unsigned int ivar_bytepos;
713 unsigned int ivar_size;
714 GC_IVAR() : ivar_bytepos(0), ivar_size(0) {}
Daniel Dunbar48445182009-04-23 01:29:05 +0000715
716 // Allow sorting based on byte pos.
717 bool operator<(const GC_IVAR &b) const {
718 return ivar_bytepos < b.ivar_bytepos;
719 }
Fariborz Jahanian37931062009-03-10 16:22:08 +0000720 };
721
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +0000722 class SKIP_SCAN {
723 public:
724 unsigned int skip;
725 unsigned int scan;
726 SKIP_SCAN() : skip(0), scan(0) {}
727 };
728
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000729protected:
730 CodeGen::CodeGenModule &CGM;
731 // FIXME! May not be needing this after all.
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000732 unsigned ObjCABI;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000733
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +0000734 // gc ivar layout bitmap calculation helper caches.
735 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
736 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Fariborz Jahanian37931062009-03-10 16:22:08 +0000737
Daniel Dunbar8ede0052008-08-25 06:02:07 +0000738 /// LazySymbols - Symbols to generate a lazy reference for. See
739 /// DefinedSymbols and FinishModule().
740 std::set<IdentifierInfo*> LazySymbols;
741
742 /// DefinedSymbols - External symbols which are defined by this
743 /// module. The symbols in this list and LazySymbols are used to add
744 /// special linker symbols which ensure that Objective-C modules are
745 /// linked properly.
746 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000747
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000748 /// ClassNames - uniqued class names.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000749 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000750
Daniel Dunbar5eec6142008-08-12 03:39:23 +0000751 /// MethodVarNames - uniqued method variable names.
752 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000753
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000754 /// MethodVarTypes - uniqued method type signatures. We have to use
755 /// a StringMap here because have no other unique reference.
756 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000757
Daniel Dunbar12996f52008-08-26 21:51:14 +0000758 /// MethodDefinitions - map of methods which have been defined in
759 /// this translation unit.
760 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000761
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000762 /// PropertyNames - uniqued method variable names.
763 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000764
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000765 /// ClassReferences - uniqued class references.
766 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000767
Daniel Dunbar5eec6142008-08-12 03:39:23 +0000768 /// SelectorReferences - uniqued selector references.
769 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000770
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000771 /// Protocols - Protocols for which an objc_protocol structure has
772 /// been emitted. Forward declarations are handled by creating an
773 /// empty structure whose initializer is filled in when/if defined.
774 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000775
Daniel Dunbar35b777f2008-10-29 22:36:39 +0000776 /// DefinedProtocols - Protocols which have actually been
777 /// defined. We should not need this, see FIXME in GenerateProtocol.
778 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000779
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000780 /// DefinedClasses - List of defined classes.
781 std::vector<llvm::GlobalValue*> DefinedClasses;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000782
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000783 /// DefinedCategories - List of defined categories.
784 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000785
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000786 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000787 /// to prevent them from being clobbered.
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000788 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000789
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +0000790 /// GetNameForMethod - Return a name for the given method.
791 /// \param[out] NameOut - The return value.
792 void GetNameForMethod(const ObjCMethodDecl *OMD,
793 const ObjCContainerDecl *CD,
794 std::string &NameOut);
795
796 /// GetMethodVarName - Return a unique constant for the given
797 /// selector's name. The return value has type char *.
798 llvm::Constant *GetMethodVarName(Selector Sel);
799 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
800 llvm::Constant *GetMethodVarName(const std::string &Name);
801
802 /// GetMethodVarType - Return a unique constant for the given
803 /// selector's name. The return value has type char *.
804
805 // FIXME: This is a horrible name.
806 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar356f0742009-04-20 06:54:31 +0000807 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +0000808
809 /// GetPropertyName - Return a unique constant for the given
810 /// name. The return value has type char *.
811 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
812
813 // FIXME: This can be dropped once string functions are unified.
814 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
815 const Decl *Container);
816
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +0000817 /// GetClassName - Return a unique constant for the given selector's
818 /// name. The return value has type char *.
819 llvm::Constant *GetClassName(IdentifierInfo *Ident);
820
Fariborz Jahanian01b3e342009-03-05 22:39:55 +0000821 /// BuildIvarLayout - Builds ivar layout bitmap for the class
822 /// implementation for the __strong or __weak case.
823 ///
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +0000824 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
825 bool ForStrongLayout);
Fariborz Jahanian01b3e342009-03-05 22:39:55 +0000826
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +0000827 void BuildAggrIvarLayout(const ObjCInterfaceDecl *OI,
828 const llvm::StructLayout *Layout,
Fariborz Jahanian37931062009-03-10 16:22:08 +0000829 const RecordDecl *RD,
Chris Lattner9329cf52009-03-31 08:48:01 +0000830 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahanian01b3e342009-03-05 22:39:55 +0000831 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian06facb72009-04-24 16:17:09 +0000832 bool &HasUnion);
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +0000833
Fariborz Jahanian7345eba2009-03-05 19:17:31 +0000834 /// GetIvarLayoutName - Returns a unique constant for the given
835 /// ivar layout bitmap.
836 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
837 const ObjCCommonTypesHelper &ObjCTypes);
838
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +0000839 /// EmitPropertyList - Emit the given property list. The return
840 /// value has type PropertyListPtrTy.
841 llvm::Constant *EmitPropertyList(const std::string &Name,
842 const Decl *Container,
843 const ObjCContainerDecl *OCD,
844 const ObjCCommonTypesHelper &ObjCTypes);
845
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +0000846 /// GetProtocolRef - Return a reference to the internal protocol
847 /// description, creating an empty one if it has not been
848 /// defined. The return value has type ProtocolPtrTy.
849 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahaniand65949b2009-03-08 20:18:37 +0000850
Chris Lattnerd391dab2009-03-31 08:33:16 +0000851 /// GetFieldBaseOffset - return's field byte offset.
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +0000852 uint64_t GetFieldBaseOffset(const ObjCInterfaceDecl *OI,
853 const llvm::StructLayout *Layout,
Chris Lattnerd391dab2009-03-31 08:33:16 +0000854 const FieldDecl *Field);
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +0000855
Daniel Dunbarc4594f22009-03-09 20:09:19 +0000856 /// CreateMetadataVar - Create a global variable with internal
857 /// linkage for use by the Objective-C runtime.
858 ///
859 /// This is a convenience wrapper which not only creates the
860 /// variable, but also sets the section and alignment and adds the
861 /// global to the UsedGlobals list.
Daniel Dunbareddddd22009-03-09 20:50:13 +0000862 ///
863 /// \param Name - The variable name.
864 /// \param Init - The variable initializer; this is also used to
865 /// define the type of the variable.
866 /// \param Section - The section the variable should go into, or 0.
867 /// \param Align - The alignment for the variable, or 0.
868 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbar6b343692009-04-14 17:42:51 +0000869 /// "llvm.used".
Daniel Dunbarc4594f22009-03-09 20:09:19 +0000870 llvm::GlobalVariable *CreateMetadataVar(const std::string &Name,
871 llvm::Constant *Init,
872 const char *Section,
Daniel Dunbareddddd22009-03-09 20:50:13 +0000873 unsigned Align,
874 bool AddToUsed);
Daniel Dunbarc4594f22009-03-09 20:09:19 +0000875
Daniel Dunbar356f0742009-04-20 06:54:31 +0000876 /// GetNamedIvarList - Return the list of ivars in the interface
877 /// itself (not including super classes and not including unnamed
878 /// bitfields).
879 ///
880 /// For the non-fragile ABI, this also includes synthesized property
881 /// ivars.
882 void GetNamedIvarList(const ObjCInterfaceDecl *OID,
883 llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const;
884
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000885public:
886 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
887 { }
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000888
Steve Naroff2c8a08e2009-03-31 16:53:37 +0000889 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL);
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +0000890
891 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
892 const ObjCContainerDecl *CD=0);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +0000893
894 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
895
896 /// GetOrEmitProtocol - Get the protocol object for the given
897 /// declaration, emitting it if necessary. The return value has type
898 /// ProtocolPtrTy.
899 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
900
901 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
902 /// object for the given declaration, emitting it if needed. These
903 /// forward references will be filled in with empty bodies if no
904 /// definition is seen. The return value has type ProtocolPtrTy.
905 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000906};
907
908class CGObjCMac : public CGObjCCommonMac {
909private:
910 ObjCTypesHelper ObjCTypes;
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000911 /// EmitImageInfo - Emit the image info marker used to encode some module
912 /// level information.
913 void EmitImageInfo();
914
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000915 /// EmitModuleInfo - Another marker encoding module level
916 /// information.
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000917 void EmitModuleInfo();
918
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000919 /// EmitModuleSymols - Emit module symbols, the list of defined
920 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000921 llvm::Constant *EmitModuleSymbols();
922
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000923 /// FinishModule - Write out global data structures at the end of
924 /// processing a translation unit.
925 void FinishModule();
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000926
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000927 /// EmitClassExtension - Generate the class extension structure used
928 /// to store the weak ivar layout and properties. The return value
929 /// has type ClassExtensionPtrTy.
930 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
931
932 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
933 /// for the given class.
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000934 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000935 const ObjCInterfaceDecl *ID);
936
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000937 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000938 QualType ResultType,
939 Selector Sel,
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000940 llvm::Value *Arg0,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000941 QualType Arg0Ty,
942 bool IsSuper,
943 const CallArgList &CallArgs);
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000944
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000945 /// EmitIvarList - Emit the ivar list for the given
946 /// implementation. If ForClass is true the list of class ivars
947 /// (i.e. metaclass ivars) is emitted, otherwise the list of
948 /// interface ivars will be emitted. The return value has type
949 /// IvarListPtrTy.
950 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +0000951 bool ForClass);
952
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000953 /// EmitMetaClass - Emit a forward reference to the class structure
954 /// for the metaclass of the given interface. The return value has
955 /// type ClassPtrTy.
956 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
957
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000958 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000959 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000960 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
961 llvm::Constant *Protocols,
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000962 const ConstantVector &Methods);
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +0000963
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000964 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +0000965
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000966 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000967
968 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000969 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar4246a8b2008-08-22 20:34:54 +0000970 llvm::Constant *EmitMethodList(const std::string &Name,
971 const char *Section,
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000972 const ConstantVector &Methods);
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000973
974 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000975 /// method declarations.
976 /// - TypeName: The name for the type containing the methods.
977 /// - IsProtocol: True iff these methods are for a protocol.
978 /// - ClassMethds: True iff these are class methods.
979 /// - Required: When true, only "required" methods are
980 /// listed. Similarly, when false only "optional" methods are
981 /// listed. For classes this should always be true.
982 /// - begin, end: The method list to output.
983 ///
984 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000985 llvm::Constant *EmitMethodDescList(const std::string &Name,
986 const char *Section,
987 const ConstantVector &Methods);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000988
Daniel Dunbar35b777f2008-10-29 22:36:39 +0000989 /// GetOrEmitProtocol - Get the protocol object for the given
990 /// declaration, emitting it if necessary. The return value has type
991 /// ProtocolPtrTy.
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +0000992 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar35b777f2008-10-29 22:36:39 +0000993
994 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
995 /// object for the given declaration, emitting it if needed. These
996 /// forward references will be filled in with empty bodies if no
997 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +0000998 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar35b777f2008-10-29 22:36:39 +0000999
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001000 /// EmitProtocolExtension - Generate the protocol extension
1001 /// structure used to store optional instance and class methods, and
1002 /// protocol properties. The return value has type
1003 /// ProtocolExtensionPtrTy.
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001004 llvm::Constant *
1005 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1006 const ConstantVector &OptInstanceMethods,
1007 const ConstantVector &OptClassMethods);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001008
1009 /// EmitProtocolList - Generate the list of referenced
1010 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar67e778b2008-08-21 21:57:41 +00001011 llvm::Constant *EmitProtocolList(const std::string &Name,
1012 ObjCProtocolDecl::protocol_iterator begin,
1013 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001014
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001015 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1016 /// for the given selector.
Daniel Dunbard916e6e2008-11-01 01:53:16 +00001017 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00001018
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00001019 public:
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001020 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001021
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001022 virtual llvm::Function *ModuleInitFunction();
1023
Daniel Dunbara04840b2008-08-23 03:46:30 +00001024 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +00001025 QualType ResultType,
1026 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001027 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +00001028 bool IsClassMessage,
1029 const CallArgList &CallArgs);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001030
Daniel Dunbara04840b2008-08-23 03:46:30 +00001031 virtual CodeGen::RValue
1032 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +00001033 QualType ResultType,
1034 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001035 const ObjCInterfaceDecl *Class,
Fariborz Jahanian17636fa2009-02-28 20:07:56 +00001036 bool isCategoryImpl,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001037 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +00001038 bool IsClassMessage,
1039 const CallArgList &CallArgs);
Daniel Dunbar434627a2008-08-16 00:25:02 +00001040
Daniel Dunbard916e6e2008-11-01 01:53:16 +00001041 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001042 const ObjCInterfaceDecl *ID);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001043
Daniel Dunbard916e6e2008-11-01 01:53:16 +00001044 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001045
Daniel Dunbarac93e472008-08-15 22:20:32 +00001046 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001047
Daniel Dunbarac93e472008-08-15 22:20:32 +00001048 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001049
Daniel Dunbard916e6e2008-11-01 01:53:16 +00001050 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar84bb85f2008-08-13 00:59:25 +00001051 const ObjCProtocolDecl *PD);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00001052
Chris Lattneraea1aee2009-03-22 21:03:39 +00001053 virtual llvm::Constant *GetPropertyGetFunction();
1054 virtual llvm::Constant *GetPropertySetFunction();
1055 virtual llvm::Constant *EnumerationMutationFunction();
Anders Carlssonb01a2112008-09-09 10:04:29 +00001056
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00001057 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1058 const Stmt &S);
Anders Carlssonb01a2112008-09-09 10:04:29 +00001059 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1060 const ObjCAtThrowStmt &S);
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00001061 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian3305ad32008-11-18 21:45:40 +00001062 llvm::Value *AddrWeakObj);
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00001063 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1064 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian17958902008-11-19 00:59:10 +00001065 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1066 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianf310b592008-11-20 19:23:36 +00001067 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1068 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian17958902008-11-19 00:59:10 +00001069 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1070 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian4337afe2009-02-02 20:02:29 +00001071
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00001072 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1073 QualType ObjectTy,
1074 llvm::Value *BaseValue,
1075 const ObjCIvarDecl *Ivar,
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00001076 unsigned CVRQualifiers);
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00001077 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar61e14a62009-04-22 05:08:15 +00001078 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00001079 const ObjCIvarDecl *Ivar);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001080};
Fariborz Jahanian48543f52009-01-21 22:04:16 +00001081
Fariborz Jahaniand0374812009-01-22 23:02:58 +00001082class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanian48543f52009-01-21 22:04:16 +00001083private:
Fariborz Jahaniand0374812009-01-22 23:02:58 +00001084 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001085 llvm::GlobalVariable* ObjCEmptyCacheVar;
1086 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbarc0318b22009-03-02 06:08:11 +00001087
Daniel Dunbar3c190812009-04-18 08:51:00 +00001088 /// SuperClassReferences - uniqued super class references.
1089 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
1090
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00001091 /// MetaClassReferences - uniqued meta class references.
1092 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbar9c285e72009-03-01 04:46:24 +00001093
1094 /// EHTypeReferences - uniqued class ehtype references.
1095 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbarc0318b22009-03-02 06:08:11 +00001096
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001097 /// FinishNonFragileABIModule - Write out global data structures at the end of
1098 /// processing a translation unit.
1099 void FinishNonFragileABIModule();
Daniel Dunbarc2129532009-04-08 04:21:03 +00001100
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00001101 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1102 unsigned InstanceStart,
1103 unsigned InstanceSize,
1104 const ObjCImplementationDecl *ID);
Fariborz Jahanian06726462009-01-24 21:21:53 +00001105 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
1106 llvm::Constant *IsAGV,
1107 llvm::Constant *SuperClassGV,
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +00001108 llvm::Constant *ClassRoGV,
1109 bool HiddenVisibility);
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00001110
1111 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
1112
Fariborz Jahanian151747b2009-01-30 00:46:37 +00001113 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
1114
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00001115 /// EmitMethodList - Emit the method list for the given
1116 /// implementation. The return value has type MethodListnfABITy.
1117 llvm::Constant *EmitMethodList(const std::string &Name,
1118 const char *Section,
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00001119 const ConstantVector &Methods);
1120 /// EmitIvarList - Emit the ivar list for the given
1121 /// implementation. If ForClass is true the list of class ivars
1122 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1123 /// interface ivars will be emitted. The return value has type
1124 /// IvarListnfABIPtrTy.
1125 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00001126
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00001127 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian150f7732009-01-28 01:36:42 +00001128 const ObjCIvarDecl *Ivar,
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00001129 unsigned long int offset);
1130
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00001131 /// GetOrEmitProtocol - Get the protocol object for the given
1132 /// declaration, emitting it if necessary. The return value has type
1133 /// ProtocolPtrTy.
1134 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
1135
1136 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1137 /// object for the given declaration, emitting it if needed. These
1138 /// forward references will be filled in with empty bodies if no
1139 /// definition is seen. The return value has type ProtocolPtrTy.
1140 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
1141
1142 /// EmitProtocolList - Generate the list of referenced
1143 /// protocols. The return value has type ProtocolListPtrTy.
1144 llvm::Constant *EmitProtocolList(const std::string &Name,
1145 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian7e881162009-02-04 00:22:57 +00001146 ObjCProtocolDecl::protocol_iterator end);
1147
1148 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1149 QualType ResultType,
1150 Selector Sel,
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00001151 llvm::Value *Receiver,
Fariborz Jahanian7e881162009-02-04 00:22:57 +00001152 QualType Arg0Ty,
1153 bool IsSuper,
1154 const CallArgList &CallArgs);
Daniel Dunbarabbda222009-03-01 04:40:10 +00001155
1156 /// GetClassGlobal - Return the global variable for the Objective-C
1157 /// class of the given name.
Fariborz Jahanianab438842009-04-14 18:41:56 +00001158 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
1159
Fariborz Jahanian917c0402009-02-05 20:41:40 +00001160 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar3c190812009-04-18 08:51:00 +00001161 /// for the given class reference.
Fariborz Jahanian917c0402009-02-05 20:41:40 +00001162 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar3c190812009-04-18 08:51:00 +00001163 const ObjCInterfaceDecl *ID);
1164
1165 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1166 /// for the given super class reference.
1167 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1168 const ObjCInterfaceDecl *ID);
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00001169
1170 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1171 /// meta-data
1172 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
1173 const ObjCInterfaceDecl *ID);
1174
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00001175 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1176 /// the given ivar.
1177 ///
Daniel Dunbar07d204a2009-04-19 00:31:15 +00001178 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Fariborz Jahaniana09a5142009-02-12 18:51:23 +00001179 const ObjCInterfaceDecl *ID,
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00001180 const ObjCIvarDecl *Ivar);
Fariborz Jahanian917c0402009-02-05 20:41:40 +00001181
Fariborz Jahanianebb82c62009-02-11 20:51:17 +00001182 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1183 /// for the given selector.
1184 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbar9c285e72009-03-01 04:46:24 +00001185
Daniel Dunbarc2129532009-04-08 04:21:03 +00001186 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbar9c285e72009-03-01 04:46:24 +00001187 /// interface. The return value has type EHTypePtrTy.
Daniel Dunbarc2129532009-04-08 04:21:03 +00001188 llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
1189 bool ForDefinition);
Daniel Dunbara2d275d2009-04-07 05:48:37 +00001190
1191 const char *getMetaclassSymbolPrefix() const {
1192 return "OBJC_METACLASS_$_";
1193 }
Daniel Dunbarc0318b22009-03-02 06:08:11 +00001194
Daniel Dunbara2d275d2009-04-07 05:48:37 +00001195 const char *getClassSymbolPrefix() const {
1196 return "OBJC_CLASS_$_";
1197 }
1198
Daniel Dunbared4d5962009-05-03 12:57:56 +00001199 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarecb5d402009-04-19 23:41:48 +00001200 uint32_t &InstanceStart,
1201 uint32_t &InstanceSize);
1202
Fariborz Jahanian48543f52009-01-21 22:04:16 +00001203public:
Fariborz Jahaniand0374812009-01-22 23:02:58 +00001204 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001205 // FIXME. All stubs for now!
1206 virtual llvm::Function *ModuleInitFunction();
1207
1208 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
1209 QualType ResultType,
1210 Selector Sel,
1211 llvm::Value *Receiver,
1212 bool IsClassMessage,
Fariborz Jahanian7e881162009-02-04 00:22:57 +00001213 const CallArgList &CallArgs);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001214
1215 virtual CodeGen::RValue
1216 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
1217 QualType ResultType,
1218 Selector Sel,
1219 const ObjCInterfaceDecl *Class,
Fariborz Jahanian17636fa2009-02-28 20:07:56 +00001220 bool isCategoryImpl,
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001221 llvm::Value *Receiver,
1222 bool IsClassMessage,
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00001223 const CallArgList &CallArgs);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001224
1225 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian917c0402009-02-05 20:41:40 +00001226 const ObjCInterfaceDecl *ID);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001227
1228 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
Fariborz Jahanianebb82c62009-02-11 20:51:17 +00001229 { return EmitSelector(Builder, Sel); }
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001230
Fariborz Jahanianfe49a092009-01-26 18:32:24 +00001231 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001232
1233 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001234 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian5d13ab12009-01-30 18:58:59 +00001235 const ObjCProtocolDecl *PD);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001236
Chris Lattneraea1aee2009-03-22 21:03:39 +00001237 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattnera7ecda42009-04-22 02:44:54 +00001238 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00001239 }
Chris Lattneraea1aee2009-03-22 21:03:39 +00001240 virtual llvm::Constant *GetPropertySetFunction() {
Chris Lattnera7ecda42009-04-22 02:44:54 +00001241 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00001242 }
Chris Lattneraea1aee2009-03-22 21:03:39 +00001243 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattnera7ecda42009-04-22 02:44:54 +00001244 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar978d2be2009-02-16 18:48:45 +00001245 }
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001246
1247 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar75de89f2009-02-24 07:47:38 +00001248 const Stmt &S);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001249 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlsson1cf75362009-02-16 22:59:18 +00001250 const ObjCAtThrowStmt &S);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001251 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00001252 llvm::Value *AddrWeakObj);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001253 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00001254 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001255 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00001256 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001257 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00001258 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001259 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00001260 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00001261 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1262 QualType ObjectTy,
1263 llvm::Value *BaseValue,
1264 const ObjCIvarDecl *Ivar,
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00001265 unsigned CVRQualifiers);
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00001266 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar61e14a62009-04-22 05:08:15 +00001267 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00001268 const ObjCIvarDecl *Ivar);
Fariborz Jahanian48543f52009-01-21 22:04:16 +00001269};
1270
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001271} // end anonymous namespace
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00001272
1273/* *** Helper Functions *** */
1274
1275/// getConstantGEP() - Help routine to construct simple GEPs.
1276static llvm::Constant *getConstantGEP(llvm::Constant *C,
1277 unsigned idx0,
1278 unsigned idx1) {
1279 llvm::Value *Idxs[] = {
1280 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
1281 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
1282 };
1283 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
1284}
1285
Daniel Dunbarc2129532009-04-08 04:21:03 +00001286/// hasObjCExceptionAttribute - Return true if this class or any super
1287/// class has the __objc_exception__ attribute.
1288static bool hasObjCExceptionAttribute(const ObjCInterfaceDecl *OID) {
Daniel Dunbar78582862009-04-13 21:08:27 +00001289 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbarc2129532009-04-08 04:21:03 +00001290 return true;
1291 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
1292 return hasObjCExceptionAttribute(Super);
1293 return false;
1294}
1295
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00001296/* *** CGObjCMac Public Interface *** */
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001297
Fariborz Jahanian48543f52009-01-21 22:04:16 +00001298CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
1299 ObjCTypes(cgm)
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00001300{
Fariborz Jahanian48543f52009-01-21 22:04:16 +00001301 ObjCABI = 1;
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00001302 EmitImageInfo();
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001303}
1304
Daniel Dunbar434627a2008-08-16 00:25:02 +00001305/// GetClass - Return a reference to the class for the given interface
1306/// decl.
Daniel Dunbard916e6e2008-11-01 01:53:16 +00001307llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001308 const ObjCInterfaceDecl *ID) {
1309 return EmitClassRef(Builder, ID);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001310}
1311
1312/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbard916e6e2008-11-01 01:53:16 +00001313llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar5eec6142008-08-12 03:39:23 +00001314 return EmitSelector(Builder, Sel);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001315}
1316
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00001317/// Generate a constant CFString object.
1318/*
1319 struct __builtin_CFString {
1320 const int *isa; // point to __CFConstantStringClassReference
1321 int flags;
1322 const char *str;
1323 long length;
1324 };
1325*/
1326
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00001327llvm::Constant *CGObjCCommonMac::GenerateConstantString(
Steve Naroff2c8a08e2009-03-31 16:53:37 +00001328 const ObjCStringLiteral *SL) {
Steve Naroff9a744e52009-04-01 13:55:36 +00001329 return CGM.GetAddrOfConstantCFString(SL->getString());
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001330}
1331
1332/// Generates a message send where the super is the receiver. This is
1333/// a message send to self with special delivery semantics indicating
1334/// which class's method should be called.
Daniel Dunbara04840b2008-08-23 03:46:30 +00001335CodeGen::RValue
1336CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +00001337 QualType ResultType,
1338 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001339 const ObjCInterfaceDecl *Class,
Fariborz Jahanian17636fa2009-02-28 20:07:56 +00001340 bool isCategoryImpl,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001341 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +00001342 bool IsClassMessage,
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +00001343 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbar15245e52008-08-23 04:28:29 +00001344 // Create and init a super structure; this is a (receiver, class)
1345 // pair we will pass to objc_msgSendSuper.
1346 llvm::Value *ObjCSuper =
1347 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
1348 llvm::Value *ReceiverAsObject =
1349 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
1350 CGF.Builder.CreateStore(ReceiverAsObject,
1351 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar15245e52008-08-23 04:28:29 +00001352
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001353 // If this is a class message the metaclass is passed as the target.
1354 llvm::Value *Target;
1355 if (IsClassMessage) {
Fariborz Jahanian17636fa2009-02-28 20:07:56 +00001356 if (isCategoryImpl) {
1357 // Message sent to 'super' in a class method defined in a category
1358 // implementation requires an odd treatment.
1359 // If we are in a class method, we must retrieve the
1360 // _metaclass_ for the current class, pointed at by
1361 // the class's "isa" pointer. The following assumes that
1362 // isa" is the first ivar in a class (which it must be).
1363 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1364 Target = CGF.Builder.CreateStructGEP(Target, 0);
1365 Target = CGF.Builder.CreateLoad(Target);
1366 }
1367 else {
1368 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1369 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1370 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1371 Target = Super;
1372 }
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001373 } else {
1374 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1375 }
Daniel Dunbar0ed60b02008-08-30 03:02:31 +00001376 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
1377 // and ObjCTypes types.
1378 const llvm::Type *ClassTy =
1379 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001380 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001381 CGF.Builder.CreateStore(Target,
1382 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
1383
Daniel Dunbardd851282008-08-30 05:35:15 +00001384 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +00001385 ObjCSuper, ObjCTypes.SuperPtrCTy,
1386 true, CallArgs);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001387}
Daniel Dunbar87062ff2008-08-23 09:25:55 +00001388
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001389/// Generate code for a message send expression.
Daniel Dunbara04840b2008-08-23 03:46:30 +00001390CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +00001391 QualType ResultType,
1392 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001393 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +00001394 bool IsClassMessage,
1395 const CallArgList &CallArgs) {
Daniel Dunbardd851282008-08-30 05:35:15 +00001396 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahanian8978f592009-04-24 21:07:43 +00001397 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbar0ed60b02008-08-30 03:02:31 +00001398 false, CallArgs);
Daniel Dunbar87062ff2008-08-23 09:25:55 +00001399}
1400
1401CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +00001402 QualType ResultType,
1403 Selector Sel,
Daniel Dunbar87062ff2008-08-23 09:25:55 +00001404 llvm::Value *Arg0,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +00001405 QualType Arg0Ty,
1406 bool IsSuper,
1407 const CallArgList &CallArgs) {
1408 CallArgList ActualArgs;
Fariborz Jahanian8978f592009-04-24 21:07:43 +00001409 if (!IsSuper)
1410 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +00001411 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
1412 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
1413 Sel)),
Daniel Dunbar0ed60b02008-08-30 03:02:31 +00001414 CGF.getContext().getObjCSelType()));
1415 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbarac93e472008-08-15 22:20:32 +00001416
Daniel Dunbar34bda882009-02-02 23:23:47 +00001417 CodeGenTypes &Types = CGM.getTypes();
1418 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
Fariborz Jahanian817d1f52009-04-29 22:47:27 +00001419 // FIXME. vararg flag must be true when this API is used for 64bit code gen.
1420 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false);
Daniel Dunbaraecef4c2008-10-17 03:24:53 +00001421
1422 llvm::Constant *Fn;
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001423 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Daniel Dunbaraecef4c2008-10-17 03:24:53 +00001424 Fn = ObjCTypes.getSendStretFn(IsSuper);
1425 } else if (ResultType->isFloatingType()) {
1426 // FIXME: Sadly, this is wrong. This actually depends on the
1427 // architecture. This happens to be right for x86-32 though.
1428 Fn = ObjCTypes.getSendFpretFn(IsSuper);
1429 } else {
1430 Fn = ObjCTypes.getSendFn(IsSuper);
1431 }
Daniel Dunbara9976a22008-09-10 07:00:50 +00001432 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001433 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001434}
1435
Daniel Dunbard916e6e2008-11-01 01:53:16 +00001436llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar84bb85f2008-08-13 00:59:25 +00001437 const ObjCProtocolDecl *PD) {
Daniel Dunbarb3518152008-09-04 04:33:15 +00001438 // FIXME: I don't understand why gcc generates this, or where it is
1439 // resolved. Investigate. Its also wasteful to look this up over and
1440 // over.
1441 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1442
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001443 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
1444 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001445}
1446
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00001447void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001448 // FIXME: We shouldn't need this, the protocol decl should contain
1449 // enough information to tell us whether this was a declaration or a
1450 // definition.
1451 DefinedProtocols.insert(PD->getIdentifier());
1452
1453 // If we have generated a forward reference to this protocol, emit
1454 // it now. Otherwise do nothing, the protocol objects are lazily
1455 // emitted.
1456 if (Protocols.count(PD->getIdentifier()))
1457 GetOrEmitProtocol(PD);
1458}
1459
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00001460llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001461 if (DefinedProtocols.count(PD->getIdentifier()))
1462 return GetOrEmitProtocol(PD);
1463 return GetOrEmitProtocolRef(PD);
1464}
1465
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001466/*
1467 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1468 struct _objc_protocol {
1469 struct _objc_protocol_extension *isa;
1470 char *protocol_name;
1471 struct _objc_protocol_list *protocol_list;
1472 struct _objc__method_prototype_list *instance_methods;
1473 struct _objc__method_prototype_list *class_methods
1474 };
1475
1476 See EmitProtocolExtension().
1477*/
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001478llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1479 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1480
1481 // Early exit if a defining object has already been generated.
1482 if (Entry && Entry->hasInitializer())
1483 return Entry;
1484
Daniel Dunbar8ede0052008-08-25 06:02:07 +00001485 // FIXME: I don't understand why gcc generates this, or where it is
1486 // resolved. Investigate. Its also wasteful to look this up over and
1487 // over.
1488 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1489
Chris Lattnerd120b9e2008-11-24 03:54:41 +00001490 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001491
1492 // Construct method lists.
1493 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1494 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001495 for (ObjCProtocolDecl::instmeth_iterator
1496 i = PD->instmeth_begin(CGM.getContext()),
1497 e = PD->instmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001498 ObjCMethodDecl *MD = *i;
1499 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1500 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1501 OptInstanceMethods.push_back(C);
1502 } else {
1503 InstanceMethods.push_back(C);
1504 }
1505 }
1506
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001507 for (ObjCProtocolDecl::classmeth_iterator
1508 i = PD->classmeth_begin(CGM.getContext()),
1509 e = PD->classmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001510 ObjCMethodDecl *MD = *i;
1511 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1512 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1513 OptClassMethods.push_back(C);
1514 } else {
1515 ClassMethods.push_back(C);
1516 }
1517 }
1518
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001519 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001520 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001521 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar67e778b2008-08-21 21:57:41 +00001522 Values[2] =
Chris Lattner271d4c22008-11-24 05:29:24 +00001523 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbar67e778b2008-08-21 21:57:41 +00001524 PD->protocol_begin(),
1525 PD->protocol_end());
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001526 Values[3] =
Chris Lattner271d4c22008-11-24 05:29:24 +00001527 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
1528 + PD->getNameAsString(),
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001529 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1530 InstanceMethods);
1531 Values[4] =
Chris Lattner271d4c22008-11-24 05:29:24 +00001532 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
1533 + PD->getNameAsString(),
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001534 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1535 ClassMethods);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001536 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
1537 Values);
1538
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001539 if (Entry) {
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001540 // Already created, fix the linkage and update the initializer.
1541 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001542 Entry->setInitializer(Init);
1543 } else {
1544 Entry =
1545 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
1546 llvm::GlobalValue::InternalLinkage,
1547 Init,
1548 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
1549 &CGM.getModule());
1550 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar56756c32009-03-09 22:18:41 +00001551 Entry->setAlignment(4);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001552 UsedGlobals.push_back(Entry);
1553 // FIXME: Is this necessary? Why only for protocol?
1554 Entry->setAlignment(4);
1555 }
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001556
1557 return Entry;
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001558}
1559
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001560llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001561 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1562
1563 if (!Entry) {
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001564 // We use the initializer as a marker of whether this is a forward
1565 // reference or not. At module finalization we add the empty
1566 // contents for protocols which were referenced but never defined.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001567 Entry =
1568 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001569 llvm::GlobalValue::ExternalLinkage,
1570 0,
Chris Lattner271d4c22008-11-24 05:29:24 +00001571 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001572 &CGM.getModule());
1573 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar56756c32009-03-09 22:18:41 +00001574 Entry->setAlignment(4);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001575 UsedGlobals.push_back(Entry);
1576 // FIXME: Is this necessary? Why only for protocol?
1577 Entry->setAlignment(4);
1578 }
1579
1580 return Entry;
1581}
1582
1583/*
1584 struct _objc_protocol_extension {
1585 uint32_t size;
1586 struct objc_method_description_list *optional_instance_methods;
1587 struct objc_method_description_list *optional_class_methods;
1588 struct objc_property_list *instance_properties;
1589 };
1590*/
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001591llvm::Constant *
1592CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1593 const ConstantVector &OptInstanceMethods,
1594 const ConstantVector &OptClassMethods) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001595 uint64_t Size =
Daniel Dunbard8439f22009-01-12 21:08:18 +00001596 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001597 std::vector<llvm::Constant*> Values(4);
1598 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001599 Values[1] =
Chris Lattner271d4c22008-11-24 05:29:24 +00001600 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1601 + PD->getNameAsString(),
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001602 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1603 OptInstanceMethods);
1604 Values[2] =
Chris Lattner271d4c22008-11-24 05:29:24 +00001605 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1606 + PD->getNameAsString(),
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001607 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1608 OptClassMethods);
Chris Lattner271d4c22008-11-24 05:29:24 +00001609 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1610 PD->getNameAsString(),
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00001611 0, PD, ObjCTypes);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001612
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001613 // Return null if no extension bits are used.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001614 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1615 Values[3]->isNullValue())
1616 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1617
1618 llvm::Constant *Init =
1619 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001620
Daniel Dunbar90d88f92009-03-09 21:49:58 +00001621 // No special section, but goes in llvm.used
1622 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
1623 Init,
1624 0, 0, true);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001625}
1626
1627/*
1628 struct objc_protocol_list {
1629 struct objc_protocol_list *next;
1630 long count;
1631 Protocol *list[];
1632 };
1633*/
Daniel Dunbar67e778b2008-08-21 21:57:41 +00001634llvm::Constant *
1635CGObjCMac::EmitProtocolList(const std::string &Name,
1636 ObjCProtocolDecl::protocol_iterator begin,
1637 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001638 std::vector<llvm::Constant*> ProtocolRefs;
1639
Daniel Dunbar67e778b2008-08-21 21:57:41 +00001640 for (; begin != end; ++begin)
1641 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001642
1643 // Just return null for empty protocol lists
1644 if (ProtocolRefs.empty())
1645 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1646
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001647 // This list is null terminated.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001648 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1649
1650 std::vector<llvm::Constant*> Values(3);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001651 // This field is only used by the runtime.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001652 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1653 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1654 Values[2] =
1655 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1656 ProtocolRefs.size()),
1657 ProtocolRefs);
1658
1659 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1660 llvm::GlobalVariable *GV =
Daniel Dunbar90d88f92009-03-09 21:49:58 +00001661 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar56756c32009-03-09 22:18:41 +00001662 4, false);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001663 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1664}
1665
1666/*
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001667 struct _objc_property {
1668 const char * const name;
1669 const char * const attributes;
1670 };
1671
1672 struct _objc_property_list {
1673 uint32_t entsize; // sizeof (struct _objc_property)
1674 uint32_t prop_count;
1675 struct _objc_property[prop_count];
1676 };
1677*/
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00001678llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1679 const Decl *Container,
1680 const ObjCContainerDecl *OCD,
1681 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001682 std::vector<llvm::Constant*> Properties, Prop(2);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001683 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(CGM.getContext()),
1684 E = OCD->prop_end(CGM.getContext()); I != E; ++I) {
Steve Naroffdcf1e842009-01-11 12:47:58 +00001685 const ObjCPropertyDecl *PD = *I;
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001686 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001687 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001688 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1689 Prop));
1690 }
1691
1692 // Return null for empty list.
1693 if (Properties.empty())
1694 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1695
1696 unsigned PropertySize =
Daniel Dunbard8439f22009-01-12 21:08:18 +00001697 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001698 std::vector<llvm::Constant*> Values(3);
1699 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1700 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1701 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1702 Properties.size());
1703 Values[2] = llvm::ConstantArray::get(AT, Properties);
1704 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1705
Daniel Dunbar90d88f92009-03-09 21:49:58 +00001706 llvm::GlobalVariable *GV =
Daniel Dunbar9dfd3a72009-04-15 02:56:18 +00001707 CreateMetadataVar(Name, Init,
1708 (ObjCABI == 2) ? "__DATA, __objc_const" :
1709 "__OBJC,__property,regular,no_dead_strip",
1710 (ObjCABI == 2) ? 8 : 4,
1711 true);
Daniel Dunbar90d88f92009-03-09 21:49:58 +00001712 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001713}
1714
1715/*
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001716 struct objc_method_description_list {
1717 int count;
1718 struct objc_method_description list[];
1719 };
1720*/
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001721llvm::Constant *
1722CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1723 std::vector<llvm::Constant*> Desc(2);
1724 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1725 ObjCTypes.SelectorPtrTy);
1726 Desc[1] = GetMethodVarType(MD);
1727 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1728 Desc);
1729}
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001730
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001731llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1732 const char *Section,
1733 const ConstantVector &Methods) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001734 // Return null for empty list.
1735 if (Methods.empty())
1736 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1737
1738 std::vector<llvm::Constant*> Values(2);
1739 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1740 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1741 Methods.size());
1742 Values[1] = llvm::ConstantArray::get(AT, Methods);
1743 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1744
Daniel Dunbar9dfd3a72009-04-15 02:56:18 +00001745 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001746 return llvm::ConstantExpr::getBitCast(GV,
1747 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001748}
1749
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001750/*
1751 struct _objc_category {
1752 char *category_name;
1753 char *class_name;
1754 struct _objc_method_list *instance_methods;
1755 struct _objc_method_list *class_methods;
1756 struct _objc_protocol_list *protocols;
1757 uint32_t size; // <rdar://4585769>
1758 struct _objc_property_list *instance_properties;
1759 };
1760 */
Daniel Dunbarac93e472008-08-15 22:20:32 +00001761void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbard8439f22009-01-12 21:08:18 +00001762 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001763
Daniel Dunbar0cd49032008-08-26 23:03:11 +00001764 // FIXME: This is poor design, the OCD should have a pointer to the
1765 // category decl. Additionally, note that Category can be null for
1766 // the @implementation w/o an @interface case. Sema should just
1767 // create one for us as it does for @implementation so everyone else
1768 // can live life under a clear blue sky.
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001769 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar0cd49032008-08-26 23:03:11 +00001770 const ObjCCategoryDecl *Category =
1771 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattner271d4c22008-11-24 05:29:24 +00001772 std::string ExtName(Interface->getNameAsString() + "_" +
1773 OCD->getNameAsString());
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001774
Daniel Dunbar12996f52008-08-26 21:51:14 +00001775 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregorcd19b572009-04-23 01:02:12 +00001776 for (ObjCCategoryImplDecl::instmeth_iterator
1777 i = OCD->instmeth_begin(CGM.getContext()),
1778 e = OCD->instmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbar12996f52008-08-26 21:51:14 +00001779 // Instance methods should always be defined.
1780 InstanceMethods.push_back(GetMethodConstant(*i));
1781 }
Douglas Gregorcd19b572009-04-23 01:02:12 +00001782 for (ObjCCategoryImplDecl::classmeth_iterator
1783 i = OCD->classmeth_begin(CGM.getContext()),
1784 e = OCD->classmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbar12996f52008-08-26 21:51:14 +00001785 // Class methods should always be defined.
1786 ClassMethods.push_back(GetMethodConstant(*i));
1787 }
1788
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001789 std::vector<llvm::Constant*> Values(7);
1790 Values[0] = GetClassName(OCD->getIdentifier());
1791 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahaniand11bc1d2009-04-29 20:40:05 +00001792 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001793 Values[2] =
1794 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1795 ExtName,
1796 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar12996f52008-08-26 21:51:14 +00001797 InstanceMethods);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001798 Values[3] =
1799 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
Daniel Dunbar9dfd3a72009-04-15 02:56:18 +00001800 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar12996f52008-08-26 21:51:14 +00001801 ClassMethods);
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001802 if (Category) {
1803 Values[4] =
1804 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1805 Category->protocol_begin(),
1806 Category->protocol_end());
1807 } else {
1808 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1809 }
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001810 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar0cd49032008-08-26 23:03:11 +00001811
1812 // If there is no category @interface then there can be no properties.
1813 if (Category) {
Daniel Dunbar9dfd3a72009-04-15 02:56:18 +00001814 Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00001815 OCD, Category, ObjCTypes);
Daniel Dunbar0cd49032008-08-26 23:03:11 +00001816 } else {
1817 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1818 }
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001819
1820 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1821 Values);
1822
1823 llvm::GlobalVariable *GV =
Daniel Dunbar90d88f92009-03-09 21:49:58 +00001824 CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init,
1825 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar56756c32009-03-09 22:18:41 +00001826 4, true);
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001827 DefinedCategories.push_back(GV);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001828}
1829
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001830// FIXME: Get from somewhere?
1831enum ClassFlags {
1832 eClassFlags_Factory = 0x00001,
1833 eClassFlags_Meta = 0x00002,
1834 // <rdr://5142207>
1835 eClassFlags_HasCXXStructors = 0x02000,
1836 eClassFlags_Hidden = 0x20000,
1837 eClassFlags_ABI2_Hidden = 0x00010,
1838 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1839};
1840
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001841/*
1842 struct _objc_class {
1843 Class isa;
1844 Class super_class;
1845 const char *name;
1846 long version;
1847 long info;
1848 long instance_size;
1849 struct _objc_ivar_list *ivars;
1850 struct _objc_method_list *methods;
1851 struct _objc_cache *cache;
1852 struct _objc_protocol_list *protocols;
1853 // Objective-C 1.0 extensions (<rdr://4585769>)
1854 const char *ivar_layout;
1855 struct _objc_class_ext *ext;
1856 };
1857
1858 See EmitClassExtension();
1859 */
1860void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar8ede0052008-08-25 06:02:07 +00001861 DefinedSymbols.insert(ID->getIdentifier());
1862
Chris Lattnerd120b9e2008-11-24 03:54:41 +00001863 std::string ClassName = ID->getNameAsString();
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001864 // FIXME: Gross
1865 ObjCInterfaceDecl *Interface =
1866 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar67e778b2008-08-21 21:57:41 +00001867 llvm::Constant *Protocols =
Chris Lattner271d4c22008-11-24 05:29:24 +00001868 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbar67e778b2008-08-21 21:57:41 +00001869 Interface->protocol_begin(),
1870 Interface->protocol_end());
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001871 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar07ddfa92009-05-03 10:46:44 +00001872 unsigned Size =
1873 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001874
1875 // FIXME: Set CXX-structors flag.
Daniel Dunbar8394fda2009-04-14 06:00:08 +00001876 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001877 Flags |= eClassFlags_Hidden;
1878
Daniel Dunbar12996f52008-08-26 21:51:14 +00001879 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregorcd19b572009-04-23 01:02:12 +00001880 for (ObjCImplementationDecl::instmeth_iterator
1881 i = ID->instmeth_begin(CGM.getContext()),
1882 e = ID->instmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbar12996f52008-08-26 21:51:14 +00001883 // Instance methods should always be defined.
1884 InstanceMethods.push_back(GetMethodConstant(*i));
1885 }
Douglas Gregorcd19b572009-04-23 01:02:12 +00001886 for (ObjCImplementationDecl::classmeth_iterator
1887 i = ID->classmeth_begin(CGM.getContext()),
1888 e = ID->classmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbar12996f52008-08-26 21:51:14 +00001889 // Class methods should always be defined.
1890 ClassMethods.push_back(GetMethodConstant(*i));
1891 }
1892
Douglas Gregorcd19b572009-04-23 01:02:12 +00001893 for (ObjCImplementationDecl::propimpl_iterator
1894 i = ID->propimpl_begin(CGM.getContext()),
1895 e = ID->propimpl_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbar12996f52008-08-26 21:51:14 +00001896 ObjCPropertyImplDecl *PID = *i;
1897
1898 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1899 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1900
1901 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1902 if (llvm::Constant *C = GetMethodConstant(MD))
1903 InstanceMethods.push_back(C);
1904 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1905 if (llvm::Constant *C = GetMethodConstant(MD))
1906 InstanceMethods.push_back(C);
1907 }
1908 }
1909
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001910 std::vector<llvm::Constant*> Values(12);
Daniel Dunbara3e92cd2009-05-03 08:56:52 +00001911 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001912 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar8ede0052008-08-25 06:02:07 +00001913 // Record a reference to the super class.
1914 LazySymbols.insert(Super->getIdentifier());
1915
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001916 Values[ 1] =
1917 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1918 ObjCTypes.ClassPtrTy);
1919 } else {
1920 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1921 }
1922 Values[ 2] = GetClassName(ID->getIdentifier());
1923 // Version is always 0.
1924 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1925 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1926 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00001927 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001928 Values[ 7] =
Chris Lattner271d4c22008-11-24 05:29:24 +00001929 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001930 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar12996f52008-08-26 21:51:14 +00001931 InstanceMethods);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001932 // cache is always NULL.
1933 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1934 Values[ 9] = Protocols;
Fariborz Jahanian31b96492009-04-22 23:00:43 +00001935 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001936 Values[11] = EmitClassExtension(ID);
1937 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1938 Values);
1939
1940 llvm::GlobalVariable *GV =
Daniel Dunbar90d88f92009-03-09 21:49:58 +00001941 CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init,
1942 "__OBJC,__class,regular,no_dead_strip",
Daniel Dunbar56756c32009-03-09 22:18:41 +00001943 4, true);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001944 DefinedClasses.push_back(GV);
1945}
1946
1947llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1948 llvm::Constant *Protocols,
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001949 const ConstantVector &Methods) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001950 unsigned Flags = eClassFlags_Meta;
Daniel Dunbard8439f22009-01-12 21:08:18 +00001951 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001952
Daniel Dunbar8394fda2009-04-14 06:00:08 +00001953 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001954 Flags |= eClassFlags_Hidden;
1955
1956 std::vector<llvm::Constant*> Values(12);
1957 // The isa for the metaclass is the root of the hierarchy.
1958 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1959 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1960 Root = Super;
1961 Values[ 0] =
1962 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1963 ObjCTypes.ClassPtrTy);
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001964 // The super class for the metaclass is emitted as the name of the
1965 // super class. The runtime fixes this up to point to the
1966 // *metaclass* for the super class.
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001967 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1968 Values[ 1] =
1969 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1970 ObjCTypes.ClassPtrTy);
1971 } else {
1972 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1973 }
1974 Values[ 2] = GetClassName(ID->getIdentifier());
1975 // Version is always 0.
1976 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1977 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1978 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00001979 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001980 Values[ 7] =
Chris Lattner271d4c22008-11-24 05:29:24 +00001981 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar9dfd3a72009-04-15 02:56:18 +00001982 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbar12996f52008-08-26 21:51:14 +00001983 Methods);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001984 // cache is always NULL.
1985 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1986 Values[ 9] = Protocols;
1987 // ivar_layout for metaclass is always NULL.
1988 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1989 // The class extension is always unused for metaclasses.
1990 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1991 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1992 Values);
1993
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001994 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattnerd120b9e2008-11-24 03:54:41 +00001995 Name += ID->getNameAsCString();
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001996
1997 // Check for a forward reference.
1998 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1999 if (GV) {
2000 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2001 "Forward metaclass reference has incorrect type.");
2002 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2003 GV->setInitializer(Init);
2004 } else {
2005 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
2006 llvm::GlobalValue::InternalLinkage,
2007 Init, Name,
2008 &CGM.getModule());
2009 }
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002010 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar56756c32009-03-09 22:18:41 +00002011 GV->setAlignment(4);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002012 UsedGlobals.push_back(GV);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002013
2014 return GV;
2015}
2016
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00002017llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattner271d4c22008-11-24 05:29:24 +00002018 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00002019
2020 // FIXME: Should we look these up somewhere other than the
2021 // module. Its a bit silly since we only generate these while
2022 // processing an implementation, so exactly one pointer would work
2023 // if know when we entered/exitted an implementation block.
2024
2025 // Check for an existing forward reference.
Fariborz Jahanian5fe09f72009-01-07 20:11:22 +00002026 // Previously, metaclass with internal linkage may have been defined.
2027 // pass 'true' as 2nd argument so it is returned.
2028 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00002029 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2030 "Forward metaclass reference has incorrect type.");
2031 return GV;
2032 } else {
2033 // Generate as an external reference to keep a consistent
2034 // module. This will be patched up when we emit the metaclass.
2035 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
2036 llvm::GlobalValue::ExternalLinkage,
2037 0,
2038 Name,
2039 &CGM.getModule());
2040 }
2041}
2042
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002043/*
2044 struct objc_class_ext {
2045 uint32_t size;
2046 const char *weak_ivar_layout;
2047 struct _objc_property_list *properties;
2048 };
2049*/
2050llvm::Constant *
2051CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
2052 uint64_t Size =
Daniel Dunbard8439f22009-01-12 21:08:18 +00002053 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002054
2055 std::vector<llvm::Constant*> Values(3);
2056 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian31b96492009-04-22 23:00:43 +00002057 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9dfd3a72009-04-15 02:56:18 +00002058 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00002059 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002060
2061 // Return null if no extension bits are used.
2062 if (Values[1]->isNullValue() && Values[2]->isNullValue())
2063 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
2064
2065 llvm::Constant *Init =
2066 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar90d88f92009-03-09 21:49:58 +00002067 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar9dfd3a72009-04-15 02:56:18 +00002068 Init, "__OBJC,__class_ext,regular,no_dead_strip",
2069 4, true);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002070}
2071
Fariborz Jahaniana09a5142009-02-12 18:51:23 +00002072/// getInterfaceDeclForIvar - Get the interface declaration node where
2073/// this ivar is declared in.
2074/// FIXME. Ideally, this info should be in the ivar node. But currently
2075/// it is not and prevailing wisdom is that ASTs should not have more
2076/// info than is absolutely needed, even though this info reflects the
2077/// source language.
2078///
2079static const ObjCInterfaceDecl *getInterfaceDeclForIvar(
2080 const ObjCInterfaceDecl *OI,
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002081 const ObjCIvarDecl *IVD,
2082 ASTContext &Context) {
Fariborz Jahaniana09a5142009-02-12 18:51:23 +00002083 if (!OI)
2084 return 0;
2085 assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface");
2086 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
2087 E = OI->ivar_end(); I != E; ++I)
2088 if ((*I)->getIdentifier() == IVD->getIdentifier())
2089 return OI;
Fariborz Jahanian13c22d72009-03-31 17:00:52 +00002090 // look into properties.
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002091 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(Context),
2092 E = OI->prop_end(Context); I != E; ++I) {
Fariborz Jahanian13c22d72009-03-31 17:00:52 +00002093 ObjCPropertyDecl *PDecl = (*I);
2094 if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl())
2095 if (IV->getIdentifier() == IVD->getIdentifier())
2096 return OI;
2097 }
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002098 return getInterfaceDeclForIvar(OI->getSuperClass(), IVD, Context);
Fariborz Jahaniana09a5142009-02-12 18:51:23 +00002099}
2100
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002101/*
2102 struct objc_ivar {
2103 char *ivar_name;
2104 char *ivar_type;
2105 int ivar_offset;
2106 };
2107
2108 struct objc_ivar_list {
2109 int ivar_count;
2110 struct objc_ivar list[count];
2111 };
2112 */
2113llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00002114 bool ForClass) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002115 std::vector<llvm::Constant*> Ivars, Ivar(3);
2116
2117 // When emitting the root class GCC emits ivar entries for the
2118 // actual class structure. It is not clear if we need to follow this
2119 // behavior; for now lets try and get away with not doing it. If so,
2120 // the cleanest solution would be to make up an ObjCInterfaceDecl
2121 // for the class.
2122 if (ForClass)
2123 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00002124
2125 ObjCInterfaceDecl *OID =
2126 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00002127
Daniel Dunbar356f0742009-04-20 06:54:31 +00002128 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
2129 GetNamedIvarList(OID, OIvars);
2130
2131 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2132 ObjCIvarDecl *IVD = OIvars[i];
Daniel Dunbare42aede2009-04-22 08:22:17 +00002133 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2134 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar72878722009-04-20 20:18:54 +00002135 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar85d37542009-04-22 07:32:20 +00002136 ComputeIvarBaseOffset(CGM, OID, IVD));
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002137 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002138 }
2139
2140 // Return null for empty list.
2141 if (Ivars.empty())
2142 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
2143
2144 std::vector<llvm::Constant*> Values(2);
2145 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
2146 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
2147 Ivars.size());
2148 Values[1] = llvm::ConstantArray::get(AT, Ivars);
2149 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2150
Daniel Dunbar90d88f92009-03-09 21:49:58 +00002151 llvm::GlobalVariable *GV;
2152 if (ForClass)
2153 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(),
Daniel Dunbar56756c32009-03-09 22:18:41 +00002154 Init, "__OBJC,__class_vars,regular,no_dead_strip",
2155 4, true);
Daniel Dunbar90d88f92009-03-09 21:49:58 +00002156 else
2157 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_"
2158 + ID->getNameAsString(),
2159 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar9dfd3a72009-04-15 02:56:18 +00002160 4, true);
2161 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002162}
2163
2164/*
2165 struct objc_method {
2166 SEL method_name;
2167 char *method_types;
2168 void *method;
2169 };
2170
2171 struct objc_method_list {
2172 struct objc_method_list *obsolete;
2173 int count;
2174 struct objc_method methods_list[count];
2175 };
2176*/
Daniel Dunbar12996f52008-08-26 21:51:14 +00002177
2178/// GetMethodConstant - Return a struct objc_method constant for the
2179/// given method if it has been defined. The result is null if the
2180/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarfe131f02008-08-27 02:31:56 +00002181llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbar12996f52008-08-26 21:51:14 +00002182 // FIXME: Use DenseMap::lookup
2183 llvm::Function *Fn = MethodDefinitions[MD];
2184 if (!Fn)
2185 return 0;
2186
2187 std::vector<llvm::Constant*> Method(3);
2188 Method[0] =
2189 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2190 ObjCTypes.SelectorPtrTy);
2191 Method[1] = GetMethodVarType(MD);
2192 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
2193 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
2194}
2195
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00002196llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
2197 const char *Section,
Daniel Dunbarfe131f02008-08-27 02:31:56 +00002198 const ConstantVector &Methods) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002199 // Return null for empty list.
2200 if (Methods.empty())
2201 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
2202
2203 std::vector<llvm::Constant*> Values(3);
2204 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
2205 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
2206 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
2207 Methods.size());
2208 Values[2] = llvm::ConstantArray::get(AT, Methods);
2209 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2210
Daniel Dunbar9dfd3a72009-04-15 02:56:18 +00002211 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002212 return llvm::ConstantExpr::getBitCast(GV,
2213 ObjCTypes.MethodListPtrTy);
Daniel Dunbarace33292008-08-16 03:19:19 +00002214}
2215
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00002216llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00002217 const ObjCContainerDecl *CD) {
Daniel Dunbarace33292008-08-16 03:19:19 +00002218 std::string Name;
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +00002219 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarace33292008-08-16 03:19:19 +00002220
Daniel Dunbar34bda882009-02-02 23:23:47 +00002221 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00002222 const llvm::FunctionType *MethodTy =
Daniel Dunbar34bda882009-02-02 23:23:47 +00002223 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarace33292008-08-16 03:19:19 +00002224 llvm::Function *Method =
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00002225 llvm::Function::Create(MethodTy,
Daniel Dunbarace33292008-08-16 03:19:19 +00002226 llvm::GlobalValue::InternalLinkage,
2227 Name,
2228 &CGM.getModule());
Daniel Dunbar12996f52008-08-26 21:51:14 +00002229 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarace33292008-08-16 03:19:19 +00002230
Daniel Dunbarace33292008-08-16 03:19:19 +00002231 return Method;
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00002232}
2233
Daniel Dunbar1cfb5192009-04-19 02:03:42 +00002234/// GetFieldBaseOffset - return the field's byte offset.
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00002235uint64_t CGObjCCommonMac::GetFieldBaseOffset(const ObjCInterfaceDecl *OI,
2236 const llvm::StructLayout *Layout,
Chris Lattnerd391dab2009-03-31 08:33:16 +00002237 const FieldDecl *Field) {
Daniel Dunbar85d37542009-04-22 07:32:20 +00002238 // Is this a C struct?
Fariborz Jahanian31614742009-04-20 22:03:45 +00002239 if (!OI)
2240 return Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
Daniel Dunbar85d37542009-04-22 07:32:20 +00002241 return ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00002242}
2243
Daniel Dunbarc4594f22009-03-09 20:09:19 +00002244llvm::GlobalVariable *
2245CGObjCCommonMac::CreateMetadataVar(const std::string &Name,
2246 llvm::Constant *Init,
2247 const char *Section,
Daniel Dunbareddddd22009-03-09 20:50:13 +00002248 unsigned Align,
2249 bool AddToUsed) {
Daniel Dunbarc4594f22009-03-09 20:09:19 +00002250 const llvm::Type *Ty = Init->getType();
2251 llvm::GlobalVariable *GV =
2252 new llvm::GlobalVariable(Ty, false,
2253 llvm::GlobalValue::InternalLinkage,
2254 Init,
2255 Name,
2256 &CGM.getModule());
2257 if (Section)
2258 GV->setSection(Section);
Daniel Dunbareddddd22009-03-09 20:50:13 +00002259 if (Align)
2260 GV->setAlignment(Align);
2261 if (AddToUsed)
Daniel Dunbarc4594f22009-03-09 20:09:19 +00002262 UsedGlobals.push_back(GV);
2263 return GV;
2264}
2265
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00002266llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002267 // Abuse this interface function as a place to finalize.
2268 FinishModule();
2269
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00002270 return NULL;
2271}
2272
Chris Lattneraea1aee2009-03-22 21:03:39 +00002273llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattnera7ecda42009-04-22 02:44:54 +00002274 return ObjCTypes.getGetPropertyFn();
Daniel Dunbarf7103722008-09-24 03:38:44 +00002275}
2276
Chris Lattneraea1aee2009-03-22 21:03:39 +00002277llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattnera7ecda42009-04-22 02:44:54 +00002278 return ObjCTypes.getSetPropertyFn();
Daniel Dunbarf7103722008-09-24 03:38:44 +00002279}
2280
Chris Lattneraea1aee2009-03-22 21:03:39 +00002281llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattnera7ecda42009-04-22 02:44:54 +00002282 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson58d16242008-08-31 04:05:03 +00002283}
2284
Daniel Dunbar83544842008-09-28 01:03:14 +00002285/*
2286
2287Objective-C setjmp-longjmp (sjlj) Exception Handling
2288--
2289
2290The basic framework for a @try-catch-finally is as follows:
2291{
2292 objc_exception_data d;
2293 id _rethrow = null;
Anders Carlsson8559de12009-02-07 21:26:04 +00002294 bool _call_try_exit = true;
2295
Daniel Dunbar83544842008-09-28 01:03:14 +00002296 objc_exception_try_enter(&d);
2297 if (!setjmp(d.jmp_buf)) {
2298 ... try body ...
2299 } else {
2300 // exception path
2301 id _caught = objc_exception_extract(&d);
2302
2303 // enter new try scope for handlers
2304 if (!setjmp(d.jmp_buf)) {
2305 ... match exception and execute catch blocks ...
2306
2307 // fell off end, rethrow.
2308 _rethrow = _caught;
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002309 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar83544842008-09-28 01:03:14 +00002310 } else {
2311 // exception in catch block
2312 _rethrow = objc_exception_extract(&d);
Anders Carlsson8559de12009-02-07 21:26:04 +00002313 _call_try_exit = false;
2314 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar83544842008-09-28 01:03:14 +00002315 }
2316 }
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002317 ... jump-through-finally to finally_end ...
Daniel Dunbar83544842008-09-28 01:03:14 +00002318
2319finally:
Anders Carlsson8559de12009-02-07 21:26:04 +00002320 if (_call_try_exit)
2321 objc_exception_try_exit(&d);
2322
Daniel Dunbar83544842008-09-28 01:03:14 +00002323 ... finally block ....
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002324 ... dispatch to finally destination ...
2325
2326finally_rethrow:
2327 objc_exception_throw(_rethrow);
2328
2329finally_end:
Daniel Dunbar83544842008-09-28 01:03:14 +00002330}
2331
2332This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002333uses _rethrow to determine if objc_exception_try_exit should be called
2334and if the object should be rethrown. This breaks in the face of
2335throwing nil and introduces unnecessary branches.
Daniel Dunbar83544842008-09-28 01:03:14 +00002336
2337We specialize this framework for a few particular circumstances:
2338
2339 - If there are no catch blocks, then we avoid emitting the second
2340 exception handling context.
2341
2342 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2343 e)) we avoid emitting the code to rethrow an uncaught exception.
2344
2345 - FIXME: If there is no @finally block we can do a few more
2346 simplifications.
2347
2348Rethrows and Jumps-Through-Finally
2349--
2350
2351Support for implicit rethrows and jumping through the finally block is
2352handled by storing the current exception-handling context in
2353ObjCEHStack.
2354
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002355In order to implement proper @finally semantics, we support one basic
2356mechanism for jumping through the finally block to an arbitrary
2357destination. Constructs which generate exits from a @try or @catch
2358block use this mechanism to implement the proper semantics by chaining
2359jumps, as necessary.
2360
2361This mechanism works like the one used for indirect goto: we
2362arbitrarily assign an ID to each destination and store the ID for the
2363destination in a variable prior to entering the finally block. At the
2364end of the finally block we simply create a switch to the proper
2365destination.
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00002366
2367Code gen for @synchronized(expr) stmt;
2368Effectively generating code for:
2369objc_sync_enter(expr);
2370@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar83544842008-09-28 01:03:14 +00002371*/
2372
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00002373void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2374 const Stmt &S) {
2375 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002376 // Create various blocks we refer to for handling @finally.
Daniel Dunbar72f96552008-11-11 02:29:29 +00002377 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson8559de12009-02-07 21:26:04 +00002378 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar72f96552008-11-11 02:29:29 +00002379 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
2380 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
2381 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar34416d62009-02-24 01:43:46 +00002382
2383 // For @synchronized, call objc_sync_enter(sync.expr). The
2384 // evaluation of the expression must occur before we enter the
2385 // @synchronized. We can safely avoid a temp here because jumps into
2386 // @synchronized are illegal & this will dominate uses.
2387 llvm::Value *SyncArg = 0;
2388 if (!isTry) {
2389 SyncArg =
2390 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2391 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattner23e24652009-04-06 16:53:45 +00002392 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar34416d62009-02-24 01:43:46 +00002393 }
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002394
2395 // Push an EH context entry, used for handling rethrows and jumps
2396 // through finally.
Anders Carlsson00ffb962009-02-09 20:38:58 +00002397 CGF.PushCleanupBlock(FinallyBlock);
2398
Anders Carlssonecd81832009-02-07 21:37:21 +00002399 CGF.ObjCEHValueStack.push_back(0);
2400
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002401 // Allocate memory for the exception data and rethrow pointer.
Anders Carlssonfca6c292008-09-09 17:59:25 +00002402 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2403 "exceptiondata.ptr");
Daniel Dunbar35b777f2008-10-29 22:36:39 +00002404 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
2405 "_rethrow");
Anders Carlsson8559de12009-02-07 21:26:04 +00002406 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
2407 "_call_try_exit");
2408 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr);
2409
Anders Carlssonfca6c292008-09-09 17:59:25 +00002410 // Enter a new try block and call setjmp.
Chris Lattnere05d4cb2009-04-22 02:26:14 +00002411 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Anders Carlssonfca6c292008-09-09 17:59:25 +00002412 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
2413 "jmpbufarray");
2414 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
Chris Lattnere05d4cb2009-04-22 02:26:14 +00002415 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlssonfca6c292008-09-09 17:59:25 +00002416 JmpBufPtr, "result");
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002417
Daniel Dunbar72f96552008-11-11 02:29:29 +00002418 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2419 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbarbe56f012008-10-02 17:05:36 +00002420 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbar0ac66f72008-09-27 23:30:04 +00002421 TryHandler, TryBlock);
Anders Carlssonfca6c292008-09-09 17:59:25 +00002422
2423 // Emit the @try block.
2424 CGF.EmitBlock(TryBlock);
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00002425 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
2426 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlsson00ffb962009-02-09 20:38:58 +00002427 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlssonfca6c292008-09-09 17:59:25 +00002428
2429 // Emit the "exception in @try" block.
Daniel Dunbar0ac66f72008-09-27 23:30:04 +00002430 CGF.EmitBlock(TryHandler);
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002431
2432 // Retrieve the exception object. We may emit multiple blocks but
2433 // nothing can cross this so the value is already in SSA form.
Chris Lattnere05d4cb2009-04-22 02:26:14 +00002434 llvm::Value *Caught =
2435 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2436 ExceptionData, "caught");
Anders Carlssonecd81832009-02-07 21:37:21 +00002437 CGF.ObjCEHValueStack.back() = Caught;
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00002438 if (!isTry)
2439 {
2440 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson8559de12009-02-07 21:26:04 +00002441 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlsson00ffb962009-02-09 20:38:58 +00002442 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00002443 }
2444 else if (const ObjCAtCatchStmt* CatchStmt =
2445 cast<ObjCAtTryStmt>(S).getCatchStmts())
2446 {
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002447 // Enter a new exception try block (in case a @catch block throws
2448 // an exception).
Chris Lattnere05d4cb2009-04-22 02:26:14 +00002449 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002450
Chris Lattnere05d4cb2009-04-22 02:26:14 +00002451 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlssonfca6c292008-09-09 17:59:25 +00002452 JmpBufPtr, "result");
Daniel Dunbarbe56f012008-10-02 17:05:36 +00002453 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlssonfca6c292008-09-09 17:59:25 +00002454
Daniel Dunbar72f96552008-11-11 02:29:29 +00002455 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
2456 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbar0ac66f72008-09-27 23:30:04 +00002457 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlssonfca6c292008-09-09 17:59:25 +00002458
2459 CGF.EmitBlock(CatchBlock);
2460
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002461 // Handle catch list. As a special case we check if everything is
2462 // matched and avoid generating code for falling off the end if
2463 // so.
2464 bool AllMatched = false;
Anders Carlssonfca6c292008-09-09 17:59:25 +00002465 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar72f96552008-11-11 02:29:29 +00002466 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlssonfca6c292008-09-09 17:59:25 +00002467
Steve Naroff0e8b96a2009-03-03 19:52:17 +00002468 const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Daniel Dunbar7a68b452008-09-27 07:36:24 +00002469 const PointerType *PT = 0;
2470
Anders Carlssonfca6c292008-09-09 17:59:25 +00002471 // catch(...) always matches.
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002472 if (!CatchParam) {
2473 AllMatched = true;
2474 } else {
Steve Naroff0e8b96a2009-03-03 19:52:17 +00002475 PT = CatchParam->getType()->getAsPointerType();
Anders Carlssonfca6c292008-09-09 17:59:25 +00002476
Daniel Dunbard04c9352008-09-27 22:21:14 +00002477 // catch(id e) always matches.
2478 // FIXME: For the time being we also match id<X>; this should
2479 // be rejected by Sema instead.
Steve Naroff17c03822009-02-12 17:52:19 +00002480 if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) ||
Steve Naroff0e8b96a2009-03-03 19:52:17 +00002481 CatchParam->getType()->isObjCQualifiedIdType())
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002482 AllMatched = true;
Anders Carlssonfca6c292008-09-09 17:59:25 +00002483 }
2484
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002485 if (AllMatched) {
Anders Carlsson75d86732008-09-11 09:15:33 +00002486 if (CatchParam) {
Steve Naroff0e8b96a2009-03-03 19:52:17 +00002487 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbar5aa22bc2008-11-11 23:11:34 +00002488 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Steve Naroff0e8b96a2009-03-03 19:52:17 +00002489 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson75d86732008-09-11 09:15:33 +00002490 }
Anders Carlsson1f4acc32008-09-11 08:21:54 +00002491
Anders Carlsson75d86732008-09-11 09:15:33 +00002492 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlsson00ffb962009-02-09 20:38:58 +00002493 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlssonfca6c292008-09-09 17:59:25 +00002494 break;
2495 }
2496
Daniel Dunbar7a68b452008-09-27 07:36:24 +00002497 assert(PT && "Unexpected non-pointer type in @catch");
2498 QualType T = PT->getPointeeType();
Anders Carlssona4519172008-09-11 06:35:14 +00002499 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlssonfca6c292008-09-09 17:59:25 +00002500 assert(ObjCType && "Catch parameter must have Objective-C type!");
2501
2502 // Check if the @catch block matches the exception object.
2503 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
2504
Chris Lattnere05d4cb2009-04-22 02:26:14 +00002505 llvm::Value *Match =
2506 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
2507 Class, Caught, "match");
Anders Carlssonfca6c292008-09-09 17:59:25 +00002508
Daniel Dunbar72f96552008-11-11 02:29:29 +00002509 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlssonfca6c292008-09-09 17:59:25 +00002510
Daniel Dunbarbe56f012008-10-02 17:05:36 +00002511 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar0ac66f72008-09-27 23:30:04 +00002512 MatchedBlock, NextCatchBlock);
Anders Carlssonfca6c292008-09-09 17:59:25 +00002513
2514 // Emit the @catch block.
2515 CGF.EmitBlock(MatchedBlock);
Steve Naroff0e8b96a2009-03-03 19:52:17 +00002516 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbar5aa22bc2008-11-11 23:11:34 +00002517 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar83544842008-09-28 01:03:14 +00002518
2519 llvm::Value *Tmp =
Steve Naroff0e8b96a2009-03-03 19:52:17 +00002520 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar83544842008-09-28 01:03:14 +00002521 "tmp");
Steve Naroff0e8b96a2009-03-03 19:52:17 +00002522 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson75d86732008-09-11 09:15:33 +00002523
2524 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlsson00ffb962009-02-09 20:38:58 +00002525 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlssonfca6c292008-09-09 17:59:25 +00002526
2527 CGF.EmitBlock(NextCatchBlock);
2528 }
2529
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002530 if (!AllMatched) {
2531 // None of the handlers caught the exception, so store it to be
2532 // rethrown at the end of the @finally block.
2533 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson00ffb962009-02-09 20:38:58 +00002534 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002535 }
2536
2537 // Emit the exception handler for the @catch blocks.
Daniel Dunbar0ac66f72008-09-27 23:30:04 +00002538 CGF.EmitBlock(CatchHandler);
Chris Lattnere05d4cb2009-04-22 02:26:14 +00002539 CGF.Builder.CreateStore(
2540 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2541 ExceptionData),
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002542 RethrowPtr);
Anders Carlsson8559de12009-02-07 21:26:04 +00002543 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlsson00ffb962009-02-09 20:38:58 +00002544 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002545 } else {
Anders Carlssonfca6c292008-09-09 17:59:25 +00002546 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson8559de12009-02-07 21:26:04 +00002547 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlsson00ffb962009-02-09 20:38:58 +00002548 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlssonfca6c292008-09-09 17:59:25 +00002549 }
2550
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002551 // Pop the exception-handling stack entry. It is important to do
2552 // this now, because the code in the @finally block is not in this
2553 // context.
Anders Carlsson00ffb962009-02-09 20:38:58 +00002554 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2555
Anders Carlssonecd81832009-02-07 21:37:21 +00002556 CGF.ObjCEHValueStack.pop_back();
2557
Anders Carlssonfca6c292008-09-09 17:59:25 +00002558 // Emit the @finally block.
2559 CGF.EmitBlock(FinallyBlock);
Anders Carlsson8559de12009-02-07 21:26:04 +00002560 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2561
2562 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2563
2564 CGF.EmitBlock(FinallyExit);
Chris Lattnere05d4cb2009-04-22 02:26:14 +00002565 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData);
Daniel Dunbar7a68b452008-09-27 07:36:24 +00002566
2567 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00002568 if (isTry) {
2569 if (const ObjCAtFinallyStmt* FinallyStmt =
2570 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2571 CGF.EmitStmt(FinallyStmt->getFinallyBody());
Daniel Dunbar34416d62009-02-24 01:43:46 +00002572 } else {
2573 // Emit objc_sync_exit(expr); as finally's sole statement for
2574 // @synchronized.
Chris Lattnerf6ec7e42009-04-22 02:38:11 +00002575 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Fariborz Jahanian3895d922008-11-21 19:21:53 +00002576 }
Anders Carlssonfca6c292008-09-09 17:59:25 +00002577
Anders Carlsson00ffb962009-02-09 20:38:58 +00002578 // Emit the switch block
2579 if (Info.SwitchBlock)
2580 CGF.EmitBlock(Info.SwitchBlock);
2581 if (Info.EndBlock)
2582 CGF.EmitBlock(Info.EndBlock);
2583
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002584 CGF.EmitBlock(FinallyRethrow);
Chris Lattnerf6ec7e42009-04-22 02:38:11 +00002585 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002586 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbar0ac66f72008-09-27 23:30:04 +00002587 CGF.Builder.CreateUnreachable();
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002588
2589 CGF.EmitBlock(FinallyEnd);
Anders Carlssonb01a2112008-09-09 10:04:29 +00002590}
2591
2592void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002593 const ObjCAtThrowStmt &S) {
Anders Carlsson05d7be72008-09-09 16:16:55 +00002594 llvm::Value *ExceptionAsObject;
2595
2596 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2597 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2598 ExceptionAsObject =
2599 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2600 } else {
Anders Carlssonecd81832009-02-07 21:37:21 +00002601 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar83544842008-09-28 01:03:14 +00002602 "Unexpected rethrow outside @catch block.");
Anders Carlssonecd81832009-02-07 21:37:21 +00002603 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson05d7be72008-09-09 16:16:55 +00002604 }
2605
Chris Lattnerf6ec7e42009-04-22 02:38:11 +00002606 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Anders Carlssonfca6c292008-09-09 17:59:25 +00002607 CGF.Builder.CreateUnreachable();
Daniel Dunbar5aa22bc2008-11-11 23:11:34 +00002608
2609 // Clear the insertion point to indicate we are in unreachable code.
2610 CGF.Builder.ClearInsertionPoint();
Anders Carlssonb01a2112008-09-09 10:04:29 +00002611}
2612
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00002613/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian3305ad32008-11-18 21:45:40 +00002614/// object: objc_read_weak (id *src)
2615///
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00002616llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian3305ad32008-11-18 21:45:40 +00002617 llvm::Value *AddrWeakObj)
2618{
Eli Friedmanf8466232009-03-07 03:57:15 +00002619 const llvm::Type* DestTy =
2620 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +00002621 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattnera7ecda42009-04-22 02:44:54 +00002622 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00002623 AddrWeakObj, "weakread");
Eli Friedmanf8466232009-03-07 03:57:15 +00002624 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian3305ad32008-11-18 21:45:40 +00002625 return read_weak;
2626}
2627
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00002628/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2629/// objc_assign_weak (id src, id *dst)
2630///
2631void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2632 llvm::Value *src, llvm::Value *dst)
2633{
Fariborz Jahanianad51ca02009-03-23 19:10:40 +00002634 const llvm::Type * SrcTy = src->getType();
2635 if (!isa<llvm::PointerType>(SrcTy)) {
2636 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2637 assert(Size <= 8 && "does not support size > 8");
2638 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2639 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian664da982009-03-13 00:42:52 +00002640 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2641 }
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +00002642 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2643 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner293c1d32009-04-17 22:12:36 +00002644 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00002645 src, dst, "weakassign");
2646 return;
2647}
2648
Fariborz Jahanian17958902008-11-19 00:59:10 +00002649/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2650/// objc_assign_global (id src, id *dst)
2651///
2652void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2653 llvm::Value *src, llvm::Value *dst)
2654{
Fariborz Jahanianad51ca02009-03-23 19:10:40 +00002655 const llvm::Type * SrcTy = src->getType();
2656 if (!isa<llvm::PointerType>(SrcTy)) {
2657 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2658 assert(Size <= 8 && "does not support size > 8");
2659 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2660 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian664da982009-03-13 00:42:52 +00002661 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2662 }
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +00002663 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2664 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerf6ec7e42009-04-22 02:38:11 +00002665 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian17958902008-11-19 00:59:10 +00002666 src, dst, "globalassign");
2667 return;
2668}
2669
Fariborz Jahanianf310b592008-11-20 19:23:36 +00002670/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2671/// objc_assign_ivar (id src, id *dst)
2672///
2673void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2674 llvm::Value *src, llvm::Value *dst)
2675{
Fariborz Jahanianad51ca02009-03-23 19:10:40 +00002676 const llvm::Type * SrcTy = src->getType();
2677 if (!isa<llvm::PointerType>(SrcTy)) {
2678 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2679 assert(Size <= 8 && "does not support size > 8");
2680 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2681 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian664da982009-03-13 00:42:52 +00002682 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2683 }
Fariborz Jahanianf310b592008-11-20 19:23:36 +00002684 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2685 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerf6ec7e42009-04-22 02:38:11 +00002686 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(),
Fariborz Jahanianf310b592008-11-20 19:23:36 +00002687 src, dst, "assignivar");
2688 return;
2689}
2690
Fariborz Jahanian17958902008-11-19 00:59:10 +00002691/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2692/// objc_assign_strongCast (id src, id *dst)
2693///
2694void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2695 llvm::Value *src, llvm::Value *dst)
2696{
Fariborz Jahanianad51ca02009-03-23 19:10:40 +00002697 const llvm::Type * SrcTy = src->getType();
2698 if (!isa<llvm::PointerType>(SrcTy)) {
2699 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2700 assert(Size <= 8 && "does not support size > 8");
2701 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2702 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian664da982009-03-13 00:42:52 +00002703 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2704 }
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +00002705 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2706 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerf6ec7e42009-04-22 02:38:11 +00002707 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian17958902008-11-19 00:59:10 +00002708 src, dst, "weakassign");
2709 return;
2710}
2711
Fariborz Jahanian4337afe2009-02-02 20:02:29 +00002712/// EmitObjCValueForIvar - Code Gen for ivar reference.
2713///
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00002714LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2715 QualType ObjectTy,
2716 llvm::Value *BaseValue,
2717 const ObjCIvarDecl *Ivar,
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00002718 unsigned CVRQualifiers) {
Daniel Dunbarf5254bd2009-04-21 01:19:28 +00002719 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar85d37542009-04-22 07:32:20 +00002720 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
2721 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian4337afe2009-02-02 20:02:29 +00002722}
2723
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00002724llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar61e14a62009-04-22 05:08:15 +00002725 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00002726 const ObjCIvarDecl *Ivar) {
Daniel Dunbar85d37542009-04-22 07:32:20 +00002727 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00002728 return llvm::ConstantInt::get(
2729 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2730 Offset);
2731}
2732
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002733/* *** Private Interface *** */
2734
2735/// EmitImageInfo - Emit the image info marker used to encode some module
2736/// level information.
2737///
2738/// See: <rdr://4810609&4810587&4810587>
2739/// struct IMAGE_INFO {
2740/// unsigned version;
2741/// unsigned flags;
2742/// };
2743enum ImageInfoFlags {
Daniel Dunbarb79f5a92009-04-20 07:11:47 +00002744 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what
2745 // this implies.
2746 eImageInfo_GarbageCollected = (1 << 1),
2747 eImageInfo_GCOnly = (1 << 2),
2748 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
2749
2750 // A flag indicating that the module has no instances of an
2751 // @synthesize of a superclass variable. <rdar://problem/6803242>
2752 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002753};
2754
2755void CGObjCMac::EmitImageInfo() {
2756 unsigned version = 0; // Version is unused?
2757 unsigned flags = 0;
2758
2759 // FIXME: Fix and continue?
2760 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2761 flags |= eImageInfo_GarbageCollected;
2762 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2763 flags |= eImageInfo_GCOnly;
Daniel Dunbarb79f5a92009-04-20 07:11:47 +00002764
2765 // We never allow @synthesize of a superclass property.
2766 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002767
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002768 // Emitted as int[2];
2769 llvm::Constant *values[2] = {
2770 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2771 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2772 };
2773 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar90d88f92009-03-09 21:49:58 +00002774
2775 const char *Section;
2776 if (ObjCABI == 1)
2777 Section = "__OBJC, __image_info,regular";
2778 else
2779 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002780 llvm::GlobalVariable *GV =
Daniel Dunbar90d88f92009-03-09 21:49:58 +00002781 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
2782 llvm::ConstantArray::get(AT, values, 2),
2783 Section,
2784 0,
2785 true);
2786 GV->setConstant(true);
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002787}
2788
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002789
2790// struct objc_module {
2791// unsigned long version;
2792// unsigned long size;
2793// const char *name;
2794// Symtab symtab;
2795// };
2796
2797// FIXME: Get from somewhere
2798static const int ModuleVersion = 7;
2799
2800void CGObjCMac::EmitModuleInfo() {
Daniel Dunbard8439f22009-01-12 21:08:18 +00002801 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002802
2803 std::vector<llvm::Constant*> Values(4);
2804 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2805 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbarac93e472008-08-15 22:20:32 +00002806 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002807 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002808 Values[3] = EmitModuleSymbols();
Daniel Dunbar90d88f92009-03-09 21:49:58 +00002809 CreateMetadataVar("\01L_OBJC_MODULES",
2810 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
2811 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar56756c32009-03-09 22:18:41 +00002812 4, true);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002813}
2814
2815llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002816 unsigned NumClasses = DefinedClasses.size();
2817 unsigned NumCategories = DefinedCategories.size();
2818
Daniel Dunbar8ede0052008-08-25 06:02:07 +00002819 // Return null if no symbols were defined.
2820 if (!NumClasses && !NumCategories)
2821 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2822
2823 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002824 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2825 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2826 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2827 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2828
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00002829 // The runtime expects exactly the list of defined classes followed
2830 // by the list of defined categories, in a single array.
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002831 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00002832 for (unsigned i=0; i<NumClasses; i++)
2833 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2834 ObjCTypes.Int8PtrTy);
2835 for (unsigned i=0; i<NumCategories; i++)
2836 Symbols[NumClasses + i] =
2837 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2838 ObjCTypes.Int8PtrTy);
2839
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002840 Values[4] =
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00002841 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002842 NumClasses + NumCategories),
2843 Symbols);
2844
2845 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2846
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002847 llvm::GlobalVariable *GV =
Daniel Dunbar90d88f92009-03-09 21:49:58 +00002848 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
2849 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar9dfd3a72009-04-15 02:56:18 +00002850 4, true);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002851 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2852}
2853
Daniel Dunbard916e6e2008-11-01 01:53:16 +00002854llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002855 const ObjCInterfaceDecl *ID) {
Daniel Dunbar8ede0052008-08-25 06:02:07 +00002856 LazySymbols.insert(ID->getIdentifier());
2857
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002858 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2859
2860 if (!Entry) {
2861 llvm::Constant *Casted =
2862 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2863 ObjCTypes.ClassPtrTy);
2864 Entry =
Daniel Dunbar90d88f92009-03-09 21:49:58 +00002865 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
2866 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar9dfd3a72009-04-15 02:56:18 +00002867 4, true);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002868 }
2869
2870 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002871}
2872
Daniel Dunbard916e6e2008-11-01 01:53:16 +00002873llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar5eec6142008-08-12 03:39:23 +00002874 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2875
2876 if (!Entry) {
2877 llvm::Constant *Casted =
2878 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2879 ObjCTypes.SelectorPtrTy);
2880 Entry =
Daniel Dunbar90d88f92009-03-09 21:49:58 +00002881 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
2882 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar9dfd3a72009-04-15 02:56:18 +00002883 4, true);
Daniel Dunbar5eec6142008-08-12 03:39:23 +00002884 }
2885
2886 return Builder.CreateLoad(Entry, false, "tmp");
2887}
2888
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00002889llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002890 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002891
Daniel Dunbar90d88f92009-03-09 21:49:58 +00002892 if (!Entry)
2893 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
2894 llvm::ConstantArray::get(Ident->getName()),
2895 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarfbfd92a2009-04-14 23:14:47 +00002896 1, true);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002897
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002898 return getConstantGEP(Entry, 0, 0);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002899}
2900
Fariborz Jahanian7345eba2009-03-05 19:17:31 +00002901/// GetIvarLayoutName - Returns a unique constant for the given
2902/// ivar layout bitmap.
2903llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
2904 const ObjCCommonTypesHelper &ObjCTypes) {
2905 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
2906}
2907
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00002908void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCInterfaceDecl *OI,
2909 const llvm::StructLayout *Layout,
Fariborz Jahanian37931062009-03-10 16:22:08 +00002910 const RecordDecl *RD,
Chris Lattner9329cf52009-03-31 08:48:01 +00002911 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahanian01b3e342009-03-05 22:39:55 +00002912 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian06facb72009-04-24 16:17:09 +00002913 bool &HasUnion) {
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00002914 bool IsUnion = (RD && RD->isUnion());
2915 uint64_t MaxUnionIvarSize = 0;
2916 uint64_t MaxSkippedUnionIvarSize = 0;
2917 FieldDecl *MaxField = 0;
2918 FieldDecl *MaxSkippedField = 0;
Fariborz Jahanian7e052812009-04-21 18:33:06 +00002919 FieldDecl *LastFieldBitfield = 0;
2920
Chris Lattner9329cf52009-03-31 08:48:01 +00002921 unsigned base = 0;
Fariborz Jahanian37931062009-03-10 16:22:08 +00002922 if (RecFields.empty())
2923 return;
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00002924 if (IsUnion)
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00002925 base = BytePos + GetFieldBaseOffset(OI, Layout, RecFields[0]);
Chris Lattner9329cf52009-03-31 08:48:01 +00002926 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
2927 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
2928
2929 llvm::SmallVector<FieldDecl*, 16> TmpRecFields;
2930
2931 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian37931062009-03-10 16:22:08 +00002932 FieldDecl *Field = RecFields[i];
2933 // Skip over unnamed or bitfields
Fariborz Jahanian7e052812009-04-21 18:33:06 +00002934 if (!Field->getIdentifier() || Field->isBitField()) {
2935 LastFieldBitfield = Field;
Fariborz Jahanian37931062009-03-10 16:22:08 +00002936 continue;
Fariborz Jahanian7e052812009-04-21 18:33:06 +00002937 }
2938 LastFieldBitfield = 0;
Fariborz Jahanian37931062009-03-10 16:22:08 +00002939 QualType FQT = Field->getType();
Fariborz Jahanian738ee712009-03-25 22:36:49 +00002940 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahanian37931062009-03-10 16:22:08 +00002941 if (FQT->isUnionType())
2942 HasUnion = true;
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00002943 else
2944 assert(FQT->isRecordType() &&
2945 "only union/record is supported for ivar layout bitmap");
2946
Fariborz Jahanian37931062009-03-10 16:22:08 +00002947 const RecordType *RT = FQT->getAsRecordType();
2948 const RecordDecl *RD = RT->getDecl();
Daniel Dunbarecb5d402009-04-19 23:41:48 +00002949 // FIXME - Find a more efficient way of passing records down.
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002950 TmpRecFields.append(RD->field_begin(CGM.getContext()),
2951 RD->field_end(CGM.getContext()));
Fariborz Jahanian31614742009-04-20 22:03:45 +00002952 const llvm::Type *Ty = CGM.getTypes().ConvertType(FQT);
2953 const llvm::StructLayout *RecLayout =
2954 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
2955
2956 BuildAggrIvarLayout(0, RecLayout, RD, TmpRecFields,
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00002957 BytePos + GetFieldBaseOffset(OI, Layout, Field),
Fariborz Jahanian06facb72009-04-24 16:17:09 +00002958 ForStrongLayout, HasUnion);
Chris Lattner9329cf52009-03-31 08:48:01 +00002959 TmpRecFields.clear();
Fariborz Jahanian37931062009-03-10 16:22:08 +00002960 continue;
2961 }
Chris Lattner9329cf52009-03-31 08:48:01 +00002962
2963 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00002964 const ConstantArrayType *CArray =
2965 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7e052812009-04-21 18:33:06 +00002966 uint64_t ElCount = CArray->getSize().getZExtValue();
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00002967 assert(CArray && "only array with know element size is supported");
2968 FQT = CArray->getElementType();
Fariborz Jahanian738ee712009-03-25 22:36:49 +00002969 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
2970 const ConstantArrayType *CArray =
2971 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7e052812009-04-21 18:33:06 +00002972 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian738ee712009-03-25 22:36:49 +00002973 FQT = CArray->getElementType();
2974 }
2975
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00002976 assert(!FQT->isUnionType() &&
2977 "layout for array of unions not supported");
2978 if (FQT->isRecordType()) {
Fariborz Jahanian06facb72009-04-24 16:17:09 +00002979 int OldIndex = IvarsInfo.size() - 1;
2980 int OldSkIndex = SkipIvars.size() -1;
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00002981
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00002982 // FIXME - Use a common routine with the above!
2983 const RecordType *RT = FQT->getAsRecordType();
2984 const RecordDecl *RD = RT->getDecl();
2985 // FIXME - Find a more efficiant way of passing records down.
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002986 TmpRecFields.append(RD->field_begin(CGM.getContext()),
2987 RD->field_end(CGM.getContext()));
Fariborz Jahanian31614742009-04-20 22:03:45 +00002988 const llvm::Type *Ty = CGM.getTypes().ConvertType(FQT);
2989 const llvm::StructLayout *RecLayout =
2990 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Chris Lattner9329cf52009-03-31 08:48:01 +00002991
Fariborz Jahanian31614742009-04-20 22:03:45 +00002992 BuildAggrIvarLayout(0, RecLayout, RD,
Chris Lattner9329cf52009-03-31 08:48:01 +00002993 TmpRecFields,
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00002994 BytePos + GetFieldBaseOffset(OI, Layout, Field),
Fariborz Jahanian06facb72009-04-24 16:17:09 +00002995 ForStrongLayout, HasUnion);
Chris Lattner9329cf52009-03-31 08:48:01 +00002996 TmpRecFields.clear();
2997
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00002998 // Replicate layout information for each array element. Note that
2999 // one element is already done.
3000 uint64_t ElIx = 1;
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003001 for (int FirstIndex = IvarsInfo.size() - 1,
3002 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003003 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00003004 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3005 {
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003006 GC_IVAR gcivar;
3007 gcivar.ivar_bytepos = IvarsInfo[i].ivar_bytepos + Size*ElIx;
3008 gcivar.ivar_size = IvarsInfo[i].ivar_size;
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003009 IvarsInfo.push_back(gcivar);
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00003010 }
3011
Chris Lattner9329cf52009-03-31 08:48:01 +00003012 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) {
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003013 GC_IVAR skivar;
3014 skivar.ivar_bytepos = SkipIvars[i].ivar_bytepos + Size*ElIx;
3015 skivar.ivar_size = SkipIvars[i].ivar_size;
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003016 SkipIvars.push_back(skivar);
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00003017 }
3018 }
3019 continue;
3020 }
Fariborz Jahanian37931062009-03-10 16:22:08 +00003021 }
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00003022 // At this point, we are done with Record/Union and array there of.
3023 // For other arrays we are down to its element type.
3024 QualType::GCAttrTypes GCAttr = QualType::GCNone;
3025 do {
3026 if (FQT.isObjCGCStrong() || FQT.isObjCGCWeak()) {
3027 GCAttr = FQT.isObjCGCStrong() ? QualType::Strong : QualType::Weak;
3028 break;
3029 }
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003030 else if (CGM.getContext().isObjCObjectPointerType(FQT)) {
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00003031 GCAttr = QualType::Strong;
3032 break;
3033 }
3034 else if (const PointerType *PT = FQT->getAsPointerType()) {
3035 FQT = PT->getPointeeType();
3036 }
3037 else {
3038 break;
3039 }
3040 } while (true);
Chris Lattner9329cf52009-03-31 08:48:01 +00003041
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00003042 if ((ForStrongLayout && GCAttr == QualType::Strong)
3043 || (!ForStrongLayout && GCAttr == QualType::Weak)) {
3044 if (IsUnion)
3045 {
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003046 uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType())
3047 / WordSizeInBits;
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00003048 if (UnionIvarSize > MaxUnionIvarSize)
3049 {
3050 MaxUnionIvarSize = UnionIvarSize;
3051 MaxField = Field;
3052 }
3053 }
3054 else
3055 {
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003056 GC_IVAR gcivar;
3057 gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field);
3058 gcivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) /
3059 WordSizeInBits;
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003060 IvarsInfo.push_back(gcivar);
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00003061 }
3062 }
3063 else if ((ForStrongLayout &&
3064 (GCAttr == QualType::GCNone || GCAttr == QualType::Weak))
3065 || (!ForStrongLayout && GCAttr != QualType::Weak)) {
3066 if (IsUnion)
3067 {
3068 uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType());
3069 if (UnionIvarSize > MaxSkippedUnionIvarSize)
3070 {
3071 MaxSkippedUnionIvarSize = UnionIvarSize;
3072 MaxSkippedField = Field;
3073 }
3074 }
3075 else
3076 {
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003077 GC_IVAR skivar;
3078 skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field);
3079 skivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) /
Fariborz Jahanian7e052812009-04-21 18:33:06 +00003080 ByteSizeInBits;
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003081 SkipIvars.push_back(skivar);
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00003082 }
3083 }
3084 }
Fariborz Jahanian7e052812009-04-21 18:33:06 +00003085 if (LastFieldBitfield) {
3086 // Last field was a bitfield. Must update skip info.
3087 GC_IVAR skivar;
3088 skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout,
3089 LastFieldBitfield);
3090 Expr *BitWidth = LastFieldBitfield->getBitWidth();
3091 uint64_t BitFieldSize =
Eli Friedman5255e7a2009-04-26 19:19:15 +00003092 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Fariborz Jahanian7e052812009-04-21 18:33:06 +00003093 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3094 + ((BitFieldSize % ByteSizeInBits) != 0);
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003095 SkipIvars.push_back(skivar);
Fariborz Jahanian7e052812009-04-21 18:33:06 +00003096 }
3097
Chris Lattner9329cf52009-03-31 08:48:01 +00003098 if (MaxField) {
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003099 GC_IVAR gcivar;
3100 gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, MaxField);
3101 gcivar.ivar_size = MaxUnionIvarSize;
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003102 IvarsInfo.push_back(gcivar);
Fariborz Jahanian7c0c17b2009-03-11 00:07:04 +00003103 }
Chris Lattner9329cf52009-03-31 08:48:01 +00003104
3105 if (MaxSkippedField) {
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003106 GC_IVAR skivar;
3107 skivar.ivar_bytepos = BytePos +
3108 GetFieldBaseOffset(OI, Layout, MaxSkippedField);
3109 skivar.ivar_size = MaxSkippedUnionIvarSize;
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003110 SkipIvars.push_back(skivar);
Fariborz Jahanian37931062009-03-10 16:22:08 +00003111 }
Fariborz Jahanian01b3e342009-03-05 22:39:55 +00003112}
3113
3114/// BuildIvarLayout - Builds ivar layout bitmap for the class
3115/// implementation for the __strong or __weak case.
3116/// The layout map displays which words in ivar list must be skipped
3117/// and which must be scanned by GC (see below). String is built of bytes.
3118/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3119/// of words to skip and right nibble is count of words to scan. So, each
3120/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3121/// represented by a 0x00 byte which also ends the string.
3122/// 1. when ForStrongLayout is true, following ivars are scanned:
3123/// - id, Class
3124/// - object *
3125/// - __strong anything
3126///
3127/// 2. When ForStrongLayout is false, following ivars are scanned:
3128/// - __weak anything
3129///
Fariborz Jahanian37931062009-03-10 16:22:08 +00003130llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003131 const ObjCImplementationDecl *OMD,
3132 bool ForStrongLayout) {
Fariborz Jahanian01b3e342009-03-05 22:39:55 +00003133 bool hasUnion = false;
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003134
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003135 unsigned int WordsToScan, WordsToSkip;
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003136 const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
3137 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
3138 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian01b3e342009-03-05 22:39:55 +00003139
Chris Lattner9329cf52009-03-31 08:48:01 +00003140 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003141 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahanian01b3e342009-03-05 22:39:55 +00003142 CGM.getContext().CollectObjCIvars(OI, RecFields);
3143 if (RecFields.empty())
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003144 return llvm::Constant::getNullValue(PtrTy);
Chris Lattner9329cf52009-03-31 08:48:01 +00003145
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003146 SkipIvars.clear();
3147 IvarsInfo.clear();
Fariborz Jahanian6d49ab62009-03-11 21:42:00 +00003148
Daniel Dunbare5bb23c2009-04-22 09:39:34 +00003149 const llvm::StructLayout *Layout =
3150 CGM.getTargetData().getStructLayout(GetConcreteClassStruct(CGM, OI));
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003151 BuildAggrIvarLayout(OI, Layout, 0, RecFields, 0, ForStrongLayout, hasUnion);
3152 if (IvarsInfo.empty())
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003153 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003154
3155 // Sort on byte position in case we encounterred a union nested in
3156 // the ivar list.
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003157 if (hasUnion && !IvarsInfo.empty())
Daniel Dunbar48445182009-04-23 01:29:05 +00003158 std::sort(IvarsInfo.begin(), IvarsInfo.end());
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003159 if (hasUnion && !SkipIvars.empty())
Daniel Dunbar48445182009-04-23 01:29:05 +00003160 std::sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003161
3162 // Build the string of skip/scan nibbles
Fariborz Jahanian1fb3c2d2009-04-24 17:15:27 +00003163 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003164 unsigned int WordSize =
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003165 CGM.getTypes().getTargetData().getTypePaddedSize(PtrTy);
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003166 if (IvarsInfo[0].ivar_bytepos == 0) {
3167 WordsToSkip = 0;
3168 WordsToScan = IvarsInfo[0].ivar_size;
3169 }
3170 else {
3171 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3172 WordsToScan = IvarsInfo[0].ivar_size;
3173 }
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003174 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++)
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003175 {
3176 unsigned int TailPrevGCObjC =
3177 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
3178 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC)
3179 {
3180 // consecutive 'scanned' object pointers.
3181 WordsToScan += IvarsInfo[i].ivar_size;
3182 }
3183 else
3184 {
3185 // Skip over 'gc'able object pointer which lay over each other.
3186 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3187 continue;
3188 // Must skip over 1 or more words. We save current skip/scan values
3189 // and start a new pair.
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003190 SKIP_SCAN SkScan;
3191 SkScan.skip = WordsToSkip;
3192 SkScan.scan = WordsToScan;
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003193 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003194
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003195 // Skip the hole.
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003196 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3197 SkScan.scan = 0;
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003198 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003199 WordsToSkip = 0;
3200 WordsToScan = IvarsInfo[i].ivar_size;
3201 }
3202 }
3203 if (WordsToScan > 0)
3204 {
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003205 SKIP_SCAN SkScan;
3206 SkScan.skip = WordsToSkip;
3207 SkScan.scan = WordsToScan;
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003208 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003209 }
3210
3211 bool BytesSkipped = false;
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003212 if (!SkipIvars.empty())
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003213 {
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003214 unsigned int LastIndex = SkipIvars.size()-1;
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003215 int LastByteSkipped =
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003216 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
3217 LastIndex = IvarsInfo.size()-1;
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003218 int LastByteScanned =
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003219 IvarsInfo[LastIndex].ivar_bytepos +
3220 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003221 BytesSkipped = (LastByteSkipped > LastByteScanned);
3222 // Compute number of bytes to skip at the tail end of the last ivar scanned.
3223 if (BytesSkipped)
3224 {
3225 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003226 SKIP_SCAN SkScan;
3227 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3228 SkScan.scan = 0;
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003229 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003230 }
3231 }
3232 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3233 // as 0xMN.
Fariborz Jahanian06facb72009-04-24 16:17:09 +00003234 int SkipScan = SkipScanIvars.size()-1;
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003235 for (int i = 0; i <= SkipScan; i++)
3236 {
3237 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3238 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3239 // 0xM0 followed by 0x0N detected.
3240 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3241 for (int j = i+1; j < SkipScan; j++)
3242 SkipScanIvars[j] = SkipScanIvars[j+1];
3243 --SkipScan;
3244 }
3245 }
3246
3247 // Generate the string.
3248 std::string BitMap;
3249 for (int i = 0; i <= SkipScan; i++)
3250 {
3251 unsigned char byte;
3252 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3253 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3254 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3255 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
3256
3257 if (skip_small > 0 || skip_big > 0)
3258 BytesSkipped = true;
3259 // first skip big.
3260 for (unsigned int ix = 0; ix < skip_big; ix++)
3261 BitMap += (unsigned char)(0xf0);
3262
3263 // next (skip small, scan)
3264 if (skip_small)
3265 {
3266 byte = skip_small << 4;
3267 if (scan_big > 0)
3268 {
3269 byte |= 0xf;
3270 --scan_big;
3271 }
3272 else if (scan_small)
3273 {
3274 byte |= scan_small;
3275 scan_small = 0;
3276 }
3277 BitMap += byte;
3278 }
3279 // next scan big
3280 for (unsigned int ix = 0; ix < scan_big; ix++)
3281 BitMap += (unsigned char)(0x0f);
3282 // last scan small
3283 if (scan_small)
3284 {
3285 byte = scan_small;
3286 BitMap += byte;
3287 }
3288 }
3289 // null terminate string.
Fariborz Jahanian738ee712009-03-25 22:36:49 +00003290 unsigned char zero = 0;
3291 BitMap += zero;
Fariborz Jahanian31614742009-04-20 22:03:45 +00003292
3293 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
3294 printf("\n%s ivar layout for class '%s': ",
3295 ForStrongLayout ? "strong" : "weak",
3296 OMD->getClassInterface()->getNameAsCString());
3297 const unsigned char *s = (unsigned char*)BitMap.c_str();
3298 for (unsigned i = 0; i < BitMap.size(); i++)
3299 if (!(s[i] & 0xf0))
3300 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3301 else
3302 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3303 printf("\n");
3304 }
3305
Fariborz Jahaniandf1d9032009-03-11 20:59:05 +00003306 // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as
3307 // final layout.
3308 if (ForStrongLayout && !BytesSkipped)
Fariborz Jahaniand0e808a2009-03-12 22:50:49 +00003309 return llvm::Constant::getNullValue(PtrTy);
3310 llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3311 llvm::ConstantArray::get(BitMap.c_str()),
3312 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarfbfd92a2009-04-14 23:14:47 +00003313 1, true);
Fariborz Jahanian31614742009-04-20 22:03:45 +00003314 return getConstantGEP(Entry, 0, 0);
Fariborz Jahanian01b3e342009-03-05 22:39:55 +00003315}
3316
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00003317llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar5eec6142008-08-12 03:39:23 +00003318 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3319
Daniel Dunbar90d88f92009-03-09 21:49:58 +00003320 // FIXME: Avoid std::string copying.
3321 if (!Entry)
3322 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
3323 llvm::ConstantArray::get(Sel.getAsString()),
3324 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarfbfd92a2009-04-14 23:14:47 +00003325 1, true);
Daniel Dunbar5eec6142008-08-12 03:39:23 +00003326
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003327 return getConstantGEP(Entry, 0, 0);
3328}
3329
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003330// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00003331llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003332 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3333}
3334
3335// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00003336llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003337 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
3338}
3339
Daniel Dunbar356f0742009-04-20 06:54:31 +00003340llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel593a07a2009-03-04 18:21:39 +00003341 std::string TypeStr;
3342 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3343
3344 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003345
Daniel Dunbar90d88f92009-03-09 21:49:58 +00003346 if (!Entry)
3347 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
3348 llvm::ConstantArray::get(TypeStr),
3349 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarfbfd92a2009-04-14 23:14:47 +00003350 1, true);
Daniel Dunbar90d88f92009-03-09 21:49:58 +00003351
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003352 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar5eec6142008-08-12 03:39:23 +00003353}
3354
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00003355llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003356 std::string TypeStr;
Daniel Dunbar12996f52008-08-26 21:51:14 +00003357 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3358 TypeStr);
Devang Patel593a07a2009-03-04 18:21:39 +00003359
3360 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3361
Daniel Dunbarfbfd92a2009-04-14 23:14:47 +00003362 if (!Entry)
3363 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
3364 llvm::ConstantArray::get(TypeStr),
3365 "__TEXT,__cstring,cstring_literals",
3366 1, true);
Devang Patel593a07a2009-03-04 18:21:39 +00003367
3368 return getConstantGEP(Entry, 0, 0);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003369}
3370
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00003371// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00003372llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00003373 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
3374
Daniel Dunbar90d88f92009-03-09 21:49:58 +00003375 if (!Entry)
3376 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
3377 llvm::ConstantArray::get(Ident->getName()),
3378 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarfbfd92a2009-04-14 23:14:47 +00003379 1, true);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00003380
3381 return getConstantGEP(Entry, 0, 0);
3382}
3383
3384// FIXME: Merge into a single cstring creation function.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00003385// FIXME: This Decl should be more precise.
Daniel Dunbar90d88f92009-03-09 21:49:58 +00003386llvm::Constant *
3387 CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3388 const Decl *Container) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00003389 std::string TypeStr;
3390 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00003391 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3392}
3393
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00003394void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
3395 const ObjCContainerDecl *CD,
3396 std::string &NameOut) {
Daniel Dunbara2d275d2009-04-07 05:48:37 +00003397 NameOut = '\01';
3398 NameOut += (D->isInstanceMethod() ? '-' : '+');
Chris Lattner3a8f2942008-11-24 03:33:13 +00003399 NameOut += '[';
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +00003400 assert (CD && "Missing container decl in GetNameForMethod");
3401 NameOut += CD->getNameAsString();
Fariborz Jahanian6e4b7372009-04-16 18:34:20 +00003402 if (const ObjCCategoryImplDecl *CID =
3403 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) {
3404 NameOut += '(';
3405 NameOut += CID->getNameAsString();
3406 NameOut+= ')';
3407 }
Chris Lattner3a8f2942008-11-24 03:33:13 +00003408 NameOut += ' ';
3409 NameOut += D->getSelector().getAsString();
3410 NameOut += ']';
Daniel Dunbarace33292008-08-16 03:19:19 +00003411}
3412
Daniel Dunbar1be1df32008-08-11 21:35:06 +00003413void CGObjCMac::FinishModule() {
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00003414 EmitModuleInfo();
3415
Daniel Dunbar35b777f2008-10-29 22:36:39 +00003416 // Emit the dummy bodies for any protocols which were referenced but
3417 // never defined.
3418 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
3419 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
3420 if (i->second->hasInitializer())
3421 continue;
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00003422
Daniel Dunbar35b777f2008-10-29 22:36:39 +00003423 std::vector<llvm::Constant*> Values(5);
3424 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
3425 Values[1] = GetClassName(i->first);
3426 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
3427 Values[3] = Values[4] =
3428 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
3429 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
3430 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
3431 Values));
3432 }
3433
3434 std::vector<llvm::Constant*> Used;
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00003435 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbar1be1df32008-08-11 21:35:06 +00003436 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003437 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbar1be1df32008-08-11 21:35:06 +00003438 }
3439
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003440 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbar1be1df32008-08-11 21:35:06 +00003441 llvm::GlobalValue *GV =
3442 new llvm::GlobalVariable(AT, false,
3443 llvm::GlobalValue::AppendingLinkage,
3444 llvm::ConstantArray::get(AT, Used),
3445 "llvm.used",
3446 &CGM.getModule());
3447
3448 GV->setSection("llvm.metadata");
Daniel Dunbar8ede0052008-08-25 06:02:07 +00003449
3450 // Add assembler directives to add lazy undefined symbol references
3451 // for classes which are referenced but not defined. This is
3452 // important for correct linker interaction.
3453
3454 // FIXME: Uh, this isn't particularly portable.
3455 std::stringstream s;
Anders Carlsson63f98352008-12-10 02:21:04 +00003456
3457 if (!CGM.getModule().getModuleInlineAsm().empty())
3458 s << "\n";
3459
Daniel Dunbar8ede0052008-08-25 06:02:07 +00003460 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
3461 e = LazySymbols.end(); i != e; ++i) {
3462 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
3463 }
3464 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
3465 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00003466 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar8ede0052008-08-25 06:02:07 +00003467 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
3468 }
Anders Carlsson63f98352008-12-10 02:21:04 +00003469
Daniel Dunbar8ede0052008-08-25 06:02:07 +00003470 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbar1be1df32008-08-11 21:35:06 +00003471}
3472
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003473CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian48543f52009-01-21 22:04:16 +00003474 : CGObjCCommonMac(cgm),
3475 ObjCTypes(cgm)
3476{
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003477 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian48543f52009-01-21 22:04:16 +00003478 ObjCABI = 2;
3479}
3480
Daniel Dunbar1be1df32008-08-11 21:35:06 +00003481/* *** */
3482
Fariborz Jahanian48543f52009-01-21 22:04:16 +00003483ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
3484: CGM(cgm)
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00003485{
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00003486 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3487 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian48543f52009-01-21 22:04:16 +00003488
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003489 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003490 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00003491 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanianad51ca02009-03-23 19:10:40 +00003492 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003493 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
3494
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00003495 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanianc192d4d2008-11-18 20:18:11 +00003496 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00003497 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003498
3499 // FIXME: It would be nice to unify this with the opaque type, so
3500 // that the IR comes out a bit cleaner.
3501 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
3502 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanian48543f52009-01-21 22:04:16 +00003503
3504 // I'm not sure I like this. The implicit coordination is a bit
3505 // gross. We should solve this in a reasonable fashion because this
3506 // is a pretty common task (match some runtime data structure with
3507 // an LLVM data structure).
3508
3509 // FIXME: This is leaked.
3510 // FIXME: Merge with rewriter code?
3511
3512 // struct _objc_super {
3513 // id self;
3514 // Class cls;
3515 // }
3516 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3517 SourceLocation(),
3518 &Ctx.Idents.get("_objc_super"));
Douglas Gregorc55b0b02009-04-09 21:40:53 +00003519 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3520 Ctx.getObjCIdType(), 0, false));
3521 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3522 Ctx.getObjCClassType(), 0, false));
Fariborz Jahanian48543f52009-01-21 22:04:16 +00003523 RD->completeDefinition(Ctx);
3524
3525 SuperCTy = Ctx.getTagDeclType(RD);
3526 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
3527
3528 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahanian4b161702009-01-22 00:37:21 +00003529 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
3530
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003531 // struct _prop_t {
3532 // char *name;
3533 // char *attributes;
3534 // }
Chris Lattnerada416b2009-04-22 02:53:24 +00003535 PropertyTy = llvm::StructType::get(Int8PtrTy, Int8PtrTy, NULL);
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003536 CGM.getModule().addTypeName("struct._prop_t",
3537 PropertyTy);
3538
3539 // struct _prop_list_t {
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003540 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003541 // uint32_t count_of_properties;
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003542 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003543 // }
3544 PropertyListTy = llvm::StructType::get(IntTy,
3545 IntTy,
3546 llvm::ArrayType::get(PropertyTy, 0),
3547 NULL);
3548 CGM.getModule().addTypeName("struct._prop_list_t",
3549 PropertyListTy);
3550 // struct _prop_list_t *
3551 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
3552
3553 // struct _objc_method {
3554 // SEL _cmd;
3555 // char *method_type;
3556 // char *_imp;
3557 // }
3558 MethodTy = llvm::StructType::get(SelectorPtrTy,
3559 Int8PtrTy,
3560 Int8PtrTy,
3561 NULL);
3562 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003563
3564 // struct _objc_cache *
3565 CacheTy = llvm::OpaqueType::get();
3566 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
3567 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian48543f52009-01-21 22:04:16 +00003568}
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00003569
Fariborz Jahanian48543f52009-01-21 22:04:16 +00003570ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
3571 : ObjCCommonTypesHelper(cgm)
3572{
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003573 // struct _objc_method_description {
3574 // SEL name;
3575 // char *types;
3576 // }
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003577 MethodDescriptionTy =
3578 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003579 Int8PtrTy,
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003580 NULL);
3581 CGM.getModule().addTypeName("struct._objc_method_description",
3582 MethodDescriptionTy);
3583
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003584 // struct _objc_method_description_list {
3585 // int count;
3586 // struct _objc_method_description[1];
3587 // }
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003588 MethodDescriptionListTy =
3589 llvm::StructType::get(IntTy,
3590 llvm::ArrayType::get(MethodDescriptionTy, 0),
3591 NULL);
3592 CGM.getModule().addTypeName("struct._objc_method_description_list",
3593 MethodDescriptionListTy);
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003594
3595 // struct _objc_method_description_list *
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003596 MethodDescriptionListPtrTy =
3597 llvm::PointerType::getUnqual(MethodDescriptionListTy);
3598
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003599 // Protocol description structures
3600
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003601 // struct _objc_protocol_extension {
3602 // uint32_t size; // sizeof(struct _objc_protocol_extension)
3603 // struct _objc_method_description_list *optional_instance_methods;
3604 // struct _objc_method_description_list *optional_class_methods;
3605 // struct _objc_property_list *instance_properties;
3606 // }
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003607 ProtocolExtensionTy =
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003608 llvm::StructType::get(IntTy,
3609 MethodDescriptionListPtrTy,
3610 MethodDescriptionListPtrTy,
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003611 PropertyListPtrTy,
3612 NULL);
3613 CGM.getModule().addTypeName("struct._objc_protocol_extension",
3614 ProtocolExtensionTy);
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003615
3616 // struct _objc_protocol_extension *
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003617 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
3618
Daniel Dunbar35b777f2008-10-29 22:36:39 +00003619 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003620
3621 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
3622 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3623
Fariborz Jahanian48543f52009-01-21 22:04:16 +00003624 const llvm::Type *T =
3625 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
3626 LongTy,
3627 llvm::ArrayType::get(ProtocolTyHolder, 0),
3628 NULL);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003629 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
3630
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003631 // struct _objc_protocol {
3632 // struct _objc_protocol_extension *isa;
3633 // char *protocol_name;
3634 // struct _objc_protocol **_objc_protocol_list;
3635 // struct _objc_method_description_list *instance_methods;
3636 // struct _objc_method_description_list *class_methods;
3637 // }
3638 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003639 Int8PtrTy,
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003640 llvm::PointerType::getUnqual(ProtocolListTyHolder),
3641 MethodDescriptionListPtrTy,
3642 MethodDescriptionListPtrTy,
3643 NULL);
3644 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
3645
3646 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
3647 CGM.getModule().addTypeName("struct._objc_protocol_list",
3648 ProtocolListTy);
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003649 // struct _objc_protocol_list *
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003650 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
3651
3652 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003653 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00003654 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003655
3656 // Class description structures
3657
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003658 // struct _objc_ivar {
3659 // char *ivar_name;
3660 // char *ivar_type;
3661 // int ivar_offset;
3662 // }
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003663 IvarTy = llvm::StructType::get(Int8PtrTy,
3664 Int8PtrTy,
3665 IntTy,
3666 NULL);
3667 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
3668
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003669 // struct _objc_ivar_list *
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003670 IvarListTy = llvm::OpaqueType::get();
3671 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
3672 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
3673
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003674 // struct _objc_method_list *
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003675 MethodListTy = llvm::OpaqueType::get();
3676 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
3677 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
3678
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003679 // struct _objc_class_extension *
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003680 ClassExtensionTy =
3681 llvm::StructType::get(IntTy,
3682 Int8PtrTy,
3683 PropertyListPtrTy,
3684 NULL);
3685 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
3686 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
3687
3688 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3689
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003690 // struct _objc_class {
3691 // Class isa;
3692 // Class super_class;
3693 // char *name;
3694 // long version;
3695 // long info;
3696 // long instance_size;
3697 // struct _objc_ivar_list *ivars;
3698 // struct _objc_method_list *methods;
3699 // struct _objc_cache *cache;
3700 // struct _objc_protocol_list *protocols;
3701 // char *ivar_layout;
3702 // struct _objc_class_ext *ext;
3703 // };
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003704 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3705 llvm::PointerType::getUnqual(ClassTyHolder),
3706 Int8PtrTy,
3707 LongTy,
3708 LongTy,
3709 LongTy,
3710 IvarListPtrTy,
3711 MethodListPtrTy,
3712 CachePtrTy,
3713 ProtocolListPtrTy,
3714 Int8PtrTy,
3715 ClassExtensionPtrTy,
3716 NULL);
3717 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
3718
3719 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
3720 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
3721 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
3722
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003723 // struct _objc_category {
3724 // char *category_name;
3725 // char *class_name;
3726 // struct _objc_method_list *instance_method;
3727 // struct _objc_method_list *class_method;
3728 // uint32_t size; // sizeof(struct _objc_category)
3729 // struct _objc_property_list *instance_properties;// category's @property
3730 // }
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00003731 CategoryTy = llvm::StructType::get(Int8PtrTy,
3732 Int8PtrTy,
3733 MethodListPtrTy,
3734 MethodListPtrTy,
3735 ProtocolListPtrTy,
3736 IntTy,
3737 PropertyListPtrTy,
3738 NULL);
3739 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
3740
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003741 // Global metadata structures
3742
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00003743 // struct _objc_symtab {
3744 // long sel_ref_cnt;
3745 // SEL *refs;
3746 // short cls_def_cnt;
3747 // short cat_def_cnt;
3748 // char *defs[cls_def_cnt + cat_def_cnt];
3749 // }
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003750 SymtabTy = llvm::StructType::get(LongTy,
3751 SelectorPtrTy,
3752 ShortTy,
3753 ShortTy,
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00003754 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003755 NULL);
3756 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
3757 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
3758
Fariborz Jahanian4b161702009-01-22 00:37:21 +00003759 // struct _objc_module {
3760 // long version;
3761 // long size; // sizeof(struct _objc_module)
3762 // char *name;
3763 // struct _objc_symtab* symtab;
3764 // }
Daniel Dunbarb050fa62008-08-21 04:36:09 +00003765 ModuleTy =
3766 llvm::StructType::get(LongTy,
3767 LongTy,
3768 Int8PtrTy,
3769 SymtabPtrTy,
3770 NULL);
3771 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar87062ff2008-08-23 09:25:55 +00003772
Anders Carlsson58d16242008-08-31 04:05:03 +00003773
Anders Carlsson9acb0a42008-09-09 10:10:21 +00003774 // FIXME: This is the size of the setjmp buffer and should be
3775 // target specific. 18 is what's used on 32-bit X86.
3776 uint64_t SetJmpBufferSize = 18;
3777
3778 // Exceptions
3779 const llvm::Type *StackPtrTy =
Daniel Dunbar1c5e4632008-09-27 06:32:25 +00003780 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson9acb0a42008-09-09 10:10:21 +00003781
3782 ExceptionDataTy =
3783 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
3784 SetJmpBufferSize),
3785 StackPtrTy, NULL);
3786 CGM.getModule().addTypeName("struct._objc_exception_data",
3787 ExceptionDataTy);
3788
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00003789}
3790
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003791ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian48543f52009-01-21 22:04:16 +00003792: ObjCCommonTypesHelper(cgm)
3793{
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003794 // struct _method_list_t {
3795 // uint32_t entsize; // sizeof(struct _objc_method)
3796 // uint32_t method_count;
3797 // struct _objc_method method_list[method_count];
3798 // }
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003799 MethodListnfABITy = llvm::StructType::get(IntTy,
3800 IntTy,
3801 llvm::ArrayType::get(MethodTy, 0),
3802 NULL);
3803 CGM.getModule().addTypeName("struct.__method_list_t",
3804 MethodListnfABITy);
3805 // struct method_list_t *
3806 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003807
3808 // struct _protocol_t {
3809 // id isa; // NULL
3810 // const char * const protocol_name;
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003811 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003812 // const struct method_list_t * const instance_methods;
3813 // const struct method_list_t * const class_methods;
3814 // const struct method_list_t *optionalInstanceMethods;
3815 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003816 // const struct _prop_list_t * properties;
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003817 // const uint32_t size; // sizeof(struct _protocol_t)
3818 // const uint32_t flags; // = 0
3819 // }
3820
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003821 // Holder for struct _protocol_list_t *
3822 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3823
3824 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3825 Int8PtrTy,
3826 llvm::PointerType::getUnqual(
3827 ProtocolListTyHolder),
3828 MethodListnfABIPtrTy,
3829 MethodListnfABIPtrTy,
3830 MethodListnfABIPtrTy,
3831 MethodListnfABIPtrTy,
3832 PropertyListPtrTy,
3833 IntTy,
3834 IntTy,
3835 NULL);
3836 CGM.getModule().addTypeName("struct._protocol_t",
3837 ProtocolnfABITy);
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00003838
3839 // struct _protocol_t*
3840 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003841
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00003842 // struct _protocol_list_t {
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003843 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00003844 // struct _protocol_t *[protocol_count];
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003845 // }
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003846 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3847 llvm::ArrayType::get(
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00003848 ProtocolnfABIPtrTy, 0),
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003849 NULL);
3850 CGM.getModule().addTypeName("struct._objc_protocol_list",
3851 ProtocolListnfABITy);
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00003852 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3853 ProtocolListnfABITy);
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003854
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003855 // struct _objc_protocol_list*
3856 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003857
3858 // struct _ivar_t {
3859 // unsigned long int *offset; // pointer to ivar offset location
3860 // char *name;
3861 // char *type;
3862 // uint32_t alignment;
3863 // uint32_t size;
3864 // }
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003865 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3866 Int8PtrTy,
3867 Int8PtrTy,
3868 IntTy,
3869 IntTy,
3870 NULL);
3871 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3872
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003873 // struct _ivar_list_t {
3874 // uint32 entsize; // sizeof(struct _ivar_t)
3875 // uint32 count;
3876 // struct _iver_t list[count];
3877 // }
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003878 IvarListnfABITy = llvm::StructType::get(IntTy,
3879 IntTy,
3880 llvm::ArrayType::get(
3881 IvarnfABITy, 0),
3882 NULL);
3883 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3884
3885 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003886
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003887 // struct _class_ro_t {
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003888 // uint32_t const flags;
3889 // uint32_t const instanceStart;
3890 // uint32_t const instanceSize;
3891 // uint32_t const reserved; // only when building for 64bit targets
3892 // const uint8_t * const ivarLayout;
3893 // const char *const name;
3894 // const struct _method_list_t * const baseMethods;
3895 // const struct _objc_protocol_list *const baseProtocols;
3896 // const struct _ivar_list_t *const ivars;
3897 // const uint8_t * const weakIvarLayout;
3898 // const struct _prop_list_t * const properties;
3899 // }
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003900
3901 // FIXME. Add 'reserved' field in 64bit abi mode!
3902 ClassRonfABITy = llvm::StructType::get(IntTy,
3903 IntTy,
3904 IntTy,
3905 Int8PtrTy,
3906 Int8PtrTy,
3907 MethodListnfABIPtrTy,
3908 ProtocolListnfABIPtrTy,
3909 IvarListnfABIPtrTy,
3910 Int8PtrTy,
3911 PropertyListPtrTy,
3912 NULL);
3913 CGM.getModule().addTypeName("struct._class_ro_t",
3914 ClassRonfABITy);
3915
3916 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3917 std::vector<const llvm::Type*> Params;
3918 Params.push_back(ObjectPtrTy);
3919 Params.push_back(SelectorPtrTy);
3920 ImpnfABITy = llvm::PointerType::getUnqual(
3921 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3922
3923 // struct _class_t {
3924 // struct _class_t *isa;
3925 // struct _class_t * const superclass;
3926 // void *cache;
3927 // IMP *vtable;
3928 // struct class_ro_t *ro;
3929 // }
3930
3931 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3932 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3933 llvm::PointerType::getUnqual(ClassTyHolder),
3934 CachePtrTy,
3935 llvm::PointerType::getUnqual(ImpnfABITy),
3936 llvm::PointerType::getUnqual(
3937 ClassRonfABITy),
3938 NULL);
3939 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3940
3941 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3942 ClassnfABITy);
3943
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003944 // LLVM for struct _class_t *
3945 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3946
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003947 // struct _category_t {
3948 // const char * const name;
3949 // struct _class_t *const cls;
3950 // const struct _method_list_t * const instance_methods;
3951 // const struct _method_list_t * const class_methods;
3952 // const struct _protocol_list_t * const protocols;
3953 // const struct _prop_list_t * const properties;
Fariborz Jahanianb9459b72009-01-23 17:41:22 +00003954 // }
3955 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003956 ClassnfABIPtrTy,
Fariborz Jahanianb9459b72009-01-23 17:41:22 +00003957 MethodListnfABIPtrTy,
3958 MethodListnfABIPtrTy,
3959 ProtocolListnfABIPtrTy,
3960 PropertyListPtrTy,
3961 NULL);
3962 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +00003963
3964 // New types for nonfragile abi messaging.
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00003965 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3966 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +00003967
3968 // MessageRefTy - LLVM for:
3969 // struct _message_ref_t {
3970 // IMP messenger;
3971 // SEL name;
3972 // };
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00003973
3974 // First the clang type for struct _message_ref_t
3975 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3976 SourceLocation(),
3977 &Ctx.Idents.get("_message_ref_t"));
Douglas Gregorc55b0b02009-04-09 21:40:53 +00003978 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3979 Ctx.VoidPtrTy, 0, false));
3980 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3981 Ctx.getObjCSelType(), 0, false));
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00003982 RD->completeDefinition(Ctx);
3983
3984 MessageRefCTy = Ctx.getTagDeclType(RD);
3985 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
3986 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +00003987
3988 // MessageRefPtrTy - LLVM for struct _message_ref_t*
3989 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
3990
3991 // SuperMessageRefTy - LLVM for:
3992 // struct _super_message_ref_t {
3993 // SUPER_IMP messenger;
3994 // SEL name;
3995 // };
3996 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy,
3997 SelectorPtrTy,
3998 NULL);
3999 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
4000
4001 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
4002 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4003
Daniel Dunbar9c285e72009-03-01 04:46:24 +00004004
4005 // struct objc_typeinfo {
4006 // const void** vtable; // objc_ehtype_vtable + 2
4007 // const char* name; // c++ typeinfo string
4008 // Class cls;
4009 // };
4010 EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy),
4011 Int8PtrTy,
4012 ClassnfABIPtrTy,
4013 NULL);
Daniel Dunbarc0318b22009-03-02 06:08:11 +00004014 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Daniel Dunbar9c285e72009-03-01 04:46:24 +00004015 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00004016}
4017
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00004018llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
4019 FinishNonFragileABIModule();
4020
4021 return NULL;
4022}
4023
4024void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4025 // nonfragile abi has no module definition.
Fariborz Jahanian11c93dd2009-01-30 20:55:31 +00004026
4027 // Build list of all implemented classe addresses in array
4028 // L_OBJC_LABEL_CLASS_$.
4029 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
4030 // list of 'nonlazy' implementations (defined as those with a +load{}
4031 // method!!).
4032 unsigned NumClasses = DefinedClasses.size();
4033 if (NumClasses) {
4034 std::vector<llvm::Constant*> Symbols(NumClasses);
4035 for (unsigned i=0; i<NumClasses; i++)
4036 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
4037 ObjCTypes.Int8PtrTy);
4038 llvm::Constant* Init =
4039 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
4040 NumClasses),
4041 Symbols);
4042
4043 llvm::GlobalVariable *GV =
4044 new llvm::GlobalVariable(Init->getType(), false,
4045 llvm::GlobalValue::InternalLinkage,
4046 Init,
4047 "\01L_OBJC_LABEL_CLASS_$",
4048 &CGM.getModule());
Daniel Dunbar56756c32009-03-09 22:18:41 +00004049 GV->setAlignment(8);
Fariborz Jahanian11c93dd2009-01-30 20:55:31 +00004050 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
4051 UsedGlobals.push_back(GV);
4052 }
4053
4054 // Build list of all implemented category addresses in array
4055 // L_OBJC_LABEL_CATEGORY_$.
4056 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
4057 // list of 'nonlazy' category implementations (defined as those with a +load{}
4058 // method!!).
4059 unsigned NumCategory = DefinedCategories.size();
4060 if (NumCategory) {
4061 std::vector<llvm::Constant*> Symbols(NumCategory);
4062 for (unsigned i=0; i<NumCategory; i++)
4063 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
4064 ObjCTypes.Int8PtrTy);
4065 llvm::Constant* Init =
4066 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
4067 NumCategory),
4068 Symbols);
4069
4070 llvm::GlobalVariable *GV =
4071 new llvm::GlobalVariable(Init->getType(), false,
4072 llvm::GlobalValue::InternalLinkage,
4073 Init,
4074 "\01L_OBJC_LABEL_CATEGORY_$",
4075 &CGM.getModule());
Daniel Dunbar56756c32009-03-09 22:18:41 +00004076 GV->setAlignment(8);
Fariborz Jahanian11c93dd2009-01-30 20:55:31 +00004077 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
4078 UsedGlobals.push_back(GV);
4079 }
4080
Fariborz Jahanian5b2f5502009-01-30 22:07:48 +00004081 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
4082 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
4083 std::vector<llvm::Constant*> Values(2);
4084 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
Fariborz Jahanian4d7933a2009-02-24 21:08:09 +00004085 unsigned int flags = 0;
Fariborz Jahanian27f58962009-02-24 23:34:44 +00004086 // FIXME: Fix and continue?
4087 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
4088 flags |= eImageInfo_GarbageCollected;
4089 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
4090 flags |= eImageInfo_GCOnly;
Fariborz Jahanian4d7933a2009-02-24 21:08:09 +00004091 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
Fariborz Jahanian5b2f5502009-01-30 22:07:48 +00004092 llvm::Constant* Init = llvm::ConstantArray::get(
4093 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
4094 Values);
4095 llvm::GlobalVariable *IMGV =
4096 new llvm::GlobalVariable(Init->getType(), false,
4097 llvm::GlobalValue::InternalLinkage,
4098 Init,
4099 "\01L_OBJC_IMAGE_INFO",
4100 &CGM.getModule());
4101 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
Daniel Dunbarac277992009-04-23 08:03:21 +00004102 IMGV->setConstant(true);
Fariborz Jahanian5b2f5502009-01-30 22:07:48 +00004103 UsedGlobals.push_back(IMGV);
4104
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00004105 std::vector<llvm::Constant*> Used;
Fariborz Jahanianab438842009-04-14 18:41:56 +00004106
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00004107 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
4108 e = UsedGlobals.end(); i != e; ++i) {
4109 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
4110 }
Fariborz Jahanianab438842009-04-14 18:41:56 +00004111
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00004112 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
4113 llvm::GlobalValue *GV =
4114 new llvm::GlobalVariable(AT, false,
4115 llvm::GlobalValue::AppendingLinkage,
4116 llvm::ConstantArray::get(AT, Used),
4117 "llvm.used",
4118 &CGM.getModule());
4119
4120 GV->setSection("llvm.metadata");
4121
4122}
4123
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004124// Metadata flags
4125enum MetaDataDlags {
4126 CLS = 0x0,
4127 CLS_META = 0x1,
4128 CLS_ROOT = 0x2,
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004129 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004130 CLS_EXCEPTION = 0x20
4131};
4132/// BuildClassRoTInitializer - generate meta-data for:
4133/// struct _class_ro_t {
4134/// uint32_t const flags;
4135/// uint32_t const instanceStart;
4136/// uint32_t const instanceSize;
4137/// uint32_t const reserved; // only when building for 64bit targets
4138/// const uint8_t * const ivarLayout;
4139/// const char *const name;
4140/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004141/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004142/// const struct _ivar_list_t *const ivars;
4143/// const uint8_t * const weakIvarLayout;
4144/// const struct _prop_list_t * const properties;
4145/// }
4146///
4147llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
4148 unsigned flags,
4149 unsigned InstanceStart,
4150 unsigned InstanceSize,
4151 const ObjCImplementationDecl *ID) {
4152 std::string ClassName = ID->getNameAsString();
4153 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
4154 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4155 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4156 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
4157 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanian31b96492009-04-22 23:00:43 +00004158 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4159 : BuildIvarLayout(ID, true);
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004160 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004161 // const struct _method_list_t * const baseMethods;
4162 std::vector<llvm::Constant*> Methods;
4163 std::string MethodListName("\01l_OBJC_$_");
4164 if (flags & CLS_META) {
4165 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Douglas Gregorcd19b572009-04-23 01:02:12 +00004166 for (ObjCImplementationDecl::classmeth_iterator
4167 i = ID->classmeth_begin(CGM.getContext()),
4168 e = ID->classmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004169 // Class methods should always be defined.
4170 Methods.push_back(GetMethodConstant(*i));
4171 }
4172 } else {
4173 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Douglas Gregorcd19b572009-04-23 01:02:12 +00004174 for (ObjCImplementationDecl::instmeth_iterator
4175 i = ID->instmeth_begin(CGM.getContext()),
4176 e = ID->instmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004177 // Instance methods should always be defined.
4178 Methods.push_back(GetMethodConstant(*i));
4179 }
Douglas Gregorcd19b572009-04-23 01:02:12 +00004180 for (ObjCImplementationDecl::propimpl_iterator
4181 i = ID->propimpl_begin(CGM.getContext()),
4182 e = ID->propimpl_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanian78355ec2009-01-28 22:46:49 +00004183 ObjCPropertyImplDecl *PID = *i;
4184
4185 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4186 ObjCPropertyDecl *PD = PID->getPropertyDecl();
4187
4188 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4189 if (llvm::Constant *C = GetMethodConstant(MD))
4190 Methods.push_back(C);
4191 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4192 if (llvm::Constant *C = GetMethodConstant(MD))
4193 Methods.push_back(C);
4194 }
4195 }
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004196 }
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004197 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004198 "__DATA, __objc_const", Methods);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004199
4200 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4201 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
4202 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
4203 + OID->getNameAsString(),
4204 OID->protocol_begin(),
4205 OID->protocol_end());
4206
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004207 if (flags & CLS_META)
4208 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4209 else
4210 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanian31b96492009-04-22 23:00:43 +00004211 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4212 : BuildIvarLayout(ID, false);
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00004213 if (flags & CLS_META)
4214 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
4215 else
4216 Values[ 9] =
4217 EmitPropertyList(
4218 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
4219 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004220 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
4221 Values);
4222 llvm::GlobalVariable *CLASS_RO_GV =
4223 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
4224 llvm::GlobalValue::InternalLinkage,
4225 Init,
4226 (flags & CLS_META) ?
4227 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4228 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
4229 &CGM.getModule());
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00004230 CLASS_RO_GV->setAlignment(
4231 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004232 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004233 return CLASS_RO_GV;
Fariborz Jahanianc98c87b2009-01-26 22:58:07 +00004234
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004235}
4236
4237/// BuildClassMetaData - This routine defines that to-level meta-data
4238/// for the given ClassName for:
4239/// struct _class_t {
4240/// struct _class_t *isa;
4241/// struct _class_t * const superclass;
4242/// void *cache;
4243/// IMP *vtable;
4244/// struct class_ro_t *ro;
4245/// }
4246///
Fariborz Jahanian06726462009-01-24 21:21:53 +00004247llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
4248 std::string &ClassName,
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004249 llvm::Constant *IsAGV,
4250 llvm::Constant *SuperClassGV,
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +00004251 llvm::Constant *ClassRoGV,
4252 bool HiddenVisibility) {
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004253 std::vector<llvm::Constant*> Values(5);
4254 Values[0] = IsAGV;
Fariborz Jahanian06726462009-01-24 21:21:53 +00004255 Values[1] = SuperClassGV
4256 ? SuperClassGV
4257 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004258 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4259 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4260 Values[4] = ClassRoGV; // &CLASS_RO_GV
4261 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
4262 Values);
Daniel Dunbarabbda222009-03-01 04:40:10 +00004263 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4264 GV->setInitializer(Init);
Fariborz Jahanian7c891592009-01-31 01:07:39 +00004265 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00004266 GV->setAlignment(
4267 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +00004268 if (HiddenVisibility)
4269 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian06726462009-01-24 21:21:53 +00004270 return GV;
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004271}
4272
Daniel Dunbared4d5962009-05-03 12:57:56 +00004273void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarecb5d402009-04-19 23:41:48 +00004274 uint32_t &InstanceStart,
4275 uint32_t &InstanceSize) {
Daniel Dunbar85d37542009-04-22 07:32:20 +00004276 // Find first and last (non-padding) ivars in this interface.
4277
4278 // FIXME: Use iterator.
4279 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Daniel Dunbared4d5962009-05-03 12:57:56 +00004280 GetNamedIvarList(OID->getClassInterface(), OIvars);
Daniel Dunbar85d37542009-04-22 07:32:20 +00004281
4282 if (OIvars.empty()) {
4283 InstanceStart = InstanceSize = 0;
4284 return;
Daniel Dunbare0edb2e2009-04-22 04:39:47 +00004285 }
Daniel Dunbar85d37542009-04-22 07:32:20 +00004286
4287 const ObjCIvarDecl *First = OIvars.front();
4288 const ObjCIvarDecl *Last = OIvars.back();
4289
4290 InstanceStart = ComputeIvarBaseOffset(CGM, OID, First);
4291 const llvm::Type *FieldTy =
4292 CGM.getTypes().ConvertTypeForMem(Last->getType());
4293 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
Fariborz Jahanian31b96492009-04-22 23:00:43 +00004294// FIXME. This breaks compatibility with llvm-gcc-4.2 (but makes it compatible
4295// with gcc-4.2). We postpone this for now.
4296#if 0
4297 if (Last->isBitField()) {
4298 Expr *BitWidth = Last->getBitWidth();
4299 uint64_t BitFieldSize =
Eli Friedman5255e7a2009-04-26 19:19:15 +00004300 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Fariborz Jahanian31b96492009-04-22 23:00:43 +00004301 Size = (BitFieldSize / 8) + ((BitFieldSize % 8) != 0);
4302 }
4303#endif
Daniel Dunbar85d37542009-04-22 07:32:20 +00004304 InstanceSize = ComputeIvarBaseOffset(CGM, OID, Last) + Size;
Daniel Dunbarecb5d402009-04-19 23:41:48 +00004305}
4306
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00004307void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4308 std::string ClassName = ID->getNameAsString();
4309 if (!ObjCEmptyCacheVar) {
4310 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004311 ObjCTypes.CacheTy,
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00004312 false,
4313 llvm::GlobalValue::ExternalLinkage,
4314 0,
Daniel Dunbara2d275d2009-04-07 05:48:37 +00004315 "_objc_empty_cache",
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00004316 &CGM.getModule());
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00004317
4318 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004319 ObjCTypes.ImpnfABITy,
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00004320 false,
4321 llvm::GlobalValue::ExternalLinkage,
4322 0,
Daniel Dunbara2d275d2009-04-07 05:48:37 +00004323 "_objc_empty_vtable",
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00004324 &CGM.getModule());
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00004325 }
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004326 assert(ID->getClassInterface() &&
4327 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar72878722009-04-20 20:18:54 +00004328 // FIXME: Is this correct (that meta class size is never computed)?
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004329 uint32_t InstanceStart =
4330 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
4331 uint32_t InstanceSize = InstanceStart;
4332 uint32_t flags = CLS_META;
Daniel Dunbara2d275d2009-04-07 05:48:37 +00004333 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4334 std::string ObjCClassName(getClassSymbolPrefix());
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00004335
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004336 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00004337
Daniel Dunbar8394fda2009-04-14 06:00:08 +00004338 bool classIsHidden =
4339 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +00004340 if (classIsHidden)
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004341 flags |= OBJC2_CLS_HIDDEN;
4342 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004343 // class is root
4344 flags |= CLS_ROOT;
Daniel Dunbarabbda222009-03-01 04:40:10 +00004345 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanianab438842009-04-14 18:41:56 +00004346 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004347 } else {
Fariborz Jahanian06726462009-01-24 21:21:53 +00004348 // Has a root. Current class is not a root.
Fariborz Jahanian514c63b2009-02-26 18:23:47 +00004349 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4350 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4351 Root = Super;
Fariborz Jahanianab438842009-04-14 18:41:56 +00004352 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanian514c63b2009-02-26 18:23:47 +00004353 // work on super class metadata symbol.
4354 std::string SuperClassName =
4355 ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanianab438842009-04-14 18:41:56 +00004356 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00004357 }
4358 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4359 InstanceStart,
4360 InstanceSize,ID);
Fariborz Jahanian06726462009-01-24 21:21:53 +00004361 std::string TClassName = ObjCMetaClassName + ClassName;
4362 llvm::GlobalVariable *MetaTClass =
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +00004363 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4364 classIsHidden);
Daniel Dunbara2d275d2009-04-07 05:48:37 +00004365
Fariborz Jahanian06726462009-01-24 21:21:53 +00004366 // Metadata for the class
4367 flags = CLS;
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +00004368 if (classIsHidden)
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004369 flags |= OBJC2_CLS_HIDDEN;
Daniel Dunbarc2129532009-04-08 04:21:03 +00004370
4371 if (hasObjCExceptionAttribute(ID->getClassInterface()))
4372 flags |= CLS_EXCEPTION;
4373
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004374 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian06726462009-01-24 21:21:53 +00004375 flags |= CLS_ROOT;
4376 SuperClassGV = 0;
Chris Lattner9fe470d2009-04-19 06:02:28 +00004377 } else {
Fariborz Jahanian06726462009-01-24 21:21:53 +00004378 // Has a root. Current class is not a root.
Fariborz Jahanian514c63b2009-02-26 18:23:47 +00004379 std::string RootClassName =
Fariborz Jahanian06726462009-01-24 21:21:53 +00004380 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbarabbda222009-03-01 04:40:10 +00004381 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahanian06726462009-01-24 21:21:53 +00004382 }
Daniel Dunbared4d5962009-05-03 12:57:56 +00004383 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian06726462009-01-24 21:21:53 +00004384 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianddd2fdd2009-01-24 23:43:01 +00004385 InstanceStart,
4386 InstanceSize,
4387 ID);
Fariborz Jahanian06726462009-01-24 21:21:53 +00004388
4389 TClassName = ObjCClassName + ClassName;
Fariborz Jahanian11c93dd2009-01-30 20:55:31 +00004390 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +00004391 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4392 classIsHidden);
Fariborz Jahanian11c93dd2009-01-30 20:55:31 +00004393 DefinedClasses.push_back(ClassMD);
Daniel Dunbarc2129532009-04-08 04:21:03 +00004394
4395 // Force the definition of the EHType if necessary.
4396 if (flags & CLS_EXCEPTION)
4397 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00004398}
4399
Fariborz Jahanian5d13ab12009-01-30 18:58:59 +00004400/// GenerateProtocolRef - This routine is called to generate code for
4401/// a protocol reference expression; as in:
4402/// @code
4403/// @protocol(Proto1);
4404/// @endcode
4405/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4406/// which will hold address of the protocol meta-data.
4407///
4408llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
4409 const ObjCProtocolDecl *PD) {
4410
Fariborz Jahaniand3243322009-04-10 18:47:34 +00004411 // This routine is called for @protocol only. So, we must build definition
4412 // of protocol's meta-data (not a reference to it!)
4413 //
4414 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Fariborz Jahanian5d13ab12009-01-30 18:58:59 +00004415 ObjCTypes.ExternalProtocolPtrTy);
4416
4417 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
4418 ProtocolName += PD->getNameAsCString();
4419
4420 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
4421 if (PTGV)
4422 return Builder.CreateLoad(PTGV, false, "tmp");
4423 PTGV = new llvm::GlobalVariable(
4424 Init->getType(), false,
Mike Stump36dbf222009-03-07 16:33:28 +00004425 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian5d13ab12009-01-30 18:58:59 +00004426 Init,
4427 ProtocolName,
4428 &CGM.getModule());
4429 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
4430 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4431 UsedGlobals.push_back(PTGV);
4432 return Builder.CreateLoad(PTGV, false, "tmp");
4433}
4434
Fariborz Jahanianfe49a092009-01-26 18:32:24 +00004435/// GenerateCategory - Build metadata for a category implementation.
4436/// struct _category_t {
4437/// const char * const name;
4438/// struct _class_t *const cls;
4439/// const struct _method_list_t * const instance_methods;
4440/// const struct _method_list_t * const class_methods;
4441/// const struct _protocol_list_t * const protocols;
4442/// const struct _prop_list_t * const properties;
4443/// }
4444///
4445void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
4446{
4447 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianc98c87b2009-01-26 22:58:07 +00004448 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
4449 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianfe49a092009-01-26 18:32:24 +00004450 "_$_" + OCD->getNameAsString());
Daniel Dunbara2d275d2009-04-07 05:48:37 +00004451 std::string ExtClassName(getClassSymbolPrefix() +
4452 Interface->getNameAsString());
Fariborz Jahanianfe49a092009-01-26 18:32:24 +00004453
4454 std::vector<llvm::Constant*> Values(6);
4455 Values[0] = GetClassName(OCD->getIdentifier());
4456 // meta-class entry symbol
Daniel Dunbarabbda222009-03-01 04:40:10 +00004457 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanianfe49a092009-01-26 18:32:24 +00004458 Values[1] = ClassGV;
Fariborz Jahanianc98c87b2009-01-26 22:58:07 +00004459 std::vector<llvm::Constant*> Methods;
4460 std::string MethodListName(Prefix);
4461 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
4462 "_$_" + OCD->getNameAsString();
4463
Douglas Gregorcd19b572009-04-23 01:02:12 +00004464 for (ObjCCategoryImplDecl::instmeth_iterator
4465 i = OCD->instmeth_begin(CGM.getContext()),
4466 e = OCD->instmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanianc98c87b2009-01-26 22:58:07 +00004467 // Instance methods should always be defined.
4468 Methods.push_back(GetMethodConstant(*i));
4469 }
4470
4471 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004472 "__DATA, __objc_const",
Fariborz Jahanianc98c87b2009-01-26 22:58:07 +00004473 Methods);
4474
4475 MethodListName = Prefix;
4476 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
4477 OCD->getNameAsString();
4478 Methods.clear();
Douglas Gregorcd19b572009-04-23 01:02:12 +00004479 for (ObjCCategoryImplDecl::classmeth_iterator
4480 i = OCD->classmeth_begin(CGM.getContext()),
4481 e = OCD->classmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanianc98c87b2009-01-26 22:58:07 +00004482 // Class methods should always be defined.
4483 Methods.push_back(GetMethodConstant(*i));
4484 }
4485
4486 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004487 "__DATA, __objc_const",
Fariborz Jahanianc98c87b2009-01-26 22:58:07 +00004488 Methods);
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00004489 const ObjCCategoryDecl *Category =
4490 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian8c7904b2009-02-13 17:52:22 +00004491 if (Category) {
4492 std::string ExtName(Interface->getNameAsString() + "_$_" +
4493 OCD->getNameAsString());
4494 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
4495 + Interface->getNameAsString() + "_$_"
4496 + Category->getNameAsString(),
4497 Category->protocol_begin(),
4498 Category->protocol_end());
4499 Values[5] =
4500 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
4501 OCD, Category, ObjCTypes);
4502 }
4503 else {
4504 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4505 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
4506 }
4507
Fariborz Jahanianfe49a092009-01-26 18:32:24 +00004508 llvm::Constant *Init =
4509 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
4510 Values);
4511 llvm::GlobalVariable *GCATV
4512 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
4513 false,
4514 llvm::GlobalValue::InternalLinkage,
4515 Init,
4516 ExtCatName,
4517 &CGM.getModule());
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00004518 GCATV->setAlignment(
4519 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004520 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianfe49a092009-01-26 18:32:24 +00004521 UsedGlobals.push_back(GCATV);
4522 DefinedCategories.push_back(GCATV);
4523}
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004524
4525/// GetMethodConstant - Return a struct objc_method constant for the
4526/// given method if it has been defined. The result is null if the
4527/// method has not been defined. The return value has type MethodPtrTy.
4528llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
4529 const ObjCMethodDecl *MD) {
4530 // FIXME: Use DenseMap::lookup
4531 llvm::Function *Fn = MethodDefinitions[MD];
4532 if (!Fn)
4533 return 0;
4534
4535 std::vector<llvm::Constant*> Method(3);
4536 Method[0] =
4537 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4538 ObjCTypes.SelectorPtrTy);
4539 Method[1] = GetMethodVarType(MD);
4540 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
4541 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
4542}
4543
4544/// EmitMethodList - Build meta-data for method declarations
4545/// struct _method_list_t {
4546/// uint32_t entsize; // sizeof(struct _objc_method)
4547/// uint32_t method_count;
4548/// struct _objc_method method_list[method_count];
4549/// }
4550///
4551llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
4552 const std::string &Name,
4553 const char *Section,
4554 const ConstantVector &Methods) {
4555 // Return null for empty list.
4556 if (Methods.empty())
4557 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
4558
4559 std::vector<llvm::Constant*> Values(3);
4560 // sizeof(struct _objc_method)
4561 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
4562 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4563 // method_count
4564 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
4565 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
4566 Methods.size());
4567 Values[2] = llvm::ConstantArray::get(AT, Methods);
4568 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4569
4570 llvm::GlobalVariable *GV =
4571 new llvm::GlobalVariable(Init->getType(), false,
4572 llvm::GlobalValue::InternalLinkage,
4573 Init,
4574 Name,
4575 &CGM.getModule());
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00004576 GV->setAlignment(
4577 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00004578 GV->setSection(Section);
4579 UsedGlobals.push_back(GV);
4580 return llvm::ConstantExpr::getBitCast(GV,
4581 ObjCTypes.MethodListnfABIPtrTy);
4582}
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004583
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00004584/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
4585/// the given ivar.
4586///
4587llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(
Fariborz Jahaniana09a5142009-02-12 18:51:23 +00004588 const ObjCInterfaceDecl *ID,
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00004589 const ObjCIvarDecl *Ivar) {
Daniel Dunbar07d204a2009-04-19 00:31:15 +00004590 std::string Name = "OBJC_IVAR_$_" +
Douglas Gregorc55b0b02009-04-09 21:40:53 +00004591 getInterfaceDeclForIvar(ID, Ivar, CGM.getContext())->getNameAsString() +
4592 '.' + Ivar->getNameAsString();
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00004593 llvm::GlobalVariable *IvarOffsetGV =
4594 CGM.getModule().getGlobalVariable(Name);
4595 if (!IvarOffsetGV)
4596 IvarOffsetGV =
4597 new llvm::GlobalVariable(ObjCTypes.LongTy,
4598 false,
4599 llvm::GlobalValue::ExternalLinkage,
4600 0,
4601 Name,
4602 &CGM.getModule());
4603 return IvarOffsetGV;
4604}
4605
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004606llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00004607 const ObjCInterfaceDecl *ID,
Fariborz Jahanian150f7732009-01-28 01:36:42 +00004608 const ObjCIvarDecl *Ivar,
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004609 unsigned long int Offset) {
Daniel Dunbar0438ff42009-04-19 00:44:02 +00004610 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
4611 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
4612 Offset));
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00004613 IvarOffsetGV->setAlignment(
Fariborz Jahanian55343922009-02-03 00:09:52 +00004614 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar0438ff42009-04-19 00:44:02 +00004615
4616 // FIXME: This matches gcc, but shouldn't the visibility be set on
4617 // the use as well (i.e., in ObjCIvarOffsetVariable).
4618 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
4619 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
4620 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian150f7732009-01-28 01:36:42 +00004621 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar8394fda2009-04-14 06:00:08 +00004622 else
Fariborz Jahanian745fd892009-04-06 18:30:00 +00004623 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004624 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian55343922009-02-03 00:09:52 +00004625 return IvarOffsetGV;
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004626}
4627
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004628/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar3c190812009-04-18 08:51:00 +00004629/// implementation. The return value has type
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004630/// IvarListnfABIPtrTy.
4631/// struct _ivar_t {
4632/// unsigned long int *offset; // pointer to ivar offset location
4633/// char *name;
4634/// char *type;
4635/// uint32_t alignment;
4636/// uint32_t size;
4637/// }
4638/// struct _ivar_list_t {
4639/// uint32 entsize; // sizeof(struct _ivar_t)
4640/// uint32 count;
4641/// struct _iver_t list[count];
4642/// }
4643///
Daniel Dunbar356f0742009-04-20 06:54:31 +00004644
4645void CGObjCCommonMac::GetNamedIvarList(const ObjCInterfaceDecl *OID,
4646 llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const {
4647 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
4648 E = OID->ivar_end(); I != E; ++I) {
4649 // Ignore unnamed bit-fields.
4650 if (!(*I)->getDeclName())
4651 continue;
4652
4653 Res.push_back(*I);
4654 }
4655
4656 for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(CGM.getContext()),
4657 E = OID->prop_end(CGM.getContext()); I != E; ++I)
4658 if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl())
4659 Res.push_back(IV);
4660}
4661
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004662llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
4663 const ObjCImplementationDecl *ID) {
4664
4665 std::vector<llvm::Constant*> Ivars, Ivar(5);
4666
4667 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4668 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
4669
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004670 // FIXME. Consolidate this with similar code in GenerateClass.
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00004671
Daniel Dunbar1748ac32009-04-20 00:33:43 +00004672 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanianfbf44642009-03-31 18:11:23 +00004673 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Daniel Dunbar356f0742009-04-20 06:54:31 +00004674 GetNamedIvarList(OID, OIvars);
Fariborz Jahanian84c45692009-04-01 19:37:34 +00004675
Daniel Dunbar356f0742009-04-20 06:54:31 +00004676 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
4677 ObjCIvarDecl *IVD = OIvars[i];
Daniel Dunbard73f5f22009-04-20 05:53:40 +00004678 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbared4d5962009-05-03 12:57:56 +00004679 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbare42aede2009-04-22 08:22:17 +00004680 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
4681 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004682 const llvm::Type *FieldTy =
Daniel Dunbare42aede2009-04-22 08:22:17 +00004683 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004684 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
4685 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbare42aede2009-04-22 08:22:17 +00004686 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004687 Align = llvm::Log2_32(Align);
4688 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar1748ac32009-04-20 00:33:43 +00004689 // NOTE. Size of a bitfield does not match gcc's, because of the
4690 // way bitfields are treated special in each. But I am told that
4691 // 'size' for bitfield ivars is ignored by the runtime so it does
4692 // not matter. If it matters, there is enough info to get the
4693 // bitfield right!
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004694 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4695 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
4696 }
4697 // Return null for empty list.
4698 if (Ivars.empty())
4699 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4700 std::vector<llvm::Constant*> Values(3);
4701 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
4702 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4703 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
4704 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
4705 Ivars.size());
4706 Values[2] = llvm::ConstantArray::get(AT, Ivars);
4707 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4708 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4709 llvm::GlobalVariable *GV =
4710 new llvm::GlobalVariable(Init->getType(), false,
4711 llvm::GlobalValue::InternalLinkage,
4712 Init,
4713 Prefix + OID->getNameAsString(),
4714 &CGM.getModule());
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00004715 GV->setAlignment(
4716 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004717 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004718
4719 UsedGlobals.push_back(GV);
4720 return llvm::ConstantExpr::getBitCast(GV,
4721 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004722}
4723
4724llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4725 const ObjCProtocolDecl *PD) {
4726 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4727
4728 if (!Entry) {
4729 // We use the initializer as a marker of whether this is a forward
4730 // reference or not. At module finalization we add the empty
4731 // contents for protocols which were referenced but never defined.
4732 Entry =
4733 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
4734 llvm::GlobalValue::ExternalLinkage,
4735 0,
4736 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
4737 &CGM.getModule());
4738 Entry->setSection("__DATA,__datacoal_nt,coalesced");
4739 UsedGlobals.push_back(Entry);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004740 }
4741
4742 return Entry;
4743}
4744
4745/// GetOrEmitProtocol - Generate the protocol meta-data:
4746/// @code
4747/// struct _protocol_t {
4748/// id isa; // NULL
4749/// const char * const protocol_name;
4750/// const struct _protocol_list_t * protocol_list; // super protocols
4751/// const struct method_list_t * const instance_methods;
4752/// const struct method_list_t * const class_methods;
4753/// const struct method_list_t *optionalInstanceMethods;
4754/// const struct method_list_t *optionalClassMethods;
4755/// const struct _prop_list_t * properties;
4756/// const uint32_t size; // sizeof(struct _protocol_t)
4757/// const uint32_t flags; // = 0
4758/// }
4759/// @endcode
4760///
4761
4762llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4763 const ObjCProtocolDecl *PD) {
4764 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4765
4766 // Early exit if a defining object has already been generated.
4767 if (Entry && Entry->hasInitializer())
4768 return Entry;
4769
4770 const char *ProtocolName = PD->getNameAsCString();
4771
4772 // Construct method lists.
4773 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4774 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregorc55b0b02009-04-09 21:40:53 +00004775 for (ObjCProtocolDecl::instmeth_iterator
4776 i = PD->instmeth_begin(CGM.getContext()),
4777 e = PD->instmeth_end(CGM.getContext());
4778 i != e; ++i) {
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004779 ObjCMethodDecl *MD = *i;
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004780 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004781 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4782 OptInstanceMethods.push_back(C);
4783 } else {
4784 InstanceMethods.push_back(C);
4785 }
4786 }
4787
Douglas Gregorc55b0b02009-04-09 21:40:53 +00004788 for (ObjCProtocolDecl::classmeth_iterator
4789 i = PD->classmeth_begin(CGM.getContext()),
4790 e = PD->classmeth_end(CGM.getContext());
4791 i != e; ++i) {
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004792 ObjCMethodDecl *MD = *i;
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004793 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004794 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4795 OptClassMethods.push_back(C);
4796 } else {
4797 ClassMethods.push_back(C);
4798 }
4799 }
4800
4801 std::vector<llvm::Constant*> Values(10);
4802 // isa is NULL
4803 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
4804 Values[1] = GetClassName(PD->getIdentifier());
4805 Values[2] = EmitProtocolList(
4806 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4807 PD->protocol_begin(),
4808 PD->protocol_end());
4809
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004810 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004811 + PD->getNameAsString(),
4812 "__DATA, __objc_const",
4813 InstanceMethods);
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004814 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004815 + PD->getNameAsString(),
4816 "__DATA, __objc_const",
4817 ClassMethods);
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004818 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004819 + PD->getNameAsString(),
4820 "__DATA, __objc_const",
4821 OptInstanceMethods);
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004822 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004823 + PD->getNameAsString(),
4824 "__DATA, __objc_const",
4825 OptClassMethods);
4826 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4827 0, PD, ObjCTypes);
4828 uint32_t Size =
4829 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
4830 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4831 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
4832 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
4833 Values);
4834
4835 if (Entry) {
4836 // Already created, fix the linkage and update the initializer.
Mike Stump36dbf222009-03-07 16:33:28 +00004837 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004838 Entry->setInitializer(Init);
4839 } else {
4840 Entry =
4841 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Mike Stump36dbf222009-03-07 16:33:28 +00004842 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004843 Init,
4844 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
4845 &CGM.getModule());
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00004846 Entry->setAlignment(
4847 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004848 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004849 }
Fariborz Jahanianfd02a662009-01-29 20:10:59 +00004850 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
4851
4852 // Use this protocol meta-data to build protocol list table in section
4853 // __DATA, __objc_protolist
Fariborz Jahanianfd02a662009-01-29 20:10:59 +00004854 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004855 ObjCTypes.ProtocolnfABIPtrTy, false,
Mike Stump36dbf222009-03-07 16:33:28 +00004856 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanianfd02a662009-01-29 20:10:59 +00004857 Entry,
4858 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
4859 +ProtocolName,
4860 &CGM.getModule());
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00004861 PTGV->setAlignment(
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004862 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar9dfd3a72009-04-15 02:56:18 +00004863 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanianfd02a662009-01-29 20:10:59 +00004864 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4865 UsedGlobals.push_back(PTGV);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004866 return Entry;
4867}
4868
4869/// EmitProtocolList - Generate protocol list meta-data:
4870/// @code
4871/// struct _protocol_list_t {
4872/// long protocol_count; // Note, this is 32/64 bit
4873/// struct _protocol_t[protocol_count];
4874/// }
4875/// @endcode
4876///
4877llvm::Constant *
4878CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4879 ObjCProtocolDecl::protocol_iterator begin,
4880 ObjCProtocolDecl::protocol_iterator end) {
4881 std::vector<llvm::Constant*> ProtocolRefs;
4882
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004883 // Just return null for empty protocol lists
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004884 if (begin == end)
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004885 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4886
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004887 // FIXME: We shouldn't need to do this lookup here, should we?
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004888 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4889 if (GV)
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004890 return llvm::ConstantExpr::getBitCast(GV,
4891 ObjCTypes.ProtocolListnfABIPtrTy);
4892
4893 for (; begin != end; ++begin)
4894 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4895
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004896 // This list is null terminated.
4897 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004898 ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004899
4900 std::vector<llvm::Constant*> Values(2);
4901 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4902 Values[1] =
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004903 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004904 ProtocolRefs.size()),
4905 ProtocolRefs);
4906
4907 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4908 GV = new llvm::GlobalVariable(Init->getType(), false,
4909 llvm::GlobalValue::InternalLinkage,
4910 Init,
4911 Name,
4912 &CGM.getModule());
4913 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00004914 GV->setAlignment(
4915 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004916 UsedGlobals.push_back(GV);
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004917 return llvm::ConstantExpr::getBitCast(GV,
4918 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004919}
4920
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004921/// GetMethodDescriptionConstant - This routine build following meta-data:
4922/// struct _objc_method {
4923/// SEL _cmd;
4924/// char *method_type;
4925/// char *_imp;
4926/// }
4927
4928llvm::Constant *
4929CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4930 std::vector<llvm::Constant*> Desc(3);
4931 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4932 ObjCTypes.SelectorPtrTy);
4933 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian5d13ab12009-01-30 18:58:59 +00004934 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004935 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4936 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4937}
Fariborz Jahanian55343922009-02-03 00:09:52 +00004938
4939/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4940/// This code gen. amounts to generating code for:
4941/// @code
4942/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4943/// @encode
4944///
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00004945LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian55343922009-02-03 00:09:52 +00004946 CodeGen::CodeGenFunction &CGF,
4947 QualType ObjectTy,
4948 llvm::Value *BaseValue,
4949 const ObjCIvarDecl *Ivar,
Fariborz Jahanian55343922009-02-03 00:09:52 +00004950 unsigned CVRQualifiers) {
Daniel Dunbarf5254bd2009-04-21 01:19:28 +00004951 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar85d37542009-04-22 07:32:20 +00004952 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
4953 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian55343922009-02-03 00:09:52 +00004954}
4955
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00004956llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
4957 CodeGen::CodeGenFunction &CGF,
Daniel Dunbar61e14a62009-04-22 05:08:15 +00004958 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00004959 const ObjCIvarDecl *Ivar) {
Daniel Dunbar07d204a2009-04-19 00:31:15 +00004960 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),
4961 false, "ivar");
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00004962}
4963
Fariborz Jahanian7e881162009-02-04 00:22:57 +00004964CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4965 CodeGen::CodeGenFunction &CGF,
4966 QualType ResultType,
4967 Selector Sel,
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004968 llvm::Value *Receiver,
Fariborz Jahanian7e881162009-02-04 00:22:57 +00004969 QualType Arg0Ty,
4970 bool IsSuper,
4971 const CallArgList &CallArgs) {
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004972 // FIXME. Even though IsSuper is passes. This function doese not
4973 // handle calls to 'super' receivers.
4974 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00004975 llvm::Value *Arg0 = Receiver;
4976 if (!IsSuper)
4977 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004978
4979 // Find the message function name.
Fariborz Jahanian10d69ea2009-02-05 01:13:09 +00004980 // FIXME. This is too much work to get the ABI-specific result type
4981 // needed to find the message name.
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004982 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4983 llvm::SmallVector<QualType, 16>());
Fariborz Jahanian9d7167d2009-04-30 23:08:58 +00004984 llvm::Constant *Fn = 0;
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004985 std::string Name("\01l_");
4986 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanian13ab25e2009-02-05 18:00:27 +00004987#if 0
4988 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004989 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattnerada416b2009-04-22 02:53:24 +00004990 Fn = ObjCTypes.getMessageSendIdStretFixupFn();
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004991 // FIXME. Is there a better way of getting these names.
4992 // They are available in RuntimeFunctions vector pair.
4993 Name += "objc_msgSendId_stret_fixup";
4994 }
Fariborz Jahanian13ab25e2009-02-05 18:00:27 +00004995 else
4996#endif
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00004997 if (IsSuper) {
Chris Lattnerada416b2009-04-22 02:53:24 +00004998 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00004999 Name += "objc_msgSendSuper2_stret_fixup";
5000 }
5001 else
Fariborz Jahanian13ab25e2009-02-05 18:00:27 +00005002 {
Chris Lattnerada416b2009-04-22 02:53:24 +00005003 Fn = ObjCTypes.getMessageSendStretFixupFn();
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00005004 Name += "objc_msgSend_stret_fixup";
5005 }
5006 }
Fariborz Jahanian865260c2009-04-30 16:31:11 +00005007 else if (!IsSuper && ResultType->isFloatingType()) {
5008 if (const BuiltinType *BT = ResultType->getAsBuiltinType()) {
5009 BuiltinType::Kind k = BT->getKind();
5010 if (k == BuiltinType::LongDouble) {
5011 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5012 Name += "objc_msgSend_fpret_fixup";
5013 }
5014 else {
5015 Fn = ObjCTypes.getMessageSendFixupFn();
5016 Name += "objc_msgSend_fixup";
5017 }
5018 }
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00005019 }
5020 else {
Fariborz Jahanian13ab25e2009-02-05 18:00:27 +00005021#if 0
5022// unlike what is documented. gcc never generates this API!!
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00005023 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattnerada416b2009-04-22 02:53:24 +00005024 Fn = ObjCTypes.getMessageSendIdFixupFn();
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00005025 Name += "objc_msgSendId_fixup";
5026 }
Fariborz Jahanian13ab25e2009-02-05 18:00:27 +00005027 else
5028#endif
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00005029 if (IsSuper) {
Chris Lattnerada416b2009-04-22 02:53:24 +00005030 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00005031 Name += "objc_msgSendSuper2_fixup";
5032 }
5033 else
Fariborz Jahanian13ab25e2009-02-05 18:00:27 +00005034 {
Chris Lattnerada416b2009-04-22 02:53:24 +00005035 Fn = ObjCTypes.getMessageSendFixupFn();
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00005036 Name += "objc_msgSend_fixup";
5037 }
5038 }
Fariborz Jahanian9d7167d2009-04-30 23:08:58 +00005039 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00005040 Name += '_';
5041 std::string SelName(Sel.getAsString());
5042 // Replace all ':' in selector name with '_' ouch!
5043 for(unsigned i = 0; i < SelName.size(); i++)
5044 if (SelName[i] == ':')
5045 SelName[i] = '_';
5046 Name += SelName;
5047 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5048 if (!GV) {
Daniel Dunbar4993e292009-04-15 19:03:14 +00005049 // Build message ref table entry.
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00005050 std::vector<llvm::Constant*> Values(2);
5051 Values[0] = Fn;
5052 Values[1] = GetMethodVarName(Sel);
5053 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
5054 GV = new llvm::GlobalVariable(Init->getType(), false,
Mike Stump36dbf222009-03-07 16:33:28 +00005055 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00005056 Init,
5057 Name,
5058 &CGM.getModule());
5059 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbara405f782009-04-15 19:04:46 +00005060 GV->setAlignment(16);
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00005061 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00005062 }
5063 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanian10d69ea2009-02-05 01:13:09 +00005064
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00005065 CallArgList ActualArgs;
5066 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
5067 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
5068 ObjCTypes.MessageRefCPtrTy));
5069 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanian10d69ea2009-02-05 01:13:09 +00005070 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
5071 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5072 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanianf3c17752009-02-14 21:25:36 +00005073 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanian10d69ea2009-02-05 01:13:09 +00005074 Callee = CGF.Builder.CreateBitCast(Callee,
5075 llvm::PointerType::getUnqual(FTy));
5076 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian7e881162009-02-04 00:22:57 +00005077}
5078
5079/// Generate code for a message send expression in the nonfragile abi.
5080CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
5081 CodeGen::CodeGenFunction &CGF,
5082 QualType ResultType,
5083 Selector Sel,
5084 llvm::Value *Receiver,
5085 bool IsClassMessage,
5086 const CallArgList &CallArgs) {
Fariborz Jahanian7e881162009-02-04 00:22:57 +00005087 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00005088 Receiver, CGF.getContext().getObjCIdType(),
Fariborz Jahanian7e881162009-02-04 00:22:57 +00005089 false, CallArgs);
5090}
5091
Daniel Dunbarabbda222009-03-01 04:40:10 +00005092llvm::GlobalVariable *
Fariborz Jahanianab438842009-04-14 18:41:56 +00005093CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbarabbda222009-03-01 04:40:10 +00005094 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5095
Daniel Dunbar66b47512009-03-02 05:18:14 +00005096 if (!GV) {
Daniel Dunbarabbda222009-03-01 04:40:10 +00005097 GV = new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
5098 llvm::GlobalValue::ExternalLinkage,
5099 0, Name, &CGM.getModule());
Daniel Dunbarabbda222009-03-01 04:40:10 +00005100 }
5101
5102 return GV;
5103}
5104
Fariborz Jahanian917c0402009-02-05 20:41:40 +00005105llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar3c190812009-04-18 08:51:00 +00005106 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian917c0402009-02-05 20:41:40 +00005107 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
5108
5109 if (!Entry) {
Daniel Dunbara2d275d2009-04-07 05:48:37 +00005110 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarabbda222009-03-01 04:40:10 +00005111 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Fariborz Jahanian917c0402009-02-05 20:41:40 +00005112 Entry =
5113 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5114 llvm::GlobalValue::InternalLinkage,
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00005115 ClassGV,
Daniel Dunbar3c190812009-04-18 08:51:00 +00005116 "\01L_OBJC_CLASSLIST_REFERENCES_$_",
Fariborz Jahanian917c0402009-02-05 20:41:40 +00005117 &CGM.getModule());
5118 Entry->setAlignment(
5119 CGM.getTargetData().getPrefTypeAlignment(
5120 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar3c190812009-04-18 08:51:00 +00005121 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
5122 UsedGlobals.push_back(Entry);
5123 }
5124
5125 return Builder.CreateLoad(Entry, false, "tmp");
5126}
Fariborz Jahanian917c0402009-02-05 20:41:40 +00005127
Daniel Dunbar3c190812009-04-18 08:51:00 +00005128llvm::Value *
5129CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
5130 const ObjCInterfaceDecl *ID) {
5131 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
5132
5133 if (!Entry) {
5134 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5135 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5136 Entry =
5137 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5138 llvm::GlobalValue::InternalLinkage,
5139 ClassGV,
5140 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
5141 &CGM.getModule());
5142 Entry->setAlignment(
5143 CGM.getTargetData().getPrefTypeAlignment(
5144 ObjCTypes.ClassnfABIPtrTy));
5145 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Fariborz Jahanian917c0402009-02-05 20:41:40 +00005146 UsedGlobals.push_back(Entry);
5147 }
5148
5149 return Builder.CreateLoad(Entry, false, "tmp");
5150}
5151
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00005152/// EmitMetaClassRef - Return a Value * of the address of _class_t
5153/// meta-data
5154///
5155llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5156 const ObjCInterfaceDecl *ID) {
5157 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5158 if (Entry)
5159 return Builder.CreateLoad(Entry, false, "tmp");
5160
Daniel Dunbara2d275d2009-04-07 05:48:37 +00005161 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanianab438842009-04-14 18:41:56 +00005162 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00005163 Entry =
5164 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5165 llvm::GlobalValue::InternalLinkage,
5166 MetaClassGV,
5167 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
5168 &CGM.getModule());
5169 Entry->setAlignment(
5170 CGM.getTargetData().getPrefTypeAlignment(
5171 ObjCTypes.ClassnfABIPtrTy));
5172
Daniel Dunbar4993e292009-04-15 19:03:14 +00005173 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00005174 UsedGlobals.push_back(Entry);
5175
5176 return Builder.CreateLoad(Entry, false, "tmp");
5177}
5178
Fariborz Jahanian917c0402009-02-05 20:41:40 +00005179/// GetClass - Return a reference to the class for the given interface
5180/// decl.
5181llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5182 const ObjCInterfaceDecl *ID) {
5183 return EmitClassRef(Builder, ID);
5184}
5185
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00005186/// Generates a message send where the super is the receiver. This is
5187/// a message send to self with special delivery semantics indicating
5188/// which class's method should be called.
5189CodeGen::RValue
5190CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
5191 QualType ResultType,
5192 Selector Sel,
5193 const ObjCInterfaceDecl *Class,
Fariborz Jahanian17636fa2009-02-28 20:07:56 +00005194 bool isCategoryImpl,
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00005195 llvm::Value *Receiver,
5196 bool IsClassMessage,
5197 const CodeGen::CallArgList &CallArgs) {
5198 // ...
5199 // Create and init a super structure; this is a (receiver, class)
5200 // pair we will pass to objc_msgSendSuper.
5201 llvm::Value *ObjCSuper =
5202 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
5203
5204 llvm::Value *ReceiverAsObject =
5205 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5206 CGF.Builder.CreateStore(ReceiverAsObject,
5207 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
5208
5209 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian17636fa2009-02-28 20:07:56 +00005210 llvm::Value *Target;
5211 if (IsClassMessage) {
5212 if (isCategoryImpl) {
5213 // Message sent to "super' in a class method defined in
5214 // a category implementation.
Daniel Dunbar3c190812009-04-18 08:51:00 +00005215 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian17636fa2009-02-28 20:07:56 +00005216 Target = CGF.Builder.CreateStructGEP(Target, 0);
5217 Target = CGF.Builder.CreateLoad(Target);
5218 }
5219 else
5220 Target = EmitMetaClassRef(CGF.Builder, Class);
5221 }
5222 else
Daniel Dunbar3c190812009-04-18 08:51:00 +00005223 Target = EmitSuperClassRef(CGF.Builder, Class);
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00005224
5225 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
5226 // and ObjCTypes types.
5227 const llvm::Type *ClassTy =
5228 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5229 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5230 CGF.Builder.CreateStore(Target,
5231 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
5232
5233 return EmitMessageSend(CGF, ResultType, Sel,
5234 ObjCSuper, ObjCTypes.SuperPtrCTy,
5235 true, CallArgs);
5236}
Fariborz Jahanianebb82c62009-02-11 20:51:17 +00005237
5238llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
5239 Selector Sel) {
5240 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
5241
5242 if (!Entry) {
5243 llvm::Constant *Casted =
5244 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5245 ObjCTypes.SelectorPtrTy);
5246 Entry =
5247 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
5248 llvm::GlobalValue::InternalLinkage,
5249 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
5250 &CGM.getModule());
5251 Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip");
5252 UsedGlobals.push_back(Entry);
5253 }
5254
5255 return Builder.CreateLoad(Entry, false, "tmp");
5256}
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00005257/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
5258/// objc_assign_ivar (id src, id *dst)
5259///
5260void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
5261 llvm::Value *src, llvm::Value *dst)
5262{
Fariborz Jahanianad51ca02009-03-23 19:10:40 +00005263 const llvm::Type * SrcTy = src->getType();
5264 if (!isa<llvm::PointerType>(SrcTy)) {
5265 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5266 assert(Size <= 8 && "does not support size > 8");
5267 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5268 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian664da982009-03-13 00:42:52 +00005269 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5270 }
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00005271 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5272 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerf6ec7e42009-04-22 02:38:11 +00005273 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(),
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00005274 src, dst, "assignivar");
5275 return;
5276}
5277
5278/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5279/// objc_assign_strongCast (id src, id *dst)
5280///
5281void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
5282 CodeGen::CodeGenFunction &CGF,
5283 llvm::Value *src, llvm::Value *dst)
5284{
Fariborz Jahanianad51ca02009-03-23 19:10:40 +00005285 const llvm::Type * SrcTy = src->getType();
5286 if (!isa<llvm::PointerType>(SrcTy)) {
5287 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5288 assert(Size <= 8 && "does not support size > 8");
5289 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5290 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian664da982009-03-13 00:42:52 +00005291 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5292 }
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00005293 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5294 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerf6ec7e42009-04-22 02:38:11 +00005295 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00005296 src, dst, "weakassign");
5297 return;
5298}
5299
5300/// EmitObjCWeakRead - Code gen for loading value of a __weak
5301/// object: objc_read_weak (id *src)
5302///
5303llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
5304 CodeGen::CodeGenFunction &CGF,
5305 llvm::Value *AddrWeakObj)
5306{
Eli Friedmanf8466232009-03-07 03:57:15 +00005307 const llvm::Type* DestTy =
5308 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00005309 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattnera7ecda42009-04-22 02:44:54 +00005310 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00005311 AddrWeakObj, "weakread");
Eli Friedmanf8466232009-03-07 03:57:15 +00005312 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00005313 return read_weak;
5314}
5315
5316/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5317/// objc_assign_weak (id src, id *dst)
5318///
5319void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
5320 llvm::Value *src, llvm::Value *dst)
5321{
Fariborz Jahanianad51ca02009-03-23 19:10:40 +00005322 const llvm::Type * SrcTy = src->getType();
5323 if (!isa<llvm::PointerType>(SrcTy)) {
5324 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5325 assert(Size <= 8 && "does not support size > 8");
5326 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5327 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian664da982009-03-13 00:42:52 +00005328 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5329 }
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00005330 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5331 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner293c1d32009-04-17 22:12:36 +00005332 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00005333 src, dst, "weakassign");
5334 return;
5335}
5336
5337/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5338/// objc_assign_global (id src, id *dst)
5339///
5340void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
5341 llvm::Value *src, llvm::Value *dst)
5342{
Fariborz Jahanianad51ca02009-03-23 19:10:40 +00005343 const llvm::Type * SrcTy = src->getType();
5344 if (!isa<llvm::PointerType>(SrcTy)) {
5345 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5346 assert(Size <= 8 && "does not support size > 8");
5347 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5348 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian664da982009-03-13 00:42:52 +00005349 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5350 }
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00005351 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5352 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerf6ec7e42009-04-22 02:38:11 +00005353 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00005354 src, dst, "globalassign");
5355 return;
5356}
Fariborz Jahanianebb82c62009-02-11 20:51:17 +00005357
Daniel Dunbar75de89f2009-02-24 07:47:38 +00005358void
5359CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5360 const Stmt &S) {
Daniel Dunbar75de89f2009-02-24 07:47:38 +00005361 bool isTry = isa<ObjCAtTryStmt>(S);
5362 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
5363 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005364 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar75de89f2009-02-24 07:47:38 +00005365 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005366 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
Daniel Dunbar75de89f2009-02-24 07:47:38 +00005367 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
5368
5369 // For @synchronized, call objc_sync_enter(sync.expr). The
5370 // evaluation of the expression must occur before we enter the
5371 // @synchronized. We can safely avoid a temp here because jumps into
5372 // @synchronized are illegal & this will dominate uses.
5373 llvm::Value *SyncArg = 0;
5374 if (!isTry) {
5375 SyncArg =
5376 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
5377 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattner23e24652009-04-06 16:53:45 +00005378 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar75de89f2009-02-24 07:47:38 +00005379 }
5380
5381 // Push an EH context entry, used for handling rethrows and jumps
5382 // through finally.
5383 CGF.PushCleanupBlock(FinallyBlock);
5384
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005385 CGF.setInvokeDest(TryHandler);
Daniel Dunbar75de89f2009-02-24 07:47:38 +00005386
5387 CGF.EmitBlock(TryBlock);
5388 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
5389 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
5390 CGF.EmitBranchThroughCleanup(FinallyEnd);
5391
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005392 // Emit the exception handler.
5393
5394 CGF.EmitBlock(TryHandler);
5395
5396 llvm::Value *llvm_eh_exception =
5397 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
5398 llvm::Value *llvm_eh_selector_i64 =
5399 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64);
5400 llvm::Value *llvm_eh_typeid_for_i64 =
5401 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64);
5402 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5403 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow");
5404
5405 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
5406 SelectorArgs.push_back(Exc);
Chris Lattner23e24652009-04-06 16:53:45 +00005407 SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005408
5409 // Construct the lists of (type, catch body) to handle.
Daniel Dunbar0098e7a2009-03-06 00:01:21 +00005410 llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers;
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005411 bool HasCatchAll = false;
5412 if (isTry) {
5413 if (const ObjCAtCatchStmt* CatchStmt =
5414 cast<ObjCAtTryStmt>(S).getCatchStmts()) {
5415 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar0098e7a2009-03-06 00:01:21 +00005416 const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Steve Naroff0e8b96a2009-03-03 19:52:17 +00005417 Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody()));
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005418
5419 // catch(...) always matches.
Steve Naroff0e8b96a2009-03-03 19:52:17 +00005420 if (!CatchDecl) {
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005421 // Use i8* null here to signal this is a catch all, not a cleanup.
5422 llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
5423 SelectorArgs.push_back(Null);
5424 HasCatchAll = true;
5425 break;
5426 }
5427
Daniel Dunbar0098e7a2009-03-06 00:01:21 +00005428 if (CGF.getContext().isObjCIdType(CatchDecl->getType()) ||
5429 CatchDecl->getType()->isObjCQualifiedIdType()) {
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005430 llvm::Value *IDEHType =
5431 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5432 if (!IDEHType)
5433 IDEHType =
5434 new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5435 llvm::GlobalValue::ExternalLinkage,
5436 0, "OBJC_EHTYPE_id", &CGM.getModule());
5437 SelectorArgs.push_back(IDEHType);
5438 HasCatchAll = true;
5439 break;
5440 }
5441
5442 // All other types should be Objective-C interface pointer types.
Daniel Dunbar0098e7a2009-03-06 00:01:21 +00005443 const PointerType *PT = CatchDecl->getType()->getAsPointerType();
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005444 assert(PT && "Invalid @catch type.");
5445 const ObjCInterfaceType *IT =
5446 PT->getPointeeType()->getAsObjCInterfaceType();
5447 assert(IT && "Invalid @catch type.");
Daniel Dunbarc2129532009-04-08 04:21:03 +00005448 llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false);
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005449 SelectorArgs.push_back(EHType);
5450 }
5451 }
5452 }
5453
5454 // We use a cleanup unless there was already a catch all.
5455 if (!HasCatchAll) {
5456 SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
Daniel Dunbar0098e7a2009-03-06 00:01:21 +00005457 Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0));
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005458 }
5459
5460 llvm::Value *Selector =
5461 CGF.Builder.CreateCall(llvm_eh_selector_i64,
5462 SelectorArgs.begin(), SelectorArgs.end(),
5463 "selector");
5464 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
Daniel Dunbar0098e7a2009-03-06 00:01:21 +00005465 const ParmVarDecl *CatchParam = Handlers[i].first;
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005466 const Stmt *CatchBody = Handlers[i].second;
5467
5468 llvm::BasicBlock *Next = 0;
5469
5470 // The last handler always matches.
5471 if (i + 1 != e) {
5472 assert(CatchParam && "Only last handler can be a catch all.");
5473
5474 llvm::BasicBlock *Match = CGF.createBasicBlock("match");
5475 Next = CGF.createBasicBlock("catch.next");
5476 llvm::Value *Id =
5477 CGF.Builder.CreateCall(llvm_eh_typeid_for_i64,
5478 CGF.Builder.CreateBitCast(SelectorArgs[i+2],
5479 ObjCTypes.Int8PtrTy));
5480 CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id),
5481 Match, Next);
5482
5483 CGF.EmitBlock(Match);
5484 }
5485
5486 if (CatchBody) {
5487 llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end");
5488 llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler");
5489
5490 // Cleanups must call objc_end_catch.
5491 //
5492 // FIXME: It seems incorrect for objc_begin_catch to be inside
5493 // this context, but this matches gcc.
5494 CGF.PushCleanupBlock(MatchEnd);
5495 CGF.setInvokeDest(MatchHandler);
5496
5497 llvm::Value *ExcObject =
Chris Lattner93dca5b2009-04-22 02:15:23 +00005498 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc);
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005499
5500 // Bind the catch parameter if it exists.
5501 if (CatchParam) {
Daniel Dunbar0098e7a2009-03-06 00:01:21 +00005502 ExcObject =
5503 CGF.Builder.CreateBitCast(ExcObject,
5504 CGF.ConvertType(CatchParam->getType()));
5505 // CatchParam is a ParmVarDecl because of the grammar
5506 // construction used to handle this, but for codegen purposes
5507 // we treat this as a local decl.
5508 CGF.EmitLocalBlockVarDecl(*CatchParam);
5509 CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005510 }
5511
5512 CGF.ObjCEHValueStack.push_back(ExcObject);
5513 CGF.EmitStmt(CatchBody);
5514 CGF.ObjCEHValueStack.pop_back();
5515
5516 CGF.EmitBranchThroughCleanup(FinallyEnd);
5517
5518 CGF.EmitBlock(MatchHandler);
5519
5520 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5521 // We are required to emit this call to satisfy LLVM, even
5522 // though we don't use the result.
5523 llvm::SmallVector<llvm::Value*, 8> Args;
5524 Args.push_back(Exc);
Chris Lattner23e24652009-04-06 16:53:45 +00005525 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005526 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
5527 0));
5528 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5529 CGF.Builder.CreateStore(Exc, RethrowPtr);
5530 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5531
5532 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5533
5534 CGF.EmitBlock(MatchEnd);
5535
5536 // Unfortunately, we also have to generate another EH frame here
5537 // in case this throws.
5538 llvm::BasicBlock *MatchEndHandler =
5539 CGF.createBasicBlock("match.end.handler");
5540 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattner93dca5b2009-04-22 02:15:23 +00005541 CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(),
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005542 Cont, MatchEndHandler,
5543 Args.begin(), Args.begin());
5544
5545 CGF.EmitBlock(Cont);
5546 if (Info.SwitchBlock)
5547 CGF.EmitBlock(Info.SwitchBlock);
5548 if (Info.EndBlock)
5549 CGF.EmitBlock(Info.EndBlock);
5550
5551 CGF.EmitBlock(MatchEndHandler);
5552 Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5553 // We are required to emit this call to satisfy LLVM, even
5554 // though we don't use the result.
5555 Args.clear();
5556 Args.push_back(Exc);
Chris Lattner23e24652009-04-06 16:53:45 +00005557 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005558 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
5559 0));
5560 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5561 CGF.Builder.CreateStore(Exc, RethrowPtr);
5562 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5563
5564 if (Next)
5565 CGF.EmitBlock(Next);
5566 } else {
5567 assert(!Next && "catchup should be last handler.");
5568
5569 CGF.Builder.CreateStore(Exc, RethrowPtr);
5570 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5571 }
5572 }
5573
Daniel Dunbar75de89f2009-02-24 07:47:38 +00005574 // Pop the cleanup entry, the @finally is outside this cleanup
5575 // scope.
5576 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5577 CGF.setInvokeDest(PrevLandingPad);
5578
5579 CGF.EmitBlock(FinallyBlock);
5580
5581 if (isTry) {
5582 if (const ObjCAtFinallyStmt* FinallyStmt =
5583 cast<ObjCAtTryStmt>(S).getFinallyStmt())
5584 CGF.EmitStmt(FinallyStmt->getFinallyBody());
5585 } else {
5586 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
5587 // @synchronized.
Chris Lattnerf6ec7e42009-04-22 02:38:11 +00005588 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005589 }
Daniel Dunbar75de89f2009-02-24 07:47:38 +00005590
5591 if (Info.SwitchBlock)
5592 CGF.EmitBlock(Info.SwitchBlock);
5593 if (Info.EndBlock)
5594 CGF.EmitBlock(Info.EndBlock);
5595
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005596 // Branch around the rethrow code.
Daniel Dunbar75de89f2009-02-24 07:47:38 +00005597 CGF.EmitBranch(FinallyEnd);
5598
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005599 CGF.EmitBlock(FinallyRethrow);
Chris Lattner93dca5b2009-04-22 02:15:23 +00005600 CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(),
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005601 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbar75de89f2009-02-24 07:47:38 +00005602 CGF.Builder.CreateUnreachable();
5603
5604 CGF.EmitBlock(FinallyEnd);
5605}
5606
Anders Carlsson1cf75362009-02-16 22:59:18 +00005607/// EmitThrowStmt - Generate code for a throw statement.
5608void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
5609 const ObjCAtThrowStmt &S) {
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005610 llvm::Value *Exception;
Anders Carlsson1cf75362009-02-16 22:59:18 +00005611 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005612 Exception = CGF.EmitScalarExpr(ThrowExpr);
Anders Carlsson1cf75362009-02-16 22:59:18 +00005613 } else {
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005614 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
5615 "Unexpected rethrow outside @catch block.");
5616 Exception = CGF.ObjCEHValueStack.back();
Anders Carlsson1cf75362009-02-16 22:59:18 +00005617 }
5618
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005619 llvm::Value *ExceptionAsObject =
5620 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
5621 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
5622 if (InvokeDest) {
5623 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattnerf6ec7e42009-04-22 02:38:11 +00005624 CGF.Builder.CreateInvoke(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005625 Cont, InvokeDest,
5626 &ExceptionAsObject, &ExceptionAsObject + 1);
5627 CGF.EmitBlock(Cont);
5628 } else
Chris Lattnerf6ec7e42009-04-22 02:38:11 +00005629 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Daniel Dunbarc0318b22009-03-02 06:08:11 +00005630 CGF.Builder.CreateUnreachable();
5631
Anders Carlsson1cf75362009-02-16 22:59:18 +00005632 // Clear the insertion point to indicate we are in unreachable code.
5633 CGF.Builder.ClearInsertionPoint();
5634}
Daniel Dunbar9c285e72009-03-01 04:46:24 +00005635
5636llvm::Value *
Daniel Dunbarc2129532009-04-08 04:21:03 +00005637CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
5638 bool ForDefinition) {
Daniel Dunbar9c285e72009-03-01 04:46:24 +00005639 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbar9c285e72009-03-01 04:46:24 +00005640
Daniel Dunbarc2129532009-04-08 04:21:03 +00005641 // If we don't need a definition, return the entry if found or check
5642 // if we use an external reference.
5643 if (!ForDefinition) {
5644 if (Entry)
5645 return Entry;
Daniel Dunbarafac9be2009-04-07 06:43:45 +00005646
Daniel Dunbarc2129532009-04-08 04:21:03 +00005647 // If this type (or a super class) has the __objc_exception__
5648 // attribute, emit an external reference.
5649 if (hasObjCExceptionAttribute(ID))
5650 return Entry =
5651 new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5652 llvm::GlobalValue::ExternalLinkage,
5653 0,
5654 (std::string("OBJC_EHTYPE_$_") +
5655 ID->getIdentifier()->getName()),
5656 &CGM.getModule());
5657 }
5658
5659 // Otherwise we need to either make a new entry or fill in the
5660 // initializer.
5661 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbara2d275d2009-04-07 05:48:37 +00005662 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar9c285e72009-03-01 04:46:24 +00005663 std::string VTableName = "objc_ehtype_vtable";
5664 llvm::GlobalVariable *VTableGV =
5665 CGM.getModule().getGlobalVariable(VTableName);
5666 if (!VTableGV)
5667 VTableGV = new llvm::GlobalVariable(ObjCTypes.Int8PtrTy, false,
5668 llvm::GlobalValue::ExternalLinkage,
5669 0, VTableName, &CGM.getModule());
5670
5671 llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2);
5672
5673 std::vector<llvm::Constant*> Values(3);
5674 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
5675 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanianab438842009-04-14 18:41:56 +00005676 Values[2] = GetClassGlobal(ClassName);
Daniel Dunbar9c285e72009-03-01 04:46:24 +00005677 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
5678
Daniel Dunbarc2129532009-04-08 04:21:03 +00005679 if (Entry) {
5680 Entry->setInitializer(Init);
5681 } else {
5682 Entry = new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5683 llvm::GlobalValue::WeakAnyLinkage,
5684 Init,
5685 (std::string("OBJC_EHTYPE_$_") +
5686 ID->getIdentifier()->getName()),
5687 &CGM.getModule());
5688 }
5689
Daniel Dunbar8394fda2009-04-14 06:00:08 +00005690 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbara2d275d2009-04-07 05:48:37 +00005691 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarc2129532009-04-08 04:21:03 +00005692 Entry->setAlignment(8);
5693
5694 if (ForDefinition) {
5695 Entry->setSection("__DATA,__objc_const");
5696 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
5697 } else {
5698 Entry->setSection("__DATA,__datacoal_nt,coalesced");
5699 }
Daniel Dunbar9c285e72009-03-01 04:46:24 +00005700
5701 return Entry;
5702}
Anders Carlsson1cf75362009-02-16 22:59:18 +00005703
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00005704/* *** */
5705
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00005706CodeGen::CGObjCRuntime *
5707CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00005708 return new CGObjCMac(CGM);
5709}
Fariborz Jahanian48543f52009-01-21 22:04:16 +00005710
5711CodeGen::CGObjCRuntime *
Fariborz Jahaniand0374812009-01-22 23:02:58 +00005712CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00005713 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanian48543f52009-01-21 22:04:16 +00005714}