blob: 4d84c775d30dde54b57a0a9ca7100f6762e250bd [file] [log] [blame]
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001//===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides Objective-C code generation targetting the Apple runtime.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000015
16#include "CodeGenModule.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000017#include "CodeGenFunction.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000019#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000020#include "clang/AST/DeclObjC.h"
Daniel Dunbar2bebbf02009-05-03 10:46:44 +000021#include "clang/AST/RecordLayout.h"
Chris Lattner16f00492009-04-26 01:32:48 +000022#include "clang/AST/StmtObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000023#include "clang/Basic/LangOptions.h"
24
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +000025#include "llvm/Intrinsics.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000026#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000027#include "llvm/ADT/DenseSet.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000028#include "llvm/Target/TargetData.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000029#include <sstream>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000030
31using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000032using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000033
Daniel Dunbar97776872009-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 Dunbar2bebbf02009-05-03 10:46:44 +000038static const llvm::StructType *
39GetConcreteClassStruct(CodeGen::CodeGenModule &CGM,
40 const ObjCInterfaceDecl *OID) {
Daniel Dunbar84ad77a2009-04-22 09:39:34 +000041 assert(!OID->isForwardDecl() && "Invalid interface decl!");
Daniel Dunbar412f59b2009-04-22 10:28:39 +000042 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
43 return cast<llvm::StructType>(CGM.getTypes().ConvertTagDeclType(RD));
Daniel Dunbar84ad77a2009-04-22 09:39:34 +000044}
45
Daniel Dunbar532d4da2009-05-03 13:15:50 +000046/// FindIvarInterface - Find the interface containing the ivar.
Daniel Dunbara2435782009-04-22 12:00:04 +000047///
Daniel Dunbar532d4da2009-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 Dunbara80a0f62009-04-22 17:43:55 +000055
Daniel Dunbar532d4da2009-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 Dunbara80a0f62009-04-22 17:43:55 +000073 }
74
Daniel Dunbar532d4da2009-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 Dunbara2435782009-04-22 12:00:04 +000080}
81
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000082static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM,
83 const ObjCInterfaceDecl *OID,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000084 const ObjCImplementationDecl *ID,
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000085 const ObjCIvarDecl *Ivar) {
Daniel Dunbar532d4da2009-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 Dunbar1d7e5392009-05-03 08:55:17 +000099}
100
101uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
102 const ObjCInterfaceDecl *OID,
103 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9f89f2b2009-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 Dunbar97776872009-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 Dunbar1d7e5392009-05-03 08:55:17 +0000119 // Compute (type*) ( (char *) BaseValue + Offset)
Daniel Dunbar97776872009-04-22 07:32:20 +0000120 llvm::Type *I8Ptr = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000121 QualType IvarTy = Ivar->getType();
122 const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy);
Daniel Dunbar97776872009-04-22 07:32:20 +0000123 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr);
Daniel Dunbar97776872009-04-22 07:32:20 +0000124 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000125 V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar97776872009-04-22 07:32:20 +0000126
127 if (Ivar->isBitField()) {
Daniel Dunbar9f89f2b2009-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 Dunbar1d7e5392009-05-03 08:55:17 +0000135 uint64_t BitFieldSize =
136 Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue();
137 return LValue::MakeBitfield(V, BitOffset, BitFieldSize,
Daniel Dunbare38df862009-05-03 07:52:00 +0000138 IvarTy->isSignedIntegerType(),
139 IvarTy.getCVRQualifiers()|CVRQualifiers);
Daniel Dunbar97776872009-04-22 07:32:20 +0000140 }
141
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000142 LValue LV = LValue::MakeAddr(V, IvarTy.getCVRQualifiers()|CVRQualifiers,
143 CGF.CGM.getContext().getObjCGCAttrKind(IvarTy));
Daniel Dunbar97776872009-04-22 07:32:20 +0000144 LValue::SetObjCIvar(LV, true);
145 return LV;
146}
147
148///
149
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000150namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000151
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000152 typedef std::vector<llvm::Constant*> ConstantVector;
153
Daniel Dunbar6efc0c52008-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 Jahanianee0af742009-01-21 22:04:16 +0000157class ObjCCommonTypesHelper {
158protected:
159 CodeGen::CodeGenModule &CGM;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000160
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000161public:
Fariborz Jahanian0a855d02009-03-23 19:10:40 +0000162 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000163 const llvm::Type *Int8PtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000164
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000165 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
166 const llvm::Type *ObjectPtrTy;
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000167
168 /// PtrObjectPtrTy - LLVM type for id *
169 const llvm::Type *PtrObjectPtrTy;
170
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000171 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000172 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000173 /// ProtocolPtrTy - LLVM type for external protocol handles
174 /// (typeof(Protocol))
175 const llvm::Type *ExternalProtocolPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000176
Daniel Dunbar19cd87e2008-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 Jahanianee0af742009-01-21 22:04:16 +0000181
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000182 /// SuperTy - LLVM type for struct objc_super.
183 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000184 /// SuperPtrTy - LLVM type for struct objc_super *.
185 const llvm::Type *SuperPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000186
Fariborz Jahanian30bc5712009-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 Jahaniand55b6fc2009-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 Lattner72db6c32009-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 Jahaniandb286862009-01-22 00:37:21 +0000220
Chris Lattner72db6c32009-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 Jahaniandb286862009-01-22 00:37:21 +0000247
248 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-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 Jahaniandb286862009-01-22 00:37:21 +0000256
257 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-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 Jahaniandb286862009-01-22 00:37:21 +0000266
267 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-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 Jahaniandb286862009-01-22 00:37:21 +0000275
276 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-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 Jahaniandb286862009-01-22 00:37:21 +0000284
285 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-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 Carlssonf57c5b22009-02-16 22:59:18 +0000293
294 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-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 Carlssonf57c5b22009-02-16 22:59:18 +0000302
Daniel Dunbar1c566672009-02-24 01:43:46 +0000303 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-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 Dunbar1c566672009-02-24 01:43:46 +0000311
312 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-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 Dunbar1c566672009-02-24 01:43:46 +0000320
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000321 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
322 ~ObjCCommonTypesHelper(){}
323};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000324
Fariborz Jahanianee0af742009-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 Lattner4176b0c2009-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 Jahanianee0af742009-01-21 22:04:16 +0000392
393public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000394 /// SymtabTy - LLVM type for struct objc_symtab.
395 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000396 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
397 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000398 /// ModuleTy - LLVM type for struct objc_module.
399 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000400
Daniel Dunbar6efc0c52008-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 Dunbar6efc0c52008-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 Dunbar86e253a2008-08-22 20:34:54 +0000424 /// CategoryTy - LLVM type for struct objc_category.
425 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-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 Dunbar27f9d772008-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 Dunbar27f9d772008-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 Carlsson124526b2008-09-09 10:10:21 +0000444
445 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
446 const llvm::Type *ExceptionDataTy;
447
Anders Carlsson124526b2008-09-09 10:10:21 +0000448 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-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 Carlsson124526b2008-09-09 10:10:21 +0000456
457 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-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 Carlsson124526b2008-09-09 10:10:21 +0000465
466 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-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 Carlsson124526b2008-09-09 10:10:21 +0000475
476 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-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 Carlsson124526b2008-09-09 10:10:21 +0000486
487 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-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 Lattner10cac6f2008-11-15 21:26:17 +0000497
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000498public:
499 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000500 ~ObjCTypesHelper() {}
Daniel Dunbar5669e572008-10-17 03:24:53 +0000501
502
Chris Lattner74391b42009-03-22 21:03:39 +0000503 llvm::Constant *getSendFn(bool IsSuper) {
Chris Lattner4176b0c2009-04-22 02:32:31 +0000504 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
Daniel Dunbar5669e572008-10-17 03:24:53 +0000505 }
506
Chris Lattner74391b42009-03-22 21:03:39 +0000507 llvm::Constant *getSendStretFn(bool IsSuper) {
Chris Lattner4176b0c2009-04-22 02:32:31 +0000508 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
Daniel Dunbar5669e572008-10-17 03:24:53 +0000509 }
510
Chris Lattner74391b42009-03-22 21:03:39 +0000511 llvm::Constant *getSendFpretFn(bool IsSuper) {
Chris Lattner4176b0c2009-04-22 02:32:31 +0000512 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
Daniel Dunbar5669e572008-10-17 03:24:53 +0000513 }
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000514};
515
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000516/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000517/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000518class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000519public:
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000520
Fariborz Jahaniand55b6fc2009-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 Dunbar948e2582009-02-15 07:36:20 +0000530 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
531 const llvm::Type *ProtocolnfABIPtrTy;
532
Fariborz Jahaniand55b6fc2009-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 Jahanianaa23b572009-01-23 23:53:38 +0000542 // ClassnfABIPtrTy - LLVM for struct _class_t*
543 const llvm::Type *ClassnfABIPtrTy;
544
Fariborz Jahaniand55b6fc2009-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 Jahanian2e4672b2009-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 Jahanian83a8a752009-02-04 20:42:28 +0000571 // MessageRefCTy - clang type for struct _message_ref_t
572 QualType MessageRefCTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000573
574 // MessageRefPtrTy - LLVM for struct _message_ref_t*
575 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000576 // MessageRefCPtrTy - clang type for struct _message_ref_t*
577 QualType MessageRefCPtrTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000578
Fariborz Jahanianef163782009-02-05 01:13:09 +0000579 // MessengerTy - Type of the messenger (shown as IMP above)
580 const llvm::FunctionType *MessengerTy;
581
Fariborz Jahanian2e4672b2009-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 Dunbar8ecbaf22009-02-24 07:47:38 +0000591
Chris Lattner1c02f862009-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 Dunbar8ecbaf22009-02-24 07:47:38 +0000665 /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C
666 /// exception personality function.
Chris Lattnerb02e53b2009-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 Dunbar8ecbaf22009-02-24 07:47:38 +0000675
Chris Lattner8a569112009-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 Dunbare588b992009-03-01 04:46:24 +0000699
700 const llvm::StructType *EHTypeTy;
701 const llvm::Type *EHTypePtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000702
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000703 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
704 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000705};
706
707class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000708public:
709 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000710 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000711 public:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000712 unsigned ivar_bytepos;
713 unsigned ivar_size;
714 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
715 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000716
717 // Allow sorting based on byte pos.
718 bool operator<(const GC_IVAR &b) const {
719 return ivar_bytepos < b.ivar_bytepos;
720 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000721 };
722
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000723 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000724 public:
725 unsigned skip;
726 unsigned scan;
727 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
728 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000729 };
730
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000731protected:
732 CodeGen::CodeGenModule &CGM;
733 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000734 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000735
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000736 // gc ivar layout bitmap calculation helper caches.
737 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
738 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000739
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000740 /// LazySymbols - Symbols to generate a lazy reference for. See
741 /// DefinedSymbols and FinishModule().
742 std::set<IdentifierInfo*> LazySymbols;
743
744 /// DefinedSymbols - External symbols which are defined by this
745 /// module. The symbols in this list and LazySymbols are used to add
746 /// special linker symbols which ensure that Objective-C modules are
747 /// linked properly.
748 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000749
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000750 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000751 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000752
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000753 /// MethodVarNames - uniqued method variable names.
754 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000755
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000756 /// MethodVarTypes - uniqued method type signatures. We have to use
757 /// a StringMap here because have no other unique reference.
758 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000759
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000760 /// MethodDefinitions - map of methods which have been defined in
761 /// this translation unit.
762 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000763
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000764 /// PropertyNames - uniqued method variable names.
765 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000766
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000767 /// ClassReferences - uniqued class references.
768 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000769
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000770 /// SelectorReferences - uniqued selector references.
771 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000772
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000773 /// Protocols - Protocols for which an objc_protocol structure has
774 /// been emitted. Forward declarations are handled by creating an
775 /// empty structure whose initializer is filled in when/if defined.
776 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000777
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000778 /// DefinedProtocols - Protocols which have actually been
779 /// defined. We should not need this, see FIXME in GenerateProtocol.
780 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000781
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000782 /// DefinedClasses - List of defined classes.
783 std::vector<llvm::GlobalValue*> DefinedClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000784
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000785 /// DefinedCategories - List of defined categories.
786 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000787
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000788 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000789 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000790 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000791
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000792 /// GetNameForMethod - Return a name for the given method.
793 /// \param[out] NameOut - The return value.
794 void GetNameForMethod(const ObjCMethodDecl *OMD,
795 const ObjCContainerDecl *CD,
796 std::string &NameOut);
797
798 /// GetMethodVarName - Return a unique constant for the given
799 /// selector's name. The return value has type char *.
800 llvm::Constant *GetMethodVarName(Selector Sel);
801 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
802 llvm::Constant *GetMethodVarName(const std::string &Name);
803
804 /// GetMethodVarType - Return a unique constant for the given
805 /// selector's name. The return value has type char *.
806
807 // FIXME: This is a horrible name.
808 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000809 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000810
811 /// GetPropertyName - Return a unique constant for the given
812 /// name. The return value has type char *.
813 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
814
815 // FIXME: This can be dropped once string functions are unified.
816 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
817 const Decl *Container);
818
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000819 /// GetClassName - Return a unique constant for the given selector's
820 /// name. The return value has type char *.
821 llvm::Constant *GetClassName(IdentifierInfo *Ident);
822
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000823 /// BuildIvarLayout - Builds ivar layout bitmap for the class
824 /// implementation for the __strong or __weak case.
825 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000826 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
827 bool ForStrongLayout);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000828
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000829 void BuildAggrIvarLayout(const ObjCInterfaceDecl *OI,
830 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000831 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +0000832 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000833 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000834 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000835
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000836 /// GetIvarLayoutName - Returns a unique constant for the given
837 /// ivar layout bitmap.
838 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
839 const ObjCCommonTypesHelper &ObjCTypes);
840
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000841 /// EmitPropertyList - Emit the given property list. The return
842 /// value has type PropertyListPtrTy.
843 llvm::Constant *EmitPropertyList(const std::string &Name,
844 const Decl *Container,
845 const ObjCContainerDecl *OCD,
846 const ObjCCommonTypesHelper &ObjCTypes);
847
Fariborz Jahanianda320092009-01-29 19:24:30 +0000848 /// GetProtocolRef - Return a reference to the internal protocol
849 /// description, creating an empty one if it has not been
850 /// defined. The return value has type ProtocolPtrTy.
851 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000852
Chris Lattnercd0ee142009-03-31 08:33:16 +0000853 /// GetFieldBaseOffset - return's field byte offset.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000854 uint64_t GetFieldBaseOffset(const ObjCInterfaceDecl *OI,
855 const llvm::StructLayout *Layout,
Chris Lattnercd0ee142009-03-31 08:33:16 +0000856 const FieldDecl *Field);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000857
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000858 /// CreateMetadataVar - Create a global variable with internal
859 /// linkage for use by the Objective-C runtime.
860 ///
861 /// This is a convenience wrapper which not only creates the
862 /// variable, but also sets the section and alignment and adds the
863 /// global to the UsedGlobals list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000864 ///
865 /// \param Name - The variable name.
866 /// \param Init - The variable initializer; this is also used to
867 /// define the type of the variable.
868 /// \param Section - The section the variable should go into, or 0.
869 /// \param Align - The alignment for the variable, or 0.
870 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000871 /// "llvm.used".
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000872 llvm::GlobalVariable *CreateMetadataVar(const std::string &Name,
873 llvm::Constant *Init,
874 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000875 unsigned Align,
876 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000877
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000878 /// GetNamedIvarList - Return the list of ivars in the interface
879 /// itself (not including super classes and not including unnamed
880 /// bitfields).
881 ///
882 /// For the non-fragile ABI, this also includes synthesized property
883 /// ivars.
884 void GetNamedIvarList(const ObjCInterfaceDecl *OID,
885 llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const;
886
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000887public:
888 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
889 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000890
Steve Naroff33fdb732009-03-31 16:53:37 +0000891 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000892
893 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
894 const ObjCContainerDecl *CD=0);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000895
896 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
897
898 /// GetOrEmitProtocol - Get the protocol object for the given
899 /// declaration, emitting it if necessary. The return value has type
900 /// ProtocolPtrTy.
901 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
902
903 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
904 /// object for the given declaration, emitting it if needed. These
905 /// forward references will be filled in with empty bodies if no
906 /// definition is seen. The return value has type ProtocolPtrTy.
907 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000908};
909
910class CGObjCMac : public CGObjCCommonMac {
911private:
912 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000913 /// EmitImageInfo - Emit the image info marker used to encode some module
914 /// level information.
915 void EmitImageInfo();
916
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000917 /// EmitModuleInfo - Another marker encoding module level
918 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000919 void EmitModuleInfo();
920
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000921 /// EmitModuleSymols - Emit module symbols, the list of defined
922 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000923 llvm::Constant *EmitModuleSymbols();
924
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000925 /// FinishModule - Write out global data structures at the end of
926 /// processing a translation unit.
927 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000928
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000929 /// EmitClassExtension - Generate the class extension structure used
930 /// to store the weak ivar layout and properties. The return value
931 /// has type ClassExtensionPtrTy.
932 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
933
934 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
935 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000936 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000937 const ObjCInterfaceDecl *ID);
938
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000939 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000940 QualType ResultType,
941 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000942 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000943 QualType Arg0Ty,
944 bool IsSuper,
945 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000946
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000947 /// EmitIvarList - Emit the ivar list for the given
948 /// implementation. If ForClass is true the list of class ivars
949 /// (i.e. metaclass ivars) is emitted, otherwise the list of
950 /// interface ivars will be emitted. The return value has type
951 /// IvarListPtrTy.
952 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000953 bool ForClass);
954
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000955 /// EmitMetaClass - Emit a forward reference to the class structure
956 /// for the metaclass of the given interface. The return value has
957 /// type ClassPtrTy.
958 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
959
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000960 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000961 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000962 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
963 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000964 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000965
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000966 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000967
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000968 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000969
970 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000971 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000972 llvm::Constant *EmitMethodList(const std::string &Name,
973 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000974 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000975
976 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000977 /// method declarations.
978 /// - TypeName: The name for the type containing the methods.
979 /// - IsProtocol: True iff these methods are for a protocol.
980 /// - ClassMethds: True iff these are class methods.
981 /// - Required: When true, only "required" methods are
982 /// listed. Similarly, when false only "optional" methods are
983 /// listed. For classes this should always be true.
984 /// - begin, end: The method list to output.
985 ///
986 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000987 llvm::Constant *EmitMethodDescList(const std::string &Name,
988 const char *Section,
989 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000990
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000991 /// GetOrEmitProtocol - Get the protocol object for the given
992 /// declaration, emitting it if necessary. The return value has type
993 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000994 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000995
996 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
997 /// object for the given declaration, emitting it if needed. These
998 /// forward references will be filled in with empty bodies if no
999 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001000 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001001
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001002 /// EmitProtocolExtension - Generate the protocol extension
1003 /// structure used to store optional instance and class methods, and
1004 /// protocol properties. The return value has type
1005 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001006 llvm::Constant *
1007 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1008 const ConstantVector &OptInstanceMethods,
1009 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001010
1011 /// EmitProtocolList - Generate the list of referenced
1012 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +00001013 llvm::Constant *EmitProtocolList(const std::string &Name,
1014 ObjCProtocolDecl::protocol_iterator begin,
1015 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001016
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001017 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1018 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001019 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001020
Fariborz Jahanianda320092009-01-29 19:24:30 +00001021 public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001022 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001023
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001024 virtual llvm::Function *ModuleInitFunction();
1025
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001026 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001027 QualType ResultType,
1028 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001029 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001030 bool IsClassMessage,
1031 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001032
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001033 virtual CodeGen::RValue
1034 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001035 QualType ResultType,
1036 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001037 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001038 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001039 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001040 bool IsClassMessage,
1041 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001042
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001043 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001044 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001045
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001046 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001047
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001048 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001049
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001050 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001051
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001052 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001053 const ObjCProtocolDecl *PD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00001054
Chris Lattner74391b42009-03-22 21:03:39 +00001055 virtual llvm::Constant *GetPropertyGetFunction();
1056 virtual llvm::Constant *GetPropertySetFunction();
1057 virtual llvm::Constant *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001058
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001059 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1060 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001061 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1062 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001063 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001064 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001065 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1066 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001067 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1068 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001069 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1070 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001071 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1072 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001073
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001074 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1075 QualType ObjectTy,
1076 llvm::Value *BaseValue,
1077 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001078 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001079 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001080 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001081 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001082};
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001083
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001084class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001085private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001086 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001087 llvm::GlobalVariable* ObjCEmptyCacheVar;
1088 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001089
Daniel Dunbar11394522009-04-18 08:51:00 +00001090 /// SuperClassReferences - uniqued super class references.
1091 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
1092
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001093 /// MetaClassReferences - uniqued meta class references.
1094 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001095
1096 /// EHTypeReferences - uniqued class ehtype references.
1097 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001098
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001099 /// FinishNonFragileABIModule - Write out global data structures at the end of
1100 /// processing a translation unit.
1101 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001102
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00001103 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1104 unsigned InstanceStart,
1105 unsigned InstanceSize,
1106 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001107 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
1108 llvm::Constant *IsAGV,
1109 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001110 llvm::Constant *ClassRoGV,
1111 bool HiddenVisibility);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001112
1113 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
1114
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001115 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
1116
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001117 /// EmitMethodList - Emit the method list for the given
1118 /// implementation. The return value has type MethodListnfABITy.
1119 llvm::Constant *EmitMethodList(const std::string &Name,
1120 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001121 const ConstantVector &Methods);
1122 /// EmitIvarList - Emit the ivar list for the given
1123 /// implementation. If ForClass is true the list of class ivars
1124 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1125 /// interface ivars will be emitted. The return value has type
1126 /// IvarListnfABIPtrTy.
1127 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00001128
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001129 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001130 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001131 unsigned long int offset);
1132
Fariborz Jahanianda320092009-01-29 19:24:30 +00001133 /// GetOrEmitProtocol - Get the protocol object for the given
1134 /// declaration, emitting it if necessary. The return value has type
1135 /// ProtocolPtrTy.
1136 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
1137
1138 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1139 /// object for the given declaration, emitting it if needed. These
1140 /// forward references will be filled in with empty bodies if no
1141 /// definition is seen. The return value has type ProtocolPtrTy.
1142 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
1143
1144 /// EmitProtocolList - Generate the list of referenced
1145 /// protocols. The return value has type ProtocolListPtrTy.
1146 llvm::Constant *EmitProtocolList(const std::string &Name,
1147 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001148 ObjCProtocolDecl::protocol_iterator end);
1149
1150 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1151 QualType ResultType,
1152 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00001153 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001154 QualType Arg0Ty,
1155 bool IsSuper,
1156 const CallArgList &CallArgs);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001157
1158 /// GetClassGlobal - Return the global variable for the Objective-C
1159 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001160 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
1161
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001162 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001163 /// for the given class reference.
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001164 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001165 const ObjCInterfaceDecl *ID);
1166
1167 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1168 /// for the given super class reference.
1169 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1170 const ObjCInterfaceDecl *ID);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001171
1172 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1173 /// meta-data
1174 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
1175 const ObjCInterfaceDecl *ID);
1176
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001177 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1178 /// the given ivar.
1179 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001180 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00001181 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001182 const ObjCIvarDecl *Ivar);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001183
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001184 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1185 /// for the given selector.
1186 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbare588b992009-03-01 04:46:24 +00001187
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001188 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001189 /// interface. The return value has type EHTypePtrTy.
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001190 llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
1191 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001192
1193 const char *getMetaclassSymbolPrefix() const {
1194 return "OBJC_METACLASS_$_";
1195 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001196
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001197 const char *getClassSymbolPrefix() const {
1198 return "OBJC_CLASS_$_";
1199 }
1200
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001201 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001202 uint32_t &InstanceStart,
1203 uint32_t &InstanceSize);
1204
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001205public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001206 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001207 // FIXME. All stubs for now!
1208 virtual llvm::Function *ModuleInitFunction();
1209
1210 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
1211 QualType ResultType,
1212 Selector Sel,
1213 llvm::Value *Receiver,
1214 bool IsClassMessage,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001215 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001216
1217 virtual CodeGen::RValue
1218 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
1219 QualType ResultType,
1220 Selector Sel,
1221 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001222 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001223 llvm::Value *Receiver,
1224 bool IsClassMessage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001225 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001226
1227 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001228 const ObjCInterfaceDecl *ID);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001229
1230 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001231 { return EmitSelector(Builder, Sel); }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001232
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001233 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001234
1235 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001236 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001237 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001238
Chris Lattner74391b42009-03-22 21:03:39 +00001239 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001240 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001241 }
Chris Lattner74391b42009-03-22 21:03:39 +00001242 virtual llvm::Constant *GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001243 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001244 }
Chris Lattner74391b42009-03-22 21:03:39 +00001245 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001246 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001247 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001248
1249 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00001250 const Stmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001251 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001252 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001253 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001254 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001255 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001256 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001257 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001258 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001259 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001260 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001261 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001262 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001263 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1264 QualType ObjectTy,
1265 llvm::Value *BaseValue,
1266 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001267 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001268 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001269 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001270 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001271};
1272
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001273} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001274
1275/* *** Helper Functions *** */
1276
1277/// getConstantGEP() - Help routine to construct simple GEPs.
1278static llvm::Constant *getConstantGEP(llvm::Constant *C,
1279 unsigned idx0,
1280 unsigned idx1) {
1281 llvm::Value *Idxs[] = {
1282 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
1283 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
1284 };
1285 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
1286}
1287
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001288/// hasObjCExceptionAttribute - Return true if this class or any super
1289/// class has the __objc_exception__ attribute.
1290static bool hasObjCExceptionAttribute(const ObjCInterfaceDecl *OID) {
Daniel Dunbarb11fa0d2009-04-13 21:08:27 +00001291 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001292 return true;
1293 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
1294 return hasObjCExceptionAttribute(Super);
1295 return false;
1296}
1297
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001298/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001299
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001300CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
1301 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001302{
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001303 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001304 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001305}
1306
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001307/// GetClass - Return a reference to the class for the given interface
1308/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001309llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001310 const ObjCInterfaceDecl *ID) {
1311 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001312}
1313
1314/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001315llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001316 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001317}
1318
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001319/// Generate a constant CFString object.
1320/*
1321 struct __builtin_CFString {
1322 const int *isa; // point to __CFConstantStringClassReference
1323 int flags;
1324 const char *str;
1325 long length;
1326 };
1327*/
1328
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001329llvm::Constant *CGObjCCommonMac::GenerateConstantString(
Steve Naroff33fdb732009-03-31 16:53:37 +00001330 const ObjCStringLiteral *SL) {
Steve Naroff8d4141f2009-04-01 13:55:36 +00001331 return CGM.GetAddrOfConstantCFString(SL->getString());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001332}
1333
1334/// Generates a message send where the super is the receiver. This is
1335/// a message send to self with special delivery semantics indicating
1336/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001337CodeGen::RValue
1338CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001339 QualType ResultType,
1340 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001341 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001342 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001343 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001344 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001345 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001346 // Create and init a super structure; this is a (receiver, class)
1347 // pair we will pass to objc_msgSendSuper.
1348 llvm::Value *ObjCSuper =
1349 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
1350 llvm::Value *ReceiverAsObject =
1351 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
1352 CGF.Builder.CreateStore(ReceiverAsObject,
1353 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001354
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001355 // If this is a class message the metaclass is passed as the target.
1356 llvm::Value *Target;
1357 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001358 if (isCategoryImpl) {
1359 // Message sent to 'super' in a class method defined in a category
1360 // implementation requires an odd treatment.
1361 // If we are in a class method, we must retrieve the
1362 // _metaclass_ for the current class, pointed at by
1363 // the class's "isa" pointer. The following assumes that
1364 // isa" is the first ivar in a class (which it must be).
1365 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1366 Target = CGF.Builder.CreateStructGEP(Target, 0);
1367 Target = CGF.Builder.CreateLoad(Target);
1368 }
1369 else {
1370 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1371 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1372 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1373 Target = Super;
1374 }
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001375 } else {
1376 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1377 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001378 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
1379 // and ObjCTypes types.
1380 const llvm::Type *ClassTy =
1381 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001382 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001383 CGF.Builder.CreateStore(Target,
1384 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
1385
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001386 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001387 ObjCSuper, ObjCTypes.SuperPtrCTy,
1388 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001389}
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001390
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001391/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001392CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001393 QualType ResultType,
1394 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001395 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001396 bool IsClassMessage,
1397 const CallArgList &CallArgs) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001398 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001399 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001400 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001401}
1402
1403CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001404 QualType ResultType,
1405 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001406 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001407 QualType Arg0Ty,
1408 bool IsSuper,
1409 const CallArgList &CallArgs) {
1410 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001411 if (!IsSuper)
1412 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001413 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
1414 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
1415 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001416 CGF.getContext().getObjCSelType()));
1417 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001418
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001419 CodeGenTypes &Types = CGM.getTypes();
1420 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
Fariborz Jahanian65257ca2009-04-29 22:47:27 +00001421 // FIXME. vararg flag must be true when this API is used for 64bit code gen.
1422 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001423
1424 llvm::Constant *Fn;
Daniel Dunbar88b53962009-02-02 22:03:45 +00001425 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Daniel Dunbar5669e572008-10-17 03:24:53 +00001426 Fn = ObjCTypes.getSendStretFn(IsSuper);
1427 } else if (ResultType->isFloatingType()) {
1428 // FIXME: Sadly, this is wrong. This actually depends on the
1429 // architecture. This happens to be right for x86-32 though.
1430 Fn = ObjCTypes.getSendFpretFn(IsSuper);
1431 } else {
1432 Fn = ObjCTypes.getSendFn(IsSuper);
1433 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +00001434 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +00001435 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001436}
1437
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001438llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001439 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001440 // FIXME: I don't understand why gcc generates this, or where it is
1441 // resolved. Investigate. Its also wasteful to look this up over and
1442 // over.
1443 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1444
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001445 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
1446 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001447}
1448
Fariborz Jahanianda320092009-01-29 19:24:30 +00001449void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001450 // FIXME: We shouldn't need this, the protocol decl should contain
1451 // enough information to tell us whether this was a declaration or a
1452 // definition.
1453 DefinedProtocols.insert(PD->getIdentifier());
1454
1455 // If we have generated a forward reference to this protocol, emit
1456 // it now. Otherwise do nothing, the protocol objects are lazily
1457 // emitted.
1458 if (Protocols.count(PD->getIdentifier()))
1459 GetOrEmitProtocol(PD);
1460}
1461
Fariborz Jahanianda320092009-01-29 19:24:30 +00001462llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001463 if (DefinedProtocols.count(PD->getIdentifier()))
1464 return GetOrEmitProtocol(PD);
1465 return GetOrEmitProtocolRef(PD);
1466}
1467
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001468/*
1469 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1470 struct _objc_protocol {
1471 struct _objc_protocol_extension *isa;
1472 char *protocol_name;
1473 struct _objc_protocol_list *protocol_list;
1474 struct _objc__method_prototype_list *instance_methods;
1475 struct _objc__method_prototype_list *class_methods
1476 };
1477
1478 See EmitProtocolExtension().
1479*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001480llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1481 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1482
1483 // Early exit if a defining object has already been generated.
1484 if (Entry && Entry->hasInitializer())
1485 return Entry;
1486
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001487 // FIXME: I don't understand why gcc generates this, or where it is
1488 // resolved. Investigate. Its also wasteful to look this up over and
1489 // over.
1490 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1491
Chris Lattner8ec03f52008-11-24 03:54:41 +00001492 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001493
1494 // Construct method lists.
1495 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1496 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00001497 for (ObjCProtocolDecl::instmeth_iterator
1498 i = PD->instmeth_begin(CGM.getContext()),
1499 e = PD->instmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001500 ObjCMethodDecl *MD = *i;
1501 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1502 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1503 OptInstanceMethods.push_back(C);
1504 } else {
1505 InstanceMethods.push_back(C);
1506 }
1507 }
1508
Douglas Gregor6ab35242009-04-09 21:40:53 +00001509 for (ObjCProtocolDecl::classmeth_iterator
1510 i = PD->classmeth_begin(CGM.getContext()),
1511 e = PD->classmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001512 ObjCMethodDecl *MD = *i;
1513 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1514 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1515 OptClassMethods.push_back(C);
1516 } else {
1517 ClassMethods.push_back(C);
1518 }
1519 }
1520
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001521 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001522 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001523 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001524 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001525 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001526 PD->protocol_begin(),
1527 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001528 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001529 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
1530 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001531 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1532 InstanceMethods);
1533 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001534 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
1535 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001536 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1537 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001538 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
1539 Values);
1540
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001541 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001542 // Already created, fix the linkage and update the initializer.
1543 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001544 Entry->setInitializer(Init);
1545 } else {
1546 Entry =
1547 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
1548 llvm::GlobalValue::InternalLinkage,
1549 Init,
1550 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
1551 &CGM.getModule());
1552 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001553 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001554 UsedGlobals.push_back(Entry);
1555 // FIXME: Is this necessary? Why only for protocol?
1556 Entry->setAlignment(4);
1557 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001558
1559 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001560}
1561
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001562llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001563 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1564
1565 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001566 // We use the initializer as a marker of whether this is a forward
1567 // reference or not. At module finalization we add the empty
1568 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001569 Entry =
1570 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001571 llvm::GlobalValue::ExternalLinkage,
1572 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001573 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001574 &CGM.getModule());
1575 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001576 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001577 UsedGlobals.push_back(Entry);
1578 // FIXME: Is this necessary? Why only for protocol?
1579 Entry->setAlignment(4);
1580 }
1581
1582 return Entry;
1583}
1584
1585/*
1586 struct _objc_protocol_extension {
1587 uint32_t size;
1588 struct objc_method_description_list *optional_instance_methods;
1589 struct objc_method_description_list *optional_class_methods;
1590 struct objc_property_list *instance_properties;
1591 };
1592*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001593llvm::Constant *
1594CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1595 const ConstantVector &OptInstanceMethods,
1596 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001597 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001598 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001599 std::vector<llvm::Constant*> Values(4);
1600 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001601 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001602 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1603 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001604 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1605 OptInstanceMethods);
1606 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001607 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1608 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001609 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1610 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001611 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1612 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001613 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001614
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001615 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001616 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1617 Values[3]->isNullValue())
1618 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1619
1620 llvm::Constant *Init =
1621 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001622
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001623 // No special section, but goes in llvm.used
1624 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
1625 Init,
1626 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001627}
1628
1629/*
1630 struct objc_protocol_list {
1631 struct objc_protocol_list *next;
1632 long count;
1633 Protocol *list[];
1634 };
1635*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001636llvm::Constant *
1637CGObjCMac::EmitProtocolList(const std::string &Name,
1638 ObjCProtocolDecl::protocol_iterator begin,
1639 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001640 std::vector<llvm::Constant*> ProtocolRefs;
1641
Daniel Dunbardbc933702008-08-21 21:57:41 +00001642 for (; begin != end; ++begin)
1643 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001644
1645 // Just return null for empty protocol lists
1646 if (ProtocolRefs.empty())
1647 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1648
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001649 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001650 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1651
1652 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001653 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001654 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1655 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1656 Values[2] =
1657 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1658 ProtocolRefs.size()),
1659 ProtocolRefs);
1660
1661 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1662 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001663 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001664 4, false);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001665 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1666}
1667
1668/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001669 struct _objc_property {
1670 const char * const name;
1671 const char * const attributes;
1672 };
1673
1674 struct _objc_property_list {
1675 uint32_t entsize; // sizeof (struct _objc_property)
1676 uint32_t prop_count;
1677 struct _objc_property[prop_count];
1678 };
1679*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001680llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1681 const Decl *Container,
1682 const ObjCContainerDecl *OCD,
1683 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001684 std::vector<llvm::Constant*> Properties, Prop(2);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001685 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(CGM.getContext()),
1686 E = OCD->prop_end(CGM.getContext()); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00001687 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001688 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001689 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001690 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1691 Prop));
1692 }
1693
1694 // Return null for empty list.
1695 if (Properties.empty())
1696 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1697
1698 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001699 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001700 std::vector<llvm::Constant*> Values(3);
1701 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1702 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1703 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1704 Properties.size());
1705 Values[2] = llvm::ConstantArray::get(AT, Properties);
1706 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1707
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001708 llvm::GlobalVariable *GV =
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001709 CreateMetadataVar(Name, Init,
1710 (ObjCABI == 2) ? "__DATA, __objc_const" :
1711 "__OBJC,__property,regular,no_dead_strip",
1712 (ObjCABI == 2) ? 8 : 4,
1713 true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001714 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001715}
1716
1717/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001718 struct objc_method_description_list {
1719 int count;
1720 struct objc_method_description list[];
1721 };
1722*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001723llvm::Constant *
1724CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1725 std::vector<llvm::Constant*> Desc(2);
1726 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1727 ObjCTypes.SelectorPtrTy);
1728 Desc[1] = GetMethodVarType(MD);
1729 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1730 Desc);
1731}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001732
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001733llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1734 const char *Section,
1735 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001736 // Return null for empty list.
1737 if (Methods.empty())
1738 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1739
1740 std::vector<llvm::Constant*> Values(2);
1741 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1742 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1743 Methods.size());
1744 Values[1] = llvm::ConstantArray::get(AT, Methods);
1745 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1746
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001747 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001748 return llvm::ConstantExpr::getBitCast(GV,
1749 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001750}
1751
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001752/*
1753 struct _objc_category {
1754 char *category_name;
1755 char *class_name;
1756 struct _objc_method_list *instance_methods;
1757 struct _objc_method_list *class_methods;
1758 struct _objc_protocol_list *protocols;
1759 uint32_t size; // <rdar://4585769>
1760 struct _objc_property_list *instance_properties;
1761 };
1762 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001763void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001764 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001765
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001766 // FIXME: This is poor design, the OCD should have a pointer to the
1767 // category decl. Additionally, note that Category can be null for
1768 // the @implementation w/o an @interface case. Sema should just
1769 // create one for us as it does for @implementation so everyone else
1770 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001771 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001772 const ObjCCategoryDecl *Category =
1773 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001774 std::string ExtName(Interface->getNameAsString() + "_" +
1775 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001776
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001777 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001778 for (ObjCCategoryImplDecl::instmeth_iterator
1779 i = OCD->instmeth_begin(CGM.getContext()),
1780 e = OCD->instmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001781 // Instance methods should always be defined.
1782 InstanceMethods.push_back(GetMethodConstant(*i));
1783 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001784 for (ObjCCategoryImplDecl::classmeth_iterator
1785 i = OCD->classmeth_begin(CGM.getContext()),
1786 e = OCD->classmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001787 // Class methods should always be defined.
1788 ClassMethods.push_back(GetMethodConstant(*i));
1789 }
1790
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001791 std::vector<llvm::Constant*> Values(7);
1792 Values[0] = GetClassName(OCD->getIdentifier());
1793 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00001794 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001795 Values[2] =
1796 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1797 ExtName,
1798 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001799 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001800 Values[3] =
1801 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001802 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001803 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001804 if (Category) {
1805 Values[4] =
1806 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1807 Category->protocol_begin(),
1808 Category->protocol_end());
1809 } else {
1810 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1811 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001812 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001813
1814 // If there is no category @interface then there can be no properties.
1815 if (Category) {
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001816 Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001817 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001818 } else {
1819 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1820 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001821
1822 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1823 Values);
1824
1825 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001826 CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init,
1827 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001828 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001829 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001830}
1831
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001832// FIXME: Get from somewhere?
1833enum ClassFlags {
1834 eClassFlags_Factory = 0x00001,
1835 eClassFlags_Meta = 0x00002,
1836 // <rdr://5142207>
1837 eClassFlags_HasCXXStructors = 0x02000,
1838 eClassFlags_Hidden = 0x20000,
1839 eClassFlags_ABI2_Hidden = 0x00010,
1840 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1841};
1842
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001843/*
1844 struct _objc_class {
1845 Class isa;
1846 Class super_class;
1847 const char *name;
1848 long version;
1849 long info;
1850 long instance_size;
1851 struct _objc_ivar_list *ivars;
1852 struct _objc_method_list *methods;
1853 struct _objc_cache *cache;
1854 struct _objc_protocol_list *protocols;
1855 // Objective-C 1.0 extensions (<rdr://4585769>)
1856 const char *ivar_layout;
1857 struct _objc_class_ext *ext;
1858 };
1859
1860 See EmitClassExtension();
1861 */
1862void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001863 DefinedSymbols.insert(ID->getIdentifier());
1864
Chris Lattner8ec03f52008-11-24 03:54:41 +00001865 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001866 // FIXME: Gross
1867 ObjCInterfaceDecl *Interface =
1868 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001869 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001870 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001871 Interface->protocol_begin(),
1872 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001873 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar2bebbf02009-05-03 10:46:44 +00001874 unsigned Size =
1875 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001876
1877 // FIXME: Set CXX-structors flag.
Daniel Dunbar04d40782009-04-14 06:00:08 +00001878 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001879 Flags |= eClassFlags_Hidden;
1880
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001881 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001882 for (ObjCImplementationDecl::instmeth_iterator
1883 i = ID->instmeth_begin(CGM.getContext()),
1884 e = ID->instmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001885 // Instance methods should always be defined.
1886 InstanceMethods.push_back(GetMethodConstant(*i));
1887 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001888 for (ObjCImplementationDecl::classmeth_iterator
1889 i = ID->classmeth_begin(CGM.getContext()),
1890 e = ID->classmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001891 // Class methods should always be defined.
1892 ClassMethods.push_back(GetMethodConstant(*i));
1893 }
1894
Douglas Gregor653f1b12009-04-23 01:02:12 +00001895 for (ObjCImplementationDecl::propimpl_iterator
1896 i = ID->propimpl_begin(CGM.getContext()),
1897 e = ID->propimpl_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001898 ObjCPropertyImplDecl *PID = *i;
1899
1900 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1901 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1902
1903 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1904 if (llvm::Constant *C = GetMethodConstant(MD))
1905 InstanceMethods.push_back(C);
1906 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1907 if (llvm::Constant *C = GetMethodConstant(MD))
1908 InstanceMethods.push_back(C);
1909 }
1910 }
1911
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001912 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar5384b092009-05-03 08:56:52 +00001913 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001914 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001915 // Record a reference to the super class.
1916 LazySymbols.insert(Super->getIdentifier());
1917
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001918 Values[ 1] =
1919 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1920 ObjCTypes.ClassPtrTy);
1921 } else {
1922 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1923 }
1924 Values[ 2] = GetClassName(ID->getIdentifier());
1925 // Version is always 0.
1926 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1927 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1928 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001929 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001930 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001931 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001932 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001933 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001934 // cache is always NULL.
1935 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1936 Values[ 9] = Protocols;
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00001937 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001938 Values[11] = EmitClassExtension(ID);
1939 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1940 Values);
1941
1942 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001943 CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init,
1944 "__OBJC,__class,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001945 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001946 DefinedClasses.push_back(GV);
1947}
1948
1949llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1950 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001951 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001952 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001953 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001954
Daniel Dunbar04d40782009-04-14 06:00:08 +00001955 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001956 Flags |= eClassFlags_Hidden;
1957
1958 std::vector<llvm::Constant*> Values(12);
1959 // The isa for the metaclass is the root of the hierarchy.
1960 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1961 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1962 Root = Super;
1963 Values[ 0] =
1964 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1965 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001966 // The super class for the metaclass is emitted as the name of the
1967 // super class. The runtime fixes this up to point to the
1968 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001969 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1970 Values[ 1] =
1971 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1972 ObjCTypes.ClassPtrTy);
1973 } else {
1974 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1975 }
1976 Values[ 2] = GetClassName(ID->getIdentifier());
1977 // Version is always 0.
1978 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1979 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1980 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001981 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001982 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001983 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001984 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001985 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001986 // cache is always NULL.
1987 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1988 Values[ 9] = Protocols;
1989 // ivar_layout for metaclass is always NULL.
1990 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1991 // The class extension is always unused for metaclasses.
1992 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1993 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1994 Values);
1995
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001996 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001997 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001998
1999 // Check for a forward reference.
2000 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2001 if (GV) {
2002 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2003 "Forward metaclass reference has incorrect type.");
2004 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2005 GV->setInitializer(Init);
2006 } else {
2007 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
2008 llvm::GlobalValue::InternalLinkage,
2009 Init, Name,
2010 &CGM.getModule());
2011 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002012 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002013 GV->setAlignment(4);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002014 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002015
2016 return GV;
2017}
2018
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002019llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002020 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002021
2022 // FIXME: Should we look these up somewhere other than the
2023 // module. Its a bit silly since we only generate these while
2024 // processing an implementation, so exactly one pointer would work
2025 // if know when we entered/exitted an implementation block.
2026
2027 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002028 // Previously, metaclass with internal linkage may have been defined.
2029 // pass 'true' as 2nd argument so it is returned.
2030 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002031 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2032 "Forward metaclass reference has incorrect type.");
2033 return GV;
2034 } else {
2035 // Generate as an external reference to keep a consistent
2036 // module. This will be patched up when we emit the metaclass.
2037 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
2038 llvm::GlobalValue::ExternalLinkage,
2039 0,
2040 Name,
2041 &CGM.getModule());
2042 }
2043}
2044
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002045/*
2046 struct objc_class_ext {
2047 uint32_t size;
2048 const char *weak_ivar_layout;
2049 struct _objc_property_list *properties;
2050 };
2051*/
2052llvm::Constant *
2053CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
2054 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002055 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002056
2057 std::vector<llvm::Constant*> Values(3);
2058 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002059 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002060 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002061 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002062
2063 // Return null if no extension bits are used.
2064 if (Values[1]->isNullValue() && Values[2]->isNullValue())
2065 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
2066
2067 llvm::Constant *Init =
2068 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002069 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002070 Init, "__OBJC,__class_ext,regular,no_dead_strip",
2071 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002072}
2073
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00002074/// getInterfaceDeclForIvar - Get the interface declaration node where
2075/// this ivar is declared in.
2076/// FIXME. Ideally, this info should be in the ivar node. But currently
2077/// it is not and prevailing wisdom is that ASTs should not have more
2078/// info than is absolutely needed, even though this info reflects the
2079/// source language.
2080///
2081static const ObjCInterfaceDecl *getInterfaceDeclForIvar(
2082 const ObjCInterfaceDecl *OI,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002083 const ObjCIvarDecl *IVD,
2084 ASTContext &Context) {
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00002085 if (!OI)
2086 return 0;
2087 assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface");
2088 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
2089 E = OI->ivar_end(); I != E; ++I)
2090 if ((*I)->getIdentifier() == IVD->getIdentifier())
2091 return OI;
Fariborz Jahanian5a4b4532009-03-31 17:00:52 +00002092 // look into properties.
Douglas Gregor6ab35242009-04-09 21:40:53 +00002093 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(Context),
2094 E = OI->prop_end(Context); I != E; ++I) {
Fariborz Jahanian5a4b4532009-03-31 17:00:52 +00002095 ObjCPropertyDecl *PDecl = (*I);
2096 if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl())
2097 if (IV->getIdentifier() == IVD->getIdentifier())
2098 return OI;
2099 }
Douglas Gregor6ab35242009-04-09 21:40:53 +00002100 return getInterfaceDeclForIvar(OI->getSuperClass(), IVD, Context);
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00002101}
2102
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002103/*
2104 struct objc_ivar {
2105 char *ivar_name;
2106 char *ivar_type;
2107 int ivar_offset;
2108 };
2109
2110 struct objc_ivar_list {
2111 int ivar_count;
2112 struct objc_ivar list[count];
2113 };
2114 */
2115llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002116 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002117 std::vector<llvm::Constant*> Ivars, Ivar(3);
2118
2119 // When emitting the root class GCC emits ivar entries for the
2120 // actual class structure. It is not clear if we need to follow this
2121 // behavior; for now lets try and get away with not doing it. If so,
2122 // the cleanest solution would be to make up an ObjCInterfaceDecl
2123 // for the class.
2124 if (ForClass)
2125 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002126
2127 ObjCInterfaceDecl *OID =
2128 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002129
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002130 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
2131 GetNamedIvarList(OID, OIvars);
2132
2133 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2134 ObjCIvarDecl *IVD = OIvars[i];
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002135 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2136 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00002137 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002138 ComputeIvarBaseOffset(CGM, OID, IVD));
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002139 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002140 }
2141
2142 // Return null for empty list.
2143 if (Ivars.empty())
2144 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
2145
2146 std::vector<llvm::Constant*> Values(2);
2147 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
2148 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
2149 Ivars.size());
2150 Values[1] = llvm::ConstantArray::get(AT, Ivars);
2151 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2152
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002153 llvm::GlobalVariable *GV;
2154 if (ForClass)
2155 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(),
Daniel Dunbar58a29122009-03-09 22:18:41 +00002156 Init, "__OBJC,__class_vars,regular,no_dead_strip",
2157 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002158 else
2159 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_"
2160 + ID->getNameAsString(),
2161 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002162 4, true);
2163 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002164}
2165
2166/*
2167 struct objc_method {
2168 SEL method_name;
2169 char *method_types;
2170 void *method;
2171 };
2172
2173 struct objc_method_list {
2174 struct objc_method_list *obsolete;
2175 int count;
2176 struct objc_method methods_list[count];
2177 };
2178*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002179
2180/// GetMethodConstant - Return a struct objc_method constant for the
2181/// given method if it has been defined. The result is null if the
2182/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002183llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002184 // FIXME: Use DenseMap::lookup
2185 llvm::Function *Fn = MethodDefinitions[MD];
2186 if (!Fn)
2187 return 0;
2188
2189 std::vector<llvm::Constant*> Method(3);
2190 Method[0] =
2191 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2192 ObjCTypes.SelectorPtrTy);
2193 Method[1] = GetMethodVarType(MD);
2194 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
2195 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
2196}
2197
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002198llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
2199 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002200 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002201 // Return null for empty list.
2202 if (Methods.empty())
2203 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
2204
2205 std::vector<llvm::Constant*> Values(3);
2206 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
2207 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
2208 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
2209 Methods.size());
2210 Values[2] = llvm::ConstantArray::get(AT, Methods);
2211 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2212
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002213 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002214 return llvm::ConstantExpr::getBitCast(GV,
2215 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002216}
2217
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002218llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00002219 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002220 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002221 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002222
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002223 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002224 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002225 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002226 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002227 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002228 llvm::GlobalValue::InternalLinkage,
2229 Name,
2230 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002231 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002232
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002233 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002234}
2235
Daniel Dunbar48fa0642009-04-19 02:03:42 +00002236/// GetFieldBaseOffset - return the field's byte offset.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002237uint64_t CGObjCCommonMac::GetFieldBaseOffset(const ObjCInterfaceDecl *OI,
2238 const llvm::StructLayout *Layout,
Chris Lattnercd0ee142009-03-31 08:33:16 +00002239 const FieldDecl *Field) {
Daniel Dunbar97776872009-04-22 07:32:20 +00002240 // Is this a C struct?
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00002241 if (!OI)
2242 return Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
Daniel Dunbar97776872009-04-22 07:32:20 +00002243 return ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002244}
2245
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002246llvm::GlobalVariable *
2247CGObjCCommonMac::CreateMetadataVar(const std::string &Name,
2248 llvm::Constant *Init,
2249 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002250 unsigned Align,
2251 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002252 const llvm::Type *Ty = Init->getType();
2253 llvm::GlobalVariable *GV =
2254 new llvm::GlobalVariable(Ty, false,
2255 llvm::GlobalValue::InternalLinkage,
2256 Init,
2257 Name,
2258 &CGM.getModule());
2259 if (Section)
2260 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002261 if (Align)
2262 GV->setAlignment(Align);
2263 if (AddToUsed)
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002264 UsedGlobals.push_back(GV);
2265 return GV;
2266}
2267
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002268llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002269 // Abuse this interface function as a place to finalize.
2270 FinishModule();
2271
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002272 return NULL;
2273}
2274
Chris Lattner74391b42009-03-22 21:03:39 +00002275llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002276 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002277}
2278
Chris Lattner74391b42009-03-22 21:03:39 +00002279llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002280 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002281}
2282
Chris Lattner74391b42009-03-22 21:03:39 +00002283llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002284 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002285}
2286
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002287/*
2288
2289Objective-C setjmp-longjmp (sjlj) Exception Handling
2290--
2291
2292The basic framework for a @try-catch-finally is as follows:
2293{
2294 objc_exception_data d;
2295 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002296 bool _call_try_exit = true;
2297
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002298 objc_exception_try_enter(&d);
2299 if (!setjmp(d.jmp_buf)) {
2300 ... try body ...
2301 } else {
2302 // exception path
2303 id _caught = objc_exception_extract(&d);
2304
2305 // enter new try scope for handlers
2306 if (!setjmp(d.jmp_buf)) {
2307 ... match exception and execute catch blocks ...
2308
2309 // fell off end, rethrow.
2310 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00002311 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002312 } else {
2313 // exception in catch block
2314 _rethrow = objc_exception_extract(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002315 _call_try_exit = false;
2316 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002317 }
2318 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002319 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002320
2321finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002322 if (_call_try_exit)
2323 objc_exception_try_exit(&d);
2324
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002325 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002326 ... dispatch to finally destination ...
2327
2328finally_rethrow:
2329 objc_exception_throw(_rethrow);
2330
2331finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002332}
2333
2334This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00002335uses _rethrow to determine if objc_exception_try_exit should be called
2336and if the object should be rethrown. This breaks in the face of
2337throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002338
2339We specialize this framework for a few particular circumstances:
2340
2341 - If there are no catch blocks, then we avoid emitting the second
2342 exception handling context.
2343
2344 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2345 e)) we avoid emitting the code to rethrow an uncaught exception.
2346
2347 - FIXME: If there is no @finally block we can do a few more
2348 simplifications.
2349
2350Rethrows and Jumps-Through-Finally
2351--
2352
2353Support for implicit rethrows and jumping through the finally block is
2354handled by storing the current exception-handling context in
2355ObjCEHStack.
2356
Daniel Dunbar898d5082008-09-30 01:06:03 +00002357In order to implement proper @finally semantics, we support one basic
2358mechanism for jumping through the finally block to an arbitrary
2359destination. Constructs which generate exits from a @try or @catch
2360block use this mechanism to implement the proper semantics by chaining
2361jumps, as necessary.
2362
2363This mechanism works like the one used for indirect goto: we
2364arbitrarily assign an ID to each destination and store the ID for the
2365destination in a variable prior to entering the finally block. At the
2366end of the finally block we simply create a switch to the proper
2367destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002368
2369Code gen for @synchronized(expr) stmt;
2370Effectively generating code for:
2371objc_sync_enter(expr);
2372@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002373*/
2374
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002375void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2376 const Stmt &S) {
2377 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002378 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00002379 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002380 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar55e87422008-11-11 02:29:29 +00002381 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
2382 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
2383 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar1c566672009-02-24 01:43:46 +00002384
2385 // For @synchronized, call objc_sync_enter(sync.expr). The
2386 // evaluation of the expression must occur before we enter the
2387 // @synchronized. We can safely avoid a temp here because jumps into
2388 // @synchronized are illegal & this will dominate uses.
2389 llvm::Value *SyncArg = 0;
2390 if (!isTry) {
2391 SyncArg =
2392 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2393 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00002394 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002395 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002396
2397 // Push an EH context entry, used for handling rethrows and jumps
2398 // through finally.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002399 CGF.PushCleanupBlock(FinallyBlock);
2400
Anders Carlsson273558f2009-02-07 21:37:21 +00002401 CGF.ObjCEHValueStack.push_back(0);
2402
Daniel Dunbar898d5082008-09-30 01:06:03 +00002403 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00002404 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2405 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002406 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
2407 "_rethrow");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002408 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
2409 "_call_try_exit");
2410 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr);
2411
Anders Carlsson80f25672008-09-09 17:59:25 +00002412 // Enter a new try block and call setjmp.
Chris Lattner34b02a12009-04-22 02:26:14 +00002413 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Anders Carlsson80f25672008-09-09 17:59:25 +00002414 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
2415 "jmpbufarray");
2416 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
Chris Lattner34b02a12009-04-22 02:26:14 +00002417 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlsson80f25672008-09-09 17:59:25 +00002418 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00002419
Daniel Dunbar55e87422008-11-11 02:29:29 +00002420 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2421 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002422 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002423 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002424
2425 // Emit the @try block.
2426 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002427 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
2428 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002429 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002430
2431 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002432 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002433
2434 // Retrieve the exception object. We may emit multiple blocks but
2435 // nothing can cross this so the value is already in SSA form.
Chris Lattner34b02a12009-04-22 02:26:14 +00002436 llvm::Value *Caught =
2437 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2438 ExceptionData, "caught");
Anders Carlsson273558f2009-02-07 21:37:21 +00002439 CGF.ObjCEHValueStack.back() = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002440 if (!isTry)
2441 {
2442 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002443 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002444 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002445 }
2446 else if (const ObjCAtCatchStmt* CatchStmt =
2447 cast<ObjCAtTryStmt>(S).getCatchStmts())
2448 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00002449 // Enter a new exception try block (in case a @catch block throws
2450 // an exception).
Chris Lattner34b02a12009-04-22 02:26:14 +00002451 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002452
Chris Lattner34b02a12009-04-22 02:26:14 +00002453 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlsson80f25672008-09-09 17:59:25 +00002454 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002455 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00002456
Daniel Dunbar55e87422008-11-11 02:29:29 +00002457 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
2458 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002459 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002460
2461 CGF.EmitBlock(CatchBlock);
2462
Daniel Dunbar55e40722008-09-27 07:03:52 +00002463 // Handle catch list. As a special case we check if everything is
2464 // matched and avoid generating code for falling off the end if
2465 // so.
2466 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00002467 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00002468 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00002469
Steve Naroff7ba138a2009-03-03 19:52:17 +00002470 const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Daniel Dunbar129271a2008-09-27 07:36:24 +00002471 const PointerType *PT = 0;
2472
Anders Carlsson80f25672008-09-09 17:59:25 +00002473 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00002474 if (!CatchParam) {
2475 AllMatched = true;
2476 } else {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002477 PT = CatchParam->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002478
Daniel Dunbar97f61d12008-09-27 22:21:14 +00002479 // catch(id e) always matches.
2480 // FIXME: For the time being we also match id<X>; this should
2481 // be rejected by Sema instead.
Steve Naroff389bf462009-02-12 17:52:19 +00002482 if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) ||
Steve Naroff7ba138a2009-03-03 19:52:17 +00002483 CatchParam->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00002484 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00002485 }
2486
Daniel Dunbar55e40722008-09-27 07:03:52 +00002487 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00002488 if (CatchParam) {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002489 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002490 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002491 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002492 }
Anders Carlsson1452f552008-09-11 08:21:54 +00002493
Anders Carlssondde0a942008-09-11 09:15:33 +00002494 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002495 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002496 break;
2497 }
2498
Daniel Dunbar129271a2008-09-27 07:36:24 +00002499 assert(PT && "Unexpected non-pointer type in @catch");
2500 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00002501 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002502 assert(ObjCType && "Catch parameter must have Objective-C type!");
2503
2504 // Check if the @catch block matches the exception object.
2505 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
2506
Chris Lattner34b02a12009-04-22 02:26:14 +00002507 llvm::Value *Match =
2508 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
2509 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00002510
Daniel Dunbar55e87422008-11-11 02:29:29 +00002511 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00002512
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002513 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002514 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002515
2516 // Emit the @catch block.
2517 CGF.EmitBlock(MatchedBlock);
Steve Naroff7ba138a2009-03-03 19:52:17 +00002518 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002519 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002520
2521 llvm::Value *Tmp =
Steve Naroff7ba138a2009-03-03 19:52:17 +00002522 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002523 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002524 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002525
2526 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002527 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002528
2529 CGF.EmitBlock(NextCatchBlock);
2530 }
2531
Daniel Dunbar55e40722008-09-27 07:03:52 +00002532 if (!AllMatched) {
2533 // None of the handlers caught the exception, so store it to be
2534 // rethrown at the end of the @finally block.
2535 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002536 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002537 }
2538
2539 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002540 CGF.EmitBlock(CatchHandler);
Chris Lattner34b02a12009-04-22 02:26:14 +00002541 CGF.Builder.CreateStore(
2542 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2543 ExceptionData),
Daniel Dunbar55e40722008-09-27 07:03:52 +00002544 RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002545 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002546 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002547 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00002548 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002549 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002550 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson80f25672008-09-09 17:59:25 +00002551 }
2552
Daniel Dunbar898d5082008-09-30 01:06:03 +00002553 // Pop the exception-handling stack entry. It is important to do
2554 // this now, because the code in the @finally block is not in this
2555 // context.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002556 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2557
Anders Carlsson273558f2009-02-07 21:37:21 +00002558 CGF.ObjCEHValueStack.pop_back();
2559
Anders Carlsson80f25672008-09-09 17:59:25 +00002560 // Emit the @finally block.
2561 CGF.EmitBlock(FinallyBlock);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002562 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2563
2564 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2565
2566 CGF.EmitBlock(FinallyExit);
Chris Lattner34b02a12009-04-22 02:26:14 +00002567 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002568
2569 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002570 if (isTry) {
2571 if (const ObjCAtFinallyStmt* FinallyStmt =
2572 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2573 CGF.EmitStmt(FinallyStmt->getFinallyBody());
Daniel Dunbar1c566672009-02-24 01:43:46 +00002574 } else {
2575 // Emit objc_sync_exit(expr); as finally's sole statement for
2576 // @synchronized.
Chris Lattnerbbccd612009-04-22 02:38:11 +00002577 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002578 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002579
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002580 // Emit the switch block
2581 if (Info.SwitchBlock)
2582 CGF.EmitBlock(Info.SwitchBlock);
2583 if (Info.EndBlock)
2584 CGF.EmitBlock(Info.EndBlock);
2585
Daniel Dunbar898d5082008-09-30 01:06:03 +00002586 CGF.EmitBlock(FinallyRethrow);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002587 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbar898d5082008-09-30 01:06:03 +00002588 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002589 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00002590
2591 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002592}
2593
2594void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002595 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002596 llvm::Value *ExceptionAsObject;
2597
2598 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2599 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2600 ExceptionAsObject =
2601 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2602 } else {
Anders Carlsson273558f2009-02-07 21:37:21 +00002603 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002604 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00002605 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002606 }
2607
Chris Lattnerbbccd612009-04-22 02:38:11 +00002608 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002609 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002610
2611 // Clear the insertion point to indicate we are in unreachable code.
2612 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002613}
2614
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002615/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002616/// object: objc_read_weak (id *src)
2617///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002618llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002619 llvm::Value *AddrWeakObj)
2620{
Eli Friedman8339b352009-03-07 03:57:15 +00002621 const llvm::Type* DestTy =
2622 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002623 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00002624 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002625 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00002626 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002627 return read_weak;
2628}
2629
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002630/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2631/// objc_assign_weak (id src, id *dst)
2632///
2633void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2634 llvm::Value *src, llvm::Value *dst)
2635{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002636 const llvm::Type * SrcTy = src->getType();
2637 if (!isa<llvm::PointerType>(SrcTy)) {
2638 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2639 assert(Size <= 8 && "does not support size > 8");
2640 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2641 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002642 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2643 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002644 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2645 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00002646 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002647 src, dst, "weakassign");
2648 return;
2649}
2650
Fariborz Jahanian58626502008-11-19 00:59:10 +00002651/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2652/// objc_assign_global (id src, id *dst)
2653///
2654void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2655 llvm::Value *src, llvm::Value *dst)
2656{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002657 const llvm::Type * SrcTy = src->getType();
2658 if (!isa<llvm::PointerType>(SrcTy)) {
2659 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2660 assert(Size <= 8 && "does not support size > 8");
2661 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2662 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002663 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2664 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002665 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2666 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002667 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00002668 src, dst, "globalassign");
2669 return;
2670}
2671
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002672/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2673/// objc_assign_ivar (id src, id *dst)
2674///
2675void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2676 llvm::Value *src, llvm::Value *dst)
2677{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002678 const llvm::Type * SrcTy = src->getType();
2679 if (!isa<llvm::PointerType>(SrcTy)) {
2680 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2681 assert(Size <= 8 && "does not support size > 8");
2682 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2683 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002684 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2685 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002686 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2687 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002688 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(),
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002689 src, dst, "assignivar");
2690 return;
2691}
2692
Fariborz Jahanian58626502008-11-19 00:59:10 +00002693/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2694/// objc_assign_strongCast (id src, id *dst)
2695///
2696void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2697 llvm::Value *src, llvm::Value *dst)
2698{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002699 const llvm::Type * SrcTy = src->getType();
2700 if (!isa<llvm::PointerType>(SrcTy)) {
2701 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2702 assert(Size <= 8 && "does not support size > 8");
2703 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2704 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002705 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2706 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002707 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2708 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002709 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00002710 src, dst, "weakassign");
2711 return;
2712}
2713
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002714/// EmitObjCValueForIvar - Code Gen for ivar reference.
2715///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002716LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2717 QualType ObjectTy,
2718 llvm::Value *BaseValue,
2719 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002720 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00002721 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00002722 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
2723 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002724}
2725
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002726llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00002727 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002728 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00002729 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002730 return llvm::ConstantInt::get(
2731 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2732 Offset);
2733}
2734
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002735/* *** Private Interface *** */
2736
2737/// EmitImageInfo - Emit the image info marker used to encode some module
2738/// level information.
2739///
2740/// See: <rdr://4810609&4810587&4810587>
2741/// struct IMAGE_INFO {
2742/// unsigned version;
2743/// unsigned flags;
2744/// };
2745enum ImageInfoFlags {
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002746 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what
2747 // this implies.
2748 eImageInfo_GarbageCollected = (1 << 1),
2749 eImageInfo_GCOnly = (1 << 2),
2750 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
2751
2752 // A flag indicating that the module has no instances of an
2753 // @synthesize of a superclass variable. <rdar://problem/6803242>
2754 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002755};
2756
2757void CGObjCMac::EmitImageInfo() {
2758 unsigned version = 0; // Version is unused?
2759 unsigned flags = 0;
2760
2761 // FIXME: Fix and continue?
2762 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2763 flags |= eImageInfo_GarbageCollected;
2764 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2765 flags |= eImageInfo_GCOnly;
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002766
2767 // We never allow @synthesize of a superclass property.
2768 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002769
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002770 // Emitted as int[2];
2771 llvm::Constant *values[2] = {
2772 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2773 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2774 };
2775 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002776
2777 const char *Section;
2778 if (ObjCABI == 1)
2779 Section = "__OBJC, __image_info,regular";
2780 else
2781 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002782 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002783 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
2784 llvm::ConstantArray::get(AT, values, 2),
2785 Section,
2786 0,
2787 true);
2788 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002789}
2790
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002791
2792// struct objc_module {
2793// unsigned long version;
2794// unsigned long size;
2795// const char *name;
2796// Symtab symtab;
2797// };
2798
2799// FIXME: Get from somewhere
2800static const int ModuleVersion = 7;
2801
2802void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002803 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002804
2805 std::vector<llvm::Constant*> Values(4);
2806 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2807 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002808 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002809 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002810 Values[3] = EmitModuleSymbols();
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002811 CreateMetadataVar("\01L_OBJC_MODULES",
2812 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
2813 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002814 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002815}
2816
2817llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002818 unsigned NumClasses = DefinedClasses.size();
2819 unsigned NumCategories = DefinedCategories.size();
2820
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002821 // Return null if no symbols were defined.
2822 if (!NumClasses && !NumCategories)
2823 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2824
2825 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002826 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2827 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2828 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2829 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2830
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002831 // The runtime expects exactly the list of defined classes followed
2832 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002833 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002834 for (unsigned i=0; i<NumClasses; i++)
2835 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2836 ObjCTypes.Int8PtrTy);
2837 for (unsigned i=0; i<NumCategories; i++)
2838 Symbols[NumClasses + i] =
2839 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2840 ObjCTypes.Int8PtrTy);
2841
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002842 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002843 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002844 NumClasses + NumCategories),
2845 Symbols);
2846
2847 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2848
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002849 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002850 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
2851 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002852 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002853 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2854}
2855
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002856llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002857 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002858 LazySymbols.insert(ID->getIdentifier());
2859
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002860 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2861
2862 if (!Entry) {
2863 llvm::Constant *Casted =
2864 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2865 ObjCTypes.ClassPtrTy);
2866 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002867 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
2868 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002869 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002870 }
2871
2872 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002873}
2874
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002875llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002876 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2877
2878 if (!Entry) {
2879 llvm::Constant *Casted =
2880 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2881 ObjCTypes.SelectorPtrTy);
2882 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002883 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
2884 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002885 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002886 }
2887
2888 return Builder.CreateLoad(Entry, false, "tmp");
2889}
2890
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002891llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002892 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002893
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002894 if (!Entry)
2895 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
2896 llvm::ConstantArray::get(Ident->getName()),
2897 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00002898 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002899
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002900 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002901}
2902
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00002903/// GetIvarLayoutName - Returns a unique constant for the given
2904/// ivar layout bitmap.
2905llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
2906 const ObjCCommonTypesHelper &ObjCTypes) {
2907 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
2908}
2909
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00002910static QualType::GCAttrTypes GetGCAttrTypeForType(QualType FQT) {
2911 if (FQT.isObjCGCStrong())
2912 return QualType::Strong;
2913
2914 if (FQT.isObjCGCWeak())
2915 return QualType::Weak;
2916
2917 if (CGM.getContext().isObjCObjectPointerType(FQT))
2918 return QualType::Strong;
2919
2920 if (const PointerType *PT = FQT->getAsPointerType())
2921 return GetGCAttrTypeForType(PT->getPointeeType());
2922
2923 return QualType::GCNone;
2924}
2925
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002926void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCInterfaceDecl *OI,
2927 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002928 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00002929 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002930 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +00002931 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002932 bool IsUnion = (RD && RD->isUnion());
2933 uint64_t MaxUnionIvarSize = 0;
2934 uint64_t MaxSkippedUnionIvarSize = 0;
2935 FieldDecl *MaxField = 0;
2936 FieldDecl *MaxSkippedField = 0;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002937 FieldDecl *LastFieldBitfield = 0;
2938
Chris Lattnerf1690852009-03-31 08:48:01 +00002939 unsigned base = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002940 if (RecFields.empty())
2941 return;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002942 if (IsUnion)
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002943 base = BytePos + GetFieldBaseOffset(OI, Layout, RecFields[0]);
Chris Lattnerf1690852009-03-31 08:48:01 +00002944 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
2945 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
2946
2947 llvm::SmallVector<FieldDecl*, 16> TmpRecFields;
2948
2949 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002950 FieldDecl *Field = RecFields[i];
2951 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002952 if (!Field->getIdentifier() || Field->isBitField()) {
2953 LastFieldBitfield = Field;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002954 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002955 }
2956 LastFieldBitfield = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002957 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00002958 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002959 if (FQT->isUnionType())
2960 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002961
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002962 const RecordType *RT = FQT->getAsRecordType();
2963 const RecordDecl *RD = RT->getDecl();
Daniel Dunbarb02532a2009-04-19 23:41:48 +00002964 // FIXME - Find a more efficient way of passing records down.
Douglas Gregor6ab35242009-04-09 21:40:53 +00002965 TmpRecFields.append(RD->field_begin(CGM.getContext()),
2966 RD->field_end(CGM.getContext()));
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00002967 const llvm::Type *Ty = CGM.getTypes().ConvertType(FQT);
2968 const llvm::StructLayout *RecLayout =
2969 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
2970
2971 BuildAggrIvarLayout(0, RecLayout, RD, TmpRecFields,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002972 BytePos + GetFieldBaseOffset(OI, Layout, Field),
Fariborz Jahanian81adc052009-04-24 16:17:09 +00002973 ForStrongLayout, HasUnion);
Chris Lattnerf1690852009-03-31 08:48:01 +00002974 TmpRecFields.clear();
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002975 continue;
2976 }
Chris Lattnerf1690852009-03-31 08:48:01 +00002977
2978 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002979 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00002980 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002981 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00002982 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002983 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00002984 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
2985 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00002986 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002987 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00002988 FQT = CArray->getElementType();
2989 }
2990
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002991 assert(!FQT->isUnionType() &&
2992 "layout for array of unions not supported");
2993 if (FQT->isRecordType()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00002994 int OldIndex = IvarsInfo.size() - 1;
2995 int OldSkIndex = SkipIvars.size() -1;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002996
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002997 // FIXME - Use a common routine with the above!
2998 const RecordType *RT = FQT->getAsRecordType();
2999 const RecordDecl *RD = RT->getDecl();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003000 // FIXME - Find a more efficient way of passing records down.
Douglas Gregor6ab35242009-04-09 21:40:53 +00003001 TmpRecFields.append(RD->field_begin(CGM.getContext()),
3002 RD->field_end(CGM.getContext()));
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003003 const llvm::Type *Ty = CGM.getTypes().ConvertType(FQT);
3004 const llvm::StructLayout *RecLayout =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003005 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Chris Lattnerf1690852009-03-31 08:48:01 +00003006
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003007 BuildAggrIvarLayout(0, RecLayout, RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00003008 TmpRecFields,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003009 BytePos + GetFieldBaseOffset(OI, Layout, Field),
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003010 ForStrongLayout, HasUnion);
Chris Lattnerf1690852009-03-31 08:48:01 +00003011 TmpRecFields.clear();
3012
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003013 // Replicate layout information for each array element. Note that
3014 // one element is already done.
3015 uint64_t ElIx = 1;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003016 for (int FirstIndex = IvarsInfo.size() - 1,
3017 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003018 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003019 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3020 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3021 IvarsInfo[i].ivar_size));
3022 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3023 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3024 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003025 }
3026 continue;
3027 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003028 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003029 // At this point, we are done with Record/Union and array there of.
3030 // For other arrays we are down to its element type.
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003031 QualType::GCAttrTypes GCAttr = GetGCAttrTypeForType(FQT):
3032
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003033 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003034 if ((ForStrongLayout && GCAttr == QualType::Strong)
3035 || (!ForStrongLayout && GCAttr == QualType::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003036 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003037 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003038 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003039 MaxUnionIvarSize = UnionIvarSize;
3040 MaxField = Field;
3041 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003042 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003043 IvarsInfo.push_back(GC_IVAR(BytePos + GetFieldBaseOffset(OI, Layout,
3044 Field),
3045 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003046 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003047 } else if ((ForStrongLayout &&
3048 (GCAttr == QualType::GCNone || GCAttr == QualType::Weak))
3049 || (!ForStrongLayout && GCAttr != QualType::Weak)) {
3050 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003051 // FIXME: Why the asymmetry? We divide by word size in bits on
3052 // other side.
3053 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003054 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003055 MaxSkippedUnionIvarSize = UnionIvarSize;
3056 MaxSkippedField = Field;
3057 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003058 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003059 // FIXME: Why the asymmetry, we divide by byte size in bits here?
3060 SkipIvars.push_back(GC_IVAR(BytePos + GetFieldBaseOffset(OI, Layout,
3061 Field),
3062 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003063 }
3064 }
3065 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003066 if (LastFieldBitfield) {
3067 // Last field was a bitfield. Must update skip info.
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003068 Expr *BitWidth = LastFieldBitfield->getBitWidth();
3069 uint64_t BitFieldSize =
Eli Friedman9a901bb2009-04-26 19:19:15 +00003070 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Daniel Dunbar487993b2009-05-03 13:32:01 +00003071 GC_IVAR skivar;
3072 skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout,
3073 LastFieldBitfield);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003074 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3075 + ((BitFieldSize % ByteSizeInBits) != 0);
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003076 SkipIvars.push_back(skivar);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003077 }
3078
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003079 if (MaxField)
3080 IvarsInfo.push_back(GC_IVAR(BytePos + GetFieldBaseOffset(OI, Layout,
3081 MaxField),
3082 MaxUnionIvarSize));
3083 if (MaxSkippedField)
3084 SkipIvars.push_back(GC_IVAR(BytePos + GetFieldBaseOffset(OI, Layout,
3085 MaxSkippedField),
3086 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003087}
3088
3089/// BuildIvarLayout - Builds ivar layout bitmap for the class
3090/// implementation for the __strong or __weak case.
3091/// The layout map displays which words in ivar list must be skipped
3092/// and which must be scanned by GC (see below). String is built of bytes.
3093/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3094/// of words to skip and right nibble is count of words to scan. So, each
3095/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3096/// represented by a 0x00 byte which also ends the string.
3097/// 1. when ForStrongLayout is true, following ivars are scanned:
3098/// - id, Class
3099/// - object *
3100/// - __strong anything
3101///
3102/// 2. When ForStrongLayout is false, following ivars are scanned:
3103/// - __weak anything
3104///
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003105llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003106 const ObjCImplementationDecl *OMD,
3107 bool ForStrongLayout) {
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003108 bool hasUnion = false;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003109
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003110 unsigned int WordsToScan, WordsToSkip;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003111 const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
3112 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
3113 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003114
Chris Lattnerf1690852009-03-31 08:48:01 +00003115 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003116 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003117 CGM.getContext().CollectObjCIvars(OI, RecFields);
3118 if (RecFields.empty())
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003119 return llvm::Constant::getNullValue(PtrTy);
Chris Lattnerf1690852009-03-31 08:48:01 +00003120
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003121 SkipIvars.clear();
3122 IvarsInfo.clear();
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00003123
Daniel Dunbar84ad77a2009-04-22 09:39:34 +00003124 const llvm::StructLayout *Layout =
3125 CGM.getTargetData().getStructLayout(GetConcreteClassStruct(CGM, OI));
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003126 BuildAggrIvarLayout(OI, Layout, 0, RecFields, 0, ForStrongLayout, hasUnion);
3127 if (IvarsInfo.empty())
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003128 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003129
3130 // Sort on byte position in case we encounterred a union nested in
3131 // the ivar list.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003132 if (hasUnion && !IvarsInfo.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003133 std::sort(IvarsInfo.begin(), IvarsInfo.end());
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003134 if (hasUnion && !SkipIvars.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003135 std::sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003136
3137 // Build the string of skip/scan nibbles
Fariborz Jahanian8c2f2d12009-04-24 17:15:27 +00003138 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003139 unsigned int WordSize =
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003140 CGM.getTypes().getTargetData().getTypePaddedSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003141 if (IvarsInfo[0].ivar_bytepos == 0) {
3142 WordsToSkip = 0;
3143 WordsToScan = IvarsInfo[0].ivar_size;
3144 }
3145 else {
3146 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3147 WordsToScan = IvarsInfo[0].ivar_size;
3148 }
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003149 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++)
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003150 {
3151 unsigned int TailPrevGCObjC =
3152 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
3153 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC)
3154 {
3155 // consecutive 'scanned' object pointers.
3156 WordsToScan += IvarsInfo[i].ivar_size;
3157 }
3158 else
3159 {
3160 // Skip over 'gc'able object pointer which lay over each other.
3161 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3162 continue;
3163 // Must skip over 1 or more words. We save current skip/scan values
3164 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003165 SKIP_SCAN SkScan;
3166 SkScan.skip = WordsToSkip;
3167 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003168 SkipScanIvars.push_back(SkScan);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003169
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003170 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003171 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3172 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003173 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003174 WordsToSkip = 0;
3175 WordsToScan = IvarsInfo[i].ivar_size;
3176 }
3177 }
3178 if (WordsToScan > 0)
3179 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003180 SKIP_SCAN SkScan;
3181 SkScan.skip = WordsToSkip;
3182 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003183 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003184 }
3185
3186 bool BytesSkipped = false;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003187 if (!SkipIvars.empty())
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003188 {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003189 unsigned int LastIndex = SkipIvars.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003190 int LastByteSkipped =
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003191 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
3192 LastIndex = IvarsInfo.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003193 int LastByteScanned =
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003194 IvarsInfo[LastIndex].ivar_bytepos +
3195 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003196 BytesSkipped = (LastByteSkipped > LastByteScanned);
3197 // Compute number of bytes to skip at the tail end of the last ivar scanned.
3198 if (BytesSkipped)
3199 {
3200 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003201 SKIP_SCAN SkScan;
3202 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3203 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003204 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003205 }
3206 }
3207 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3208 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003209 int SkipScan = SkipScanIvars.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003210 for (int i = 0; i <= SkipScan; i++)
3211 {
3212 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3213 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3214 // 0xM0 followed by 0x0N detected.
3215 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3216 for (int j = i+1; j < SkipScan; j++)
3217 SkipScanIvars[j] = SkipScanIvars[j+1];
3218 --SkipScan;
3219 }
3220 }
3221
3222 // Generate the string.
3223 std::string BitMap;
3224 for (int i = 0; i <= SkipScan; i++)
3225 {
3226 unsigned char byte;
3227 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3228 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3229 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3230 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
3231
3232 if (skip_small > 0 || skip_big > 0)
3233 BytesSkipped = true;
3234 // first skip big.
3235 for (unsigned int ix = 0; ix < skip_big; ix++)
3236 BitMap += (unsigned char)(0xf0);
3237
3238 // next (skip small, scan)
3239 if (skip_small)
3240 {
3241 byte = skip_small << 4;
3242 if (scan_big > 0)
3243 {
3244 byte |= 0xf;
3245 --scan_big;
3246 }
3247 else if (scan_small)
3248 {
3249 byte |= scan_small;
3250 scan_small = 0;
3251 }
3252 BitMap += byte;
3253 }
3254 // next scan big
3255 for (unsigned int ix = 0; ix < scan_big; ix++)
3256 BitMap += (unsigned char)(0x0f);
3257 // last scan small
3258 if (scan_small)
3259 {
3260 byte = scan_small;
3261 BitMap += byte;
3262 }
3263 }
3264 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003265 unsigned char zero = 0;
3266 BitMap += zero;
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003267
3268 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
3269 printf("\n%s ivar layout for class '%s': ",
3270 ForStrongLayout ? "strong" : "weak",
3271 OMD->getClassInterface()->getNameAsCString());
3272 const unsigned char *s = (unsigned char*)BitMap.c_str();
3273 for (unsigned i = 0; i < BitMap.size(); i++)
3274 if (!(s[i] & 0xf0))
3275 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3276 else
3277 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3278 printf("\n");
3279 }
3280
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003281 // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as
3282 // final layout.
3283 if (ForStrongLayout && !BytesSkipped)
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003284 return llvm::Constant::getNullValue(PtrTy);
3285 llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3286 llvm::ConstantArray::get(BitMap.c_str()),
3287 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003288 1, true);
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003289 return getConstantGEP(Entry, 0, 0);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003290}
3291
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003292llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003293 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3294
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003295 // FIXME: Avoid std::string copying.
3296 if (!Entry)
3297 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
3298 llvm::ConstantArray::get(Sel.getAsString()),
3299 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003300 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003301
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003302 return getConstantGEP(Entry, 0, 0);
3303}
3304
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003305// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003306llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003307 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3308}
3309
3310// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003311llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003312 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
3313}
3314
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003315llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003316 std::string TypeStr;
3317 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3318
3319 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003320
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003321 if (!Entry)
3322 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
3323 llvm::ConstantArray::get(TypeStr),
3324 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003325 1, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003326
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003327 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003328}
3329
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003330llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003331 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003332 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3333 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00003334
3335 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3336
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003337 if (!Entry)
3338 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
3339 llvm::ConstantArray::get(TypeStr),
3340 "__TEXT,__cstring,cstring_literals",
3341 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00003342
3343 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003344}
3345
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003346// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003347llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003348 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
3349
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003350 if (!Entry)
3351 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
3352 llvm::ConstantArray::get(Ident->getName()),
3353 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003354 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003355
3356 return getConstantGEP(Entry, 0, 0);
3357}
3358
3359// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003360// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003361llvm::Constant *
3362 CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3363 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003364 std::string TypeStr;
3365 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003366 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3367}
3368
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003369void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
3370 const ObjCContainerDecl *CD,
3371 std::string &NameOut) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00003372 NameOut = '\01';
3373 NameOut += (D->isInstanceMethod() ? '-' : '+');
Chris Lattner077bf5e2008-11-24 03:33:13 +00003374 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003375 assert (CD && "Missing container decl in GetNameForMethod");
3376 NameOut += CD->getNameAsString();
Fariborz Jahanian1e9aef32009-04-16 18:34:20 +00003377 if (const ObjCCategoryImplDecl *CID =
3378 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) {
3379 NameOut += '(';
3380 NameOut += CID->getNameAsString();
3381 NameOut+= ')';
3382 }
Chris Lattner077bf5e2008-11-24 03:33:13 +00003383 NameOut += ' ';
3384 NameOut += D->getSelector().getAsString();
3385 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003386}
3387
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003388void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003389 EmitModuleInfo();
3390
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003391 // Emit the dummy bodies for any protocols which were referenced but
3392 // never defined.
3393 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
3394 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
3395 if (i->second->hasInitializer())
3396 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003397
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003398 std::vector<llvm::Constant*> Values(5);
3399 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
3400 Values[1] = GetClassName(i->first);
3401 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
3402 Values[3] = Values[4] =
3403 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
3404 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
3405 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
3406 Values));
3407 }
3408
3409 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003410 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003411 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003412 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003413 }
3414
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003415 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003416 llvm::GlobalValue *GV =
3417 new llvm::GlobalVariable(AT, false,
3418 llvm::GlobalValue::AppendingLinkage,
3419 llvm::ConstantArray::get(AT, Used),
3420 "llvm.used",
3421 &CGM.getModule());
3422
3423 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003424
3425 // Add assembler directives to add lazy undefined symbol references
3426 // for classes which are referenced but not defined. This is
3427 // important for correct linker interaction.
3428
3429 // FIXME: Uh, this isn't particularly portable.
3430 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00003431
3432 if (!CGM.getModule().getModuleInlineAsm().empty())
3433 s << "\n";
3434
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003435 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
3436 e = LazySymbols.end(); i != e; ++i) {
3437 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
3438 }
3439 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
3440 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003441 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003442 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
3443 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00003444
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003445 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003446}
3447
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003448CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003449 : CGObjCCommonMac(cgm),
3450 ObjCTypes(cgm)
3451{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003452 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003453 ObjCABI = 2;
3454}
3455
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003456/* *** */
3457
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003458ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
3459: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003460{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003461 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3462 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003463
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003464 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003465 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003466 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003467 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003468 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
3469
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003470 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00003471 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003472 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003473
3474 // FIXME: It would be nice to unify this with the opaque type, so
3475 // that the IR comes out a bit cleaner.
3476 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
3477 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003478
3479 // I'm not sure I like this. The implicit coordination is a bit
3480 // gross. We should solve this in a reasonable fashion because this
3481 // is a pretty common task (match some runtime data structure with
3482 // an LLVM data structure).
3483
3484 // FIXME: This is leaked.
3485 // FIXME: Merge with rewriter code?
3486
3487 // struct _objc_super {
3488 // id self;
3489 // Class cls;
3490 // }
3491 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3492 SourceLocation(),
3493 &Ctx.Idents.get("_objc_super"));
Douglas Gregor6ab35242009-04-09 21:40:53 +00003494 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3495 Ctx.getObjCIdType(), 0, false));
3496 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3497 Ctx.getObjCClassType(), 0, false));
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003498 RD->completeDefinition(Ctx);
3499
3500 SuperCTy = Ctx.getTagDeclType(RD);
3501 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
3502
3503 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003504 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
3505
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003506 // struct _prop_t {
3507 // char *name;
3508 // char *attributes;
3509 // }
Chris Lattner1c02f862009-04-22 02:53:24 +00003510 PropertyTy = llvm::StructType::get(Int8PtrTy, Int8PtrTy, NULL);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003511 CGM.getModule().addTypeName("struct._prop_t",
3512 PropertyTy);
3513
3514 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003515 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003516 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003517 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003518 // }
3519 PropertyListTy = llvm::StructType::get(IntTy,
3520 IntTy,
3521 llvm::ArrayType::get(PropertyTy, 0),
3522 NULL);
3523 CGM.getModule().addTypeName("struct._prop_list_t",
3524 PropertyListTy);
3525 // struct _prop_list_t *
3526 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
3527
3528 // struct _objc_method {
3529 // SEL _cmd;
3530 // char *method_type;
3531 // char *_imp;
3532 // }
3533 MethodTy = llvm::StructType::get(SelectorPtrTy,
3534 Int8PtrTy,
3535 Int8PtrTy,
3536 NULL);
3537 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003538
3539 // struct _objc_cache *
3540 CacheTy = llvm::OpaqueType::get();
3541 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
3542 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003543}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003544
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003545ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
3546 : ObjCCommonTypesHelper(cgm)
3547{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003548 // struct _objc_method_description {
3549 // SEL name;
3550 // char *types;
3551 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003552 MethodDescriptionTy =
3553 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003554 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003555 NULL);
3556 CGM.getModule().addTypeName("struct._objc_method_description",
3557 MethodDescriptionTy);
3558
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003559 // struct _objc_method_description_list {
3560 // int count;
3561 // struct _objc_method_description[1];
3562 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003563 MethodDescriptionListTy =
3564 llvm::StructType::get(IntTy,
3565 llvm::ArrayType::get(MethodDescriptionTy, 0),
3566 NULL);
3567 CGM.getModule().addTypeName("struct._objc_method_description_list",
3568 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003569
3570 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003571 MethodDescriptionListPtrTy =
3572 llvm::PointerType::getUnqual(MethodDescriptionListTy);
3573
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003574 // Protocol description structures
3575
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003576 // struct _objc_protocol_extension {
3577 // uint32_t size; // sizeof(struct _objc_protocol_extension)
3578 // struct _objc_method_description_list *optional_instance_methods;
3579 // struct _objc_method_description_list *optional_class_methods;
3580 // struct _objc_property_list *instance_properties;
3581 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003582 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003583 llvm::StructType::get(IntTy,
3584 MethodDescriptionListPtrTy,
3585 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003586 PropertyListPtrTy,
3587 NULL);
3588 CGM.getModule().addTypeName("struct._objc_protocol_extension",
3589 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003590
3591 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003592 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
3593
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003594 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003595
3596 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
3597 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3598
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003599 const llvm::Type *T =
3600 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
3601 LongTy,
3602 llvm::ArrayType::get(ProtocolTyHolder, 0),
3603 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003604 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
3605
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003606 // struct _objc_protocol {
3607 // struct _objc_protocol_extension *isa;
3608 // char *protocol_name;
3609 // struct _objc_protocol **_objc_protocol_list;
3610 // struct _objc_method_description_list *instance_methods;
3611 // struct _objc_method_description_list *class_methods;
3612 // }
3613 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003614 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003615 llvm::PointerType::getUnqual(ProtocolListTyHolder),
3616 MethodDescriptionListPtrTy,
3617 MethodDescriptionListPtrTy,
3618 NULL);
3619 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
3620
3621 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
3622 CGM.getModule().addTypeName("struct._objc_protocol_list",
3623 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003624 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003625 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
3626
3627 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003628 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003629 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003630
3631 // Class description structures
3632
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003633 // struct _objc_ivar {
3634 // char *ivar_name;
3635 // char *ivar_type;
3636 // int ivar_offset;
3637 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003638 IvarTy = llvm::StructType::get(Int8PtrTy,
3639 Int8PtrTy,
3640 IntTy,
3641 NULL);
3642 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
3643
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003644 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003645 IvarListTy = llvm::OpaqueType::get();
3646 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
3647 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
3648
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003649 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003650 MethodListTy = llvm::OpaqueType::get();
3651 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
3652 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
3653
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003654 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003655 ClassExtensionTy =
3656 llvm::StructType::get(IntTy,
3657 Int8PtrTy,
3658 PropertyListPtrTy,
3659 NULL);
3660 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
3661 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
3662
3663 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3664
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003665 // struct _objc_class {
3666 // Class isa;
3667 // Class super_class;
3668 // char *name;
3669 // long version;
3670 // long info;
3671 // long instance_size;
3672 // struct _objc_ivar_list *ivars;
3673 // struct _objc_method_list *methods;
3674 // struct _objc_cache *cache;
3675 // struct _objc_protocol_list *protocols;
3676 // char *ivar_layout;
3677 // struct _objc_class_ext *ext;
3678 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003679 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3680 llvm::PointerType::getUnqual(ClassTyHolder),
3681 Int8PtrTy,
3682 LongTy,
3683 LongTy,
3684 LongTy,
3685 IvarListPtrTy,
3686 MethodListPtrTy,
3687 CachePtrTy,
3688 ProtocolListPtrTy,
3689 Int8PtrTy,
3690 ClassExtensionPtrTy,
3691 NULL);
3692 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
3693
3694 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
3695 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
3696 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
3697
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003698 // struct _objc_category {
3699 // char *category_name;
3700 // char *class_name;
3701 // struct _objc_method_list *instance_method;
3702 // struct _objc_method_list *class_method;
3703 // uint32_t size; // sizeof(struct _objc_category)
3704 // struct _objc_property_list *instance_properties;// category's @property
3705 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003706 CategoryTy = llvm::StructType::get(Int8PtrTy,
3707 Int8PtrTy,
3708 MethodListPtrTy,
3709 MethodListPtrTy,
3710 ProtocolListPtrTy,
3711 IntTy,
3712 PropertyListPtrTy,
3713 NULL);
3714 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
3715
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003716 // Global metadata structures
3717
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003718 // struct _objc_symtab {
3719 // long sel_ref_cnt;
3720 // SEL *refs;
3721 // short cls_def_cnt;
3722 // short cat_def_cnt;
3723 // char *defs[cls_def_cnt + cat_def_cnt];
3724 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003725 SymtabTy = llvm::StructType::get(LongTy,
3726 SelectorPtrTy,
3727 ShortTy,
3728 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003729 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003730 NULL);
3731 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
3732 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
3733
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003734 // struct _objc_module {
3735 // long version;
3736 // long size; // sizeof(struct _objc_module)
3737 // char *name;
3738 // struct _objc_symtab* symtab;
3739 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003740 ModuleTy =
3741 llvm::StructType::get(LongTy,
3742 LongTy,
3743 Int8PtrTy,
3744 SymtabPtrTy,
3745 NULL);
3746 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003747
Anders Carlsson2abd89c2008-08-31 04:05:03 +00003748
Anders Carlsson124526b2008-09-09 10:10:21 +00003749 // FIXME: This is the size of the setjmp buffer and should be
3750 // target specific. 18 is what's used on 32-bit X86.
3751 uint64_t SetJmpBufferSize = 18;
3752
3753 // Exceptions
3754 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00003755 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00003756
3757 ExceptionDataTy =
3758 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
3759 SetJmpBufferSize),
3760 StackPtrTy, NULL);
3761 CGM.getModule().addTypeName("struct._objc_exception_data",
3762 ExceptionDataTy);
3763
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003764}
3765
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003766ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003767: ObjCCommonTypesHelper(cgm)
3768{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003769 // struct _method_list_t {
3770 // uint32_t entsize; // sizeof(struct _objc_method)
3771 // uint32_t method_count;
3772 // struct _objc_method method_list[method_count];
3773 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003774 MethodListnfABITy = llvm::StructType::get(IntTy,
3775 IntTy,
3776 llvm::ArrayType::get(MethodTy, 0),
3777 NULL);
3778 CGM.getModule().addTypeName("struct.__method_list_t",
3779 MethodListnfABITy);
3780 // struct method_list_t *
3781 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003782
3783 // struct _protocol_t {
3784 // id isa; // NULL
3785 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003786 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003787 // const struct method_list_t * const instance_methods;
3788 // const struct method_list_t * const class_methods;
3789 // const struct method_list_t *optionalInstanceMethods;
3790 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003791 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003792 // const uint32_t size; // sizeof(struct _protocol_t)
3793 // const uint32_t flags; // = 0
3794 // }
3795
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003796 // Holder for struct _protocol_list_t *
3797 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3798
3799 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3800 Int8PtrTy,
3801 llvm::PointerType::getUnqual(
3802 ProtocolListTyHolder),
3803 MethodListnfABIPtrTy,
3804 MethodListnfABIPtrTy,
3805 MethodListnfABIPtrTy,
3806 MethodListnfABIPtrTy,
3807 PropertyListPtrTy,
3808 IntTy,
3809 IntTy,
3810 NULL);
3811 CGM.getModule().addTypeName("struct._protocol_t",
3812 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003813
3814 // struct _protocol_t*
3815 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003816
Fariborz Jahanianda320092009-01-29 19:24:30 +00003817 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003818 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00003819 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003820 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003821 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3822 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003823 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003824 NULL);
3825 CGM.getModule().addTypeName("struct._objc_protocol_list",
3826 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003827 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3828 ProtocolListnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003829
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003830 // struct _objc_protocol_list*
3831 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003832
3833 // struct _ivar_t {
3834 // unsigned long int *offset; // pointer to ivar offset location
3835 // char *name;
3836 // char *type;
3837 // uint32_t alignment;
3838 // uint32_t size;
3839 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003840 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3841 Int8PtrTy,
3842 Int8PtrTy,
3843 IntTy,
3844 IntTy,
3845 NULL);
3846 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3847
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003848 // struct _ivar_list_t {
3849 // uint32 entsize; // sizeof(struct _ivar_t)
3850 // uint32 count;
3851 // struct _iver_t list[count];
3852 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003853 IvarListnfABITy = llvm::StructType::get(IntTy,
3854 IntTy,
3855 llvm::ArrayType::get(
3856 IvarnfABITy, 0),
3857 NULL);
3858 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3859
3860 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003861
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003862 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003863 // uint32_t const flags;
3864 // uint32_t const instanceStart;
3865 // uint32_t const instanceSize;
3866 // uint32_t const reserved; // only when building for 64bit targets
3867 // const uint8_t * const ivarLayout;
3868 // const char *const name;
3869 // const struct _method_list_t * const baseMethods;
3870 // const struct _objc_protocol_list *const baseProtocols;
3871 // const struct _ivar_list_t *const ivars;
3872 // const uint8_t * const weakIvarLayout;
3873 // const struct _prop_list_t * const properties;
3874 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003875
3876 // FIXME. Add 'reserved' field in 64bit abi mode!
3877 ClassRonfABITy = llvm::StructType::get(IntTy,
3878 IntTy,
3879 IntTy,
3880 Int8PtrTy,
3881 Int8PtrTy,
3882 MethodListnfABIPtrTy,
3883 ProtocolListnfABIPtrTy,
3884 IvarListnfABIPtrTy,
3885 Int8PtrTy,
3886 PropertyListPtrTy,
3887 NULL);
3888 CGM.getModule().addTypeName("struct._class_ro_t",
3889 ClassRonfABITy);
3890
3891 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3892 std::vector<const llvm::Type*> Params;
3893 Params.push_back(ObjectPtrTy);
3894 Params.push_back(SelectorPtrTy);
3895 ImpnfABITy = llvm::PointerType::getUnqual(
3896 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3897
3898 // struct _class_t {
3899 // struct _class_t *isa;
3900 // struct _class_t * const superclass;
3901 // void *cache;
3902 // IMP *vtable;
3903 // struct class_ro_t *ro;
3904 // }
3905
3906 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3907 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3908 llvm::PointerType::getUnqual(ClassTyHolder),
3909 CachePtrTy,
3910 llvm::PointerType::getUnqual(ImpnfABITy),
3911 llvm::PointerType::getUnqual(
3912 ClassRonfABITy),
3913 NULL);
3914 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3915
3916 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3917 ClassnfABITy);
3918
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003919 // LLVM for struct _class_t *
3920 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3921
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003922 // struct _category_t {
3923 // const char * const name;
3924 // struct _class_t *const cls;
3925 // const struct _method_list_t * const instance_methods;
3926 // const struct _method_list_t * const class_methods;
3927 // const struct _protocol_list_t * const protocols;
3928 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003929 // }
3930 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003931 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003932 MethodListnfABIPtrTy,
3933 MethodListnfABIPtrTy,
3934 ProtocolListnfABIPtrTy,
3935 PropertyListPtrTy,
3936 NULL);
3937 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003938
3939 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003940 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3941 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003942
3943 // MessageRefTy - LLVM for:
3944 // struct _message_ref_t {
3945 // IMP messenger;
3946 // SEL name;
3947 // };
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003948
3949 // First the clang type for struct _message_ref_t
3950 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3951 SourceLocation(),
3952 &Ctx.Idents.get("_message_ref_t"));
Douglas Gregor6ab35242009-04-09 21:40:53 +00003953 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3954 Ctx.VoidPtrTy, 0, false));
3955 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3956 Ctx.getObjCSelType(), 0, false));
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003957 RD->completeDefinition(Ctx);
3958
3959 MessageRefCTy = Ctx.getTagDeclType(RD);
3960 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
3961 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003962
3963 // MessageRefPtrTy - LLVM for struct _message_ref_t*
3964 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
3965
3966 // SuperMessageRefTy - LLVM for:
3967 // struct _super_message_ref_t {
3968 // SUPER_IMP messenger;
3969 // SEL name;
3970 // };
3971 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy,
3972 SelectorPtrTy,
3973 NULL);
3974 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
3975
3976 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
3977 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
3978
Daniel Dunbare588b992009-03-01 04:46:24 +00003979
3980 // struct objc_typeinfo {
3981 // const void** vtable; // objc_ehtype_vtable + 2
3982 // const char* name; // c++ typeinfo string
3983 // Class cls;
3984 // };
3985 EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy),
3986 Int8PtrTy,
3987 ClassnfABIPtrTy,
3988 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00003989 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Daniel Dunbare588b992009-03-01 04:46:24 +00003990 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003991}
3992
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003993llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3994 FinishNonFragileABIModule();
3995
3996 return NULL;
3997}
3998
3999void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4000 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004001
4002 // Build list of all implemented classe addresses in array
4003 // L_OBJC_LABEL_CLASS_$.
4004 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
4005 // list of 'nonlazy' implementations (defined as those with a +load{}
4006 // method!!).
4007 unsigned NumClasses = DefinedClasses.size();
4008 if (NumClasses) {
4009 std::vector<llvm::Constant*> Symbols(NumClasses);
4010 for (unsigned i=0; i<NumClasses; i++)
4011 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
4012 ObjCTypes.Int8PtrTy);
4013 llvm::Constant* Init =
4014 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
4015 NumClasses),
4016 Symbols);
4017
4018 llvm::GlobalVariable *GV =
4019 new llvm::GlobalVariable(Init->getType(), false,
4020 llvm::GlobalValue::InternalLinkage,
4021 Init,
4022 "\01L_OBJC_LABEL_CLASS_$",
4023 &CGM.getModule());
Daniel Dunbar58a29122009-03-09 22:18:41 +00004024 GV->setAlignment(8);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004025 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
4026 UsedGlobals.push_back(GV);
4027 }
4028
4029 // Build list of all implemented category addresses in array
4030 // L_OBJC_LABEL_CATEGORY_$.
4031 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
4032 // list of 'nonlazy' category implementations (defined as those with a +load{}
4033 // method!!).
4034 unsigned NumCategory = DefinedCategories.size();
4035 if (NumCategory) {
4036 std::vector<llvm::Constant*> Symbols(NumCategory);
4037 for (unsigned i=0; i<NumCategory; i++)
4038 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
4039 ObjCTypes.Int8PtrTy);
4040 llvm::Constant* Init =
4041 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
4042 NumCategory),
4043 Symbols);
4044
4045 llvm::GlobalVariable *GV =
4046 new llvm::GlobalVariable(Init->getType(), false,
4047 llvm::GlobalValue::InternalLinkage,
4048 Init,
4049 "\01L_OBJC_LABEL_CATEGORY_$",
4050 &CGM.getModule());
Daniel Dunbar58a29122009-03-09 22:18:41 +00004051 GV->setAlignment(8);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004052 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
4053 UsedGlobals.push_back(GV);
4054 }
4055
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004056 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
4057 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
4058 std::vector<llvm::Constant*> Values(2);
4059 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
Fariborz Jahanian067986e2009-02-24 21:08:09 +00004060 unsigned int flags = 0;
Fariborz Jahanian66a5c2c2009-02-24 23:34:44 +00004061 // FIXME: Fix and continue?
4062 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
4063 flags |= eImageInfo_GarbageCollected;
4064 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
4065 flags |= eImageInfo_GCOnly;
Fariborz Jahanian067986e2009-02-24 21:08:09 +00004066 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004067 llvm::Constant* Init = llvm::ConstantArray::get(
4068 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
4069 Values);
4070 llvm::GlobalVariable *IMGV =
4071 new llvm::GlobalVariable(Init->getType(), false,
4072 llvm::GlobalValue::InternalLinkage,
4073 Init,
4074 "\01L_OBJC_IMAGE_INFO",
4075 &CGM.getModule());
4076 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
Daniel Dunbar325f7582009-04-23 08:03:21 +00004077 IMGV->setConstant(true);
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004078 UsedGlobals.push_back(IMGV);
4079
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004080 std::vector<llvm::Constant*> Used;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004081
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004082 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
4083 e = UsedGlobals.end(); i != e; ++i) {
4084 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
4085 }
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004086
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004087 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
4088 llvm::GlobalValue *GV =
4089 new llvm::GlobalVariable(AT, false,
4090 llvm::GlobalValue::AppendingLinkage,
4091 llvm::ConstantArray::get(AT, Used),
4092 "llvm.used",
4093 &CGM.getModule());
4094
4095 GV->setSection("llvm.metadata");
4096
4097}
4098
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004099// Metadata flags
4100enum MetaDataDlags {
4101 CLS = 0x0,
4102 CLS_META = 0x1,
4103 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004104 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004105 CLS_EXCEPTION = 0x20
4106};
4107/// BuildClassRoTInitializer - generate meta-data for:
4108/// struct _class_ro_t {
4109/// uint32_t const flags;
4110/// uint32_t const instanceStart;
4111/// uint32_t const instanceSize;
4112/// uint32_t const reserved; // only when building for 64bit targets
4113/// const uint8_t * const ivarLayout;
4114/// const char *const name;
4115/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004116/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004117/// const struct _ivar_list_t *const ivars;
4118/// const uint8_t * const weakIvarLayout;
4119/// const struct _prop_list_t * const properties;
4120/// }
4121///
4122llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
4123 unsigned flags,
4124 unsigned InstanceStart,
4125 unsigned InstanceSize,
4126 const ObjCImplementationDecl *ID) {
4127 std::string ClassName = ID->getNameAsString();
4128 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
4129 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4130 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4131 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
4132 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004133 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4134 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004135 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004136 // const struct _method_list_t * const baseMethods;
4137 std::vector<llvm::Constant*> Methods;
4138 std::string MethodListName("\01l_OBJC_$_");
4139 if (flags & CLS_META) {
4140 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004141 for (ObjCImplementationDecl::classmeth_iterator
4142 i = ID->classmeth_begin(CGM.getContext()),
4143 e = ID->classmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004144 // Class methods should always be defined.
4145 Methods.push_back(GetMethodConstant(*i));
4146 }
4147 } else {
4148 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004149 for (ObjCImplementationDecl::instmeth_iterator
4150 i = ID->instmeth_begin(CGM.getContext()),
4151 e = ID->instmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004152 // Instance methods should always be defined.
4153 Methods.push_back(GetMethodConstant(*i));
4154 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00004155 for (ObjCImplementationDecl::propimpl_iterator
4156 i = ID->propimpl_begin(CGM.getContext()),
4157 e = ID->propimpl_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004158 ObjCPropertyImplDecl *PID = *i;
4159
4160 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4161 ObjCPropertyDecl *PD = PID->getPropertyDecl();
4162
4163 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4164 if (llvm::Constant *C = GetMethodConstant(MD))
4165 Methods.push_back(C);
4166 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4167 if (llvm::Constant *C = GetMethodConstant(MD))
4168 Methods.push_back(C);
4169 }
4170 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004171 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004172 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004173 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004174
4175 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4176 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
4177 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
4178 + OID->getNameAsString(),
4179 OID->protocol_begin(),
4180 OID->protocol_end());
4181
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004182 if (flags & CLS_META)
4183 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4184 else
4185 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004186 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4187 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004188 if (flags & CLS_META)
4189 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
4190 else
4191 Values[ 9] =
4192 EmitPropertyList(
4193 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
4194 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004195 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
4196 Values);
4197 llvm::GlobalVariable *CLASS_RO_GV =
4198 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
4199 llvm::GlobalValue::InternalLinkage,
4200 Init,
4201 (flags & CLS_META) ?
4202 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4203 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
4204 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004205 CLASS_RO_GV->setAlignment(
4206 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004207 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004208 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004209
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004210}
4211
4212/// BuildClassMetaData - This routine defines that to-level meta-data
4213/// for the given ClassName for:
4214/// struct _class_t {
4215/// struct _class_t *isa;
4216/// struct _class_t * const superclass;
4217/// void *cache;
4218/// IMP *vtable;
4219/// struct class_ro_t *ro;
4220/// }
4221///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004222llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
4223 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004224 llvm::Constant *IsAGV,
4225 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004226 llvm::Constant *ClassRoGV,
4227 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004228 std::vector<llvm::Constant*> Values(5);
4229 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004230 Values[1] = SuperClassGV
4231 ? SuperClassGV
4232 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004233 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4234 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4235 Values[4] = ClassRoGV; // &CLASS_RO_GV
4236 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
4237 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004238 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4239 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004240 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004241 GV->setAlignment(
4242 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004243 if (HiddenVisibility)
4244 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004245 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004246}
4247
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004248void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004249 uint32_t &InstanceStart,
4250 uint32_t &InstanceSize) {
Daniel Dunbar97776872009-04-22 07:32:20 +00004251 // Find first and last (non-padding) ivars in this interface.
4252
4253 // FIXME: Use iterator.
4254 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004255 GetNamedIvarList(OID->getClassInterface(), OIvars);
Daniel Dunbar97776872009-04-22 07:32:20 +00004256
4257 if (OIvars.empty()) {
4258 InstanceStart = InstanceSize = 0;
4259 return;
Daniel Dunbard4ae6c02009-04-22 04:39:47 +00004260 }
Daniel Dunbar97776872009-04-22 07:32:20 +00004261
4262 const ObjCIvarDecl *First = OIvars.front();
4263 const ObjCIvarDecl *Last = OIvars.back();
4264
4265 InstanceStart = ComputeIvarBaseOffset(CGM, OID, First);
4266 const llvm::Type *FieldTy =
4267 CGM.getTypes().ConvertTypeForMem(Last->getType());
4268 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004269// FIXME. This breaks compatibility with llvm-gcc-4.2 (but makes it compatible
4270// with gcc-4.2). We postpone this for now.
4271#if 0
4272 if (Last->isBitField()) {
4273 Expr *BitWidth = Last->getBitWidth();
4274 uint64_t BitFieldSize =
Eli Friedman9a901bb2009-04-26 19:19:15 +00004275 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004276 Size = (BitFieldSize / 8) + ((BitFieldSize % 8) != 0);
4277 }
4278#endif
Daniel Dunbar97776872009-04-22 07:32:20 +00004279 InstanceSize = ComputeIvarBaseOffset(CGM, OID, Last) + Size;
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004280}
4281
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004282void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4283 std::string ClassName = ID->getNameAsString();
4284 if (!ObjCEmptyCacheVar) {
4285 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004286 ObjCTypes.CacheTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004287 false,
4288 llvm::GlobalValue::ExternalLinkage,
4289 0,
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004290 "_objc_empty_cache",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004291 &CGM.getModule());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004292
4293 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004294 ObjCTypes.ImpnfABITy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004295 false,
4296 llvm::GlobalValue::ExternalLinkage,
4297 0,
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004298 "_objc_empty_vtable",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004299 &CGM.getModule());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004300 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004301 assert(ID->getClassInterface() &&
4302 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004303 // FIXME: Is this correct (that meta class size is never computed)?
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004304 uint32_t InstanceStart =
4305 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
4306 uint32_t InstanceSize = InstanceStart;
4307 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004308 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4309 std::string ObjCClassName(getClassSymbolPrefix());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004310
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004311 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004312
Daniel Dunbar04d40782009-04-14 06:00:08 +00004313 bool classIsHidden =
4314 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004315 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004316 flags |= OBJC2_CLS_HIDDEN;
4317 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004318 // class is root
4319 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004320 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004321 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004322 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004323 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004324 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4325 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4326 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004327 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004328 // work on super class metadata symbol.
4329 std::string SuperClassName =
4330 ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004331 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004332 }
4333 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4334 InstanceStart,
4335 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004336 std::string TClassName = ObjCMetaClassName + ClassName;
4337 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004338 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4339 classIsHidden);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004340
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004341 // Metadata for the class
4342 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004343 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004344 flags |= OBJC2_CLS_HIDDEN;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004345
4346 if (hasObjCExceptionAttribute(ID->getClassInterface()))
4347 flags |= CLS_EXCEPTION;
4348
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004349 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004350 flags |= CLS_ROOT;
4351 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00004352 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004353 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004354 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004355 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004356 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004357 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004358 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004359 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004360 InstanceStart,
4361 InstanceSize,
4362 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004363
4364 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004365 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004366 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4367 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004368 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004369
4370 // Force the definition of the EHType if necessary.
4371 if (flags & CLS_EXCEPTION)
4372 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004373}
4374
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004375/// GenerateProtocolRef - This routine is called to generate code for
4376/// a protocol reference expression; as in:
4377/// @code
4378/// @protocol(Proto1);
4379/// @endcode
4380/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4381/// which will hold address of the protocol meta-data.
4382///
4383llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
4384 const ObjCProtocolDecl *PD) {
4385
Fariborz Jahanian960cd062009-04-10 18:47:34 +00004386 // This routine is called for @protocol only. So, we must build definition
4387 // of protocol's meta-data (not a reference to it!)
4388 //
4389 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004390 ObjCTypes.ExternalProtocolPtrTy);
4391
4392 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
4393 ProtocolName += PD->getNameAsCString();
4394
4395 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
4396 if (PTGV)
4397 return Builder.CreateLoad(PTGV, false, "tmp");
4398 PTGV = new llvm::GlobalVariable(
4399 Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00004400 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004401 Init,
4402 ProtocolName,
4403 &CGM.getModule());
4404 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
4405 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4406 UsedGlobals.push_back(PTGV);
4407 return Builder.CreateLoad(PTGV, false, "tmp");
4408}
4409
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004410/// GenerateCategory - Build metadata for a category implementation.
4411/// struct _category_t {
4412/// const char * const name;
4413/// struct _class_t *const cls;
4414/// const struct _method_list_t * const instance_methods;
4415/// const struct _method_list_t * const class_methods;
4416/// const struct _protocol_list_t * const protocols;
4417/// const struct _prop_list_t * const properties;
4418/// }
4419///
4420void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
4421{
4422 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004423 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
4424 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004425 "_$_" + OCD->getNameAsString());
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004426 std::string ExtClassName(getClassSymbolPrefix() +
4427 Interface->getNameAsString());
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004428
4429 std::vector<llvm::Constant*> Values(6);
4430 Values[0] = GetClassName(OCD->getIdentifier());
4431 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004432 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004433 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004434 std::vector<llvm::Constant*> Methods;
4435 std::string MethodListName(Prefix);
4436 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
4437 "_$_" + OCD->getNameAsString();
4438
Douglas Gregor653f1b12009-04-23 01:02:12 +00004439 for (ObjCCategoryImplDecl::instmeth_iterator
4440 i = OCD->instmeth_begin(CGM.getContext()),
4441 e = OCD->instmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004442 // Instance methods should always be defined.
4443 Methods.push_back(GetMethodConstant(*i));
4444 }
4445
4446 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004447 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004448 Methods);
4449
4450 MethodListName = Prefix;
4451 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
4452 OCD->getNameAsString();
4453 Methods.clear();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004454 for (ObjCCategoryImplDecl::classmeth_iterator
4455 i = OCD->classmeth_begin(CGM.getContext()),
4456 e = OCD->classmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004457 // Class methods should always be defined.
4458 Methods.push_back(GetMethodConstant(*i));
4459 }
4460
4461 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004462 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004463 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004464 const ObjCCategoryDecl *Category =
4465 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004466 if (Category) {
4467 std::string ExtName(Interface->getNameAsString() + "_$_" +
4468 OCD->getNameAsString());
4469 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
4470 + Interface->getNameAsString() + "_$_"
4471 + Category->getNameAsString(),
4472 Category->protocol_begin(),
4473 Category->protocol_end());
4474 Values[5] =
4475 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
4476 OCD, Category, ObjCTypes);
4477 }
4478 else {
4479 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4480 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
4481 }
4482
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004483 llvm::Constant *Init =
4484 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
4485 Values);
4486 llvm::GlobalVariable *GCATV
4487 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
4488 false,
4489 llvm::GlobalValue::InternalLinkage,
4490 Init,
4491 ExtCatName,
4492 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004493 GCATV->setAlignment(
4494 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004495 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004496 UsedGlobals.push_back(GCATV);
4497 DefinedCategories.push_back(GCATV);
4498}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004499
4500/// GetMethodConstant - Return a struct objc_method constant for the
4501/// given method if it has been defined. The result is null if the
4502/// method has not been defined. The return value has type MethodPtrTy.
4503llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
4504 const ObjCMethodDecl *MD) {
4505 // FIXME: Use DenseMap::lookup
4506 llvm::Function *Fn = MethodDefinitions[MD];
4507 if (!Fn)
4508 return 0;
4509
4510 std::vector<llvm::Constant*> Method(3);
4511 Method[0] =
4512 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4513 ObjCTypes.SelectorPtrTy);
4514 Method[1] = GetMethodVarType(MD);
4515 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
4516 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
4517}
4518
4519/// EmitMethodList - Build meta-data for method declarations
4520/// struct _method_list_t {
4521/// uint32_t entsize; // sizeof(struct _objc_method)
4522/// uint32_t method_count;
4523/// struct _objc_method method_list[method_count];
4524/// }
4525///
4526llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
4527 const std::string &Name,
4528 const char *Section,
4529 const ConstantVector &Methods) {
4530 // Return null for empty list.
4531 if (Methods.empty())
4532 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
4533
4534 std::vector<llvm::Constant*> Values(3);
4535 // sizeof(struct _objc_method)
4536 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
4537 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4538 // method_count
4539 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
4540 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
4541 Methods.size());
4542 Values[2] = llvm::ConstantArray::get(AT, Methods);
4543 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4544
4545 llvm::GlobalVariable *GV =
4546 new llvm::GlobalVariable(Init->getType(), false,
4547 llvm::GlobalValue::InternalLinkage,
4548 Init,
4549 Name,
4550 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004551 GV->setAlignment(
4552 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004553 GV->setSection(Section);
4554 UsedGlobals.push_back(GV);
4555 return llvm::ConstantExpr::getBitCast(GV,
4556 ObjCTypes.MethodListnfABIPtrTy);
4557}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004558
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004559/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
4560/// the given ivar.
4561///
4562llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004563 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004564 const ObjCIvarDecl *Ivar) {
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00004565 std::string Name = "OBJC_IVAR_$_" +
Douglas Gregor6ab35242009-04-09 21:40:53 +00004566 getInterfaceDeclForIvar(ID, Ivar, CGM.getContext())->getNameAsString() +
4567 '.' + Ivar->getNameAsString();
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004568 llvm::GlobalVariable *IvarOffsetGV =
4569 CGM.getModule().getGlobalVariable(Name);
4570 if (!IvarOffsetGV)
4571 IvarOffsetGV =
4572 new llvm::GlobalVariable(ObjCTypes.LongTy,
4573 false,
4574 llvm::GlobalValue::ExternalLinkage,
4575 0,
4576 Name,
4577 &CGM.getModule());
4578 return IvarOffsetGV;
4579}
4580
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004581llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004582 const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004583 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004584 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00004585 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
4586 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
4587 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004588 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004589 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00004590
4591 // FIXME: This matches gcc, but shouldn't the visibility be set on
4592 // the use as well (i.e., in ObjCIvarOffsetVariable).
4593 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
4594 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
4595 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004596 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00004597 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00004598 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004599 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004600 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004601}
4602
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004603/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00004604/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004605/// IvarListnfABIPtrTy.
4606/// struct _ivar_t {
4607/// unsigned long int *offset; // pointer to ivar offset location
4608/// char *name;
4609/// char *type;
4610/// uint32_t alignment;
4611/// uint32_t size;
4612/// }
4613/// struct _ivar_list_t {
4614/// uint32 entsize; // sizeof(struct _ivar_t)
4615/// uint32 count;
4616/// struct _iver_t list[count];
4617/// }
4618///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004619
4620void CGObjCCommonMac::GetNamedIvarList(const ObjCInterfaceDecl *OID,
4621 llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const {
4622 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
4623 E = OID->ivar_end(); I != E; ++I) {
4624 // Ignore unnamed bit-fields.
4625 if (!(*I)->getDeclName())
4626 continue;
4627
4628 Res.push_back(*I);
4629 }
4630
4631 for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(CGM.getContext()),
4632 E = OID->prop_end(CGM.getContext()); I != E; ++I)
4633 if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl())
4634 Res.push_back(IV);
4635}
4636
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004637llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
4638 const ObjCImplementationDecl *ID) {
4639
4640 std::vector<llvm::Constant*> Ivars, Ivar(5);
4641
4642 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4643 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
4644
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004645 // FIXME. Consolidate this with similar code in GenerateClass.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00004646
Daniel Dunbar91636d62009-04-20 00:33:43 +00004647 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00004648 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004649 GetNamedIvarList(OID, OIvars);
Fariborz Jahanian99eee362009-04-01 19:37:34 +00004650
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004651 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
4652 ObjCIvarDecl *IVD = OIvars[i];
Daniel Dunbar3eec8aa2009-04-20 05:53:40 +00004653 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004654 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004655 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
4656 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004657 const llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004658 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004659 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
4660 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004661 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004662 Align = llvm::Log2_32(Align);
4663 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00004664 // NOTE. Size of a bitfield does not match gcc's, because of the
4665 // way bitfields are treated special in each. But I am told that
4666 // 'size' for bitfield ivars is ignored by the runtime so it does
4667 // not matter. If it matters, there is enough info to get the
4668 // bitfield right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004669 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4670 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
4671 }
4672 // Return null for empty list.
4673 if (Ivars.empty())
4674 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4675 std::vector<llvm::Constant*> Values(3);
4676 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
4677 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4678 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
4679 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
4680 Ivars.size());
4681 Values[2] = llvm::ConstantArray::get(AT, Ivars);
4682 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4683 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4684 llvm::GlobalVariable *GV =
4685 new llvm::GlobalVariable(Init->getType(), false,
4686 llvm::GlobalValue::InternalLinkage,
4687 Init,
4688 Prefix + OID->getNameAsString(),
4689 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004690 GV->setAlignment(
4691 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004692 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004693
4694 UsedGlobals.push_back(GV);
4695 return llvm::ConstantExpr::getBitCast(GV,
4696 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004697}
4698
4699llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4700 const ObjCProtocolDecl *PD) {
4701 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4702
4703 if (!Entry) {
4704 // We use the initializer as a marker of whether this is a forward
4705 // reference or not. At module finalization we add the empty
4706 // contents for protocols which were referenced but never defined.
4707 Entry =
4708 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
4709 llvm::GlobalValue::ExternalLinkage,
4710 0,
4711 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
4712 &CGM.getModule());
4713 Entry->setSection("__DATA,__datacoal_nt,coalesced");
4714 UsedGlobals.push_back(Entry);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004715 }
4716
4717 return Entry;
4718}
4719
4720/// GetOrEmitProtocol - Generate the protocol meta-data:
4721/// @code
4722/// struct _protocol_t {
4723/// id isa; // NULL
4724/// const char * const protocol_name;
4725/// const struct _protocol_list_t * protocol_list; // super protocols
4726/// const struct method_list_t * const instance_methods;
4727/// const struct method_list_t * const class_methods;
4728/// const struct method_list_t *optionalInstanceMethods;
4729/// const struct method_list_t *optionalClassMethods;
4730/// const struct _prop_list_t * properties;
4731/// const uint32_t size; // sizeof(struct _protocol_t)
4732/// const uint32_t flags; // = 0
4733/// }
4734/// @endcode
4735///
4736
4737llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4738 const ObjCProtocolDecl *PD) {
4739 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4740
4741 // Early exit if a defining object has already been generated.
4742 if (Entry && Entry->hasInitializer())
4743 return Entry;
4744
4745 const char *ProtocolName = PD->getNameAsCString();
4746
4747 // Construct method lists.
4748 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4749 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00004750 for (ObjCProtocolDecl::instmeth_iterator
4751 i = PD->instmeth_begin(CGM.getContext()),
4752 e = PD->instmeth_end(CGM.getContext());
4753 i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004754 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004755 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004756 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4757 OptInstanceMethods.push_back(C);
4758 } else {
4759 InstanceMethods.push_back(C);
4760 }
4761 }
4762
Douglas Gregor6ab35242009-04-09 21:40:53 +00004763 for (ObjCProtocolDecl::classmeth_iterator
4764 i = PD->classmeth_begin(CGM.getContext()),
4765 e = PD->classmeth_end(CGM.getContext());
4766 i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004767 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004768 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004769 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4770 OptClassMethods.push_back(C);
4771 } else {
4772 ClassMethods.push_back(C);
4773 }
4774 }
4775
4776 std::vector<llvm::Constant*> Values(10);
4777 // isa is NULL
4778 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
4779 Values[1] = GetClassName(PD->getIdentifier());
4780 Values[2] = EmitProtocolList(
4781 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4782 PD->protocol_begin(),
4783 PD->protocol_end());
4784
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004785 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004786 + PD->getNameAsString(),
4787 "__DATA, __objc_const",
4788 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004789 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004790 + PD->getNameAsString(),
4791 "__DATA, __objc_const",
4792 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004793 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004794 + PD->getNameAsString(),
4795 "__DATA, __objc_const",
4796 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004797 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004798 + PD->getNameAsString(),
4799 "__DATA, __objc_const",
4800 OptClassMethods);
4801 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4802 0, PD, ObjCTypes);
4803 uint32_t Size =
4804 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
4805 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4806 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
4807 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
4808 Values);
4809
4810 if (Entry) {
4811 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00004812 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004813 Entry->setInitializer(Init);
4814 } else {
4815 Entry =
4816 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004817 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004818 Init,
4819 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
4820 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004821 Entry->setAlignment(
4822 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004823 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004824 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004825 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
4826
4827 // Use this protocol meta-data to build protocol list table in section
4828 // __DATA, __objc_protolist
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004829 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004830 ObjCTypes.ProtocolnfABIPtrTy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004831 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004832 Entry,
4833 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
4834 +ProtocolName,
4835 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004836 PTGV->setAlignment(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004837 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00004838 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004839 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4840 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004841 return Entry;
4842}
4843
4844/// EmitProtocolList - Generate protocol list meta-data:
4845/// @code
4846/// struct _protocol_list_t {
4847/// long protocol_count; // Note, this is 32/64 bit
4848/// struct _protocol_t[protocol_count];
4849/// }
4850/// @endcode
4851///
4852llvm::Constant *
4853CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4854 ObjCProtocolDecl::protocol_iterator begin,
4855 ObjCProtocolDecl::protocol_iterator end) {
4856 std::vector<llvm::Constant*> ProtocolRefs;
4857
Fariborz Jahanianda320092009-01-29 19:24:30 +00004858 // Just return null for empty protocol lists
Daniel Dunbar948e2582009-02-15 07:36:20 +00004859 if (begin == end)
Fariborz Jahanianda320092009-01-29 19:24:30 +00004860 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4861
Daniel Dunbar948e2582009-02-15 07:36:20 +00004862 // FIXME: We shouldn't need to do this lookup here, should we?
Fariborz Jahanianda320092009-01-29 19:24:30 +00004863 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4864 if (GV)
Daniel Dunbar948e2582009-02-15 07:36:20 +00004865 return llvm::ConstantExpr::getBitCast(GV,
4866 ObjCTypes.ProtocolListnfABIPtrTy);
4867
4868 for (; begin != end; ++begin)
4869 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4870
Fariborz Jahanianda320092009-01-29 19:24:30 +00004871 // This list is null terminated.
4872 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004873 ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004874
4875 std::vector<llvm::Constant*> Values(2);
4876 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4877 Values[1] =
Daniel Dunbar948e2582009-02-15 07:36:20 +00004878 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004879 ProtocolRefs.size()),
4880 ProtocolRefs);
4881
4882 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4883 GV = new llvm::GlobalVariable(Init->getType(), false,
4884 llvm::GlobalValue::InternalLinkage,
4885 Init,
4886 Name,
4887 &CGM.getModule());
4888 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004889 GV->setAlignment(
4890 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004891 UsedGlobals.push_back(GV);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004892 return llvm::ConstantExpr::getBitCast(GV,
4893 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004894}
4895
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004896/// GetMethodDescriptionConstant - This routine build following meta-data:
4897/// struct _objc_method {
4898/// SEL _cmd;
4899/// char *method_type;
4900/// char *_imp;
4901/// }
4902
4903llvm::Constant *
4904CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4905 std::vector<llvm::Constant*> Desc(3);
4906 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4907 ObjCTypes.SelectorPtrTy);
4908 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004909 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004910 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4911 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4912}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004913
4914/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4915/// This code gen. amounts to generating code for:
4916/// @code
4917/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4918/// @encode
4919///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004920LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004921 CodeGen::CodeGenFunction &CGF,
4922 QualType ObjectTy,
4923 llvm::Value *BaseValue,
4924 const ObjCIvarDecl *Ivar,
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004925 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00004926 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00004927 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
4928 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004929}
4930
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004931llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
4932 CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00004933 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004934 const ObjCIvarDecl *Ivar) {
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00004935 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),
4936 false, "ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004937}
4938
Fariborz Jahanian46551122009-02-04 00:22:57 +00004939CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4940 CodeGen::CodeGenFunction &CGF,
4941 QualType ResultType,
4942 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004943 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00004944 QualType Arg0Ty,
4945 bool IsSuper,
4946 const CallArgList &CallArgs) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004947 // FIXME. Even though IsSuper is passes. This function doese not
4948 // handle calls to 'super' receivers.
4949 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004950 llvm::Value *Arg0 = Receiver;
4951 if (!IsSuper)
4952 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004953
4954 // Find the message function name.
Fariborz Jahanianef163782009-02-05 01:13:09 +00004955 // FIXME. This is too much work to get the ABI-specific result type
4956 // needed to find the message name.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004957 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4958 llvm::SmallVector<QualType, 16>());
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00004959 llvm::Constant *Fn = 0;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004960 std::string Name("\01l_");
4961 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004962#if 0
4963 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004964 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00004965 Fn = ObjCTypes.getMessageSendIdStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004966 // FIXME. Is there a better way of getting these names.
4967 // They are available in RuntimeFunctions vector pair.
4968 Name += "objc_msgSendId_stret_fixup";
4969 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004970 else
4971#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004972 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00004973 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004974 Name += "objc_msgSendSuper2_stret_fixup";
4975 }
4976 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004977 {
Chris Lattner1c02f862009-04-22 02:53:24 +00004978 Fn = ObjCTypes.getMessageSendStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004979 Name += "objc_msgSend_stret_fixup";
4980 }
4981 }
Fariborz Jahanian5b2bad02009-04-30 16:31:11 +00004982 else if (!IsSuper && ResultType->isFloatingType()) {
4983 if (const BuiltinType *BT = ResultType->getAsBuiltinType()) {
4984 BuiltinType::Kind k = BT->getKind();
4985 if (k == BuiltinType::LongDouble) {
4986 Fn = ObjCTypes.getMessageSendFpretFixupFn();
4987 Name += "objc_msgSend_fpret_fixup";
4988 }
4989 else {
4990 Fn = ObjCTypes.getMessageSendFixupFn();
4991 Name += "objc_msgSend_fixup";
4992 }
4993 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004994 }
4995 else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004996#if 0
4997// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004998 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00004999 Fn = ObjCTypes.getMessageSendIdFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005000 Name += "objc_msgSendId_fixup";
5001 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005002 else
5003#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005004 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005005 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005006 Name += "objc_msgSendSuper2_fixup";
5007 }
5008 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005009 {
Chris Lattner1c02f862009-04-22 02:53:24 +00005010 Fn = ObjCTypes.getMessageSendFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005011 Name += "objc_msgSend_fixup";
5012 }
5013 }
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005014 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005015 Name += '_';
5016 std::string SelName(Sel.getAsString());
5017 // Replace all ':' in selector name with '_' ouch!
5018 for(unsigned i = 0; i < SelName.size(); i++)
5019 if (SelName[i] == ':')
5020 SelName[i] = '_';
5021 Name += SelName;
5022 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5023 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005024 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005025 std::vector<llvm::Constant*> Values(2);
5026 Values[0] = Fn;
5027 Values[1] = GetMethodVarName(Sel);
5028 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
5029 GV = new llvm::GlobalVariable(Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005030 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005031 Init,
5032 Name,
5033 &CGM.getModule());
5034 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005035 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005036 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005037 }
5038 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005039
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005040 CallArgList ActualArgs;
5041 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
5042 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
5043 ObjCTypes.MessageRefCPtrTy));
5044 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005045 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
5046 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5047 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00005048 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005049 Callee = CGF.Builder.CreateBitCast(Callee,
5050 llvm::PointerType::getUnqual(FTy));
5051 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005052}
5053
5054/// Generate code for a message send expression in the nonfragile abi.
5055CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
5056 CodeGen::CodeGenFunction &CGF,
5057 QualType ResultType,
5058 Selector Sel,
5059 llvm::Value *Receiver,
5060 bool IsClassMessage,
5061 const CallArgList &CallArgs) {
Fariborz Jahanian46551122009-02-04 00:22:57 +00005062 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005063 Receiver, CGF.getContext().getObjCIdType(),
Fariborz Jahanian46551122009-02-04 00:22:57 +00005064 false, CallArgs);
5065}
5066
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005067llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005068CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005069 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5070
Daniel Dunbardfff2302009-03-02 05:18:14 +00005071 if (!GV) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005072 GV = new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
5073 llvm::GlobalValue::ExternalLinkage,
5074 0, Name, &CGM.getModule());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005075 }
5076
5077 return GV;
5078}
5079
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005080llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005081 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005082 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
5083
5084 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005085 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005086 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005087 Entry =
5088 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5089 llvm::GlobalValue::InternalLinkage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005090 ClassGV,
Daniel Dunbar11394522009-04-18 08:51:00 +00005091 "\01L_OBJC_CLASSLIST_REFERENCES_$_",
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005092 &CGM.getModule());
5093 Entry->setAlignment(
5094 CGM.getTargetData().getPrefTypeAlignment(
5095 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005096 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
5097 UsedGlobals.push_back(Entry);
5098 }
5099
5100 return Builder.CreateLoad(Entry, false, "tmp");
5101}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005102
Daniel Dunbar11394522009-04-18 08:51:00 +00005103llvm::Value *
5104CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
5105 const ObjCInterfaceDecl *ID) {
5106 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
5107
5108 if (!Entry) {
5109 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5110 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5111 Entry =
5112 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5113 llvm::GlobalValue::InternalLinkage,
5114 ClassGV,
5115 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
5116 &CGM.getModule());
5117 Entry->setAlignment(
5118 CGM.getTargetData().getPrefTypeAlignment(
5119 ObjCTypes.ClassnfABIPtrTy));
5120 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005121 UsedGlobals.push_back(Entry);
5122 }
5123
5124 return Builder.CreateLoad(Entry, false, "tmp");
5125}
5126
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005127/// EmitMetaClassRef - Return a Value * of the address of _class_t
5128/// meta-data
5129///
5130llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5131 const ObjCInterfaceDecl *ID) {
5132 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5133 if (Entry)
5134 return Builder.CreateLoad(Entry, false, "tmp");
5135
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005136 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005137 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005138 Entry =
5139 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5140 llvm::GlobalValue::InternalLinkage,
5141 MetaClassGV,
5142 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
5143 &CGM.getModule());
5144 Entry->setAlignment(
5145 CGM.getTargetData().getPrefTypeAlignment(
5146 ObjCTypes.ClassnfABIPtrTy));
5147
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005148 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005149 UsedGlobals.push_back(Entry);
5150
5151 return Builder.CreateLoad(Entry, false, "tmp");
5152}
5153
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005154/// GetClass - Return a reference to the class for the given interface
5155/// decl.
5156llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5157 const ObjCInterfaceDecl *ID) {
5158 return EmitClassRef(Builder, ID);
5159}
5160
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005161/// Generates a message send where the super is the receiver. This is
5162/// a message send to self with special delivery semantics indicating
5163/// which class's method should be called.
5164CodeGen::RValue
5165CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
5166 QualType ResultType,
5167 Selector Sel,
5168 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005169 bool isCategoryImpl,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005170 llvm::Value *Receiver,
5171 bool IsClassMessage,
5172 const CodeGen::CallArgList &CallArgs) {
5173 // ...
5174 // Create and init a super structure; this is a (receiver, class)
5175 // pair we will pass to objc_msgSendSuper.
5176 llvm::Value *ObjCSuper =
5177 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
5178
5179 llvm::Value *ReceiverAsObject =
5180 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5181 CGF.Builder.CreateStore(ReceiverAsObject,
5182 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
5183
5184 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005185 llvm::Value *Target;
5186 if (IsClassMessage) {
5187 if (isCategoryImpl) {
5188 // Message sent to "super' in a class method defined in
5189 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005190 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005191 Target = CGF.Builder.CreateStructGEP(Target, 0);
5192 Target = CGF.Builder.CreateLoad(Target);
5193 }
5194 else
5195 Target = EmitMetaClassRef(CGF.Builder, Class);
5196 }
5197 else
Daniel Dunbar11394522009-04-18 08:51:00 +00005198 Target = EmitSuperClassRef(CGF.Builder, Class);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005199
5200 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
5201 // and ObjCTypes types.
5202 const llvm::Type *ClassTy =
5203 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5204 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5205 CGF.Builder.CreateStore(Target,
5206 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
5207
5208 return EmitMessageSend(CGF, ResultType, Sel,
5209 ObjCSuper, ObjCTypes.SuperPtrCTy,
5210 true, CallArgs);
5211}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005212
5213llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
5214 Selector Sel) {
5215 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
5216
5217 if (!Entry) {
5218 llvm::Constant *Casted =
5219 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5220 ObjCTypes.SelectorPtrTy);
5221 Entry =
5222 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
5223 llvm::GlobalValue::InternalLinkage,
5224 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
5225 &CGM.getModule());
5226 Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip");
5227 UsedGlobals.push_back(Entry);
5228 }
5229
5230 return Builder.CreateLoad(Entry, false, "tmp");
5231}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005232/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
5233/// objc_assign_ivar (id src, id *dst)
5234///
5235void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
5236 llvm::Value *src, llvm::Value *dst)
5237{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005238 const llvm::Type * SrcTy = src->getType();
5239 if (!isa<llvm::PointerType>(SrcTy)) {
5240 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5241 assert(Size <= 8 && "does not support size > 8");
5242 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5243 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005244 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5245 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005246 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5247 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005248 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005249 src, dst, "assignivar");
5250 return;
5251}
5252
5253/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5254/// objc_assign_strongCast (id src, id *dst)
5255///
5256void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
5257 CodeGen::CodeGenFunction &CGF,
5258 llvm::Value *src, llvm::Value *dst)
5259{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005260 const llvm::Type * SrcTy = src->getType();
5261 if (!isa<llvm::PointerType>(SrcTy)) {
5262 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5263 assert(Size <= 8 && "does not support size > 8");
5264 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5265 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005266 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5267 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005268 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5269 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005270 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005271 src, dst, "weakassign");
5272 return;
5273}
5274
5275/// EmitObjCWeakRead - Code gen for loading value of a __weak
5276/// object: objc_read_weak (id *src)
5277///
5278llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
5279 CodeGen::CodeGenFunction &CGF,
5280 llvm::Value *AddrWeakObj)
5281{
Eli Friedman8339b352009-03-07 03:57:15 +00005282 const llvm::Type* DestTy =
5283 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005284 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005285 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005286 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005287 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005288 return read_weak;
5289}
5290
5291/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5292/// objc_assign_weak (id src, id *dst)
5293///
5294void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
5295 llvm::Value *src, llvm::Value *dst)
5296{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005297 const llvm::Type * SrcTy = src->getType();
5298 if (!isa<llvm::PointerType>(SrcTy)) {
5299 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5300 assert(Size <= 8 && "does not support size > 8");
5301 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5302 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005303 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5304 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005305 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5306 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005307 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005308 src, dst, "weakassign");
5309 return;
5310}
5311
5312/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5313/// objc_assign_global (id src, id *dst)
5314///
5315void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
5316 llvm::Value *src, llvm::Value *dst)
5317{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005318 const llvm::Type * SrcTy = src->getType();
5319 if (!isa<llvm::PointerType>(SrcTy)) {
5320 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5321 assert(Size <= 8 && "does not support size > 8");
5322 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5323 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005324 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5325 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005326 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5327 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005328 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005329 src, dst, "globalassign");
5330 return;
5331}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005332
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005333void
5334CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5335 const Stmt &S) {
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005336 bool isTry = isa<ObjCAtTryStmt>(S);
5337 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
5338 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005339 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005340 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005341 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005342 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
5343
5344 // For @synchronized, call objc_sync_enter(sync.expr). The
5345 // evaluation of the expression must occur before we enter the
5346 // @synchronized. We can safely avoid a temp here because jumps into
5347 // @synchronized are illegal & this will dominate uses.
5348 llvm::Value *SyncArg = 0;
5349 if (!isTry) {
5350 SyncArg =
5351 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
5352 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005353 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005354 }
5355
5356 // Push an EH context entry, used for handling rethrows and jumps
5357 // through finally.
5358 CGF.PushCleanupBlock(FinallyBlock);
5359
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005360 CGF.setInvokeDest(TryHandler);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005361
5362 CGF.EmitBlock(TryBlock);
5363 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
5364 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
5365 CGF.EmitBranchThroughCleanup(FinallyEnd);
5366
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005367 // Emit the exception handler.
5368
5369 CGF.EmitBlock(TryHandler);
5370
5371 llvm::Value *llvm_eh_exception =
5372 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
5373 llvm::Value *llvm_eh_selector_i64 =
5374 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64);
5375 llvm::Value *llvm_eh_typeid_for_i64 =
5376 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64);
5377 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5378 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow");
5379
5380 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
5381 SelectorArgs.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005382 SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005383
5384 // Construct the lists of (type, catch body) to handle.
Daniel Dunbarede8de92009-03-06 00:01:21 +00005385 llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005386 bool HasCatchAll = false;
5387 if (isTry) {
5388 if (const ObjCAtCatchStmt* CatchStmt =
5389 cast<ObjCAtTryStmt>(S).getCatchStmts()) {
5390 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005391 const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Steve Naroff7ba138a2009-03-03 19:52:17 +00005392 Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody()));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005393
5394 // catch(...) always matches.
Steve Naroff7ba138a2009-03-03 19:52:17 +00005395 if (!CatchDecl) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005396 // Use i8* null here to signal this is a catch all, not a cleanup.
5397 llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
5398 SelectorArgs.push_back(Null);
5399 HasCatchAll = true;
5400 break;
5401 }
5402
Daniel Dunbarede8de92009-03-06 00:01:21 +00005403 if (CGF.getContext().isObjCIdType(CatchDecl->getType()) ||
5404 CatchDecl->getType()->isObjCQualifiedIdType()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005405 llvm::Value *IDEHType =
5406 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5407 if (!IDEHType)
5408 IDEHType =
5409 new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5410 llvm::GlobalValue::ExternalLinkage,
5411 0, "OBJC_EHTYPE_id", &CGM.getModule());
5412 SelectorArgs.push_back(IDEHType);
5413 HasCatchAll = true;
5414 break;
5415 }
5416
5417 // All other types should be Objective-C interface pointer types.
Daniel Dunbarede8de92009-03-06 00:01:21 +00005418 const PointerType *PT = CatchDecl->getType()->getAsPointerType();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005419 assert(PT && "Invalid @catch type.");
5420 const ObjCInterfaceType *IT =
5421 PT->getPointeeType()->getAsObjCInterfaceType();
5422 assert(IT && "Invalid @catch type.");
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005423 llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005424 SelectorArgs.push_back(EHType);
5425 }
5426 }
5427 }
5428
5429 // We use a cleanup unless there was already a catch all.
5430 if (!HasCatchAll) {
5431 SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
Daniel Dunbarede8de92009-03-06 00:01:21 +00005432 Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005433 }
5434
5435 llvm::Value *Selector =
5436 CGF.Builder.CreateCall(llvm_eh_selector_i64,
5437 SelectorArgs.begin(), SelectorArgs.end(),
5438 "selector");
5439 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005440 const ParmVarDecl *CatchParam = Handlers[i].first;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005441 const Stmt *CatchBody = Handlers[i].second;
5442
5443 llvm::BasicBlock *Next = 0;
5444
5445 // The last handler always matches.
5446 if (i + 1 != e) {
5447 assert(CatchParam && "Only last handler can be a catch all.");
5448
5449 llvm::BasicBlock *Match = CGF.createBasicBlock("match");
5450 Next = CGF.createBasicBlock("catch.next");
5451 llvm::Value *Id =
5452 CGF.Builder.CreateCall(llvm_eh_typeid_for_i64,
5453 CGF.Builder.CreateBitCast(SelectorArgs[i+2],
5454 ObjCTypes.Int8PtrTy));
5455 CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id),
5456 Match, Next);
5457
5458 CGF.EmitBlock(Match);
5459 }
5460
5461 if (CatchBody) {
5462 llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end");
5463 llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler");
5464
5465 // Cleanups must call objc_end_catch.
5466 //
5467 // FIXME: It seems incorrect for objc_begin_catch to be inside
5468 // this context, but this matches gcc.
5469 CGF.PushCleanupBlock(MatchEnd);
5470 CGF.setInvokeDest(MatchHandler);
5471
5472 llvm::Value *ExcObject =
Chris Lattner8a569112009-04-22 02:15:23 +00005473 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005474
5475 // Bind the catch parameter if it exists.
5476 if (CatchParam) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005477 ExcObject =
5478 CGF.Builder.CreateBitCast(ExcObject,
5479 CGF.ConvertType(CatchParam->getType()));
5480 // CatchParam is a ParmVarDecl because of the grammar
5481 // construction used to handle this, but for codegen purposes
5482 // we treat this as a local decl.
5483 CGF.EmitLocalBlockVarDecl(*CatchParam);
5484 CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005485 }
5486
5487 CGF.ObjCEHValueStack.push_back(ExcObject);
5488 CGF.EmitStmt(CatchBody);
5489 CGF.ObjCEHValueStack.pop_back();
5490
5491 CGF.EmitBranchThroughCleanup(FinallyEnd);
5492
5493 CGF.EmitBlock(MatchHandler);
5494
5495 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5496 // We are required to emit this call to satisfy LLVM, even
5497 // though we don't use the result.
5498 llvm::SmallVector<llvm::Value*, 8> Args;
5499 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005500 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005501 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
5502 0));
5503 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5504 CGF.Builder.CreateStore(Exc, RethrowPtr);
5505 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5506
5507 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5508
5509 CGF.EmitBlock(MatchEnd);
5510
5511 // Unfortunately, we also have to generate another EH frame here
5512 // in case this throws.
5513 llvm::BasicBlock *MatchEndHandler =
5514 CGF.createBasicBlock("match.end.handler");
5515 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattner8a569112009-04-22 02:15:23 +00005516 CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005517 Cont, MatchEndHandler,
5518 Args.begin(), Args.begin());
5519
5520 CGF.EmitBlock(Cont);
5521 if (Info.SwitchBlock)
5522 CGF.EmitBlock(Info.SwitchBlock);
5523 if (Info.EndBlock)
5524 CGF.EmitBlock(Info.EndBlock);
5525
5526 CGF.EmitBlock(MatchEndHandler);
5527 Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5528 // We are required to emit this call to satisfy LLVM, even
5529 // though we don't use the result.
5530 Args.clear();
5531 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005532 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005533 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
5534 0));
5535 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5536 CGF.Builder.CreateStore(Exc, RethrowPtr);
5537 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5538
5539 if (Next)
5540 CGF.EmitBlock(Next);
5541 } else {
5542 assert(!Next && "catchup should be last handler.");
5543
5544 CGF.Builder.CreateStore(Exc, RethrowPtr);
5545 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5546 }
5547 }
5548
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005549 // Pop the cleanup entry, the @finally is outside this cleanup
5550 // scope.
5551 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5552 CGF.setInvokeDest(PrevLandingPad);
5553
5554 CGF.EmitBlock(FinallyBlock);
5555
5556 if (isTry) {
5557 if (const ObjCAtFinallyStmt* FinallyStmt =
5558 cast<ObjCAtTryStmt>(S).getFinallyStmt())
5559 CGF.EmitStmt(FinallyStmt->getFinallyBody());
5560 } else {
5561 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
5562 // @synchronized.
Chris Lattnerbbccd612009-04-22 02:38:11 +00005563 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005564 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005565
5566 if (Info.SwitchBlock)
5567 CGF.EmitBlock(Info.SwitchBlock);
5568 if (Info.EndBlock)
5569 CGF.EmitBlock(Info.EndBlock);
5570
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005571 // Branch around the rethrow code.
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005572 CGF.EmitBranch(FinallyEnd);
5573
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005574 CGF.EmitBlock(FinallyRethrow);
Chris Lattner8a569112009-04-22 02:15:23 +00005575 CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005576 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005577 CGF.Builder.CreateUnreachable();
5578
5579 CGF.EmitBlock(FinallyEnd);
5580}
5581
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005582/// EmitThrowStmt - Generate code for a throw statement.
5583void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
5584 const ObjCAtThrowStmt &S) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005585 llvm::Value *Exception;
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005586 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005587 Exception = CGF.EmitScalarExpr(ThrowExpr);
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005588 } else {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005589 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
5590 "Unexpected rethrow outside @catch block.");
5591 Exception = CGF.ObjCEHValueStack.back();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005592 }
5593
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005594 llvm::Value *ExceptionAsObject =
5595 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
5596 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
5597 if (InvokeDest) {
5598 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattnerbbccd612009-04-22 02:38:11 +00005599 CGF.Builder.CreateInvoke(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005600 Cont, InvokeDest,
5601 &ExceptionAsObject, &ExceptionAsObject + 1);
5602 CGF.EmitBlock(Cont);
5603 } else
Chris Lattnerbbccd612009-04-22 02:38:11 +00005604 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005605 CGF.Builder.CreateUnreachable();
5606
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005607 // Clear the insertion point to indicate we are in unreachable code.
5608 CGF.Builder.ClearInsertionPoint();
5609}
Daniel Dunbare588b992009-03-01 04:46:24 +00005610
5611llvm::Value *
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005612CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
5613 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00005614 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00005615
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005616 // If we don't need a definition, return the entry if found or check
5617 // if we use an external reference.
5618 if (!ForDefinition) {
5619 if (Entry)
5620 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00005621
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005622 // If this type (or a super class) has the __objc_exception__
5623 // attribute, emit an external reference.
5624 if (hasObjCExceptionAttribute(ID))
5625 return Entry =
5626 new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5627 llvm::GlobalValue::ExternalLinkage,
5628 0,
5629 (std::string("OBJC_EHTYPE_$_") +
5630 ID->getIdentifier()->getName()),
5631 &CGM.getModule());
5632 }
5633
5634 // Otherwise we need to either make a new entry or fill in the
5635 // initializer.
5636 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005637 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00005638 std::string VTableName = "objc_ehtype_vtable";
5639 llvm::GlobalVariable *VTableGV =
5640 CGM.getModule().getGlobalVariable(VTableName);
5641 if (!VTableGV)
5642 VTableGV = new llvm::GlobalVariable(ObjCTypes.Int8PtrTy, false,
5643 llvm::GlobalValue::ExternalLinkage,
5644 0, VTableName, &CGM.getModule());
5645
5646 llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2);
5647
5648 std::vector<llvm::Constant*> Values(3);
5649 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
5650 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005651 Values[2] = GetClassGlobal(ClassName);
Daniel Dunbare588b992009-03-01 04:46:24 +00005652 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
5653
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005654 if (Entry) {
5655 Entry->setInitializer(Init);
5656 } else {
5657 Entry = new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5658 llvm::GlobalValue::WeakAnyLinkage,
5659 Init,
5660 (std::string("OBJC_EHTYPE_$_") +
5661 ID->getIdentifier()->getName()),
5662 &CGM.getModule());
5663 }
5664
Daniel Dunbar04d40782009-04-14 06:00:08 +00005665 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005666 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005667 Entry->setAlignment(8);
5668
5669 if (ForDefinition) {
5670 Entry->setSection("__DATA,__objc_const");
5671 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
5672 } else {
5673 Entry->setSection("__DATA,__datacoal_nt,coalesced");
5674 }
Daniel Dunbare588b992009-03-01 04:46:24 +00005675
5676 return Entry;
5677}
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005678
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005679/* *** */
5680
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005681CodeGen::CGObjCRuntime *
5682CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00005683 return new CGObjCMac(CGM);
5684}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005685
5686CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005687CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005688 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005689}