blob: 35fb54fae43e7575c4c706b85c8413136a70ff85 [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
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000829 void BuildAggrIvarRecordLayout(const RecordType *RT,
830 unsigned int BytePos, bool ForStrongLayout,
831 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000832 void BuildAggrIvarLayout(const ObjCInterfaceDecl *OI,
833 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000834 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +0000835 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000836 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000837 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000838
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000839 /// GetIvarLayoutName - Returns a unique constant for the given
840 /// ivar layout bitmap.
841 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
842 const ObjCCommonTypesHelper &ObjCTypes);
843
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000844 /// EmitPropertyList - Emit the given property list. The return
845 /// value has type PropertyListPtrTy.
846 llvm::Constant *EmitPropertyList(const std::string &Name,
847 const Decl *Container,
848 const ObjCContainerDecl *OCD,
849 const ObjCCommonTypesHelper &ObjCTypes);
850
Fariborz Jahanianda320092009-01-29 19:24:30 +0000851 /// GetProtocolRef - Return a reference to the internal protocol
852 /// description, creating an empty one if it has not been
853 /// defined. The return value has type ProtocolPtrTy.
854 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000855
Chris Lattnercd0ee142009-03-31 08:33:16 +0000856 /// GetFieldBaseOffset - return's field byte offset.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000857 uint64_t GetFieldBaseOffset(const ObjCInterfaceDecl *OI,
858 const llvm::StructLayout *Layout,
Chris Lattnercd0ee142009-03-31 08:33:16 +0000859 const FieldDecl *Field);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000860
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000861 /// CreateMetadataVar - Create a global variable with internal
862 /// linkage for use by the Objective-C runtime.
863 ///
864 /// This is a convenience wrapper which not only creates the
865 /// variable, but also sets the section and alignment and adds the
866 /// global to the UsedGlobals list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000867 ///
868 /// \param Name - The variable name.
869 /// \param Init - The variable initializer; this is also used to
870 /// define the type of the variable.
871 /// \param Section - The section the variable should go into, or 0.
872 /// \param Align - The alignment for the variable, or 0.
873 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000874 /// "llvm.used".
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000875 llvm::GlobalVariable *CreateMetadataVar(const std::string &Name,
876 llvm::Constant *Init,
877 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000878 unsigned Align,
879 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000880
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000881 /// GetNamedIvarList - Return the list of ivars in the interface
882 /// itself (not including super classes and not including unnamed
883 /// bitfields).
884 ///
885 /// For the non-fragile ABI, this also includes synthesized property
886 /// ivars.
887 void GetNamedIvarList(const ObjCInterfaceDecl *OID,
888 llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const;
889
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000890public:
891 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
892 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000893
Steve Naroff33fdb732009-03-31 16:53:37 +0000894 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000895
896 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
897 const ObjCContainerDecl *CD=0);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000898
899 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
900
901 /// GetOrEmitProtocol - Get the protocol object for the given
902 /// declaration, emitting it if necessary. The return value has type
903 /// ProtocolPtrTy.
904 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
905
906 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
907 /// object for the given declaration, emitting it if needed. These
908 /// forward references will be filled in with empty bodies if no
909 /// definition is seen. The return value has type ProtocolPtrTy.
910 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000911};
912
913class CGObjCMac : public CGObjCCommonMac {
914private:
915 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000916 /// EmitImageInfo - Emit the image info marker used to encode some module
917 /// level information.
918 void EmitImageInfo();
919
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000920 /// EmitModuleInfo - Another marker encoding module level
921 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000922 void EmitModuleInfo();
923
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000924 /// EmitModuleSymols - Emit module symbols, the list of defined
925 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000926 llvm::Constant *EmitModuleSymbols();
927
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000928 /// FinishModule - Write out global data structures at the end of
929 /// processing a translation unit.
930 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000931
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000932 /// EmitClassExtension - Generate the class extension structure used
933 /// to store the weak ivar layout and properties. The return value
934 /// has type ClassExtensionPtrTy.
935 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
936
937 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
938 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000939 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000940 const ObjCInterfaceDecl *ID);
941
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000942 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000943 QualType ResultType,
944 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000945 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000946 QualType Arg0Ty,
947 bool IsSuper,
948 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000949
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000950 /// EmitIvarList - Emit the ivar list for the given
951 /// implementation. If ForClass is true the list of class ivars
952 /// (i.e. metaclass ivars) is emitted, otherwise the list of
953 /// interface ivars will be emitted. The return value has type
954 /// IvarListPtrTy.
955 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000956 bool ForClass);
957
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000958 /// EmitMetaClass - Emit a forward reference to the class structure
959 /// for the metaclass of the given interface. The return value has
960 /// type ClassPtrTy.
961 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
962
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000963 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000964 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000965 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
966 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000967 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000968
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000969 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000970
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000971 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000972
973 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000974 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000975 llvm::Constant *EmitMethodList(const std::string &Name,
976 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000977 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000978
979 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000980 /// method declarations.
981 /// - TypeName: The name for the type containing the methods.
982 /// - IsProtocol: True iff these methods are for a protocol.
983 /// - ClassMethds: True iff these are class methods.
984 /// - Required: When true, only "required" methods are
985 /// listed. Similarly, when false only "optional" methods are
986 /// listed. For classes this should always be true.
987 /// - begin, end: The method list to output.
988 ///
989 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000990 llvm::Constant *EmitMethodDescList(const std::string &Name,
991 const char *Section,
992 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000993
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000994 /// GetOrEmitProtocol - Get the protocol object for the given
995 /// declaration, emitting it if necessary. The return value has type
996 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000997 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000998
999 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1000 /// object for the given declaration, emitting it if needed. These
1001 /// forward references will be filled in with empty bodies if no
1002 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001003 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001004
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001005 /// EmitProtocolExtension - Generate the protocol extension
1006 /// structure used to store optional instance and class methods, and
1007 /// protocol properties. The return value has type
1008 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001009 llvm::Constant *
1010 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1011 const ConstantVector &OptInstanceMethods,
1012 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001013
1014 /// EmitProtocolList - Generate the list of referenced
1015 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +00001016 llvm::Constant *EmitProtocolList(const std::string &Name,
1017 ObjCProtocolDecl::protocol_iterator begin,
1018 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001019
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001020 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1021 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001022 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001023
Fariborz Jahanianda320092009-01-29 19:24:30 +00001024 public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001025 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001026
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001027 virtual llvm::Function *ModuleInitFunction();
1028
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001029 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001030 QualType ResultType,
1031 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001032 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001033 bool IsClassMessage,
1034 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001035
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001036 virtual CodeGen::RValue
1037 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001038 QualType ResultType,
1039 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001040 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001041 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001042 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001043 bool IsClassMessage,
1044 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001045
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001046 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001047 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001048
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001049 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001050
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001051 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001052
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001053 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001054
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001055 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001056 const ObjCProtocolDecl *PD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00001057
Chris Lattner74391b42009-03-22 21:03:39 +00001058 virtual llvm::Constant *GetPropertyGetFunction();
1059 virtual llvm::Constant *GetPropertySetFunction();
1060 virtual llvm::Constant *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001061
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001062 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1063 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001064 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1065 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001066 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001067 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001068 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1069 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001070 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1071 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001072 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1073 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001074 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1075 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001076
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001077 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1078 QualType ObjectTy,
1079 llvm::Value *BaseValue,
1080 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001081 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001082 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001083 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001084 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001085};
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001086
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001087class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001088private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001089 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001090 llvm::GlobalVariable* ObjCEmptyCacheVar;
1091 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001092
Daniel Dunbar11394522009-04-18 08:51:00 +00001093 /// SuperClassReferences - uniqued super class references.
1094 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
1095
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001096 /// MetaClassReferences - uniqued meta class references.
1097 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001098
1099 /// EHTypeReferences - uniqued class ehtype references.
1100 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001101
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001102 /// FinishNonFragileABIModule - Write out global data structures at the end of
1103 /// processing a translation unit.
1104 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001105
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00001106 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1107 unsigned InstanceStart,
1108 unsigned InstanceSize,
1109 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001110 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
1111 llvm::Constant *IsAGV,
1112 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001113 llvm::Constant *ClassRoGV,
1114 bool HiddenVisibility);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001115
1116 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
1117
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001118 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
1119
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001120 /// EmitMethodList - Emit the method list for the given
1121 /// implementation. The return value has type MethodListnfABITy.
1122 llvm::Constant *EmitMethodList(const std::string &Name,
1123 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001124 const ConstantVector &Methods);
1125 /// EmitIvarList - Emit the ivar list for the given
1126 /// implementation. If ForClass is true the list of class ivars
1127 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1128 /// interface ivars will be emitted. The return value has type
1129 /// IvarListnfABIPtrTy.
1130 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00001131
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001132 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001133 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001134 unsigned long int offset);
1135
Fariborz Jahanianda320092009-01-29 19:24:30 +00001136 /// GetOrEmitProtocol - Get the protocol object for the given
1137 /// declaration, emitting it if necessary. The return value has type
1138 /// ProtocolPtrTy.
1139 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
1140
1141 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1142 /// object for the given declaration, emitting it if needed. These
1143 /// forward references will be filled in with empty bodies if no
1144 /// definition is seen. The return value has type ProtocolPtrTy.
1145 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
1146
1147 /// EmitProtocolList - Generate the list of referenced
1148 /// protocols. The return value has type ProtocolListPtrTy.
1149 llvm::Constant *EmitProtocolList(const std::string &Name,
1150 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001151 ObjCProtocolDecl::protocol_iterator end);
1152
1153 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1154 QualType ResultType,
1155 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00001156 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001157 QualType Arg0Ty,
1158 bool IsSuper,
1159 const CallArgList &CallArgs);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001160
1161 /// GetClassGlobal - Return the global variable for the Objective-C
1162 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001163 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
1164
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001165 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001166 /// for the given class reference.
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001167 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001168 const ObjCInterfaceDecl *ID);
1169
1170 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1171 /// for the given super class reference.
1172 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1173 const ObjCInterfaceDecl *ID);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001174
1175 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1176 /// meta-data
1177 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
1178 const ObjCInterfaceDecl *ID);
1179
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001180 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1181 /// the given ivar.
1182 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001183 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00001184 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001185 const ObjCIvarDecl *Ivar);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001186
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001187 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1188 /// for the given selector.
1189 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbare588b992009-03-01 04:46:24 +00001190
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001191 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001192 /// interface. The return value has type EHTypePtrTy.
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001193 llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
1194 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001195
1196 const char *getMetaclassSymbolPrefix() const {
1197 return "OBJC_METACLASS_$_";
1198 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001199
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001200 const char *getClassSymbolPrefix() const {
1201 return "OBJC_CLASS_$_";
1202 }
1203
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001204 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001205 uint32_t &InstanceStart,
1206 uint32_t &InstanceSize);
1207
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001208public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001209 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001210 // FIXME. All stubs for now!
1211 virtual llvm::Function *ModuleInitFunction();
1212
1213 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
1214 QualType ResultType,
1215 Selector Sel,
1216 llvm::Value *Receiver,
1217 bool IsClassMessage,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001218 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001219
1220 virtual CodeGen::RValue
1221 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
1222 QualType ResultType,
1223 Selector Sel,
1224 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001225 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001226 llvm::Value *Receiver,
1227 bool IsClassMessage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001228 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001229
1230 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001231 const ObjCInterfaceDecl *ID);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001232
1233 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001234 { return EmitSelector(Builder, Sel); }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001235
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001236 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001237
1238 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001239 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001240 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001241
Chris Lattner74391b42009-03-22 21:03:39 +00001242 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001243 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001244 }
Chris Lattner74391b42009-03-22 21:03:39 +00001245 virtual llvm::Constant *GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001246 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001247 }
Chris Lattner74391b42009-03-22 21:03:39 +00001248 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001249 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001250 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001251
1252 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00001253 const Stmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001254 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001255 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001256 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001257 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001258 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001259 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001260 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001261 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001262 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001263 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001264 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001265 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001266 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1267 QualType ObjectTy,
1268 llvm::Value *BaseValue,
1269 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001270 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001271 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001272 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001273 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001274};
1275
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001276} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001277
1278/* *** Helper Functions *** */
1279
1280/// getConstantGEP() - Help routine to construct simple GEPs.
1281static llvm::Constant *getConstantGEP(llvm::Constant *C,
1282 unsigned idx0,
1283 unsigned idx1) {
1284 llvm::Value *Idxs[] = {
1285 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
1286 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
1287 };
1288 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
1289}
1290
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001291/// hasObjCExceptionAttribute - Return true if this class or any super
1292/// class has the __objc_exception__ attribute.
1293static bool hasObjCExceptionAttribute(const ObjCInterfaceDecl *OID) {
Daniel Dunbarb11fa0d2009-04-13 21:08:27 +00001294 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001295 return true;
1296 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
1297 return hasObjCExceptionAttribute(Super);
1298 return false;
1299}
1300
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001301/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001302
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001303CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
1304 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001305{
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001306 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001307 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001308}
1309
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001310/// GetClass - Return a reference to the class for the given interface
1311/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001312llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001313 const ObjCInterfaceDecl *ID) {
1314 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001315}
1316
1317/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001318llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001319 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001320}
1321
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001322/// Generate a constant CFString object.
1323/*
1324 struct __builtin_CFString {
1325 const int *isa; // point to __CFConstantStringClassReference
1326 int flags;
1327 const char *str;
1328 long length;
1329 };
1330*/
1331
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001332llvm::Constant *CGObjCCommonMac::GenerateConstantString(
Steve Naroff33fdb732009-03-31 16:53:37 +00001333 const ObjCStringLiteral *SL) {
Steve Naroff8d4141f2009-04-01 13:55:36 +00001334 return CGM.GetAddrOfConstantCFString(SL->getString());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001335}
1336
1337/// Generates a message send where the super is the receiver. This is
1338/// a message send to self with special delivery semantics indicating
1339/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001340CodeGen::RValue
1341CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001342 QualType ResultType,
1343 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001344 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001345 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001346 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001347 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001348 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001349 // Create and init a super structure; this is a (receiver, class)
1350 // pair we will pass to objc_msgSendSuper.
1351 llvm::Value *ObjCSuper =
1352 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
1353 llvm::Value *ReceiverAsObject =
1354 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
1355 CGF.Builder.CreateStore(ReceiverAsObject,
1356 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001357
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001358 // If this is a class message the metaclass is passed as the target.
1359 llvm::Value *Target;
1360 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001361 if (isCategoryImpl) {
1362 // Message sent to 'super' in a class method defined in a category
1363 // implementation requires an odd treatment.
1364 // If we are in a class method, we must retrieve the
1365 // _metaclass_ for the current class, pointed at by
1366 // the class's "isa" pointer. The following assumes that
1367 // isa" is the first ivar in a class (which it must be).
1368 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1369 Target = CGF.Builder.CreateStructGEP(Target, 0);
1370 Target = CGF.Builder.CreateLoad(Target);
1371 }
1372 else {
1373 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1374 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1375 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1376 Target = Super;
1377 }
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001378 } else {
1379 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1380 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001381 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
1382 // and ObjCTypes types.
1383 const llvm::Type *ClassTy =
1384 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001385 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001386 CGF.Builder.CreateStore(Target,
1387 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
1388
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001389 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001390 ObjCSuper, ObjCTypes.SuperPtrCTy,
1391 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001392}
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001393
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001394/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001395CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001396 QualType ResultType,
1397 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001398 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001399 bool IsClassMessage,
1400 const CallArgList &CallArgs) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001401 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001402 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001403 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001404}
1405
1406CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001407 QualType ResultType,
1408 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001409 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001410 QualType Arg0Ty,
1411 bool IsSuper,
1412 const CallArgList &CallArgs) {
1413 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001414 if (!IsSuper)
1415 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001416 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
1417 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
1418 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001419 CGF.getContext().getObjCSelType()));
1420 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001421
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001422 CodeGenTypes &Types = CGM.getTypes();
1423 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
Fariborz Jahanian65257ca2009-04-29 22:47:27 +00001424 // FIXME. vararg flag must be true when this API is used for 64bit code gen.
1425 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001426
1427 llvm::Constant *Fn;
Daniel Dunbar88b53962009-02-02 22:03:45 +00001428 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Daniel Dunbar5669e572008-10-17 03:24:53 +00001429 Fn = ObjCTypes.getSendStretFn(IsSuper);
1430 } else if (ResultType->isFloatingType()) {
1431 // FIXME: Sadly, this is wrong. This actually depends on the
1432 // architecture. This happens to be right for x86-32 though.
1433 Fn = ObjCTypes.getSendFpretFn(IsSuper);
1434 } else {
1435 Fn = ObjCTypes.getSendFn(IsSuper);
1436 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +00001437 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +00001438 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001439}
1440
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001441llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001442 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001443 // FIXME: I don't understand why gcc generates this, or where it is
1444 // resolved. Investigate. Its also wasteful to look this up over and
1445 // over.
1446 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1447
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001448 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
1449 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001450}
1451
Fariborz Jahanianda320092009-01-29 19:24:30 +00001452void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001453 // FIXME: We shouldn't need this, the protocol decl should contain
1454 // enough information to tell us whether this was a declaration or a
1455 // definition.
1456 DefinedProtocols.insert(PD->getIdentifier());
1457
1458 // If we have generated a forward reference to this protocol, emit
1459 // it now. Otherwise do nothing, the protocol objects are lazily
1460 // emitted.
1461 if (Protocols.count(PD->getIdentifier()))
1462 GetOrEmitProtocol(PD);
1463}
1464
Fariborz Jahanianda320092009-01-29 19:24:30 +00001465llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001466 if (DefinedProtocols.count(PD->getIdentifier()))
1467 return GetOrEmitProtocol(PD);
1468 return GetOrEmitProtocolRef(PD);
1469}
1470
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001471/*
1472 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1473 struct _objc_protocol {
1474 struct _objc_protocol_extension *isa;
1475 char *protocol_name;
1476 struct _objc_protocol_list *protocol_list;
1477 struct _objc__method_prototype_list *instance_methods;
1478 struct _objc__method_prototype_list *class_methods
1479 };
1480
1481 See EmitProtocolExtension().
1482*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001483llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1484 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1485
1486 // Early exit if a defining object has already been generated.
1487 if (Entry && Entry->hasInitializer())
1488 return Entry;
1489
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001490 // FIXME: I don't understand why gcc generates this, or where it is
1491 // resolved. Investigate. Its also wasteful to look this up over and
1492 // over.
1493 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1494
Chris Lattner8ec03f52008-11-24 03:54:41 +00001495 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001496
1497 // Construct method lists.
1498 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1499 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00001500 for (ObjCProtocolDecl::instmeth_iterator
1501 i = PD->instmeth_begin(CGM.getContext()),
1502 e = PD->instmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001503 ObjCMethodDecl *MD = *i;
1504 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1505 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1506 OptInstanceMethods.push_back(C);
1507 } else {
1508 InstanceMethods.push_back(C);
1509 }
1510 }
1511
Douglas Gregor6ab35242009-04-09 21:40:53 +00001512 for (ObjCProtocolDecl::classmeth_iterator
1513 i = PD->classmeth_begin(CGM.getContext()),
1514 e = PD->classmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001515 ObjCMethodDecl *MD = *i;
1516 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1517 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1518 OptClassMethods.push_back(C);
1519 } else {
1520 ClassMethods.push_back(C);
1521 }
1522 }
1523
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001524 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001525 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001526 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001527 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001528 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001529 PD->protocol_begin(),
1530 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001531 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001532 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
1533 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001534 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1535 InstanceMethods);
1536 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001537 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
1538 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001539 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1540 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001541 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
1542 Values);
1543
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001544 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001545 // Already created, fix the linkage and update the initializer.
1546 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001547 Entry->setInitializer(Init);
1548 } else {
1549 Entry =
1550 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
1551 llvm::GlobalValue::InternalLinkage,
1552 Init,
1553 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
1554 &CGM.getModule());
1555 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001556 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001557 UsedGlobals.push_back(Entry);
1558 // FIXME: Is this necessary? Why only for protocol?
1559 Entry->setAlignment(4);
1560 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001561
1562 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001563}
1564
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001565llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001566 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1567
1568 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001569 // We use the initializer as a marker of whether this is a forward
1570 // reference or not. At module finalization we add the empty
1571 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001572 Entry =
1573 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001574 llvm::GlobalValue::ExternalLinkage,
1575 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001576 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001577 &CGM.getModule());
1578 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001579 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001580 UsedGlobals.push_back(Entry);
1581 // FIXME: Is this necessary? Why only for protocol?
1582 Entry->setAlignment(4);
1583 }
1584
1585 return Entry;
1586}
1587
1588/*
1589 struct _objc_protocol_extension {
1590 uint32_t size;
1591 struct objc_method_description_list *optional_instance_methods;
1592 struct objc_method_description_list *optional_class_methods;
1593 struct objc_property_list *instance_properties;
1594 };
1595*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001596llvm::Constant *
1597CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1598 const ConstantVector &OptInstanceMethods,
1599 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001600 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001601 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001602 std::vector<llvm::Constant*> Values(4);
1603 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001604 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001605 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1606 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001607 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1608 OptInstanceMethods);
1609 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001610 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1611 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001612 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1613 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001614 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1615 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001616 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001617
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001618 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001619 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1620 Values[3]->isNullValue())
1621 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1622
1623 llvm::Constant *Init =
1624 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001625
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001626 // No special section, but goes in llvm.used
1627 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
1628 Init,
1629 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001630}
1631
1632/*
1633 struct objc_protocol_list {
1634 struct objc_protocol_list *next;
1635 long count;
1636 Protocol *list[];
1637 };
1638*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001639llvm::Constant *
1640CGObjCMac::EmitProtocolList(const std::string &Name,
1641 ObjCProtocolDecl::protocol_iterator begin,
1642 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001643 std::vector<llvm::Constant*> ProtocolRefs;
1644
Daniel Dunbardbc933702008-08-21 21:57:41 +00001645 for (; begin != end; ++begin)
1646 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001647
1648 // Just return null for empty protocol lists
1649 if (ProtocolRefs.empty())
1650 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1651
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001652 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001653 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1654
1655 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001656 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001657 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1658 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1659 Values[2] =
1660 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1661 ProtocolRefs.size()),
1662 ProtocolRefs);
1663
1664 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1665 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001666 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001667 4, false);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001668 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1669}
1670
1671/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001672 struct _objc_property {
1673 const char * const name;
1674 const char * const attributes;
1675 };
1676
1677 struct _objc_property_list {
1678 uint32_t entsize; // sizeof (struct _objc_property)
1679 uint32_t prop_count;
1680 struct _objc_property[prop_count];
1681 };
1682*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001683llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1684 const Decl *Container,
1685 const ObjCContainerDecl *OCD,
1686 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001687 std::vector<llvm::Constant*> Properties, Prop(2);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001688 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(CGM.getContext()),
1689 E = OCD->prop_end(CGM.getContext()); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00001690 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001691 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001692 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001693 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1694 Prop));
1695 }
1696
1697 // Return null for empty list.
1698 if (Properties.empty())
1699 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1700
1701 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001702 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001703 std::vector<llvm::Constant*> Values(3);
1704 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1705 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1706 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1707 Properties.size());
1708 Values[2] = llvm::ConstantArray::get(AT, Properties);
1709 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1710
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001711 llvm::GlobalVariable *GV =
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001712 CreateMetadataVar(Name, Init,
1713 (ObjCABI == 2) ? "__DATA, __objc_const" :
1714 "__OBJC,__property,regular,no_dead_strip",
1715 (ObjCABI == 2) ? 8 : 4,
1716 true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001717 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001718}
1719
1720/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001721 struct objc_method_description_list {
1722 int count;
1723 struct objc_method_description list[];
1724 };
1725*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001726llvm::Constant *
1727CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1728 std::vector<llvm::Constant*> Desc(2);
1729 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1730 ObjCTypes.SelectorPtrTy);
1731 Desc[1] = GetMethodVarType(MD);
1732 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1733 Desc);
1734}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001735
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001736llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1737 const char *Section,
1738 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001739 // Return null for empty list.
1740 if (Methods.empty())
1741 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1742
1743 std::vector<llvm::Constant*> Values(2);
1744 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1745 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1746 Methods.size());
1747 Values[1] = llvm::ConstantArray::get(AT, Methods);
1748 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1749
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001750 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001751 return llvm::ConstantExpr::getBitCast(GV,
1752 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001753}
1754
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001755/*
1756 struct _objc_category {
1757 char *category_name;
1758 char *class_name;
1759 struct _objc_method_list *instance_methods;
1760 struct _objc_method_list *class_methods;
1761 struct _objc_protocol_list *protocols;
1762 uint32_t size; // <rdar://4585769>
1763 struct _objc_property_list *instance_properties;
1764 };
1765 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001766void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001767 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001768
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001769 // FIXME: This is poor design, the OCD should have a pointer to the
1770 // category decl. Additionally, note that Category can be null for
1771 // the @implementation w/o an @interface case. Sema should just
1772 // create one for us as it does for @implementation so everyone else
1773 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001774 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001775 const ObjCCategoryDecl *Category =
1776 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001777 std::string ExtName(Interface->getNameAsString() + "_" +
1778 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001779
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001780 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001781 for (ObjCCategoryImplDecl::instmeth_iterator
1782 i = OCD->instmeth_begin(CGM.getContext()),
1783 e = OCD->instmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001784 // Instance methods should always be defined.
1785 InstanceMethods.push_back(GetMethodConstant(*i));
1786 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001787 for (ObjCCategoryImplDecl::classmeth_iterator
1788 i = OCD->classmeth_begin(CGM.getContext()),
1789 e = OCD->classmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001790 // Class methods should always be defined.
1791 ClassMethods.push_back(GetMethodConstant(*i));
1792 }
1793
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001794 std::vector<llvm::Constant*> Values(7);
1795 Values[0] = GetClassName(OCD->getIdentifier());
1796 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00001797 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001798 Values[2] =
1799 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1800 ExtName,
1801 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001802 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001803 Values[3] =
1804 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001805 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001806 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001807 if (Category) {
1808 Values[4] =
1809 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1810 Category->protocol_begin(),
1811 Category->protocol_end());
1812 } else {
1813 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1814 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001815 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001816
1817 // If there is no category @interface then there can be no properties.
1818 if (Category) {
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001819 Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001820 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001821 } else {
1822 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1823 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001824
1825 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1826 Values);
1827
1828 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001829 CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init,
1830 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001831 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001832 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001833}
1834
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001835// FIXME: Get from somewhere?
1836enum ClassFlags {
1837 eClassFlags_Factory = 0x00001,
1838 eClassFlags_Meta = 0x00002,
1839 // <rdr://5142207>
1840 eClassFlags_HasCXXStructors = 0x02000,
1841 eClassFlags_Hidden = 0x20000,
1842 eClassFlags_ABI2_Hidden = 0x00010,
1843 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1844};
1845
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001846/*
1847 struct _objc_class {
1848 Class isa;
1849 Class super_class;
1850 const char *name;
1851 long version;
1852 long info;
1853 long instance_size;
1854 struct _objc_ivar_list *ivars;
1855 struct _objc_method_list *methods;
1856 struct _objc_cache *cache;
1857 struct _objc_protocol_list *protocols;
1858 // Objective-C 1.0 extensions (<rdr://4585769>)
1859 const char *ivar_layout;
1860 struct _objc_class_ext *ext;
1861 };
1862
1863 See EmitClassExtension();
1864 */
1865void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001866 DefinedSymbols.insert(ID->getIdentifier());
1867
Chris Lattner8ec03f52008-11-24 03:54:41 +00001868 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001869 // FIXME: Gross
1870 ObjCInterfaceDecl *Interface =
1871 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001872 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001873 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001874 Interface->protocol_begin(),
1875 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001876 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar2bebbf02009-05-03 10:46:44 +00001877 unsigned Size =
1878 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001879
1880 // FIXME: Set CXX-structors flag.
Daniel Dunbar04d40782009-04-14 06:00:08 +00001881 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001882 Flags |= eClassFlags_Hidden;
1883
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001884 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001885 for (ObjCImplementationDecl::instmeth_iterator
1886 i = ID->instmeth_begin(CGM.getContext()),
1887 e = ID->instmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001888 // Instance methods should always be defined.
1889 InstanceMethods.push_back(GetMethodConstant(*i));
1890 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001891 for (ObjCImplementationDecl::classmeth_iterator
1892 i = ID->classmeth_begin(CGM.getContext()),
1893 e = ID->classmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001894 // Class methods should always be defined.
1895 ClassMethods.push_back(GetMethodConstant(*i));
1896 }
1897
Douglas Gregor653f1b12009-04-23 01:02:12 +00001898 for (ObjCImplementationDecl::propimpl_iterator
1899 i = ID->propimpl_begin(CGM.getContext()),
1900 e = ID->propimpl_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001901 ObjCPropertyImplDecl *PID = *i;
1902
1903 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1904 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1905
1906 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1907 if (llvm::Constant *C = GetMethodConstant(MD))
1908 InstanceMethods.push_back(C);
1909 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1910 if (llvm::Constant *C = GetMethodConstant(MD))
1911 InstanceMethods.push_back(C);
1912 }
1913 }
1914
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001915 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar5384b092009-05-03 08:56:52 +00001916 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001917 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001918 // Record a reference to the super class.
1919 LazySymbols.insert(Super->getIdentifier());
1920
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001921 Values[ 1] =
1922 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1923 ObjCTypes.ClassPtrTy);
1924 } else {
1925 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1926 }
1927 Values[ 2] = GetClassName(ID->getIdentifier());
1928 // Version is always 0.
1929 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1930 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1931 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001932 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001933 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001934 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001935 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001936 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001937 // cache is always NULL.
1938 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1939 Values[ 9] = Protocols;
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00001940 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001941 Values[11] = EmitClassExtension(ID);
1942 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1943 Values);
1944
1945 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001946 CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init,
1947 "__OBJC,__class,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001948 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001949 DefinedClasses.push_back(GV);
1950}
1951
1952llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1953 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001954 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001955 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001956 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001957
Daniel Dunbar04d40782009-04-14 06:00:08 +00001958 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001959 Flags |= eClassFlags_Hidden;
1960
1961 std::vector<llvm::Constant*> Values(12);
1962 // The isa for the metaclass is the root of the hierarchy.
1963 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1964 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1965 Root = Super;
1966 Values[ 0] =
1967 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1968 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001969 // The super class for the metaclass is emitted as the name of the
1970 // super class. The runtime fixes this up to point to the
1971 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001972 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1973 Values[ 1] =
1974 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1975 ObjCTypes.ClassPtrTy);
1976 } else {
1977 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1978 }
1979 Values[ 2] = GetClassName(ID->getIdentifier());
1980 // Version is always 0.
1981 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1982 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1983 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001984 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001985 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001986 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001987 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001988 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001989 // cache is always NULL.
1990 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1991 Values[ 9] = Protocols;
1992 // ivar_layout for metaclass is always NULL.
1993 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1994 // The class extension is always unused for metaclasses.
1995 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1996 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1997 Values);
1998
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001999 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002000 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002001
2002 // Check for a forward reference.
2003 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2004 if (GV) {
2005 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2006 "Forward metaclass reference has incorrect type.");
2007 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2008 GV->setInitializer(Init);
2009 } else {
2010 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
2011 llvm::GlobalValue::InternalLinkage,
2012 Init, Name,
2013 &CGM.getModule());
2014 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002015 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002016 GV->setAlignment(4);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002017 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002018
2019 return GV;
2020}
2021
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002022llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002023 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002024
2025 // FIXME: Should we look these up somewhere other than the
2026 // module. Its a bit silly since we only generate these while
2027 // processing an implementation, so exactly one pointer would work
2028 // if know when we entered/exitted an implementation block.
2029
2030 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002031 // Previously, metaclass with internal linkage may have been defined.
2032 // pass 'true' as 2nd argument so it is returned.
2033 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002034 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2035 "Forward metaclass reference has incorrect type.");
2036 return GV;
2037 } else {
2038 // Generate as an external reference to keep a consistent
2039 // module. This will be patched up when we emit the metaclass.
2040 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
2041 llvm::GlobalValue::ExternalLinkage,
2042 0,
2043 Name,
2044 &CGM.getModule());
2045 }
2046}
2047
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002048/*
2049 struct objc_class_ext {
2050 uint32_t size;
2051 const char *weak_ivar_layout;
2052 struct _objc_property_list *properties;
2053 };
2054*/
2055llvm::Constant *
2056CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
2057 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002058 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002059
2060 std::vector<llvm::Constant*> Values(3);
2061 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002062 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002063 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002064 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002065
2066 // Return null if no extension bits are used.
2067 if (Values[1]->isNullValue() && Values[2]->isNullValue())
2068 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
2069
2070 llvm::Constant *Init =
2071 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002072 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002073 Init, "__OBJC,__class_ext,regular,no_dead_strip",
2074 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002075}
2076
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00002077/// getInterfaceDeclForIvar - Get the interface declaration node where
2078/// this ivar is declared in.
2079/// FIXME. Ideally, this info should be in the ivar node. But currently
2080/// it is not and prevailing wisdom is that ASTs should not have more
2081/// info than is absolutely needed, even though this info reflects the
2082/// source language.
2083///
2084static const ObjCInterfaceDecl *getInterfaceDeclForIvar(
2085 const ObjCInterfaceDecl *OI,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002086 const ObjCIvarDecl *IVD,
2087 ASTContext &Context) {
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00002088 if (!OI)
2089 return 0;
2090 assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface");
2091 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
2092 E = OI->ivar_end(); I != E; ++I)
2093 if ((*I)->getIdentifier() == IVD->getIdentifier())
2094 return OI;
Fariborz Jahanian5a4b4532009-03-31 17:00:52 +00002095 // look into properties.
Douglas Gregor6ab35242009-04-09 21:40:53 +00002096 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(Context),
2097 E = OI->prop_end(Context); I != E; ++I) {
Fariborz Jahanian5a4b4532009-03-31 17:00:52 +00002098 ObjCPropertyDecl *PDecl = (*I);
2099 if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl())
2100 if (IV->getIdentifier() == IVD->getIdentifier())
2101 return OI;
2102 }
Douglas Gregor6ab35242009-04-09 21:40:53 +00002103 return getInterfaceDeclForIvar(OI->getSuperClass(), IVD, Context);
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00002104}
2105
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002106/*
2107 struct objc_ivar {
2108 char *ivar_name;
2109 char *ivar_type;
2110 int ivar_offset;
2111 };
2112
2113 struct objc_ivar_list {
2114 int ivar_count;
2115 struct objc_ivar list[count];
2116 };
2117 */
2118llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002119 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002120 std::vector<llvm::Constant*> Ivars, Ivar(3);
2121
2122 // When emitting the root class GCC emits ivar entries for the
2123 // actual class structure. It is not clear if we need to follow this
2124 // behavior; for now lets try and get away with not doing it. If so,
2125 // the cleanest solution would be to make up an ObjCInterfaceDecl
2126 // for the class.
2127 if (ForClass)
2128 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002129
2130 ObjCInterfaceDecl *OID =
2131 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002132
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002133 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
2134 GetNamedIvarList(OID, OIvars);
2135
2136 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2137 ObjCIvarDecl *IVD = OIvars[i];
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002138 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2139 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00002140 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002141 ComputeIvarBaseOffset(CGM, OID, IVD));
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002142 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002143 }
2144
2145 // Return null for empty list.
2146 if (Ivars.empty())
2147 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
2148
2149 std::vector<llvm::Constant*> Values(2);
2150 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
2151 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
2152 Ivars.size());
2153 Values[1] = llvm::ConstantArray::get(AT, Ivars);
2154 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2155
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002156 llvm::GlobalVariable *GV;
2157 if (ForClass)
2158 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(),
Daniel Dunbar58a29122009-03-09 22:18:41 +00002159 Init, "__OBJC,__class_vars,regular,no_dead_strip",
2160 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002161 else
2162 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_"
2163 + ID->getNameAsString(),
2164 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002165 4, true);
2166 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002167}
2168
2169/*
2170 struct objc_method {
2171 SEL method_name;
2172 char *method_types;
2173 void *method;
2174 };
2175
2176 struct objc_method_list {
2177 struct objc_method_list *obsolete;
2178 int count;
2179 struct objc_method methods_list[count];
2180 };
2181*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002182
2183/// GetMethodConstant - Return a struct objc_method constant for the
2184/// given method if it has been defined. The result is null if the
2185/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002186llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002187 // FIXME: Use DenseMap::lookup
2188 llvm::Function *Fn = MethodDefinitions[MD];
2189 if (!Fn)
2190 return 0;
2191
2192 std::vector<llvm::Constant*> Method(3);
2193 Method[0] =
2194 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2195 ObjCTypes.SelectorPtrTy);
2196 Method[1] = GetMethodVarType(MD);
2197 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
2198 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
2199}
2200
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002201llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
2202 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002203 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002204 // Return null for empty list.
2205 if (Methods.empty())
2206 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
2207
2208 std::vector<llvm::Constant*> Values(3);
2209 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
2210 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
2211 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
2212 Methods.size());
2213 Values[2] = llvm::ConstantArray::get(AT, Methods);
2214 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2215
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002216 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002217 return llvm::ConstantExpr::getBitCast(GV,
2218 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002219}
2220
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002221llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00002222 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002223 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002224 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002225
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002226 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002227 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002228 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002229 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002230 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002231 llvm::GlobalValue::InternalLinkage,
2232 Name,
2233 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002234 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002235
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002236 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002237}
2238
Daniel Dunbar48fa0642009-04-19 02:03:42 +00002239/// GetFieldBaseOffset - return the field's byte offset.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002240uint64_t CGObjCCommonMac::GetFieldBaseOffset(const ObjCInterfaceDecl *OI,
2241 const llvm::StructLayout *Layout,
Chris Lattnercd0ee142009-03-31 08:33:16 +00002242 const FieldDecl *Field) {
Daniel Dunbar97776872009-04-22 07:32:20 +00002243 // Is this a C struct?
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00002244 if (!OI)
2245 return Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
Daniel Dunbar97776872009-04-22 07:32:20 +00002246 return ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002247}
2248
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002249llvm::GlobalVariable *
2250CGObjCCommonMac::CreateMetadataVar(const std::string &Name,
2251 llvm::Constant *Init,
2252 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002253 unsigned Align,
2254 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002255 const llvm::Type *Ty = Init->getType();
2256 llvm::GlobalVariable *GV =
2257 new llvm::GlobalVariable(Ty, false,
2258 llvm::GlobalValue::InternalLinkage,
2259 Init,
2260 Name,
2261 &CGM.getModule());
2262 if (Section)
2263 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002264 if (Align)
2265 GV->setAlignment(Align);
2266 if (AddToUsed)
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002267 UsedGlobals.push_back(GV);
2268 return GV;
2269}
2270
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002271llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002272 // Abuse this interface function as a place to finalize.
2273 FinishModule();
2274
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002275 return NULL;
2276}
2277
Chris Lattner74391b42009-03-22 21:03:39 +00002278llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002279 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002280}
2281
Chris Lattner74391b42009-03-22 21:03:39 +00002282llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002283 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002284}
2285
Chris Lattner74391b42009-03-22 21:03:39 +00002286llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002287 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002288}
2289
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002290/*
2291
2292Objective-C setjmp-longjmp (sjlj) Exception Handling
2293--
2294
2295The basic framework for a @try-catch-finally is as follows:
2296{
2297 objc_exception_data d;
2298 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002299 bool _call_try_exit = true;
2300
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002301 objc_exception_try_enter(&d);
2302 if (!setjmp(d.jmp_buf)) {
2303 ... try body ...
2304 } else {
2305 // exception path
2306 id _caught = objc_exception_extract(&d);
2307
2308 // enter new try scope for handlers
2309 if (!setjmp(d.jmp_buf)) {
2310 ... match exception and execute catch blocks ...
2311
2312 // fell off end, rethrow.
2313 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00002314 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002315 } else {
2316 // exception in catch block
2317 _rethrow = objc_exception_extract(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002318 _call_try_exit = false;
2319 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002320 }
2321 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002322 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002323
2324finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002325 if (_call_try_exit)
2326 objc_exception_try_exit(&d);
2327
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002328 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002329 ... dispatch to finally destination ...
2330
2331finally_rethrow:
2332 objc_exception_throw(_rethrow);
2333
2334finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002335}
2336
2337This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00002338uses _rethrow to determine if objc_exception_try_exit should be called
2339and if the object should be rethrown. This breaks in the face of
2340throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002341
2342We specialize this framework for a few particular circumstances:
2343
2344 - If there are no catch blocks, then we avoid emitting the second
2345 exception handling context.
2346
2347 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2348 e)) we avoid emitting the code to rethrow an uncaught exception.
2349
2350 - FIXME: If there is no @finally block we can do a few more
2351 simplifications.
2352
2353Rethrows and Jumps-Through-Finally
2354--
2355
2356Support for implicit rethrows and jumping through the finally block is
2357handled by storing the current exception-handling context in
2358ObjCEHStack.
2359
Daniel Dunbar898d5082008-09-30 01:06:03 +00002360In order to implement proper @finally semantics, we support one basic
2361mechanism for jumping through the finally block to an arbitrary
2362destination. Constructs which generate exits from a @try or @catch
2363block use this mechanism to implement the proper semantics by chaining
2364jumps, as necessary.
2365
2366This mechanism works like the one used for indirect goto: we
2367arbitrarily assign an ID to each destination and store the ID for the
2368destination in a variable prior to entering the finally block. At the
2369end of the finally block we simply create a switch to the proper
2370destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002371
2372Code gen for @synchronized(expr) stmt;
2373Effectively generating code for:
2374objc_sync_enter(expr);
2375@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002376*/
2377
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002378void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2379 const Stmt &S) {
2380 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002381 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00002382 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002383 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar55e87422008-11-11 02:29:29 +00002384 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
2385 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
2386 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar1c566672009-02-24 01:43:46 +00002387
2388 // For @synchronized, call objc_sync_enter(sync.expr). The
2389 // evaluation of the expression must occur before we enter the
2390 // @synchronized. We can safely avoid a temp here because jumps into
2391 // @synchronized are illegal & this will dominate uses.
2392 llvm::Value *SyncArg = 0;
2393 if (!isTry) {
2394 SyncArg =
2395 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2396 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00002397 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002398 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002399
2400 // Push an EH context entry, used for handling rethrows and jumps
2401 // through finally.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002402 CGF.PushCleanupBlock(FinallyBlock);
2403
Anders Carlsson273558f2009-02-07 21:37:21 +00002404 CGF.ObjCEHValueStack.push_back(0);
2405
Daniel Dunbar898d5082008-09-30 01:06:03 +00002406 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00002407 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2408 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002409 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
2410 "_rethrow");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002411 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
2412 "_call_try_exit");
2413 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr);
2414
Anders Carlsson80f25672008-09-09 17:59:25 +00002415 // Enter a new try block and call setjmp.
Chris Lattner34b02a12009-04-22 02:26:14 +00002416 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Anders Carlsson80f25672008-09-09 17:59:25 +00002417 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
2418 "jmpbufarray");
2419 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
Chris Lattner34b02a12009-04-22 02:26:14 +00002420 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlsson80f25672008-09-09 17:59:25 +00002421 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00002422
Daniel Dunbar55e87422008-11-11 02:29:29 +00002423 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2424 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002425 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002426 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002427
2428 // Emit the @try block.
2429 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002430 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
2431 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002432 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002433
2434 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002435 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002436
2437 // Retrieve the exception object. We may emit multiple blocks but
2438 // nothing can cross this so the value is already in SSA form.
Chris Lattner34b02a12009-04-22 02:26:14 +00002439 llvm::Value *Caught =
2440 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2441 ExceptionData, "caught");
Anders Carlsson273558f2009-02-07 21:37:21 +00002442 CGF.ObjCEHValueStack.back() = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002443 if (!isTry)
2444 {
2445 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002446 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002447 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002448 }
2449 else if (const ObjCAtCatchStmt* CatchStmt =
2450 cast<ObjCAtTryStmt>(S).getCatchStmts())
2451 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00002452 // Enter a new exception try block (in case a @catch block throws
2453 // an exception).
Chris Lattner34b02a12009-04-22 02:26:14 +00002454 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002455
Chris Lattner34b02a12009-04-22 02:26:14 +00002456 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlsson80f25672008-09-09 17:59:25 +00002457 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002458 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00002459
Daniel Dunbar55e87422008-11-11 02:29:29 +00002460 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
2461 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002462 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002463
2464 CGF.EmitBlock(CatchBlock);
2465
Daniel Dunbar55e40722008-09-27 07:03:52 +00002466 // Handle catch list. As a special case we check if everything is
2467 // matched and avoid generating code for falling off the end if
2468 // so.
2469 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00002470 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00002471 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00002472
Steve Naroff7ba138a2009-03-03 19:52:17 +00002473 const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Daniel Dunbar129271a2008-09-27 07:36:24 +00002474 const PointerType *PT = 0;
2475
Anders Carlsson80f25672008-09-09 17:59:25 +00002476 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00002477 if (!CatchParam) {
2478 AllMatched = true;
2479 } else {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002480 PT = CatchParam->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002481
Daniel Dunbar97f61d12008-09-27 22:21:14 +00002482 // catch(id e) always matches.
2483 // FIXME: For the time being we also match id<X>; this should
2484 // be rejected by Sema instead.
Steve Naroff389bf462009-02-12 17:52:19 +00002485 if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) ||
Steve Naroff7ba138a2009-03-03 19:52:17 +00002486 CatchParam->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00002487 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00002488 }
2489
Daniel Dunbar55e40722008-09-27 07:03:52 +00002490 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00002491 if (CatchParam) {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002492 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002493 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002494 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002495 }
Anders Carlsson1452f552008-09-11 08:21:54 +00002496
Anders Carlssondde0a942008-09-11 09:15:33 +00002497 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002498 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002499 break;
2500 }
2501
Daniel Dunbar129271a2008-09-27 07:36:24 +00002502 assert(PT && "Unexpected non-pointer type in @catch");
2503 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00002504 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002505 assert(ObjCType && "Catch parameter must have Objective-C type!");
2506
2507 // Check if the @catch block matches the exception object.
2508 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
2509
Chris Lattner34b02a12009-04-22 02:26:14 +00002510 llvm::Value *Match =
2511 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
2512 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00002513
Daniel Dunbar55e87422008-11-11 02:29:29 +00002514 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00002515
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002516 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002517 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002518
2519 // Emit the @catch block.
2520 CGF.EmitBlock(MatchedBlock);
Steve Naroff7ba138a2009-03-03 19:52:17 +00002521 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002522 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002523
2524 llvm::Value *Tmp =
Steve Naroff7ba138a2009-03-03 19:52:17 +00002525 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002526 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002527 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002528
2529 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002530 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002531
2532 CGF.EmitBlock(NextCatchBlock);
2533 }
2534
Daniel Dunbar55e40722008-09-27 07:03:52 +00002535 if (!AllMatched) {
2536 // None of the handlers caught the exception, so store it to be
2537 // rethrown at the end of the @finally block.
2538 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002539 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002540 }
2541
2542 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002543 CGF.EmitBlock(CatchHandler);
Chris Lattner34b02a12009-04-22 02:26:14 +00002544 CGF.Builder.CreateStore(
2545 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2546 ExceptionData),
Daniel Dunbar55e40722008-09-27 07:03:52 +00002547 RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002548 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002549 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002550 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00002551 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002552 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002553 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson80f25672008-09-09 17:59:25 +00002554 }
2555
Daniel Dunbar898d5082008-09-30 01:06:03 +00002556 // Pop the exception-handling stack entry. It is important to do
2557 // this now, because the code in the @finally block is not in this
2558 // context.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002559 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2560
Anders Carlsson273558f2009-02-07 21:37:21 +00002561 CGF.ObjCEHValueStack.pop_back();
2562
Anders Carlsson80f25672008-09-09 17:59:25 +00002563 // Emit the @finally block.
2564 CGF.EmitBlock(FinallyBlock);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002565 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2566
2567 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2568
2569 CGF.EmitBlock(FinallyExit);
Chris Lattner34b02a12009-04-22 02:26:14 +00002570 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002571
2572 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002573 if (isTry) {
2574 if (const ObjCAtFinallyStmt* FinallyStmt =
2575 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2576 CGF.EmitStmt(FinallyStmt->getFinallyBody());
Daniel Dunbar1c566672009-02-24 01:43:46 +00002577 } else {
2578 // Emit objc_sync_exit(expr); as finally's sole statement for
2579 // @synchronized.
Chris Lattnerbbccd612009-04-22 02:38:11 +00002580 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002581 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002582
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002583 // Emit the switch block
2584 if (Info.SwitchBlock)
2585 CGF.EmitBlock(Info.SwitchBlock);
2586 if (Info.EndBlock)
2587 CGF.EmitBlock(Info.EndBlock);
2588
Daniel Dunbar898d5082008-09-30 01:06:03 +00002589 CGF.EmitBlock(FinallyRethrow);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002590 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbar898d5082008-09-30 01:06:03 +00002591 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002592 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00002593
2594 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002595}
2596
2597void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002598 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002599 llvm::Value *ExceptionAsObject;
2600
2601 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2602 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2603 ExceptionAsObject =
2604 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2605 } else {
Anders Carlsson273558f2009-02-07 21:37:21 +00002606 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002607 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00002608 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002609 }
2610
Chris Lattnerbbccd612009-04-22 02:38:11 +00002611 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002612 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002613
2614 // Clear the insertion point to indicate we are in unreachable code.
2615 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002616}
2617
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002618/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002619/// object: objc_read_weak (id *src)
2620///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002621llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002622 llvm::Value *AddrWeakObj)
2623{
Eli Friedman8339b352009-03-07 03:57:15 +00002624 const llvm::Type* DestTy =
2625 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002626 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00002627 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002628 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00002629 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002630 return read_weak;
2631}
2632
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002633/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2634/// objc_assign_weak (id src, id *dst)
2635///
2636void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2637 llvm::Value *src, llvm::Value *dst)
2638{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002639 const llvm::Type * SrcTy = src->getType();
2640 if (!isa<llvm::PointerType>(SrcTy)) {
2641 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2642 assert(Size <= 8 && "does not support size > 8");
2643 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2644 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002645 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2646 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002647 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2648 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00002649 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002650 src, dst, "weakassign");
2651 return;
2652}
2653
Fariborz Jahanian58626502008-11-19 00:59:10 +00002654/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2655/// objc_assign_global (id src, id *dst)
2656///
2657void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2658 llvm::Value *src, llvm::Value *dst)
2659{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002660 const llvm::Type * SrcTy = src->getType();
2661 if (!isa<llvm::PointerType>(SrcTy)) {
2662 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2663 assert(Size <= 8 && "does not support size > 8");
2664 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2665 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002666 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2667 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002668 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2669 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002670 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00002671 src, dst, "globalassign");
2672 return;
2673}
2674
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002675/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2676/// objc_assign_ivar (id src, id *dst)
2677///
2678void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2679 llvm::Value *src, llvm::Value *dst)
2680{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002681 const llvm::Type * SrcTy = src->getType();
2682 if (!isa<llvm::PointerType>(SrcTy)) {
2683 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2684 assert(Size <= 8 && "does not support size > 8");
2685 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2686 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002687 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2688 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002689 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2690 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002691 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(),
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002692 src, dst, "assignivar");
2693 return;
2694}
2695
Fariborz Jahanian58626502008-11-19 00:59:10 +00002696/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2697/// objc_assign_strongCast (id src, id *dst)
2698///
2699void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2700 llvm::Value *src, llvm::Value *dst)
2701{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002702 const llvm::Type * SrcTy = src->getType();
2703 if (!isa<llvm::PointerType>(SrcTy)) {
2704 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2705 assert(Size <= 8 && "does not support size > 8");
2706 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2707 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002708 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2709 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002710 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2711 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002712 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00002713 src, dst, "weakassign");
2714 return;
2715}
2716
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002717/// EmitObjCValueForIvar - Code Gen for ivar reference.
2718///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002719LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2720 QualType ObjectTy,
2721 llvm::Value *BaseValue,
2722 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002723 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00002724 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00002725 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
2726 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002727}
2728
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002729llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00002730 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002731 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00002732 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002733 return llvm::ConstantInt::get(
2734 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2735 Offset);
2736}
2737
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002738/* *** Private Interface *** */
2739
2740/// EmitImageInfo - Emit the image info marker used to encode some module
2741/// level information.
2742///
2743/// See: <rdr://4810609&4810587&4810587>
2744/// struct IMAGE_INFO {
2745/// unsigned version;
2746/// unsigned flags;
2747/// };
2748enum ImageInfoFlags {
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002749 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what
2750 // this implies.
2751 eImageInfo_GarbageCollected = (1 << 1),
2752 eImageInfo_GCOnly = (1 << 2),
2753 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
2754
2755 // A flag indicating that the module has no instances of an
2756 // @synthesize of a superclass variable. <rdar://problem/6803242>
2757 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002758};
2759
2760void CGObjCMac::EmitImageInfo() {
2761 unsigned version = 0; // Version is unused?
2762 unsigned flags = 0;
2763
2764 // FIXME: Fix and continue?
2765 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2766 flags |= eImageInfo_GarbageCollected;
2767 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2768 flags |= eImageInfo_GCOnly;
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002769
2770 // We never allow @synthesize of a superclass property.
2771 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002772
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002773 // Emitted as int[2];
2774 llvm::Constant *values[2] = {
2775 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2776 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2777 };
2778 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002779
2780 const char *Section;
2781 if (ObjCABI == 1)
2782 Section = "__OBJC, __image_info,regular";
2783 else
2784 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002785 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002786 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
2787 llvm::ConstantArray::get(AT, values, 2),
2788 Section,
2789 0,
2790 true);
2791 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002792}
2793
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002794
2795// struct objc_module {
2796// unsigned long version;
2797// unsigned long size;
2798// const char *name;
2799// Symtab symtab;
2800// };
2801
2802// FIXME: Get from somewhere
2803static const int ModuleVersion = 7;
2804
2805void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002806 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002807
2808 std::vector<llvm::Constant*> Values(4);
2809 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2810 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002811 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002812 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002813 Values[3] = EmitModuleSymbols();
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002814 CreateMetadataVar("\01L_OBJC_MODULES",
2815 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
2816 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002817 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002818}
2819
2820llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002821 unsigned NumClasses = DefinedClasses.size();
2822 unsigned NumCategories = DefinedCategories.size();
2823
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002824 // Return null if no symbols were defined.
2825 if (!NumClasses && !NumCategories)
2826 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2827
2828 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002829 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2830 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2831 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2832 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2833
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002834 // The runtime expects exactly the list of defined classes followed
2835 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002836 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002837 for (unsigned i=0; i<NumClasses; i++)
2838 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2839 ObjCTypes.Int8PtrTy);
2840 for (unsigned i=0; i<NumCategories; i++)
2841 Symbols[NumClasses + i] =
2842 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2843 ObjCTypes.Int8PtrTy);
2844
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002845 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002846 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002847 NumClasses + NumCategories),
2848 Symbols);
2849
2850 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2851
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002852 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002853 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
2854 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002855 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002856 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2857}
2858
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002859llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002860 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002861 LazySymbols.insert(ID->getIdentifier());
2862
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002863 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2864
2865 if (!Entry) {
2866 llvm::Constant *Casted =
2867 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2868 ObjCTypes.ClassPtrTy);
2869 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002870 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
2871 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002872 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002873 }
2874
2875 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002876}
2877
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002878llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002879 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2880
2881 if (!Entry) {
2882 llvm::Constant *Casted =
2883 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2884 ObjCTypes.SelectorPtrTy);
2885 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002886 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
2887 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002888 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002889 }
2890
2891 return Builder.CreateLoad(Entry, false, "tmp");
2892}
2893
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002894llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002895 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002896
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002897 if (!Entry)
2898 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
2899 llvm::ConstantArray::get(Ident->getName()),
2900 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00002901 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002902
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002903 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002904}
2905
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00002906/// GetIvarLayoutName - Returns a unique constant for the given
2907/// ivar layout bitmap.
2908llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
2909 const ObjCCommonTypesHelper &ObjCTypes) {
2910 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
2911}
2912
Daniel Dunbard58edcb2009-05-03 14:10:34 +00002913static QualType::GCAttrTypes GetGCAttrTypeForType(ASTContext &Ctx,
2914 QualType FQT) {
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00002915 if (FQT.isObjCGCStrong())
2916 return QualType::Strong;
2917
2918 if (FQT.isObjCGCWeak())
2919 return QualType::Weak;
2920
Daniel Dunbard58edcb2009-05-03 14:10:34 +00002921 if (Ctx.isObjCObjectPointerType(FQT))
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00002922 return QualType::Strong;
2923
2924 if (const PointerType *PT = FQT->getAsPointerType())
Daniel Dunbard58edcb2009-05-03 14:10:34 +00002925 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00002926
2927 return QualType::GCNone;
2928}
2929
Daniel Dunbard58edcb2009-05-03 14:10:34 +00002930void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
2931 unsigned int BytePos,
2932 bool ForStrongLayout,
2933 bool &HasUnion) {
2934 const RecordDecl *RD = RT->getDecl();
2935 // FIXME - Use iterator.
2936 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(CGM.getContext()),
2937 RD->field_end(CGM.getContext()));
2938 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
2939 const llvm::StructLayout *RecLayout =
2940 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
2941
2942 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
2943 ForStrongLayout, HasUnion);
2944}
2945
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002946void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCInterfaceDecl *OI,
2947 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002948 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00002949 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002950 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +00002951 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002952 bool IsUnion = (RD && RD->isUnion());
2953 uint64_t MaxUnionIvarSize = 0;
2954 uint64_t MaxSkippedUnionIvarSize = 0;
2955 FieldDecl *MaxField = 0;
2956 FieldDecl *MaxSkippedField = 0;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002957 FieldDecl *LastFieldBitfield = 0;
2958
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002959 if (RecFields.empty())
2960 return;
Chris Lattnerf1690852009-03-31 08:48:01 +00002961 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
2962 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
2963
Chris Lattnerf1690852009-03-31 08:48:01 +00002964 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002965 FieldDecl *Field = RecFields[i];
Daniel Dunbar25d583e2009-05-03 14:17:18 +00002966
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002967 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002968 if (!Field->getIdentifier() || Field->isBitField()) {
2969 LastFieldBitfield = Field;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002970 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002971 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00002972
2973 unsigned FieldOffset = GetFieldBaseOffset(OI, Layout, Field);
2974
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002975 LastFieldBitfield = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002976 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00002977 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002978 if (FQT->isUnionType())
2979 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002980
Daniel Dunbard58edcb2009-05-03 14:10:34 +00002981 BuildAggrIvarRecordLayout(FQT->getAsRecordType(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00002982 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00002983 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002984 continue;
2985 }
Chris Lattnerf1690852009-03-31 08:48:01 +00002986
2987 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002988 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00002989 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002990 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00002991 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002992 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00002993 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
2994 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00002995 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002996 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00002997 FQT = CArray->getElementType();
2998 }
2999
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003000 assert(!FQT->isUnionType() &&
3001 "layout for array of unions not supported");
3002 if (FQT->isRecordType()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003003 int OldIndex = IvarsInfo.size() - 1;
3004 int OldSkIndex = SkipIvars.size() -1;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003005
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003006 const RecordType *RT = FQT->getAsRecordType();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003007 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003008 ForStrongLayout, HasUnion);
3009
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003010 // Replicate layout information for each array element. Note that
3011 // one element is already done.
3012 uint64_t ElIx = 1;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003013 for (int FirstIndex = IvarsInfo.size() - 1,
3014 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003015 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003016 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3017 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3018 IvarsInfo[i].ivar_size));
3019 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3020 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3021 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003022 }
3023 continue;
3024 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003025 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003026 // At this point, we are done with Record/Union and array there of.
3027 // For other arrays we are down to its element type.
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003028 QualType::GCAttrTypes GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003029
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003030 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003031 if ((ForStrongLayout && GCAttr == QualType::Strong)
3032 || (!ForStrongLayout && GCAttr == QualType::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003033 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003034 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003035 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003036 MaxUnionIvarSize = UnionIvarSize;
3037 MaxField = Field;
3038 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003039 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003040 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003041 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003042 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003043 } else if ((ForStrongLayout &&
3044 (GCAttr == QualType::GCNone || GCAttr == QualType::Weak))
3045 || (!ForStrongLayout && GCAttr != QualType::Weak)) {
3046 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003047 // FIXME: Why the asymmetry? We divide by word size in bits on
3048 // other side.
3049 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003050 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003051 MaxSkippedUnionIvarSize = UnionIvarSize;
3052 MaxSkippedField = Field;
3053 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003054 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003055 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003056 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003057 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003058 }
3059 }
3060 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003061
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003062 if (LastFieldBitfield) {
3063 // Last field was a bitfield. Must update skip info.
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003064 Expr *BitWidth = LastFieldBitfield->getBitWidth();
3065 uint64_t BitFieldSize =
Eli Friedman9a901bb2009-04-26 19:19:15 +00003066 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Daniel Dunbar487993b2009-05-03 13:32:01 +00003067 GC_IVAR skivar;
3068 skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout,
3069 LastFieldBitfield);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003070 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3071 + ((BitFieldSize % ByteSizeInBits) != 0);
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003072 SkipIvars.push_back(skivar);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003073 }
3074
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003075 if (MaxField)
3076 IvarsInfo.push_back(GC_IVAR(BytePos + GetFieldBaseOffset(OI, Layout,
3077 MaxField),
3078 MaxUnionIvarSize));
3079 if (MaxSkippedField)
3080 SkipIvars.push_back(GC_IVAR(BytePos + GetFieldBaseOffset(OI, Layout,
3081 MaxSkippedField),
3082 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003083}
3084
3085/// BuildIvarLayout - Builds ivar layout bitmap for the class
3086/// implementation for the __strong or __weak case.
3087/// The layout map displays which words in ivar list must be skipped
3088/// and which must be scanned by GC (see below). String is built of bytes.
3089/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3090/// of words to skip and right nibble is count of words to scan. So, each
3091/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3092/// represented by a 0x00 byte which also ends the string.
3093/// 1. when ForStrongLayout is true, following ivars are scanned:
3094/// - id, Class
3095/// - object *
3096/// - __strong anything
3097///
3098/// 2. When ForStrongLayout is false, following ivars are scanned:
3099/// - __weak anything
3100///
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003101llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003102 const ObjCImplementationDecl *OMD,
3103 bool ForStrongLayout) {
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003104 bool hasUnion = false;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003105
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003106 unsigned int WordsToScan, WordsToSkip;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003107 const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
3108 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
3109 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003110
Chris Lattnerf1690852009-03-31 08:48:01 +00003111 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003112 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003113 CGM.getContext().CollectObjCIvars(OI, RecFields);
3114 if (RecFields.empty())
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003115 return llvm::Constant::getNullValue(PtrTy);
Chris Lattnerf1690852009-03-31 08:48:01 +00003116
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003117 SkipIvars.clear();
3118 IvarsInfo.clear();
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00003119
Daniel Dunbar84ad77a2009-04-22 09:39:34 +00003120 const llvm::StructLayout *Layout =
3121 CGM.getTargetData().getStructLayout(GetConcreteClassStruct(CGM, OI));
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003122 BuildAggrIvarLayout(OI, Layout, 0, RecFields, 0, ForStrongLayout, hasUnion);
3123 if (IvarsInfo.empty())
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003124 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003125
3126 // Sort on byte position in case we encounterred a union nested in
3127 // the ivar list.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003128 if (hasUnion && !IvarsInfo.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003129 std::sort(IvarsInfo.begin(), IvarsInfo.end());
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003130 if (hasUnion && !SkipIvars.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003131 std::sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003132
3133 // Build the string of skip/scan nibbles
Fariborz Jahanian8c2f2d12009-04-24 17:15:27 +00003134 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003135 unsigned int WordSize =
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003136 CGM.getTypes().getTargetData().getTypePaddedSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003137 if (IvarsInfo[0].ivar_bytepos == 0) {
3138 WordsToSkip = 0;
3139 WordsToScan = IvarsInfo[0].ivar_size;
3140 }
3141 else {
3142 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3143 WordsToScan = IvarsInfo[0].ivar_size;
3144 }
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003145 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++)
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003146 {
3147 unsigned int TailPrevGCObjC =
3148 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
3149 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC)
3150 {
3151 // consecutive 'scanned' object pointers.
3152 WordsToScan += IvarsInfo[i].ivar_size;
3153 }
3154 else
3155 {
3156 // Skip over 'gc'able object pointer which lay over each other.
3157 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3158 continue;
3159 // Must skip over 1 or more words. We save current skip/scan values
3160 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003161 SKIP_SCAN SkScan;
3162 SkScan.skip = WordsToSkip;
3163 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003164 SkipScanIvars.push_back(SkScan);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003165
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003166 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003167 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3168 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003169 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003170 WordsToSkip = 0;
3171 WordsToScan = IvarsInfo[i].ivar_size;
3172 }
3173 }
3174 if (WordsToScan > 0)
3175 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003176 SKIP_SCAN SkScan;
3177 SkScan.skip = WordsToSkip;
3178 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003179 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003180 }
3181
3182 bool BytesSkipped = false;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003183 if (!SkipIvars.empty())
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003184 {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003185 unsigned int LastIndex = SkipIvars.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003186 int LastByteSkipped =
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003187 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
3188 LastIndex = IvarsInfo.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003189 int LastByteScanned =
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003190 IvarsInfo[LastIndex].ivar_bytepos +
3191 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003192 BytesSkipped = (LastByteSkipped > LastByteScanned);
3193 // Compute number of bytes to skip at the tail end of the last ivar scanned.
3194 if (BytesSkipped)
3195 {
3196 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003197 SKIP_SCAN SkScan;
3198 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3199 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003200 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003201 }
3202 }
3203 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3204 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003205 int SkipScan = SkipScanIvars.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003206 for (int i = 0; i <= SkipScan; i++)
3207 {
3208 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3209 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3210 // 0xM0 followed by 0x0N detected.
3211 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3212 for (int j = i+1; j < SkipScan; j++)
3213 SkipScanIvars[j] = SkipScanIvars[j+1];
3214 --SkipScan;
3215 }
3216 }
3217
3218 // Generate the string.
3219 std::string BitMap;
3220 for (int i = 0; i <= SkipScan; i++)
3221 {
3222 unsigned char byte;
3223 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3224 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3225 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3226 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
3227
3228 if (skip_small > 0 || skip_big > 0)
3229 BytesSkipped = true;
3230 // first skip big.
3231 for (unsigned int ix = 0; ix < skip_big; ix++)
3232 BitMap += (unsigned char)(0xf0);
3233
3234 // next (skip small, scan)
3235 if (skip_small)
3236 {
3237 byte = skip_small << 4;
3238 if (scan_big > 0)
3239 {
3240 byte |= 0xf;
3241 --scan_big;
3242 }
3243 else if (scan_small)
3244 {
3245 byte |= scan_small;
3246 scan_small = 0;
3247 }
3248 BitMap += byte;
3249 }
3250 // next scan big
3251 for (unsigned int ix = 0; ix < scan_big; ix++)
3252 BitMap += (unsigned char)(0x0f);
3253 // last scan small
3254 if (scan_small)
3255 {
3256 byte = scan_small;
3257 BitMap += byte;
3258 }
3259 }
3260 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003261 unsigned char zero = 0;
3262 BitMap += zero;
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003263
3264 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
3265 printf("\n%s ivar layout for class '%s': ",
3266 ForStrongLayout ? "strong" : "weak",
3267 OMD->getClassInterface()->getNameAsCString());
3268 const unsigned char *s = (unsigned char*)BitMap.c_str();
3269 for (unsigned i = 0; i < BitMap.size(); i++)
3270 if (!(s[i] & 0xf0))
3271 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3272 else
3273 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3274 printf("\n");
3275 }
3276
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003277 // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as
3278 // final layout.
3279 if (ForStrongLayout && !BytesSkipped)
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003280 return llvm::Constant::getNullValue(PtrTy);
3281 llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3282 llvm::ConstantArray::get(BitMap.c_str()),
3283 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003284 1, true);
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003285 return getConstantGEP(Entry, 0, 0);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003286}
3287
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003288llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003289 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3290
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003291 // FIXME: Avoid std::string copying.
3292 if (!Entry)
3293 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
3294 llvm::ConstantArray::get(Sel.getAsString()),
3295 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003296 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003297
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003298 return getConstantGEP(Entry, 0, 0);
3299}
3300
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003301// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003302llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003303 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3304}
3305
3306// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003307llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003308 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
3309}
3310
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003311llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003312 std::string TypeStr;
3313 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3314
3315 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003316
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003317 if (!Entry)
3318 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
3319 llvm::ConstantArray::get(TypeStr),
3320 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003321 1, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003322
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003323 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003324}
3325
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003326llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003327 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003328 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3329 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00003330
3331 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3332
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003333 if (!Entry)
3334 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
3335 llvm::ConstantArray::get(TypeStr),
3336 "__TEXT,__cstring,cstring_literals",
3337 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00003338
3339 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003340}
3341
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003342// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003343llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003344 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
3345
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003346 if (!Entry)
3347 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
3348 llvm::ConstantArray::get(Ident->getName()),
3349 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003350 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003351
3352 return getConstantGEP(Entry, 0, 0);
3353}
3354
3355// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003356// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003357llvm::Constant *
3358 CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3359 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003360 std::string TypeStr;
3361 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003362 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3363}
3364
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003365void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
3366 const ObjCContainerDecl *CD,
3367 std::string &NameOut) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00003368 NameOut = '\01';
3369 NameOut += (D->isInstanceMethod() ? '-' : '+');
Chris Lattner077bf5e2008-11-24 03:33:13 +00003370 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003371 assert (CD && "Missing container decl in GetNameForMethod");
3372 NameOut += CD->getNameAsString();
Fariborz Jahanian1e9aef32009-04-16 18:34:20 +00003373 if (const ObjCCategoryImplDecl *CID =
3374 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) {
3375 NameOut += '(';
3376 NameOut += CID->getNameAsString();
3377 NameOut+= ')';
3378 }
Chris Lattner077bf5e2008-11-24 03:33:13 +00003379 NameOut += ' ';
3380 NameOut += D->getSelector().getAsString();
3381 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003382}
3383
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003384void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003385 EmitModuleInfo();
3386
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003387 // Emit the dummy bodies for any protocols which were referenced but
3388 // never defined.
3389 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
3390 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
3391 if (i->second->hasInitializer())
3392 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003393
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003394 std::vector<llvm::Constant*> Values(5);
3395 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
3396 Values[1] = GetClassName(i->first);
3397 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
3398 Values[3] = Values[4] =
3399 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
3400 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
3401 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
3402 Values));
3403 }
3404
3405 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003406 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003407 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003408 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003409 }
3410
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003411 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003412 llvm::GlobalValue *GV =
3413 new llvm::GlobalVariable(AT, false,
3414 llvm::GlobalValue::AppendingLinkage,
3415 llvm::ConstantArray::get(AT, Used),
3416 "llvm.used",
3417 &CGM.getModule());
3418
3419 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003420
3421 // Add assembler directives to add lazy undefined symbol references
3422 // for classes which are referenced but not defined. This is
3423 // important for correct linker interaction.
3424
3425 // FIXME: Uh, this isn't particularly portable.
3426 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00003427
3428 if (!CGM.getModule().getModuleInlineAsm().empty())
3429 s << "\n";
3430
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003431 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
3432 e = LazySymbols.end(); i != e; ++i) {
3433 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
3434 }
3435 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
3436 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003437 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003438 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
3439 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00003440
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003441 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003442}
3443
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003444CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003445 : CGObjCCommonMac(cgm),
3446 ObjCTypes(cgm)
3447{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003448 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003449 ObjCABI = 2;
3450}
3451
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003452/* *** */
3453
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003454ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
3455: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003456{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003457 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3458 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003459
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003460 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003461 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003462 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003463 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003464 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
3465
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003466 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00003467 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003468 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003469
3470 // FIXME: It would be nice to unify this with the opaque type, so
3471 // that the IR comes out a bit cleaner.
3472 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
3473 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003474
3475 // I'm not sure I like this. The implicit coordination is a bit
3476 // gross. We should solve this in a reasonable fashion because this
3477 // is a pretty common task (match some runtime data structure with
3478 // an LLVM data structure).
3479
3480 // FIXME: This is leaked.
3481 // FIXME: Merge with rewriter code?
3482
3483 // struct _objc_super {
3484 // id self;
3485 // Class cls;
3486 // }
3487 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3488 SourceLocation(),
3489 &Ctx.Idents.get("_objc_super"));
Douglas Gregor6ab35242009-04-09 21:40:53 +00003490 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3491 Ctx.getObjCIdType(), 0, false));
3492 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3493 Ctx.getObjCClassType(), 0, false));
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003494 RD->completeDefinition(Ctx);
3495
3496 SuperCTy = Ctx.getTagDeclType(RD);
3497 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
3498
3499 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003500 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
3501
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003502 // struct _prop_t {
3503 // char *name;
3504 // char *attributes;
3505 // }
Chris Lattner1c02f862009-04-22 02:53:24 +00003506 PropertyTy = llvm::StructType::get(Int8PtrTy, Int8PtrTy, NULL);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003507 CGM.getModule().addTypeName("struct._prop_t",
3508 PropertyTy);
3509
3510 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003511 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003512 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003513 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003514 // }
3515 PropertyListTy = llvm::StructType::get(IntTy,
3516 IntTy,
3517 llvm::ArrayType::get(PropertyTy, 0),
3518 NULL);
3519 CGM.getModule().addTypeName("struct._prop_list_t",
3520 PropertyListTy);
3521 // struct _prop_list_t *
3522 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
3523
3524 // struct _objc_method {
3525 // SEL _cmd;
3526 // char *method_type;
3527 // char *_imp;
3528 // }
3529 MethodTy = llvm::StructType::get(SelectorPtrTy,
3530 Int8PtrTy,
3531 Int8PtrTy,
3532 NULL);
3533 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003534
3535 // struct _objc_cache *
3536 CacheTy = llvm::OpaqueType::get();
3537 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
3538 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003539}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003540
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003541ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
3542 : ObjCCommonTypesHelper(cgm)
3543{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003544 // struct _objc_method_description {
3545 // SEL name;
3546 // char *types;
3547 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003548 MethodDescriptionTy =
3549 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003550 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003551 NULL);
3552 CGM.getModule().addTypeName("struct._objc_method_description",
3553 MethodDescriptionTy);
3554
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003555 // struct _objc_method_description_list {
3556 // int count;
3557 // struct _objc_method_description[1];
3558 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003559 MethodDescriptionListTy =
3560 llvm::StructType::get(IntTy,
3561 llvm::ArrayType::get(MethodDescriptionTy, 0),
3562 NULL);
3563 CGM.getModule().addTypeName("struct._objc_method_description_list",
3564 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003565
3566 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003567 MethodDescriptionListPtrTy =
3568 llvm::PointerType::getUnqual(MethodDescriptionListTy);
3569
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003570 // Protocol description structures
3571
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003572 // struct _objc_protocol_extension {
3573 // uint32_t size; // sizeof(struct _objc_protocol_extension)
3574 // struct _objc_method_description_list *optional_instance_methods;
3575 // struct _objc_method_description_list *optional_class_methods;
3576 // struct _objc_property_list *instance_properties;
3577 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003578 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003579 llvm::StructType::get(IntTy,
3580 MethodDescriptionListPtrTy,
3581 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003582 PropertyListPtrTy,
3583 NULL);
3584 CGM.getModule().addTypeName("struct._objc_protocol_extension",
3585 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003586
3587 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003588 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
3589
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003590 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003591
3592 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
3593 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3594
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003595 const llvm::Type *T =
3596 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
3597 LongTy,
3598 llvm::ArrayType::get(ProtocolTyHolder, 0),
3599 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003600 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
3601
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003602 // struct _objc_protocol {
3603 // struct _objc_protocol_extension *isa;
3604 // char *protocol_name;
3605 // struct _objc_protocol **_objc_protocol_list;
3606 // struct _objc_method_description_list *instance_methods;
3607 // struct _objc_method_description_list *class_methods;
3608 // }
3609 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003610 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003611 llvm::PointerType::getUnqual(ProtocolListTyHolder),
3612 MethodDescriptionListPtrTy,
3613 MethodDescriptionListPtrTy,
3614 NULL);
3615 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
3616
3617 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
3618 CGM.getModule().addTypeName("struct._objc_protocol_list",
3619 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003620 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003621 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
3622
3623 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003624 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003625 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003626
3627 // Class description structures
3628
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003629 // struct _objc_ivar {
3630 // char *ivar_name;
3631 // char *ivar_type;
3632 // int ivar_offset;
3633 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003634 IvarTy = llvm::StructType::get(Int8PtrTy,
3635 Int8PtrTy,
3636 IntTy,
3637 NULL);
3638 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
3639
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003640 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003641 IvarListTy = llvm::OpaqueType::get();
3642 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
3643 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
3644
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003645 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003646 MethodListTy = llvm::OpaqueType::get();
3647 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
3648 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
3649
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003650 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003651 ClassExtensionTy =
3652 llvm::StructType::get(IntTy,
3653 Int8PtrTy,
3654 PropertyListPtrTy,
3655 NULL);
3656 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
3657 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
3658
3659 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3660
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003661 // struct _objc_class {
3662 // Class isa;
3663 // Class super_class;
3664 // char *name;
3665 // long version;
3666 // long info;
3667 // long instance_size;
3668 // struct _objc_ivar_list *ivars;
3669 // struct _objc_method_list *methods;
3670 // struct _objc_cache *cache;
3671 // struct _objc_protocol_list *protocols;
3672 // char *ivar_layout;
3673 // struct _objc_class_ext *ext;
3674 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003675 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3676 llvm::PointerType::getUnqual(ClassTyHolder),
3677 Int8PtrTy,
3678 LongTy,
3679 LongTy,
3680 LongTy,
3681 IvarListPtrTy,
3682 MethodListPtrTy,
3683 CachePtrTy,
3684 ProtocolListPtrTy,
3685 Int8PtrTy,
3686 ClassExtensionPtrTy,
3687 NULL);
3688 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
3689
3690 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
3691 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
3692 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
3693
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003694 // struct _objc_category {
3695 // char *category_name;
3696 // char *class_name;
3697 // struct _objc_method_list *instance_method;
3698 // struct _objc_method_list *class_method;
3699 // uint32_t size; // sizeof(struct _objc_category)
3700 // struct _objc_property_list *instance_properties;// category's @property
3701 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003702 CategoryTy = llvm::StructType::get(Int8PtrTy,
3703 Int8PtrTy,
3704 MethodListPtrTy,
3705 MethodListPtrTy,
3706 ProtocolListPtrTy,
3707 IntTy,
3708 PropertyListPtrTy,
3709 NULL);
3710 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
3711
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003712 // Global metadata structures
3713
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003714 // struct _objc_symtab {
3715 // long sel_ref_cnt;
3716 // SEL *refs;
3717 // short cls_def_cnt;
3718 // short cat_def_cnt;
3719 // char *defs[cls_def_cnt + cat_def_cnt];
3720 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003721 SymtabTy = llvm::StructType::get(LongTy,
3722 SelectorPtrTy,
3723 ShortTy,
3724 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003725 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003726 NULL);
3727 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
3728 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
3729
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003730 // struct _objc_module {
3731 // long version;
3732 // long size; // sizeof(struct _objc_module)
3733 // char *name;
3734 // struct _objc_symtab* symtab;
3735 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003736 ModuleTy =
3737 llvm::StructType::get(LongTy,
3738 LongTy,
3739 Int8PtrTy,
3740 SymtabPtrTy,
3741 NULL);
3742 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003743
Anders Carlsson2abd89c2008-08-31 04:05:03 +00003744
Anders Carlsson124526b2008-09-09 10:10:21 +00003745 // FIXME: This is the size of the setjmp buffer and should be
3746 // target specific. 18 is what's used on 32-bit X86.
3747 uint64_t SetJmpBufferSize = 18;
3748
3749 // Exceptions
3750 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00003751 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00003752
3753 ExceptionDataTy =
3754 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
3755 SetJmpBufferSize),
3756 StackPtrTy, NULL);
3757 CGM.getModule().addTypeName("struct._objc_exception_data",
3758 ExceptionDataTy);
3759
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003760}
3761
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003762ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003763: ObjCCommonTypesHelper(cgm)
3764{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003765 // struct _method_list_t {
3766 // uint32_t entsize; // sizeof(struct _objc_method)
3767 // uint32_t method_count;
3768 // struct _objc_method method_list[method_count];
3769 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003770 MethodListnfABITy = llvm::StructType::get(IntTy,
3771 IntTy,
3772 llvm::ArrayType::get(MethodTy, 0),
3773 NULL);
3774 CGM.getModule().addTypeName("struct.__method_list_t",
3775 MethodListnfABITy);
3776 // struct method_list_t *
3777 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003778
3779 // struct _protocol_t {
3780 // id isa; // NULL
3781 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003782 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003783 // const struct method_list_t * const instance_methods;
3784 // const struct method_list_t * const class_methods;
3785 // const struct method_list_t *optionalInstanceMethods;
3786 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003787 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003788 // const uint32_t size; // sizeof(struct _protocol_t)
3789 // const uint32_t flags; // = 0
3790 // }
3791
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003792 // Holder for struct _protocol_list_t *
3793 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3794
3795 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3796 Int8PtrTy,
3797 llvm::PointerType::getUnqual(
3798 ProtocolListTyHolder),
3799 MethodListnfABIPtrTy,
3800 MethodListnfABIPtrTy,
3801 MethodListnfABIPtrTy,
3802 MethodListnfABIPtrTy,
3803 PropertyListPtrTy,
3804 IntTy,
3805 IntTy,
3806 NULL);
3807 CGM.getModule().addTypeName("struct._protocol_t",
3808 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003809
3810 // struct _protocol_t*
3811 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003812
Fariborz Jahanianda320092009-01-29 19:24:30 +00003813 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003814 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00003815 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003816 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003817 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3818 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003819 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003820 NULL);
3821 CGM.getModule().addTypeName("struct._objc_protocol_list",
3822 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003823 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3824 ProtocolListnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003825
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003826 // struct _objc_protocol_list*
3827 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003828
3829 // struct _ivar_t {
3830 // unsigned long int *offset; // pointer to ivar offset location
3831 // char *name;
3832 // char *type;
3833 // uint32_t alignment;
3834 // uint32_t size;
3835 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003836 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3837 Int8PtrTy,
3838 Int8PtrTy,
3839 IntTy,
3840 IntTy,
3841 NULL);
3842 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3843
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003844 // struct _ivar_list_t {
3845 // uint32 entsize; // sizeof(struct _ivar_t)
3846 // uint32 count;
3847 // struct _iver_t list[count];
3848 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003849 IvarListnfABITy = llvm::StructType::get(IntTy,
3850 IntTy,
3851 llvm::ArrayType::get(
3852 IvarnfABITy, 0),
3853 NULL);
3854 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3855
3856 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003857
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003858 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003859 // uint32_t const flags;
3860 // uint32_t const instanceStart;
3861 // uint32_t const instanceSize;
3862 // uint32_t const reserved; // only when building for 64bit targets
3863 // const uint8_t * const ivarLayout;
3864 // const char *const name;
3865 // const struct _method_list_t * const baseMethods;
3866 // const struct _objc_protocol_list *const baseProtocols;
3867 // const struct _ivar_list_t *const ivars;
3868 // const uint8_t * const weakIvarLayout;
3869 // const struct _prop_list_t * const properties;
3870 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003871
3872 // FIXME. Add 'reserved' field in 64bit abi mode!
3873 ClassRonfABITy = llvm::StructType::get(IntTy,
3874 IntTy,
3875 IntTy,
3876 Int8PtrTy,
3877 Int8PtrTy,
3878 MethodListnfABIPtrTy,
3879 ProtocolListnfABIPtrTy,
3880 IvarListnfABIPtrTy,
3881 Int8PtrTy,
3882 PropertyListPtrTy,
3883 NULL);
3884 CGM.getModule().addTypeName("struct._class_ro_t",
3885 ClassRonfABITy);
3886
3887 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3888 std::vector<const llvm::Type*> Params;
3889 Params.push_back(ObjectPtrTy);
3890 Params.push_back(SelectorPtrTy);
3891 ImpnfABITy = llvm::PointerType::getUnqual(
3892 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3893
3894 // struct _class_t {
3895 // struct _class_t *isa;
3896 // struct _class_t * const superclass;
3897 // void *cache;
3898 // IMP *vtable;
3899 // struct class_ro_t *ro;
3900 // }
3901
3902 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3903 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3904 llvm::PointerType::getUnqual(ClassTyHolder),
3905 CachePtrTy,
3906 llvm::PointerType::getUnqual(ImpnfABITy),
3907 llvm::PointerType::getUnqual(
3908 ClassRonfABITy),
3909 NULL);
3910 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3911
3912 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3913 ClassnfABITy);
3914
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003915 // LLVM for struct _class_t *
3916 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3917
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003918 // struct _category_t {
3919 // const char * const name;
3920 // struct _class_t *const cls;
3921 // const struct _method_list_t * const instance_methods;
3922 // const struct _method_list_t * const class_methods;
3923 // const struct _protocol_list_t * const protocols;
3924 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003925 // }
3926 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003927 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003928 MethodListnfABIPtrTy,
3929 MethodListnfABIPtrTy,
3930 ProtocolListnfABIPtrTy,
3931 PropertyListPtrTy,
3932 NULL);
3933 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003934
3935 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003936 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3937 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003938
3939 // MessageRefTy - LLVM for:
3940 // struct _message_ref_t {
3941 // IMP messenger;
3942 // SEL name;
3943 // };
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003944
3945 // First the clang type for struct _message_ref_t
3946 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3947 SourceLocation(),
3948 &Ctx.Idents.get("_message_ref_t"));
Douglas Gregor6ab35242009-04-09 21:40:53 +00003949 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3950 Ctx.VoidPtrTy, 0, false));
3951 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3952 Ctx.getObjCSelType(), 0, false));
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003953 RD->completeDefinition(Ctx);
3954
3955 MessageRefCTy = Ctx.getTagDeclType(RD);
3956 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
3957 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003958
3959 // MessageRefPtrTy - LLVM for struct _message_ref_t*
3960 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
3961
3962 // SuperMessageRefTy - LLVM for:
3963 // struct _super_message_ref_t {
3964 // SUPER_IMP messenger;
3965 // SEL name;
3966 // };
3967 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy,
3968 SelectorPtrTy,
3969 NULL);
3970 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
3971
3972 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
3973 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
3974
Daniel Dunbare588b992009-03-01 04:46:24 +00003975
3976 // struct objc_typeinfo {
3977 // const void** vtable; // objc_ehtype_vtable + 2
3978 // const char* name; // c++ typeinfo string
3979 // Class cls;
3980 // };
3981 EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy),
3982 Int8PtrTy,
3983 ClassnfABIPtrTy,
3984 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00003985 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Daniel Dunbare588b992009-03-01 04:46:24 +00003986 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003987}
3988
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003989llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3990 FinishNonFragileABIModule();
3991
3992 return NULL;
3993}
3994
3995void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3996 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003997
3998 // Build list of all implemented classe addresses in array
3999 // L_OBJC_LABEL_CLASS_$.
4000 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
4001 // list of 'nonlazy' implementations (defined as those with a +load{}
4002 // method!!).
4003 unsigned NumClasses = DefinedClasses.size();
4004 if (NumClasses) {
4005 std::vector<llvm::Constant*> Symbols(NumClasses);
4006 for (unsigned i=0; i<NumClasses; i++)
4007 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
4008 ObjCTypes.Int8PtrTy);
4009 llvm::Constant* Init =
4010 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
4011 NumClasses),
4012 Symbols);
4013
4014 llvm::GlobalVariable *GV =
4015 new llvm::GlobalVariable(Init->getType(), false,
4016 llvm::GlobalValue::InternalLinkage,
4017 Init,
4018 "\01L_OBJC_LABEL_CLASS_$",
4019 &CGM.getModule());
Daniel Dunbar58a29122009-03-09 22:18:41 +00004020 GV->setAlignment(8);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004021 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
4022 UsedGlobals.push_back(GV);
4023 }
4024
4025 // Build list of all implemented category addresses in array
4026 // L_OBJC_LABEL_CATEGORY_$.
4027 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
4028 // list of 'nonlazy' category implementations (defined as those with a +load{}
4029 // method!!).
4030 unsigned NumCategory = DefinedCategories.size();
4031 if (NumCategory) {
4032 std::vector<llvm::Constant*> Symbols(NumCategory);
4033 for (unsigned i=0; i<NumCategory; i++)
4034 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
4035 ObjCTypes.Int8PtrTy);
4036 llvm::Constant* Init =
4037 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
4038 NumCategory),
4039 Symbols);
4040
4041 llvm::GlobalVariable *GV =
4042 new llvm::GlobalVariable(Init->getType(), false,
4043 llvm::GlobalValue::InternalLinkage,
4044 Init,
4045 "\01L_OBJC_LABEL_CATEGORY_$",
4046 &CGM.getModule());
Daniel Dunbar58a29122009-03-09 22:18:41 +00004047 GV->setAlignment(8);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004048 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
4049 UsedGlobals.push_back(GV);
4050 }
4051
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004052 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
4053 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
4054 std::vector<llvm::Constant*> Values(2);
4055 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
Fariborz Jahanian067986e2009-02-24 21:08:09 +00004056 unsigned int flags = 0;
Fariborz Jahanian66a5c2c2009-02-24 23:34:44 +00004057 // FIXME: Fix and continue?
4058 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
4059 flags |= eImageInfo_GarbageCollected;
4060 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
4061 flags |= eImageInfo_GCOnly;
Fariborz Jahanian067986e2009-02-24 21:08:09 +00004062 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004063 llvm::Constant* Init = llvm::ConstantArray::get(
4064 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
4065 Values);
4066 llvm::GlobalVariable *IMGV =
4067 new llvm::GlobalVariable(Init->getType(), false,
4068 llvm::GlobalValue::InternalLinkage,
4069 Init,
4070 "\01L_OBJC_IMAGE_INFO",
4071 &CGM.getModule());
4072 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
Daniel Dunbar325f7582009-04-23 08:03:21 +00004073 IMGV->setConstant(true);
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004074 UsedGlobals.push_back(IMGV);
4075
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004076 std::vector<llvm::Constant*> Used;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004077
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004078 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
4079 e = UsedGlobals.end(); i != e; ++i) {
4080 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
4081 }
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004082
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004083 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
4084 llvm::GlobalValue *GV =
4085 new llvm::GlobalVariable(AT, false,
4086 llvm::GlobalValue::AppendingLinkage,
4087 llvm::ConstantArray::get(AT, Used),
4088 "llvm.used",
4089 &CGM.getModule());
4090
4091 GV->setSection("llvm.metadata");
4092
4093}
4094
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004095// Metadata flags
4096enum MetaDataDlags {
4097 CLS = 0x0,
4098 CLS_META = 0x1,
4099 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004100 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004101 CLS_EXCEPTION = 0x20
4102};
4103/// BuildClassRoTInitializer - generate meta-data for:
4104/// struct _class_ro_t {
4105/// uint32_t const flags;
4106/// uint32_t const instanceStart;
4107/// uint32_t const instanceSize;
4108/// uint32_t const reserved; // only when building for 64bit targets
4109/// const uint8_t * const ivarLayout;
4110/// const char *const name;
4111/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004112/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004113/// const struct _ivar_list_t *const ivars;
4114/// const uint8_t * const weakIvarLayout;
4115/// const struct _prop_list_t * const properties;
4116/// }
4117///
4118llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
4119 unsigned flags,
4120 unsigned InstanceStart,
4121 unsigned InstanceSize,
4122 const ObjCImplementationDecl *ID) {
4123 std::string ClassName = ID->getNameAsString();
4124 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
4125 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4126 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4127 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
4128 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004129 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4130 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004131 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004132 // const struct _method_list_t * const baseMethods;
4133 std::vector<llvm::Constant*> Methods;
4134 std::string MethodListName("\01l_OBJC_$_");
4135 if (flags & CLS_META) {
4136 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004137 for (ObjCImplementationDecl::classmeth_iterator
4138 i = ID->classmeth_begin(CGM.getContext()),
4139 e = ID->classmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004140 // Class methods should always be defined.
4141 Methods.push_back(GetMethodConstant(*i));
4142 }
4143 } else {
4144 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004145 for (ObjCImplementationDecl::instmeth_iterator
4146 i = ID->instmeth_begin(CGM.getContext()),
4147 e = ID->instmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004148 // Instance methods should always be defined.
4149 Methods.push_back(GetMethodConstant(*i));
4150 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00004151 for (ObjCImplementationDecl::propimpl_iterator
4152 i = ID->propimpl_begin(CGM.getContext()),
4153 e = ID->propimpl_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004154 ObjCPropertyImplDecl *PID = *i;
4155
4156 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4157 ObjCPropertyDecl *PD = PID->getPropertyDecl();
4158
4159 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4160 if (llvm::Constant *C = GetMethodConstant(MD))
4161 Methods.push_back(C);
4162 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4163 if (llvm::Constant *C = GetMethodConstant(MD))
4164 Methods.push_back(C);
4165 }
4166 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004167 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004168 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004169 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004170
4171 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4172 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
4173 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
4174 + OID->getNameAsString(),
4175 OID->protocol_begin(),
4176 OID->protocol_end());
4177
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004178 if (flags & CLS_META)
4179 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4180 else
4181 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004182 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4183 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004184 if (flags & CLS_META)
4185 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
4186 else
4187 Values[ 9] =
4188 EmitPropertyList(
4189 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
4190 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004191 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
4192 Values);
4193 llvm::GlobalVariable *CLASS_RO_GV =
4194 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
4195 llvm::GlobalValue::InternalLinkage,
4196 Init,
4197 (flags & CLS_META) ?
4198 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4199 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
4200 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004201 CLASS_RO_GV->setAlignment(
4202 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004203 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004204 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004205
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004206}
4207
4208/// BuildClassMetaData - This routine defines that to-level meta-data
4209/// for the given ClassName for:
4210/// struct _class_t {
4211/// struct _class_t *isa;
4212/// struct _class_t * const superclass;
4213/// void *cache;
4214/// IMP *vtable;
4215/// struct class_ro_t *ro;
4216/// }
4217///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004218llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
4219 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004220 llvm::Constant *IsAGV,
4221 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004222 llvm::Constant *ClassRoGV,
4223 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004224 std::vector<llvm::Constant*> Values(5);
4225 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004226 Values[1] = SuperClassGV
4227 ? SuperClassGV
4228 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004229 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4230 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4231 Values[4] = ClassRoGV; // &CLASS_RO_GV
4232 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
4233 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004234 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4235 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004236 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004237 GV->setAlignment(
4238 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004239 if (HiddenVisibility)
4240 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004241 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004242}
4243
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004244void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004245 uint32_t &InstanceStart,
4246 uint32_t &InstanceSize) {
Daniel Dunbar97776872009-04-22 07:32:20 +00004247 // Find first and last (non-padding) ivars in this interface.
4248
4249 // FIXME: Use iterator.
4250 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004251 GetNamedIvarList(OID->getClassInterface(), OIvars);
Daniel Dunbar97776872009-04-22 07:32:20 +00004252
4253 if (OIvars.empty()) {
4254 InstanceStart = InstanceSize = 0;
4255 return;
Daniel Dunbard4ae6c02009-04-22 04:39:47 +00004256 }
Daniel Dunbar97776872009-04-22 07:32:20 +00004257
4258 const ObjCIvarDecl *First = OIvars.front();
4259 const ObjCIvarDecl *Last = OIvars.back();
4260
4261 InstanceStart = ComputeIvarBaseOffset(CGM, OID, First);
4262 const llvm::Type *FieldTy =
4263 CGM.getTypes().ConvertTypeForMem(Last->getType());
4264 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004265// FIXME. This breaks compatibility with llvm-gcc-4.2 (but makes it compatible
4266// with gcc-4.2). We postpone this for now.
4267#if 0
4268 if (Last->isBitField()) {
4269 Expr *BitWidth = Last->getBitWidth();
4270 uint64_t BitFieldSize =
Eli Friedman9a901bb2009-04-26 19:19:15 +00004271 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004272 Size = (BitFieldSize / 8) + ((BitFieldSize % 8) != 0);
4273 }
4274#endif
Daniel Dunbar97776872009-04-22 07:32:20 +00004275 InstanceSize = ComputeIvarBaseOffset(CGM, OID, Last) + Size;
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004276}
4277
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004278void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4279 std::string ClassName = ID->getNameAsString();
4280 if (!ObjCEmptyCacheVar) {
4281 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004282 ObjCTypes.CacheTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004283 false,
4284 llvm::GlobalValue::ExternalLinkage,
4285 0,
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004286 "_objc_empty_cache",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004287 &CGM.getModule());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004288
4289 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004290 ObjCTypes.ImpnfABITy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004291 false,
4292 llvm::GlobalValue::ExternalLinkage,
4293 0,
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004294 "_objc_empty_vtable",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004295 &CGM.getModule());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004296 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004297 assert(ID->getClassInterface() &&
4298 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004299 // FIXME: Is this correct (that meta class size is never computed)?
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004300 uint32_t InstanceStart =
4301 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
4302 uint32_t InstanceSize = InstanceStart;
4303 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004304 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4305 std::string ObjCClassName(getClassSymbolPrefix());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004306
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004307 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004308
Daniel Dunbar04d40782009-04-14 06:00:08 +00004309 bool classIsHidden =
4310 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004311 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004312 flags |= OBJC2_CLS_HIDDEN;
4313 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004314 // class is root
4315 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004316 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004317 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004318 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004319 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004320 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4321 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4322 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004323 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004324 // work on super class metadata symbol.
4325 std::string SuperClassName =
4326 ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004327 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004328 }
4329 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4330 InstanceStart,
4331 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004332 std::string TClassName = ObjCMetaClassName + ClassName;
4333 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004334 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4335 classIsHidden);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004336
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004337 // Metadata for the class
4338 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004339 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004340 flags |= OBJC2_CLS_HIDDEN;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004341
4342 if (hasObjCExceptionAttribute(ID->getClassInterface()))
4343 flags |= CLS_EXCEPTION;
4344
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004345 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004346 flags |= CLS_ROOT;
4347 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00004348 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004349 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004350 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004351 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004352 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004353 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004354 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004355 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004356 InstanceStart,
4357 InstanceSize,
4358 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004359
4360 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004361 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004362 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4363 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004364 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004365
4366 // Force the definition of the EHType if necessary.
4367 if (flags & CLS_EXCEPTION)
4368 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004369}
4370
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004371/// GenerateProtocolRef - This routine is called to generate code for
4372/// a protocol reference expression; as in:
4373/// @code
4374/// @protocol(Proto1);
4375/// @endcode
4376/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4377/// which will hold address of the protocol meta-data.
4378///
4379llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
4380 const ObjCProtocolDecl *PD) {
4381
Fariborz Jahanian960cd062009-04-10 18:47:34 +00004382 // This routine is called for @protocol only. So, we must build definition
4383 // of protocol's meta-data (not a reference to it!)
4384 //
4385 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004386 ObjCTypes.ExternalProtocolPtrTy);
4387
4388 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
4389 ProtocolName += PD->getNameAsCString();
4390
4391 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
4392 if (PTGV)
4393 return Builder.CreateLoad(PTGV, false, "tmp");
4394 PTGV = new llvm::GlobalVariable(
4395 Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00004396 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004397 Init,
4398 ProtocolName,
4399 &CGM.getModule());
4400 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
4401 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4402 UsedGlobals.push_back(PTGV);
4403 return Builder.CreateLoad(PTGV, false, "tmp");
4404}
4405
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004406/// GenerateCategory - Build metadata for a category implementation.
4407/// struct _category_t {
4408/// const char * const name;
4409/// struct _class_t *const cls;
4410/// const struct _method_list_t * const instance_methods;
4411/// const struct _method_list_t * const class_methods;
4412/// const struct _protocol_list_t * const protocols;
4413/// const struct _prop_list_t * const properties;
4414/// }
4415///
4416void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
4417{
4418 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004419 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
4420 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004421 "_$_" + OCD->getNameAsString());
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004422 std::string ExtClassName(getClassSymbolPrefix() +
4423 Interface->getNameAsString());
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004424
4425 std::vector<llvm::Constant*> Values(6);
4426 Values[0] = GetClassName(OCD->getIdentifier());
4427 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004428 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004429 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004430 std::vector<llvm::Constant*> Methods;
4431 std::string MethodListName(Prefix);
4432 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
4433 "_$_" + OCD->getNameAsString();
4434
Douglas Gregor653f1b12009-04-23 01:02:12 +00004435 for (ObjCCategoryImplDecl::instmeth_iterator
4436 i = OCD->instmeth_begin(CGM.getContext()),
4437 e = OCD->instmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004438 // Instance methods should always be defined.
4439 Methods.push_back(GetMethodConstant(*i));
4440 }
4441
4442 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004443 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004444 Methods);
4445
4446 MethodListName = Prefix;
4447 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
4448 OCD->getNameAsString();
4449 Methods.clear();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004450 for (ObjCCategoryImplDecl::classmeth_iterator
4451 i = OCD->classmeth_begin(CGM.getContext()),
4452 e = OCD->classmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004453 // Class methods should always be defined.
4454 Methods.push_back(GetMethodConstant(*i));
4455 }
4456
4457 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004458 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004459 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004460 const ObjCCategoryDecl *Category =
4461 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004462 if (Category) {
4463 std::string ExtName(Interface->getNameAsString() + "_$_" +
4464 OCD->getNameAsString());
4465 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
4466 + Interface->getNameAsString() + "_$_"
4467 + Category->getNameAsString(),
4468 Category->protocol_begin(),
4469 Category->protocol_end());
4470 Values[5] =
4471 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
4472 OCD, Category, ObjCTypes);
4473 }
4474 else {
4475 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4476 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
4477 }
4478
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004479 llvm::Constant *Init =
4480 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
4481 Values);
4482 llvm::GlobalVariable *GCATV
4483 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
4484 false,
4485 llvm::GlobalValue::InternalLinkage,
4486 Init,
4487 ExtCatName,
4488 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004489 GCATV->setAlignment(
4490 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004491 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004492 UsedGlobals.push_back(GCATV);
4493 DefinedCategories.push_back(GCATV);
4494}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004495
4496/// GetMethodConstant - Return a struct objc_method constant for the
4497/// given method if it has been defined. The result is null if the
4498/// method has not been defined. The return value has type MethodPtrTy.
4499llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
4500 const ObjCMethodDecl *MD) {
4501 // FIXME: Use DenseMap::lookup
4502 llvm::Function *Fn = MethodDefinitions[MD];
4503 if (!Fn)
4504 return 0;
4505
4506 std::vector<llvm::Constant*> Method(3);
4507 Method[0] =
4508 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4509 ObjCTypes.SelectorPtrTy);
4510 Method[1] = GetMethodVarType(MD);
4511 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
4512 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
4513}
4514
4515/// EmitMethodList - Build meta-data for method declarations
4516/// struct _method_list_t {
4517/// uint32_t entsize; // sizeof(struct _objc_method)
4518/// uint32_t method_count;
4519/// struct _objc_method method_list[method_count];
4520/// }
4521///
4522llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
4523 const std::string &Name,
4524 const char *Section,
4525 const ConstantVector &Methods) {
4526 // Return null for empty list.
4527 if (Methods.empty())
4528 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
4529
4530 std::vector<llvm::Constant*> Values(3);
4531 // sizeof(struct _objc_method)
4532 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
4533 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4534 // method_count
4535 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
4536 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
4537 Methods.size());
4538 Values[2] = llvm::ConstantArray::get(AT, Methods);
4539 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4540
4541 llvm::GlobalVariable *GV =
4542 new llvm::GlobalVariable(Init->getType(), false,
4543 llvm::GlobalValue::InternalLinkage,
4544 Init,
4545 Name,
4546 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004547 GV->setAlignment(
4548 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004549 GV->setSection(Section);
4550 UsedGlobals.push_back(GV);
4551 return llvm::ConstantExpr::getBitCast(GV,
4552 ObjCTypes.MethodListnfABIPtrTy);
4553}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004554
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004555/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
4556/// the given ivar.
4557///
4558llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004559 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004560 const ObjCIvarDecl *Ivar) {
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00004561 std::string Name = "OBJC_IVAR_$_" +
Douglas Gregor6ab35242009-04-09 21:40:53 +00004562 getInterfaceDeclForIvar(ID, Ivar, CGM.getContext())->getNameAsString() +
4563 '.' + Ivar->getNameAsString();
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004564 llvm::GlobalVariable *IvarOffsetGV =
4565 CGM.getModule().getGlobalVariable(Name);
4566 if (!IvarOffsetGV)
4567 IvarOffsetGV =
4568 new llvm::GlobalVariable(ObjCTypes.LongTy,
4569 false,
4570 llvm::GlobalValue::ExternalLinkage,
4571 0,
4572 Name,
4573 &CGM.getModule());
4574 return IvarOffsetGV;
4575}
4576
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004577llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004578 const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004579 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004580 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00004581 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
4582 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
4583 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004584 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004585 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00004586
4587 // FIXME: This matches gcc, but shouldn't the visibility be set on
4588 // the use as well (i.e., in ObjCIvarOffsetVariable).
4589 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
4590 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
4591 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004592 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00004593 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00004594 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004595 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004596 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004597}
4598
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004599/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00004600/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004601/// IvarListnfABIPtrTy.
4602/// struct _ivar_t {
4603/// unsigned long int *offset; // pointer to ivar offset location
4604/// char *name;
4605/// char *type;
4606/// uint32_t alignment;
4607/// uint32_t size;
4608/// }
4609/// struct _ivar_list_t {
4610/// uint32 entsize; // sizeof(struct _ivar_t)
4611/// uint32 count;
4612/// struct _iver_t list[count];
4613/// }
4614///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004615
4616void CGObjCCommonMac::GetNamedIvarList(const ObjCInterfaceDecl *OID,
4617 llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const {
4618 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
4619 E = OID->ivar_end(); I != E; ++I) {
4620 // Ignore unnamed bit-fields.
4621 if (!(*I)->getDeclName())
4622 continue;
4623
4624 Res.push_back(*I);
4625 }
4626
4627 for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(CGM.getContext()),
4628 E = OID->prop_end(CGM.getContext()); I != E; ++I)
4629 if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl())
4630 Res.push_back(IV);
4631}
4632
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004633llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
4634 const ObjCImplementationDecl *ID) {
4635
4636 std::vector<llvm::Constant*> Ivars, Ivar(5);
4637
4638 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4639 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
4640
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004641 // FIXME. Consolidate this with similar code in GenerateClass.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00004642
Daniel Dunbar91636d62009-04-20 00:33:43 +00004643 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00004644 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004645 GetNamedIvarList(OID, OIvars);
Fariborz Jahanian99eee362009-04-01 19:37:34 +00004646
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004647 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
4648 ObjCIvarDecl *IVD = OIvars[i];
Daniel Dunbar3eec8aa2009-04-20 05:53:40 +00004649 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004650 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004651 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
4652 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004653 const llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004654 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004655 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
4656 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004657 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004658 Align = llvm::Log2_32(Align);
4659 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00004660 // NOTE. Size of a bitfield does not match gcc's, because of the
4661 // way bitfields are treated special in each. But I am told that
4662 // 'size' for bitfield ivars is ignored by the runtime so it does
4663 // not matter. If it matters, there is enough info to get the
4664 // bitfield right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004665 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4666 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
4667 }
4668 // Return null for empty list.
4669 if (Ivars.empty())
4670 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4671 std::vector<llvm::Constant*> Values(3);
4672 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
4673 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4674 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
4675 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
4676 Ivars.size());
4677 Values[2] = llvm::ConstantArray::get(AT, Ivars);
4678 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4679 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4680 llvm::GlobalVariable *GV =
4681 new llvm::GlobalVariable(Init->getType(), false,
4682 llvm::GlobalValue::InternalLinkage,
4683 Init,
4684 Prefix + OID->getNameAsString(),
4685 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004686 GV->setAlignment(
4687 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004688 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004689
4690 UsedGlobals.push_back(GV);
4691 return llvm::ConstantExpr::getBitCast(GV,
4692 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004693}
4694
4695llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4696 const ObjCProtocolDecl *PD) {
4697 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4698
4699 if (!Entry) {
4700 // We use the initializer as a marker of whether this is a forward
4701 // reference or not. At module finalization we add the empty
4702 // contents for protocols which were referenced but never defined.
4703 Entry =
4704 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
4705 llvm::GlobalValue::ExternalLinkage,
4706 0,
4707 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
4708 &CGM.getModule());
4709 Entry->setSection("__DATA,__datacoal_nt,coalesced");
4710 UsedGlobals.push_back(Entry);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004711 }
4712
4713 return Entry;
4714}
4715
4716/// GetOrEmitProtocol - Generate the protocol meta-data:
4717/// @code
4718/// struct _protocol_t {
4719/// id isa; // NULL
4720/// const char * const protocol_name;
4721/// const struct _protocol_list_t * protocol_list; // super protocols
4722/// const struct method_list_t * const instance_methods;
4723/// const struct method_list_t * const class_methods;
4724/// const struct method_list_t *optionalInstanceMethods;
4725/// const struct method_list_t *optionalClassMethods;
4726/// const struct _prop_list_t * properties;
4727/// const uint32_t size; // sizeof(struct _protocol_t)
4728/// const uint32_t flags; // = 0
4729/// }
4730/// @endcode
4731///
4732
4733llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4734 const ObjCProtocolDecl *PD) {
4735 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4736
4737 // Early exit if a defining object has already been generated.
4738 if (Entry && Entry->hasInitializer())
4739 return Entry;
4740
4741 const char *ProtocolName = PD->getNameAsCString();
4742
4743 // Construct method lists.
4744 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4745 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00004746 for (ObjCProtocolDecl::instmeth_iterator
4747 i = PD->instmeth_begin(CGM.getContext()),
4748 e = PD->instmeth_end(CGM.getContext());
4749 i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004750 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004751 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004752 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4753 OptInstanceMethods.push_back(C);
4754 } else {
4755 InstanceMethods.push_back(C);
4756 }
4757 }
4758
Douglas Gregor6ab35242009-04-09 21:40:53 +00004759 for (ObjCProtocolDecl::classmeth_iterator
4760 i = PD->classmeth_begin(CGM.getContext()),
4761 e = PD->classmeth_end(CGM.getContext());
4762 i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004763 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004764 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004765 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4766 OptClassMethods.push_back(C);
4767 } else {
4768 ClassMethods.push_back(C);
4769 }
4770 }
4771
4772 std::vector<llvm::Constant*> Values(10);
4773 // isa is NULL
4774 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
4775 Values[1] = GetClassName(PD->getIdentifier());
4776 Values[2] = EmitProtocolList(
4777 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4778 PD->protocol_begin(),
4779 PD->protocol_end());
4780
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004781 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004782 + PD->getNameAsString(),
4783 "__DATA, __objc_const",
4784 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004785 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004786 + PD->getNameAsString(),
4787 "__DATA, __objc_const",
4788 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004789 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004790 + PD->getNameAsString(),
4791 "__DATA, __objc_const",
4792 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004793 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004794 + PD->getNameAsString(),
4795 "__DATA, __objc_const",
4796 OptClassMethods);
4797 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4798 0, PD, ObjCTypes);
4799 uint32_t Size =
4800 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
4801 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4802 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
4803 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
4804 Values);
4805
4806 if (Entry) {
4807 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00004808 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004809 Entry->setInitializer(Init);
4810 } else {
4811 Entry =
4812 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004813 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004814 Init,
4815 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
4816 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004817 Entry->setAlignment(
4818 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004819 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004820 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004821 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
4822
4823 // Use this protocol meta-data to build protocol list table in section
4824 // __DATA, __objc_protolist
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004825 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004826 ObjCTypes.ProtocolnfABIPtrTy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004827 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004828 Entry,
4829 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
4830 +ProtocolName,
4831 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004832 PTGV->setAlignment(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004833 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00004834 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004835 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4836 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004837 return Entry;
4838}
4839
4840/// EmitProtocolList - Generate protocol list meta-data:
4841/// @code
4842/// struct _protocol_list_t {
4843/// long protocol_count; // Note, this is 32/64 bit
4844/// struct _protocol_t[protocol_count];
4845/// }
4846/// @endcode
4847///
4848llvm::Constant *
4849CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4850 ObjCProtocolDecl::protocol_iterator begin,
4851 ObjCProtocolDecl::protocol_iterator end) {
4852 std::vector<llvm::Constant*> ProtocolRefs;
4853
Fariborz Jahanianda320092009-01-29 19:24:30 +00004854 // Just return null for empty protocol lists
Daniel Dunbar948e2582009-02-15 07:36:20 +00004855 if (begin == end)
Fariborz Jahanianda320092009-01-29 19:24:30 +00004856 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4857
Daniel Dunbar948e2582009-02-15 07:36:20 +00004858 // FIXME: We shouldn't need to do this lookup here, should we?
Fariborz Jahanianda320092009-01-29 19:24:30 +00004859 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4860 if (GV)
Daniel Dunbar948e2582009-02-15 07:36:20 +00004861 return llvm::ConstantExpr::getBitCast(GV,
4862 ObjCTypes.ProtocolListnfABIPtrTy);
4863
4864 for (; begin != end; ++begin)
4865 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4866
Fariborz Jahanianda320092009-01-29 19:24:30 +00004867 // This list is null terminated.
4868 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004869 ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004870
4871 std::vector<llvm::Constant*> Values(2);
4872 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4873 Values[1] =
Daniel Dunbar948e2582009-02-15 07:36:20 +00004874 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004875 ProtocolRefs.size()),
4876 ProtocolRefs);
4877
4878 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4879 GV = new llvm::GlobalVariable(Init->getType(), false,
4880 llvm::GlobalValue::InternalLinkage,
4881 Init,
4882 Name,
4883 &CGM.getModule());
4884 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004885 GV->setAlignment(
4886 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004887 UsedGlobals.push_back(GV);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004888 return llvm::ConstantExpr::getBitCast(GV,
4889 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004890}
4891
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004892/// GetMethodDescriptionConstant - This routine build following meta-data:
4893/// struct _objc_method {
4894/// SEL _cmd;
4895/// char *method_type;
4896/// char *_imp;
4897/// }
4898
4899llvm::Constant *
4900CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4901 std::vector<llvm::Constant*> Desc(3);
4902 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4903 ObjCTypes.SelectorPtrTy);
4904 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004905 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004906 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4907 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4908}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004909
4910/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4911/// This code gen. amounts to generating code for:
4912/// @code
4913/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4914/// @encode
4915///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004916LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004917 CodeGen::CodeGenFunction &CGF,
4918 QualType ObjectTy,
4919 llvm::Value *BaseValue,
4920 const ObjCIvarDecl *Ivar,
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004921 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00004922 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00004923 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
4924 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004925}
4926
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004927llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
4928 CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00004929 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004930 const ObjCIvarDecl *Ivar) {
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00004931 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),
4932 false, "ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004933}
4934
Fariborz Jahanian46551122009-02-04 00:22:57 +00004935CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4936 CodeGen::CodeGenFunction &CGF,
4937 QualType ResultType,
4938 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004939 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00004940 QualType Arg0Ty,
4941 bool IsSuper,
4942 const CallArgList &CallArgs) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004943 // FIXME. Even though IsSuper is passes. This function doese not
4944 // handle calls to 'super' receivers.
4945 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004946 llvm::Value *Arg0 = Receiver;
4947 if (!IsSuper)
4948 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004949
4950 // Find the message function name.
Fariborz Jahanianef163782009-02-05 01:13:09 +00004951 // FIXME. This is too much work to get the ABI-specific result type
4952 // needed to find the message name.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004953 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4954 llvm::SmallVector<QualType, 16>());
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00004955 llvm::Constant *Fn = 0;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004956 std::string Name("\01l_");
4957 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004958#if 0
4959 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004960 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00004961 Fn = ObjCTypes.getMessageSendIdStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004962 // FIXME. Is there a better way of getting these names.
4963 // They are available in RuntimeFunctions vector pair.
4964 Name += "objc_msgSendId_stret_fixup";
4965 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004966 else
4967#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004968 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00004969 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004970 Name += "objc_msgSendSuper2_stret_fixup";
4971 }
4972 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004973 {
Chris Lattner1c02f862009-04-22 02:53:24 +00004974 Fn = ObjCTypes.getMessageSendStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004975 Name += "objc_msgSend_stret_fixup";
4976 }
4977 }
Fariborz Jahanian5b2bad02009-04-30 16:31:11 +00004978 else if (!IsSuper && ResultType->isFloatingType()) {
4979 if (const BuiltinType *BT = ResultType->getAsBuiltinType()) {
4980 BuiltinType::Kind k = BT->getKind();
4981 if (k == BuiltinType::LongDouble) {
4982 Fn = ObjCTypes.getMessageSendFpretFixupFn();
4983 Name += "objc_msgSend_fpret_fixup";
4984 }
4985 else {
4986 Fn = ObjCTypes.getMessageSendFixupFn();
4987 Name += "objc_msgSend_fixup";
4988 }
4989 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004990 }
4991 else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004992#if 0
4993// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004994 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00004995 Fn = ObjCTypes.getMessageSendIdFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004996 Name += "objc_msgSendId_fixup";
4997 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004998 else
4999#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005000 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005001 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005002 Name += "objc_msgSendSuper2_fixup";
5003 }
5004 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005005 {
Chris Lattner1c02f862009-04-22 02:53:24 +00005006 Fn = ObjCTypes.getMessageSendFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005007 Name += "objc_msgSend_fixup";
5008 }
5009 }
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005010 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005011 Name += '_';
5012 std::string SelName(Sel.getAsString());
5013 // Replace all ':' in selector name with '_' ouch!
5014 for(unsigned i = 0; i < SelName.size(); i++)
5015 if (SelName[i] == ':')
5016 SelName[i] = '_';
5017 Name += SelName;
5018 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5019 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005020 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005021 std::vector<llvm::Constant*> Values(2);
5022 Values[0] = Fn;
5023 Values[1] = GetMethodVarName(Sel);
5024 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
5025 GV = new llvm::GlobalVariable(Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005026 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005027 Init,
5028 Name,
5029 &CGM.getModule());
5030 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005031 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005032 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005033 }
5034 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005035
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005036 CallArgList ActualArgs;
5037 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
5038 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
5039 ObjCTypes.MessageRefCPtrTy));
5040 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005041 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
5042 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5043 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00005044 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005045 Callee = CGF.Builder.CreateBitCast(Callee,
5046 llvm::PointerType::getUnqual(FTy));
5047 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005048}
5049
5050/// Generate code for a message send expression in the nonfragile abi.
5051CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
5052 CodeGen::CodeGenFunction &CGF,
5053 QualType ResultType,
5054 Selector Sel,
5055 llvm::Value *Receiver,
5056 bool IsClassMessage,
5057 const CallArgList &CallArgs) {
Fariborz Jahanian46551122009-02-04 00:22:57 +00005058 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005059 Receiver, CGF.getContext().getObjCIdType(),
Fariborz Jahanian46551122009-02-04 00:22:57 +00005060 false, CallArgs);
5061}
5062
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005063llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005064CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005065 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5066
Daniel Dunbardfff2302009-03-02 05:18:14 +00005067 if (!GV) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005068 GV = new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
5069 llvm::GlobalValue::ExternalLinkage,
5070 0, Name, &CGM.getModule());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005071 }
5072
5073 return GV;
5074}
5075
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005076llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005077 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005078 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
5079
5080 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005081 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005082 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005083 Entry =
5084 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5085 llvm::GlobalValue::InternalLinkage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005086 ClassGV,
Daniel Dunbar11394522009-04-18 08:51:00 +00005087 "\01L_OBJC_CLASSLIST_REFERENCES_$_",
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005088 &CGM.getModule());
5089 Entry->setAlignment(
5090 CGM.getTargetData().getPrefTypeAlignment(
5091 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005092 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
5093 UsedGlobals.push_back(Entry);
5094 }
5095
5096 return Builder.CreateLoad(Entry, false, "tmp");
5097}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005098
Daniel Dunbar11394522009-04-18 08:51:00 +00005099llvm::Value *
5100CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
5101 const ObjCInterfaceDecl *ID) {
5102 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
5103
5104 if (!Entry) {
5105 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5106 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5107 Entry =
5108 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5109 llvm::GlobalValue::InternalLinkage,
5110 ClassGV,
5111 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
5112 &CGM.getModule());
5113 Entry->setAlignment(
5114 CGM.getTargetData().getPrefTypeAlignment(
5115 ObjCTypes.ClassnfABIPtrTy));
5116 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005117 UsedGlobals.push_back(Entry);
5118 }
5119
5120 return Builder.CreateLoad(Entry, false, "tmp");
5121}
5122
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005123/// EmitMetaClassRef - Return a Value * of the address of _class_t
5124/// meta-data
5125///
5126llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5127 const ObjCInterfaceDecl *ID) {
5128 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5129 if (Entry)
5130 return Builder.CreateLoad(Entry, false, "tmp");
5131
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005132 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005133 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005134 Entry =
5135 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5136 llvm::GlobalValue::InternalLinkage,
5137 MetaClassGV,
5138 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
5139 &CGM.getModule());
5140 Entry->setAlignment(
5141 CGM.getTargetData().getPrefTypeAlignment(
5142 ObjCTypes.ClassnfABIPtrTy));
5143
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005144 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005145 UsedGlobals.push_back(Entry);
5146
5147 return Builder.CreateLoad(Entry, false, "tmp");
5148}
5149
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005150/// GetClass - Return a reference to the class for the given interface
5151/// decl.
5152llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5153 const ObjCInterfaceDecl *ID) {
5154 return EmitClassRef(Builder, ID);
5155}
5156
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005157/// Generates a message send where the super is the receiver. This is
5158/// a message send to self with special delivery semantics indicating
5159/// which class's method should be called.
5160CodeGen::RValue
5161CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
5162 QualType ResultType,
5163 Selector Sel,
5164 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005165 bool isCategoryImpl,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005166 llvm::Value *Receiver,
5167 bool IsClassMessage,
5168 const CodeGen::CallArgList &CallArgs) {
5169 // ...
5170 // Create and init a super structure; this is a (receiver, class)
5171 // pair we will pass to objc_msgSendSuper.
5172 llvm::Value *ObjCSuper =
5173 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
5174
5175 llvm::Value *ReceiverAsObject =
5176 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5177 CGF.Builder.CreateStore(ReceiverAsObject,
5178 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
5179
5180 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005181 llvm::Value *Target;
5182 if (IsClassMessage) {
5183 if (isCategoryImpl) {
5184 // Message sent to "super' in a class method defined in
5185 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005186 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005187 Target = CGF.Builder.CreateStructGEP(Target, 0);
5188 Target = CGF.Builder.CreateLoad(Target);
5189 }
5190 else
5191 Target = EmitMetaClassRef(CGF.Builder, Class);
5192 }
5193 else
Daniel Dunbar11394522009-04-18 08:51:00 +00005194 Target = EmitSuperClassRef(CGF.Builder, Class);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005195
5196 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
5197 // and ObjCTypes types.
5198 const llvm::Type *ClassTy =
5199 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5200 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5201 CGF.Builder.CreateStore(Target,
5202 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
5203
5204 return EmitMessageSend(CGF, ResultType, Sel,
5205 ObjCSuper, ObjCTypes.SuperPtrCTy,
5206 true, CallArgs);
5207}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005208
5209llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
5210 Selector Sel) {
5211 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
5212
5213 if (!Entry) {
5214 llvm::Constant *Casted =
5215 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5216 ObjCTypes.SelectorPtrTy);
5217 Entry =
5218 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
5219 llvm::GlobalValue::InternalLinkage,
5220 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
5221 &CGM.getModule());
5222 Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip");
5223 UsedGlobals.push_back(Entry);
5224 }
5225
5226 return Builder.CreateLoad(Entry, false, "tmp");
5227}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005228/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
5229/// objc_assign_ivar (id src, id *dst)
5230///
5231void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
5232 llvm::Value *src, llvm::Value *dst)
5233{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005234 const llvm::Type * SrcTy = src->getType();
5235 if (!isa<llvm::PointerType>(SrcTy)) {
5236 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5237 assert(Size <= 8 && "does not support size > 8");
5238 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5239 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005240 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5241 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005242 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5243 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005244 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005245 src, dst, "assignivar");
5246 return;
5247}
5248
5249/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5250/// objc_assign_strongCast (id src, id *dst)
5251///
5252void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
5253 CodeGen::CodeGenFunction &CGF,
5254 llvm::Value *src, llvm::Value *dst)
5255{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005256 const llvm::Type * SrcTy = src->getType();
5257 if (!isa<llvm::PointerType>(SrcTy)) {
5258 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5259 assert(Size <= 8 && "does not support size > 8");
5260 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5261 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005262 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5263 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005264 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5265 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005266 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005267 src, dst, "weakassign");
5268 return;
5269}
5270
5271/// EmitObjCWeakRead - Code gen for loading value of a __weak
5272/// object: objc_read_weak (id *src)
5273///
5274llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
5275 CodeGen::CodeGenFunction &CGF,
5276 llvm::Value *AddrWeakObj)
5277{
Eli Friedman8339b352009-03-07 03:57:15 +00005278 const llvm::Type* DestTy =
5279 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005280 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005281 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005282 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005283 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005284 return read_weak;
5285}
5286
5287/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5288/// objc_assign_weak (id src, id *dst)
5289///
5290void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
5291 llvm::Value *src, llvm::Value *dst)
5292{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005293 const llvm::Type * SrcTy = src->getType();
5294 if (!isa<llvm::PointerType>(SrcTy)) {
5295 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5296 assert(Size <= 8 && "does not support size > 8");
5297 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5298 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005299 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5300 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005301 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5302 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005303 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005304 src, dst, "weakassign");
5305 return;
5306}
5307
5308/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5309/// objc_assign_global (id src, id *dst)
5310///
5311void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
5312 llvm::Value *src, llvm::Value *dst)
5313{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005314 const llvm::Type * SrcTy = src->getType();
5315 if (!isa<llvm::PointerType>(SrcTy)) {
5316 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5317 assert(Size <= 8 && "does not support size > 8");
5318 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5319 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005320 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5321 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005322 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5323 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005324 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005325 src, dst, "globalassign");
5326 return;
5327}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005328
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005329void
5330CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5331 const Stmt &S) {
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005332 bool isTry = isa<ObjCAtTryStmt>(S);
5333 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
5334 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005335 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005336 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005337 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005338 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
5339
5340 // For @synchronized, call objc_sync_enter(sync.expr). The
5341 // evaluation of the expression must occur before we enter the
5342 // @synchronized. We can safely avoid a temp here because jumps into
5343 // @synchronized are illegal & this will dominate uses.
5344 llvm::Value *SyncArg = 0;
5345 if (!isTry) {
5346 SyncArg =
5347 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
5348 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005349 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005350 }
5351
5352 // Push an EH context entry, used for handling rethrows and jumps
5353 // through finally.
5354 CGF.PushCleanupBlock(FinallyBlock);
5355
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005356 CGF.setInvokeDest(TryHandler);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005357
5358 CGF.EmitBlock(TryBlock);
5359 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
5360 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
5361 CGF.EmitBranchThroughCleanup(FinallyEnd);
5362
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005363 // Emit the exception handler.
5364
5365 CGF.EmitBlock(TryHandler);
5366
5367 llvm::Value *llvm_eh_exception =
5368 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
5369 llvm::Value *llvm_eh_selector_i64 =
5370 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64);
5371 llvm::Value *llvm_eh_typeid_for_i64 =
5372 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64);
5373 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5374 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow");
5375
5376 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
5377 SelectorArgs.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005378 SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005379
5380 // Construct the lists of (type, catch body) to handle.
Daniel Dunbarede8de92009-03-06 00:01:21 +00005381 llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005382 bool HasCatchAll = false;
5383 if (isTry) {
5384 if (const ObjCAtCatchStmt* CatchStmt =
5385 cast<ObjCAtTryStmt>(S).getCatchStmts()) {
5386 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005387 const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Steve Naroff7ba138a2009-03-03 19:52:17 +00005388 Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody()));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005389
5390 // catch(...) always matches.
Steve Naroff7ba138a2009-03-03 19:52:17 +00005391 if (!CatchDecl) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005392 // Use i8* null here to signal this is a catch all, not a cleanup.
5393 llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
5394 SelectorArgs.push_back(Null);
5395 HasCatchAll = true;
5396 break;
5397 }
5398
Daniel Dunbarede8de92009-03-06 00:01:21 +00005399 if (CGF.getContext().isObjCIdType(CatchDecl->getType()) ||
5400 CatchDecl->getType()->isObjCQualifiedIdType()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005401 llvm::Value *IDEHType =
5402 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5403 if (!IDEHType)
5404 IDEHType =
5405 new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5406 llvm::GlobalValue::ExternalLinkage,
5407 0, "OBJC_EHTYPE_id", &CGM.getModule());
5408 SelectorArgs.push_back(IDEHType);
5409 HasCatchAll = true;
5410 break;
5411 }
5412
5413 // All other types should be Objective-C interface pointer types.
Daniel Dunbarede8de92009-03-06 00:01:21 +00005414 const PointerType *PT = CatchDecl->getType()->getAsPointerType();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005415 assert(PT && "Invalid @catch type.");
5416 const ObjCInterfaceType *IT =
5417 PT->getPointeeType()->getAsObjCInterfaceType();
5418 assert(IT && "Invalid @catch type.");
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005419 llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005420 SelectorArgs.push_back(EHType);
5421 }
5422 }
5423 }
5424
5425 // We use a cleanup unless there was already a catch all.
5426 if (!HasCatchAll) {
5427 SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
Daniel Dunbarede8de92009-03-06 00:01:21 +00005428 Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005429 }
5430
5431 llvm::Value *Selector =
5432 CGF.Builder.CreateCall(llvm_eh_selector_i64,
5433 SelectorArgs.begin(), SelectorArgs.end(),
5434 "selector");
5435 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005436 const ParmVarDecl *CatchParam = Handlers[i].first;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005437 const Stmt *CatchBody = Handlers[i].second;
5438
5439 llvm::BasicBlock *Next = 0;
5440
5441 // The last handler always matches.
5442 if (i + 1 != e) {
5443 assert(CatchParam && "Only last handler can be a catch all.");
5444
5445 llvm::BasicBlock *Match = CGF.createBasicBlock("match");
5446 Next = CGF.createBasicBlock("catch.next");
5447 llvm::Value *Id =
5448 CGF.Builder.CreateCall(llvm_eh_typeid_for_i64,
5449 CGF.Builder.CreateBitCast(SelectorArgs[i+2],
5450 ObjCTypes.Int8PtrTy));
5451 CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id),
5452 Match, Next);
5453
5454 CGF.EmitBlock(Match);
5455 }
5456
5457 if (CatchBody) {
5458 llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end");
5459 llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler");
5460
5461 // Cleanups must call objc_end_catch.
5462 //
5463 // FIXME: It seems incorrect for objc_begin_catch to be inside
5464 // this context, but this matches gcc.
5465 CGF.PushCleanupBlock(MatchEnd);
5466 CGF.setInvokeDest(MatchHandler);
5467
5468 llvm::Value *ExcObject =
Chris Lattner8a569112009-04-22 02:15:23 +00005469 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005470
5471 // Bind the catch parameter if it exists.
5472 if (CatchParam) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005473 ExcObject =
5474 CGF.Builder.CreateBitCast(ExcObject,
5475 CGF.ConvertType(CatchParam->getType()));
5476 // CatchParam is a ParmVarDecl because of the grammar
5477 // construction used to handle this, but for codegen purposes
5478 // we treat this as a local decl.
5479 CGF.EmitLocalBlockVarDecl(*CatchParam);
5480 CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005481 }
5482
5483 CGF.ObjCEHValueStack.push_back(ExcObject);
5484 CGF.EmitStmt(CatchBody);
5485 CGF.ObjCEHValueStack.pop_back();
5486
5487 CGF.EmitBranchThroughCleanup(FinallyEnd);
5488
5489 CGF.EmitBlock(MatchHandler);
5490
5491 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5492 // We are required to emit this call to satisfy LLVM, even
5493 // though we don't use the result.
5494 llvm::SmallVector<llvm::Value*, 8> Args;
5495 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005496 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005497 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
5498 0));
5499 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5500 CGF.Builder.CreateStore(Exc, RethrowPtr);
5501 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5502
5503 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5504
5505 CGF.EmitBlock(MatchEnd);
5506
5507 // Unfortunately, we also have to generate another EH frame here
5508 // in case this throws.
5509 llvm::BasicBlock *MatchEndHandler =
5510 CGF.createBasicBlock("match.end.handler");
5511 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattner8a569112009-04-22 02:15:23 +00005512 CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005513 Cont, MatchEndHandler,
5514 Args.begin(), Args.begin());
5515
5516 CGF.EmitBlock(Cont);
5517 if (Info.SwitchBlock)
5518 CGF.EmitBlock(Info.SwitchBlock);
5519 if (Info.EndBlock)
5520 CGF.EmitBlock(Info.EndBlock);
5521
5522 CGF.EmitBlock(MatchEndHandler);
5523 Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5524 // We are required to emit this call to satisfy LLVM, even
5525 // though we don't use the result.
5526 Args.clear();
5527 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005528 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005529 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
5530 0));
5531 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5532 CGF.Builder.CreateStore(Exc, RethrowPtr);
5533 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5534
5535 if (Next)
5536 CGF.EmitBlock(Next);
5537 } else {
5538 assert(!Next && "catchup should be last handler.");
5539
5540 CGF.Builder.CreateStore(Exc, RethrowPtr);
5541 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5542 }
5543 }
5544
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005545 // Pop the cleanup entry, the @finally is outside this cleanup
5546 // scope.
5547 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5548 CGF.setInvokeDest(PrevLandingPad);
5549
5550 CGF.EmitBlock(FinallyBlock);
5551
5552 if (isTry) {
5553 if (const ObjCAtFinallyStmt* FinallyStmt =
5554 cast<ObjCAtTryStmt>(S).getFinallyStmt())
5555 CGF.EmitStmt(FinallyStmt->getFinallyBody());
5556 } else {
5557 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
5558 // @synchronized.
Chris Lattnerbbccd612009-04-22 02:38:11 +00005559 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005560 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005561
5562 if (Info.SwitchBlock)
5563 CGF.EmitBlock(Info.SwitchBlock);
5564 if (Info.EndBlock)
5565 CGF.EmitBlock(Info.EndBlock);
5566
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005567 // Branch around the rethrow code.
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005568 CGF.EmitBranch(FinallyEnd);
5569
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005570 CGF.EmitBlock(FinallyRethrow);
Chris Lattner8a569112009-04-22 02:15:23 +00005571 CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005572 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005573 CGF.Builder.CreateUnreachable();
5574
5575 CGF.EmitBlock(FinallyEnd);
5576}
5577
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005578/// EmitThrowStmt - Generate code for a throw statement.
5579void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
5580 const ObjCAtThrowStmt &S) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005581 llvm::Value *Exception;
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005582 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005583 Exception = CGF.EmitScalarExpr(ThrowExpr);
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005584 } else {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005585 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
5586 "Unexpected rethrow outside @catch block.");
5587 Exception = CGF.ObjCEHValueStack.back();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005588 }
5589
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005590 llvm::Value *ExceptionAsObject =
5591 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
5592 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
5593 if (InvokeDest) {
5594 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattnerbbccd612009-04-22 02:38:11 +00005595 CGF.Builder.CreateInvoke(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005596 Cont, InvokeDest,
5597 &ExceptionAsObject, &ExceptionAsObject + 1);
5598 CGF.EmitBlock(Cont);
5599 } else
Chris Lattnerbbccd612009-04-22 02:38:11 +00005600 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005601 CGF.Builder.CreateUnreachable();
5602
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005603 // Clear the insertion point to indicate we are in unreachable code.
5604 CGF.Builder.ClearInsertionPoint();
5605}
Daniel Dunbare588b992009-03-01 04:46:24 +00005606
5607llvm::Value *
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005608CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
5609 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00005610 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00005611
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005612 // If we don't need a definition, return the entry if found or check
5613 // if we use an external reference.
5614 if (!ForDefinition) {
5615 if (Entry)
5616 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00005617
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005618 // If this type (or a super class) has the __objc_exception__
5619 // attribute, emit an external reference.
5620 if (hasObjCExceptionAttribute(ID))
5621 return Entry =
5622 new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5623 llvm::GlobalValue::ExternalLinkage,
5624 0,
5625 (std::string("OBJC_EHTYPE_$_") +
5626 ID->getIdentifier()->getName()),
5627 &CGM.getModule());
5628 }
5629
5630 // Otherwise we need to either make a new entry or fill in the
5631 // initializer.
5632 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005633 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00005634 std::string VTableName = "objc_ehtype_vtable";
5635 llvm::GlobalVariable *VTableGV =
5636 CGM.getModule().getGlobalVariable(VTableName);
5637 if (!VTableGV)
5638 VTableGV = new llvm::GlobalVariable(ObjCTypes.Int8PtrTy, false,
5639 llvm::GlobalValue::ExternalLinkage,
5640 0, VTableName, &CGM.getModule());
5641
5642 llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2);
5643
5644 std::vector<llvm::Constant*> Values(3);
5645 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
5646 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005647 Values[2] = GetClassGlobal(ClassName);
Daniel Dunbare588b992009-03-01 04:46:24 +00005648 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
5649
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005650 if (Entry) {
5651 Entry->setInitializer(Init);
5652 } else {
5653 Entry = new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5654 llvm::GlobalValue::WeakAnyLinkage,
5655 Init,
5656 (std::string("OBJC_EHTYPE_$_") +
5657 ID->getIdentifier()->getName()),
5658 &CGM.getModule());
5659 }
5660
Daniel Dunbar04d40782009-04-14 06:00:08 +00005661 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005662 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005663 Entry->setAlignment(8);
5664
5665 if (ForDefinition) {
5666 Entry->setSection("__DATA,__objc_const");
5667 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
5668 } else {
5669 Entry->setSection("__DATA,__datacoal_nt,coalesced");
5670 }
Daniel Dunbare588b992009-03-01 04:46:24 +00005671
5672 return Entry;
5673}
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005674
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005675/* *** */
5676
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005677CodeGen::CGObjCRuntime *
5678CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00005679 return new CGObjCMac(CGM);
5680}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005681
5682CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005683CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005684 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005685}