blob: 8517c876644fbd4fa3c9fac0056eea2650796159 [file] [log] [blame]
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001//===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides Objective-C code generation targetting the Apple runtime.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000015
16#include "CodeGenModule.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000017#include "CodeGenFunction.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000019#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000020#include "clang/AST/DeclObjC.h"
Daniel Dunbar2bebbf02009-05-03 10:46:44 +000021#include "clang/AST/RecordLayout.h"
Chris Lattner16f00492009-04-26 01:32:48 +000022#include "clang/AST/StmtObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000023#include "clang/Basic/LangOptions.h"
24
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +000025#include "llvm/Intrinsics.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000026#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000027#include "llvm/ADT/DenseSet.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000028#include "llvm/Target/TargetData.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000029#include <sstream>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000030
31using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000032using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000033
Daniel Dunbar97776872009-04-22 07:32:20 +000034// Common CGObjCRuntime functions, these don't belong here, but they
35// don't belong in CGObjCRuntime either so we will live with it for
36// now.
37
Daniel Dunbar2bebbf02009-05-03 10:46:44 +000038static const llvm::StructType *
39GetConcreteClassStruct(CodeGen::CodeGenModule &CGM,
40 const ObjCInterfaceDecl *OID) {
Daniel Dunbar84ad77a2009-04-22 09:39:34 +000041 assert(!OID->isForwardDecl() && "Invalid interface decl!");
Daniel Dunbar412f59b2009-04-22 10:28:39 +000042 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
43 return cast<llvm::StructType>(CGM.getTypes().ConvertTagDeclType(RD));
Daniel Dunbar84ad77a2009-04-22 09:39:34 +000044}
45
Daniel Dunbar532d4da2009-05-03 13:15:50 +000046/// FindIvarInterface - Find the interface containing the ivar.
Daniel Dunbara2435782009-04-22 12:00:04 +000047///
Daniel Dunbar532d4da2009-05-03 13:15:50 +000048/// FIXME: We shouldn't need to do this, the containing context should
49/// be fixed.
50static const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context,
51 const ObjCInterfaceDecl *OID,
52 const ObjCIvarDecl *OIVD,
53 unsigned &Index) {
54 const ObjCInterfaceDecl *Super = OID->getSuperClass();
Daniel Dunbara80a0f62009-04-22 17:43:55 +000055
Daniel Dunbar532d4da2009-05-03 13:15:50 +000056 // FIXME: The index here is closely tied to how
57 // ASTContext::getObjCLayout is implemented. This should be fixed to
58 // get the information from the layout directly.
59 Index = 0;
60 for (ObjCInterfaceDecl::ivar_iterator IVI = OID->ivar_begin(),
61 IVE = OID->ivar_end(); IVI != IVE; ++IVI, ++Index)
62 if (OIVD == *IVI)
63 return OID;
64
65 // Also look in synthesized ivars.
66 for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(Context),
67 E = OID->prop_end(Context); I != E; ++I) {
68 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl()) {
69 if (OIVD == Ivar)
70 return OID;
71 ++Index;
72 }
Daniel Dunbara80a0f62009-04-22 17:43:55 +000073 }
74
Daniel Dunbar532d4da2009-05-03 13:15:50 +000075 // Otherwise check in the super class.
76 if (Super)
77 return FindIvarInterface(Context, Super, OIVD, Index);
78
79 return 0;
Daniel Dunbara2435782009-04-22 12:00:04 +000080}
81
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000082static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM,
83 const ObjCInterfaceDecl *OID,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000084 const ObjCImplementationDecl *ID,
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000085 const ObjCIvarDecl *Ivar) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +000086 unsigned Index;
87 const ObjCInterfaceDecl *Container =
88 FindIvarInterface(CGM.getContext(), OID, Ivar, Index);
89 assert(Container && "Unable to find ivar container");
90
91 // If we know have an implementation (and the ivar is in it) then
92 // look up in the implementation layout.
93 const ASTRecordLayout *RL;
94 if (ID && ID->getClassInterface() == Container)
95 RL = &CGM.getContext().getASTObjCImplementationLayout(ID);
96 else
97 RL = &CGM.getContext().getASTObjCInterfaceLayout(Container);
98 return RL->getFieldOffset(Index);
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000099}
100
101uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
102 const ObjCInterfaceDecl *OID,
103 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +0000104 return LookupFieldBitOffset(CGM, OID, 0, Ivar) / 8;
105}
106
107uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
108 const ObjCImplementationDecl *OID,
109 const ObjCIvarDecl *Ivar) {
110 return LookupFieldBitOffset(CGM, OID->getClassInterface(), OID, Ivar) / 8;
Daniel Dunbar97776872009-04-22 07:32:20 +0000111}
112
113LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
114 const ObjCInterfaceDecl *OID,
115 llvm::Value *BaseValue,
116 const ObjCIvarDecl *Ivar,
117 unsigned CVRQualifiers,
118 llvm::Value *Offset) {
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000119 // Compute (type*) ( (char *) BaseValue + Offset)
Daniel Dunbar97776872009-04-22 07:32:20 +0000120 llvm::Type *I8Ptr = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000121 QualType IvarTy = Ivar->getType();
122 const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy);
Daniel Dunbar97776872009-04-22 07:32:20 +0000123 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr);
Daniel Dunbar97776872009-04-22 07:32:20 +0000124 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000125 V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar97776872009-04-22 07:32:20 +0000126
127 if (Ivar->isBitField()) {
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +0000128 // We need to compute the bit offset for the bit-field, the offset
129 // is to the byte. Note, there is a subtle invariant here: we can
130 // only call this routine on non-sythesized ivars but we may be
131 // called for synthesized ivars. However, a synthesized ivar can
132 // never be a bit-field so this is safe.
133 uint64_t BitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar) % 8;
134
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000135 uint64_t BitFieldSize =
136 Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue();
137 return LValue::MakeBitfield(V, BitOffset, BitFieldSize,
Daniel Dunbare38df862009-05-03 07:52:00 +0000138 IvarTy->isSignedIntegerType(),
139 IvarTy.getCVRQualifiers()|CVRQualifiers);
Daniel Dunbar97776872009-04-22 07:32:20 +0000140 }
141
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000142 LValue LV = LValue::MakeAddr(V, IvarTy.getCVRQualifiers()|CVRQualifiers,
143 CGF.CGM.getContext().getObjCGCAttrKind(IvarTy));
Daniel Dunbar97776872009-04-22 07:32:20 +0000144 LValue::SetObjCIvar(LV, true);
145 return LV;
146}
147
148///
149
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000150namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000151
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000152 typedef std::vector<llvm::Constant*> ConstantVector;
153
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000154 // FIXME: We should find a nicer way to make the labels for
155 // metadata, string concatenation is lame.
156
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000157class ObjCCommonTypesHelper {
158protected:
159 CodeGen::CodeGenModule &CGM;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000160
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000161public:
Fariborz Jahanian0a855d02009-03-23 19:10:40 +0000162 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000163 const llvm::Type *Int8PtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000164
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000165 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
166 const llvm::Type *ObjectPtrTy;
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000167
168 /// PtrObjectPtrTy - LLVM type for id *
169 const llvm::Type *PtrObjectPtrTy;
170
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000171 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000172 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000173 /// ProtocolPtrTy - LLVM type for external protocol handles
174 /// (typeof(Protocol))
175 const llvm::Type *ExternalProtocolPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000176
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000177 // SuperCTy - clang type for struct objc_super.
178 QualType SuperCTy;
179 // SuperPtrCTy - clang type for struct objc_super *.
180 QualType SuperPtrCTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000181
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000182 /// SuperTy - LLVM type for struct objc_super.
183 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000184 /// SuperPtrTy - LLVM type for struct objc_super *.
185 const llvm::Type *SuperPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000186
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000187 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
188 /// in GCC parlance).
189 const llvm::StructType *PropertyTy;
190
191 /// PropertyListTy - LLVM type for struct objc_property_list
192 /// (_prop_list_t in GCC parlance).
193 const llvm::StructType *PropertyListTy;
194 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
195 const llvm::Type *PropertyListPtrTy;
196
197 // MethodTy - LLVM type for struct objc_method.
198 const llvm::StructType *MethodTy;
199
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000200 /// CacheTy - LLVM type for struct objc_cache.
201 const llvm::Type *CacheTy;
202 /// CachePtrTy - LLVM type for struct objc_cache *.
203 const llvm::Type *CachePtrTy;
204
Chris Lattner72db6c32009-04-22 02:44:54 +0000205 llvm::Constant *getGetPropertyFn() {
206 CodeGen::CodeGenTypes &Types = CGM.getTypes();
207 ASTContext &Ctx = CGM.getContext();
208 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
209 llvm::SmallVector<QualType,16> Params;
210 QualType IdType = Ctx.getObjCIdType();
211 QualType SelType = Ctx.getObjCSelType();
212 Params.push_back(IdType);
213 Params.push_back(SelType);
214 Params.push_back(Ctx.LongTy);
215 Params.push_back(Ctx.BoolTy);
216 const llvm::FunctionType *FTy =
217 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
218 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
219 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000220
Chris Lattner72db6c32009-04-22 02:44:54 +0000221 llvm::Constant *getSetPropertyFn() {
222 CodeGen::CodeGenTypes &Types = CGM.getTypes();
223 ASTContext &Ctx = CGM.getContext();
224 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
225 llvm::SmallVector<QualType,16> Params;
226 QualType IdType = Ctx.getObjCIdType();
227 QualType SelType = Ctx.getObjCSelType();
228 Params.push_back(IdType);
229 Params.push_back(SelType);
230 Params.push_back(Ctx.LongTy);
231 Params.push_back(IdType);
232 Params.push_back(Ctx.BoolTy);
233 Params.push_back(Ctx.BoolTy);
234 const llvm::FunctionType *FTy =
235 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
236 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
237 }
238
239 llvm::Constant *getEnumerationMutationFn() {
240 // void objc_enumerationMutation (id)
241 std::vector<const llvm::Type*> Args;
242 Args.push_back(ObjectPtrTy);
243 llvm::FunctionType *FTy =
244 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
245 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
246 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000247
248 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000249 llvm::Constant *getGcReadWeakFn() {
250 // id objc_read_weak (id *)
251 std::vector<const llvm::Type*> Args;
252 Args.push_back(ObjectPtrTy->getPointerTo());
253 llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false);
254 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
255 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000256
257 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000258 llvm::Constant *getGcAssignWeakFn() {
259 // id objc_assign_weak (id, id *)
260 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
261 Args.push_back(ObjectPtrTy->getPointerTo());
262 llvm::FunctionType *FTy =
263 llvm::FunctionType::get(ObjectPtrTy, Args, false);
264 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
265 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000266
267 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000268 llvm::Constant *getGcAssignGlobalFn() {
269 // id objc_assign_global(id, id *)
270 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
271 Args.push_back(ObjectPtrTy->getPointerTo());
272 llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false);
273 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
274 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000275
276 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000277 llvm::Constant *getGcAssignIvarFn() {
278 // id objc_assign_ivar(id, id *)
279 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
280 Args.push_back(ObjectPtrTy->getPointerTo());
281 llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false);
282 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
283 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000284
285 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000286 llvm::Constant *getGcAssignStrongCastFn() {
287 // id objc_assign_global(id, id *)
288 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
289 Args.push_back(ObjectPtrTy->getPointerTo());
290 llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false);
291 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
292 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000293
294 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000295 llvm::Constant *getExceptionThrowFn() {
296 // void objc_exception_throw(id)
297 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
298 llvm::FunctionType *FTy =
299 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
300 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
301 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000302
Daniel Dunbar1c566672009-02-24 01:43:46 +0000303 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000304 llvm::Constant *getSyncEnterFn() {
305 // void objc_sync_enter (id)
306 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
307 llvm::FunctionType *FTy =
308 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
309 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
310 }
Daniel Dunbar1c566672009-02-24 01:43:46 +0000311
312 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000313 llvm::Constant *getSyncExitFn() {
314 // void objc_sync_exit (id)
315 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
316 llvm::FunctionType *FTy =
317 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
318 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
319 }
Daniel Dunbar1c566672009-02-24 01:43:46 +0000320
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000321 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
322 ~ObjCCommonTypesHelper(){}
323};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000324
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000325/// ObjCTypesHelper - Helper class that encapsulates lazy
326/// construction of varies types used during ObjC generation.
327class ObjCTypesHelper : public ObjCCommonTypesHelper {
328private:
329
Chris Lattner4176b0c2009-04-22 02:32:31 +0000330 llvm::Constant *getMessageSendFn() {
331 // id objc_msgSend (id, SEL, ...)
332 std::vector<const llvm::Type*> Params;
333 Params.push_back(ObjectPtrTy);
334 Params.push_back(SelectorPtrTy);
335 return
336 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
337 Params, true),
338 "objc_msgSend");
339 }
340
341 llvm::Constant *getMessageSendStretFn() {
342 // id objc_msgSend_stret (id, SEL, ...)
343 std::vector<const llvm::Type*> Params;
344 Params.push_back(ObjectPtrTy);
345 Params.push_back(SelectorPtrTy);
346 return
347 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
348 Params, true),
349 "objc_msgSend_stret");
350
351 }
352
353 llvm::Constant *getMessageSendFpretFn() {
354 // FIXME: This should be long double on x86_64?
355 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
356 std::vector<const llvm::Type*> Params;
357 Params.push_back(ObjectPtrTy);
358 Params.push_back(SelectorPtrTy);
359 return
360 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
361 Params,
362 true),
363 "objc_msgSend_fpret");
364
365 }
366
367 llvm::Constant *getMessageSendSuperFn() {
368 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
369 std::vector<const llvm::Type*> Params;
370 Params.push_back(SuperPtrTy);
371 Params.push_back(SelectorPtrTy);
372 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
373 Params, true),
374 "objc_msgSendSuper");
375 }
376 llvm::Constant *getMessageSendSuperStretFn() {
377 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
378 // SEL op, ...)
379 std::vector<const llvm::Type*> Params;
380 Params.push_back(Int8PtrTy);
381 Params.push_back(SuperPtrTy);
382 Params.push_back(SelectorPtrTy);
383 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
384 Params, true),
385 "objc_msgSendSuper_stret");
386 }
387
388 llvm::Constant *getMessageSendSuperFpretFn() {
389 // There is no objc_msgSendSuper_fpret? How can that work?
390 return getMessageSendSuperFn();
391 }
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000392
393public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000394 /// SymtabTy - LLVM type for struct objc_symtab.
395 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000396 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
397 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000398 /// ModuleTy - LLVM type for struct objc_module.
399 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000400
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000401 /// ProtocolTy - LLVM type for struct objc_protocol.
402 const llvm::StructType *ProtocolTy;
403 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
404 const llvm::Type *ProtocolPtrTy;
405 /// ProtocolExtensionTy - LLVM type for struct
406 /// objc_protocol_extension.
407 const llvm::StructType *ProtocolExtensionTy;
408 /// ProtocolExtensionTy - LLVM type for struct
409 /// objc_protocol_extension *.
410 const llvm::Type *ProtocolExtensionPtrTy;
411 /// MethodDescriptionTy - LLVM type for struct
412 /// objc_method_description.
413 const llvm::StructType *MethodDescriptionTy;
414 /// MethodDescriptionListTy - LLVM type for struct
415 /// objc_method_description_list.
416 const llvm::StructType *MethodDescriptionListTy;
417 /// MethodDescriptionListPtrTy - LLVM type for struct
418 /// objc_method_description_list *.
419 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000420 /// ProtocolListTy - LLVM type for struct objc_property_list.
421 const llvm::Type *ProtocolListTy;
422 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
423 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000424 /// CategoryTy - LLVM type for struct objc_category.
425 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000426 /// ClassTy - LLVM type for struct objc_class.
427 const llvm::StructType *ClassTy;
428 /// ClassPtrTy - LLVM type for struct objc_class *.
429 const llvm::Type *ClassPtrTy;
430 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
431 const llvm::StructType *ClassExtensionTy;
432 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
433 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000434 // IvarTy - LLVM type for struct objc_ivar.
435 const llvm::StructType *IvarTy;
436 /// IvarListTy - LLVM type for struct objc_ivar_list.
437 const llvm::Type *IvarListTy;
438 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
439 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000440 /// MethodListTy - LLVM type for struct objc_method_list.
441 const llvm::Type *MethodListTy;
442 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
443 const llvm::Type *MethodListPtrTy;
Anders Carlsson124526b2008-09-09 10:10:21 +0000444
445 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
446 const llvm::Type *ExceptionDataTy;
447
Anders Carlsson124526b2008-09-09 10:10:21 +0000448 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000449 llvm::Constant *getExceptionTryEnterFn() {
450 std::vector<const llvm::Type*> Params;
451 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
452 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
453 Params, false),
454 "objc_exception_try_enter");
455 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000456
457 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000458 llvm::Constant *getExceptionTryExitFn() {
459 std::vector<const llvm::Type*> Params;
460 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
461 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
462 Params, false),
463 "objc_exception_try_exit");
464 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000465
466 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000467 llvm::Constant *getExceptionExtractFn() {
468 std::vector<const llvm::Type*> Params;
469 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
470 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
471 Params, false),
472 "objc_exception_extract");
473
474 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000475
476 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000477 llvm::Constant *getExceptionMatchFn() {
478 std::vector<const llvm::Type*> Params;
479 Params.push_back(ClassPtrTy);
480 Params.push_back(ObjectPtrTy);
481 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
482 Params, false),
483 "objc_exception_match");
484
485 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000486
487 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000488 llvm::Constant *getSetJmpFn() {
489 std::vector<const llvm::Type*> Params;
490 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
491 return
492 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
493 Params, false),
494 "_setjmp");
495
496 }
Chris Lattner10cac6f2008-11-15 21:26:17 +0000497
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000498public:
499 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000500 ~ObjCTypesHelper() {}
Daniel Dunbar5669e572008-10-17 03:24:53 +0000501
502
Chris Lattner74391b42009-03-22 21:03:39 +0000503 llvm::Constant *getSendFn(bool IsSuper) {
Chris Lattner4176b0c2009-04-22 02:32:31 +0000504 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
Daniel Dunbar5669e572008-10-17 03:24:53 +0000505 }
506
Chris Lattner74391b42009-03-22 21:03:39 +0000507 llvm::Constant *getSendStretFn(bool IsSuper) {
Chris Lattner4176b0c2009-04-22 02:32:31 +0000508 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
Daniel Dunbar5669e572008-10-17 03:24:53 +0000509 }
510
Chris Lattner74391b42009-03-22 21:03:39 +0000511 llvm::Constant *getSendFpretFn(bool IsSuper) {
Chris Lattner4176b0c2009-04-22 02:32:31 +0000512 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
Daniel Dunbar5669e572008-10-17 03:24:53 +0000513 }
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000514};
515
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000516/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000517/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000518class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000519public:
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000520
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000521 // MethodListnfABITy - LLVM for struct _method_list_t
522 const llvm::StructType *MethodListnfABITy;
523
524 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
525 const llvm::Type *MethodListnfABIPtrTy;
526
527 // ProtocolnfABITy = LLVM for struct _protocol_t
528 const llvm::StructType *ProtocolnfABITy;
529
Daniel Dunbar948e2582009-02-15 07:36:20 +0000530 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
531 const llvm::Type *ProtocolnfABIPtrTy;
532
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000533 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
534 const llvm::StructType *ProtocolListnfABITy;
535
536 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
537 const llvm::Type *ProtocolListnfABIPtrTy;
538
539 // ClassnfABITy - LLVM for struct _class_t
540 const llvm::StructType *ClassnfABITy;
541
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000542 // ClassnfABIPtrTy - LLVM for struct _class_t*
543 const llvm::Type *ClassnfABIPtrTy;
544
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000545 // IvarnfABITy - LLVM for struct _ivar_t
546 const llvm::StructType *IvarnfABITy;
547
548 // IvarListnfABITy - LLVM for struct _ivar_list_t
549 const llvm::StructType *IvarListnfABITy;
550
551 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
552 const llvm::Type *IvarListnfABIPtrTy;
553
554 // ClassRonfABITy - LLVM for struct _class_ro_t
555 const llvm::StructType *ClassRonfABITy;
556
557 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
558 const llvm::Type *ImpnfABITy;
559
560 // CategorynfABITy - LLVM for struct _category_t
561 const llvm::StructType *CategorynfABITy;
562
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000563 // New types for nonfragile abi messaging.
564
565 // MessageRefTy - LLVM for:
566 // struct _message_ref_t {
567 // IMP messenger;
568 // SEL name;
569 // };
570 const llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000571 // MessageRefCTy - clang type for struct _message_ref_t
572 QualType MessageRefCTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000573
574 // MessageRefPtrTy - LLVM for struct _message_ref_t*
575 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000576 // MessageRefCPtrTy - clang type for struct _message_ref_t*
577 QualType MessageRefCPtrTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000578
Fariborz Jahanianef163782009-02-05 01:13:09 +0000579 // MessengerTy - Type of the messenger (shown as IMP above)
580 const llvm::FunctionType *MessengerTy;
581
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000582 // SuperMessageRefTy - LLVM for:
583 // struct _super_message_ref_t {
584 // SUPER_IMP messenger;
585 // SEL name;
586 // };
587 const llvm::StructType *SuperMessageRefTy;
588
589 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
590 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000591
Chris Lattner1c02f862009-04-22 02:53:24 +0000592 llvm::Constant *getMessageSendFixupFn() {
593 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
594 std::vector<const llvm::Type*> Params;
595 Params.push_back(ObjectPtrTy);
596 Params.push_back(MessageRefPtrTy);
597 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
598 Params, true),
599 "objc_msgSend_fixup");
600 }
601
602 llvm::Constant *getMessageSendFpretFixupFn() {
603 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
604 std::vector<const llvm::Type*> Params;
605 Params.push_back(ObjectPtrTy);
606 Params.push_back(MessageRefPtrTy);
607 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
608 Params, true),
609 "objc_msgSend_fpret_fixup");
610 }
611
612 llvm::Constant *getMessageSendStretFixupFn() {
613 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
614 std::vector<const llvm::Type*> Params;
615 Params.push_back(ObjectPtrTy);
616 Params.push_back(MessageRefPtrTy);
617 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
618 Params, true),
619 "objc_msgSend_stret_fixup");
620 }
621
622 llvm::Constant *getMessageSendIdFixupFn() {
623 // id objc_msgSendId_fixup(id, struct message_ref_t*, ...)
624 std::vector<const llvm::Type*> Params;
625 Params.push_back(ObjectPtrTy);
626 Params.push_back(MessageRefPtrTy);
627 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
628 Params, true),
629 "objc_msgSendId_fixup");
630 }
631
632 llvm::Constant *getMessageSendIdStretFixupFn() {
633 // id objc_msgSendId_stret_fixup(id, struct message_ref_t*, ...)
634 std::vector<const llvm::Type*> Params;
635 Params.push_back(ObjectPtrTy);
636 Params.push_back(MessageRefPtrTy);
637 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
638 Params, true),
639 "objc_msgSendId_stret_fixup");
640 }
641 llvm::Constant *getMessageSendSuper2FixupFn() {
642 // id objc_msgSendSuper2_fixup (struct objc_super *,
643 // struct _super_message_ref_t*, ...)
644 std::vector<const llvm::Type*> Params;
645 Params.push_back(SuperPtrTy);
646 Params.push_back(SuperMessageRefPtrTy);
647 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
648 Params, true),
649 "objc_msgSendSuper2_fixup");
650 }
651
652 llvm::Constant *getMessageSendSuper2StretFixupFn() {
653 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
654 // struct _super_message_ref_t*, ...)
655 std::vector<const llvm::Type*> Params;
656 Params.push_back(SuperPtrTy);
657 Params.push_back(SuperMessageRefPtrTy);
658 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
659 Params, true),
660 "objc_msgSendSuper2_stret_fixup");
661 }
662
663
664
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000665 /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C
666 /// exception personality function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000667 llvm::Value *getEHPersonalityPtr() {
668 llvm::Constant *Personality =
669 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
670 std::vector<const llvm::Type*>(),
671 true),
672 "__objc_personality_v0");
673 return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy);
674 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000675
Chris Lattner8a569112009-04-22 02:15:23 +0000676 llvm::Constant *getUnwindResumeOrRethrowFn() {
677 std::vector<const llvm::Type*> Params;
678 Params.push_back(Int8PtrTy);
679 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
680 Params, false),
681 "_Unwind_Resume_or_Rethrow");
682 }
683
684 llvm::Constant *getObjCEndCatchFn() {
685 std::vector<const llvm::Type*> Params;
686 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
687 Params, false),
688 "objc_end_catch");
689
690 }
691
692 llvm::Constant *getObjCBeginCatchFn() {
693 std::vector<const llvm::Type*> Params;
694 Params.push_back(Int8PtrTy);
695 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
696 Params, false),
697 "objc_begin_catch");
698 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000699
700 const llvm::StructType *EHTypeTy;
701 const llvm::Type *EHTypePtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000702
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000703 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
704 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000705};
706
707class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000708public:
709 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000710 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000711 public:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000712 unsigned ivar_bytepos;
713 unsigned ivar_size;
714 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
715 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000716
717 // Allow sorting based on byte pos.
718 bool operator<(const GC_IVAR &b) const {
719 return ivar_bytepos < b.ivar_bytepos;
720 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000721 };
722
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000723 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000724 public:
725 unsigned skip;
726 unsigned scan;
727 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
728 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000729 };
730
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000731protected:
732 CodeGen::CodeGenModule &CGM;
733 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000734 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000735
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000736 // gc ivar layout bitmap calculation helper caches.
737 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
738 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000739
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000740 /// LazySymbols - Symbols to generate a lazy reference for. See
741 /// DefinedSymbols and FinishModule().
742 std::set<IdentifierInfo*> LazySymbols;
743
744 /// DefinedSymbols - External symbols which are defined by this
745 /// module. The symbols in this list and LazySymbols are used to add
746 /// special linker symbols which ensure that Objective-C modules are
747 /// linked properly.
748 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000749
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000750 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000751 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000752
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000753 /// MethodVarNames - uniqued method variable names.
754 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000755
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000756 /// MethodVarTypes - uniqued method type signatures. We have to use
757 /// a StringMap here because have no other unique reference.
758 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000759
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000760 /// MethodDefinitions - map of methods which have been defined in
761 /// this translation unit.
762 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000763
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000764 /// PropertyNames - uniqued method variable names.
765 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000766
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000767 /// ClassReferences - uniqued class references.
768 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000769
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000770 /// SelectorReferences - uniqued selector references.
771 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000772
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000773 /// Protocols - Protocols for which an objc_protocol structure has
774 /// been emitted. Forward declarations are handled by creating an
775 /// empty structure whose initializer is filled in when/if defined.
776 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000777
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000778 /// DefinedProtocols - Protocols which have actually been
779 /// defined. We should not need this, see FIXME in GenerateProtocol.
780 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000781
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000782 /// DefinedClasses - List of defined classes.
783 std::vector<llvm::GlobalValue*> DefinedClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000784
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000785 /// DefinedCategories - List of defined categories.
786 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000787
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000788 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000789 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000790 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000791
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000792 /// GetNameForMethod - Return a name for the given method.
793 /// \param[out] NameOut - The return value.
794 void GetNameForMethod(const ObjCMethodDecl *OMD,
795 const ObjCContainerDecl *CD,
796 std::string &NameOut);
797
798 /// GetMethodVarName - Return a unique constant for the given
799 /// selector's name. The return value has type char *.
800 llvm::Constant *GetMethodVarName(Selector Sel);
801 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
802 llvm::Constant *GetMethodVarName(const std::string &Name);
803
804 /// GetMethodVarType - Return a unique constant for the given
805 /// selector's name. The return value has type char *.
806
807 // FIXME: This is a horrible name.
808 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000809 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000810
811 /// GetPropertyName - Return a unique constant for the given
812 /// name. The return value has type char *.
813 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
814
815 // FIXME: This can be dropped once string functions are unified.
816 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
817 const Decl *Container);
818
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000819 /// GetClassName - Return a unique constant for the given selector's
820 /// name. The return value has type char *.
821 llvm::Constant *GetClassName(IdentifierInfo *Ident);
822
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000823 /// BuildIvarLayout - Builds ivar layout bitmap for the class
824 /// implementation for the __strong or __weak case.
825 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000826 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
827 bool ForStrongLayout);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000828
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000829 void BuildAggrIvarLayout(const ObjCInterfaceDecl *OI,
830 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000831 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +0000832 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000833 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000834 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000835
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000836 /// GetIvarLayoutName - Returns a unique constant for the given
837 /// ivar layout bitmap.
838 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
839 const ObjCCommonTypesHelper &ObjCTypes);
840
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000841 /// EmitPropertyList - Emit the given property list. The return
842 /// value has type PropertyListPtrTy.
843 llvm::Constant *EmitPropertyList(const std::string &Name,
844 const Decl *Container,
845 const ObjCContainerDecl *OCD,
846 const ObjCCommonTypesHelper &ObjCTypes);
847
Fariborz Jahanianda320092009-01-29 19:24:30 +0000848 /// GetProtocolRef - Return a reference to the internal protocol
849 /// description, creating an empty one if it has not been
850 /// defined. The return value has type ProtocolPtrTy.
851 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000852
Chris Lattnercd0ee142009-03-31 08:33:16 +0000853 /// GetFieldBaseOffset - return's field byte offset.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000854 uint64_t GetFieldBaseOffset(const ObjCInterfaceDecl *OI,
855 const llvm::StructLayout *Layout,
Chris Lattnercd0ee142009-03-31 08:33:16 +0000856 const FieldDecl *Field);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000857
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000858 /// CreateMetadataVar - Create a global variable with internal
859 /// linkage for use by the Objective-C runtime.
860 ///
861 /// This is a convenience wrapper which not only creates the
862 /// variable, but also sets the section and alignment and adds the
863 /// global to the UsedGlobals list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000864 ///
865 /// \param Name - The variable name.
866 /// \param Init - The variable initializer; this is also used to
867 /// define the type of the variable.
868 /// \param Section - The section the variable should go into, or 0.
869 /// \param Align - The alignment for the variable, or 0.
870 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000871 /// "llvm.used".
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000872 llvm::GlobalVariable *CreateMetadataVar(const std::string &Name,
873 llvm::Constant *Init,
874 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000875 unsigned Align,
876 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000877
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000878 /// GetNamedIvarList - Return the list of ivars in the interface
879 /// itself (not including super classes and not including unnamed
880 /// bitfields).
881 ///
882 /// For the non-fragile ABI, this also includes synthesized property
883 /// ivars.
884 void GetNamedIvarList(const ObjCInterfaceDecl *OID,
885 llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const;
886
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000887public:
888 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
889 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000890
Steve Naroff33fdb732009-03-31 16:53:37 +0000891 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000892
893 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
894 const ObjCContainerDecl *CD=0);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000895
896 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
897
898 /// GetOrEmitProtocol - Get the protocol object for the given
899 /// declaration, emitting it if necessary. The return value has type
900 /// ProtocolPtrTy.
901 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
902
903 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
904 /// object for the given declaration, emitting it if needed. These
905 /// forward references will be filled in with empty bodies if no
906 /// definition is seen. The return value has type ProtocolPtrTy.
907 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000908};
909
910class CGObjCMac : public CGObjCCommonMac {
911private:
912 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000913 /// EmitImageInfo - Emit the image info marker used to encode some module
914 /// level information.
915 void EmitImageInfo();
916
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000917 /// EmitModuleInfo - Another marker encoding module level
918 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000919 void EmitModuleInfo();
920
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000921 /// EmitModuleSymols - Emit module symbols, the list of defined
922 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000923 llvm::Constant *EmitModuleSymbols();
924
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000925 /// FinishModule - Write out global data structures at the end of
926 /// processing a translation unit.
927 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000928
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000929 /// EmitClassExtension - Generate the class extension structure used
930 /// to store the weak ivar layout and properties. The return value
931 /// has type ClassExtensionPtrTy.
932 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
933
934 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
935 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000936 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000937 const ObjCInterfaceDecl *ID);
938
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000939 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000940 QualType ResultType,
941 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000942 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000943 QualType Arg0Ty,
944 bool IsSuper,
945 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000946
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000947 /// EmitIvarList - Emit the ivar list for the given
948 /// implementation. If ForClass is true the list of class ivars
949 /// (i.e. metaclass ivars) is emitted, otherwise the list of
950 /// interface ivars will be emitted. The return value has type
951 /// IvarListPtrTy.
952 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000953 bool ForClass);
954
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000955 /// EmitMetaClass - Emit a forward reference to the class structure
956 /// for the metaclass of the given interface. The return value has
957 /// type ClassPtrTy.
958 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
959
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000960 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000961 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000962 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
963 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000964 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000965
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000966 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000967
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000968 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000969
970 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000971 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000972 llvm::Constant *EmitMethodList(const std::string &Name,
973 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000974 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000975
976 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000977 /// method declarations.
978 /// - TypeName: The name for the type containing the methods.
979 /// - IsProtocol: True iff these methods are for a protocol.
980 /// - ClassMethds: True iff these are class methods.
981 /// - Required: When true, only "required" methods are
982 /// listed. Similarly, when false only "optional" methods are
983 /// listed. For classes this should always be true.
984 /// - begin, end: The method list to output.
985 ///
986 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000987 llvm::Constant *EmitMethodDescList(const std::string &Name,
988 const char *Section,
989 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000990
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000991 /// GetOrEmitProtocol - Get the protocol object for the given
992 /// declaration, emitting it if necessary. The return value has type
993 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000994 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000995
996 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
997 /// object for the given declaration, emitting it if needed. These
998 /// forward references will be filled in with empty bodies if no
999 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001000 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001001
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001002 /// EmitProtocolExtension - Generate the protocol extension
1003 /// structure used to store optional instance and class methods, and
1004 /// protocol properties. The return value has type
1005 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001006 llvm::Constant *
1007 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1008 const ConstantVector &OptInstanceMethods,
1009 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001010
1011 /// EmitProtocolList - Generate the list of referenced
1012 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +00001013 llvm::Constant *EmitProtocolList(const std::string &Name,
1014 ObjCProtocolDecl::protocol_iterator begin,
1015 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001016
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001017 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1018 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001019 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001020
Fariborz Jahanianda320092009-01-29 19:24:30 +00001021 public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001022 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001023
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001024 virtual llvm::Function *ModuleInitFunction();
1025
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001026 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001027 QualType ResultType,
1028 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001029 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001030 bool IsClassMessage,
1031 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001032
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001033 virtual CodeGen::RValue
1034 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001035 QualType ResultType,
1036 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001037 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001038 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001039 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001040 bool IsClassMessage,
1041 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001042
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001043 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001044 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001045
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001046 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001047
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001048 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001049
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001050 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001051
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001052 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001053 const ObjCProtocolDecl *PD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00001054
Chris Lattner74391b42009-03-22 21:03:39 +00001055 virtual llvm::Constant *GetPropertyGetFunction();
1056 virtual llvm::Constant *GetPropertySetFunction();
1057 virtual llvm::Constant *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001058
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001059 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1060 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001061 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1062 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001063 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001064 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001065 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1066 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001067 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1068 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001069 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1070 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001071 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1072 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001073
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001074 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1075 QualType ObjectTy,
1076 llvm::Value *BaseValue,
1077 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001078 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001079 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001080 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001081 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001082};
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001083
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001084class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001085private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001086 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001087 llvm::GlobalVariable* ObjCEmptyCacheVar;
1088 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001089
Daniel Dunbar11394522009-04-18 08:51:00 +00001090 /// SuperClassReferences - uniqued super class references.
1091 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
1092
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001093 /// MetaClassReferences - uniqued meta class references.
1094 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001095
1096 /// EHTypeReferences - uniqued class ehtype references.
1097 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001098
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001099 /// FinishNonFragileABIModule - Write out global data structures at the end of
1100 /// processing a translation unit.
1101 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001102
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00001103 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1104 unsigned InstanceStart,
1105 unsigned InstanceSize,
1106 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001107 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
1108 llvm::Constant *IsAGV,
1109 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001110 llvm::Constant *ClassRoGV,
1111 bool HiddenVisibility);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001112
1113 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
1114
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001115 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
1116
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001117 /// EmitMethodList - Emit the method list for the given
1118 /// implementation. The return value has type MethodListnfABITy.
1119 llvm::Constant *EmitMethodList(const std::string &Name,
1120 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001121 const ConstantVector &Methods);
1122 /// EmitIvarList - Emit the ivar list for the given
1123 /// implementation. If ForClass is true the list of class ivars
1124 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1125 /// interface ivars will be emitted. The return value has type
1126 /// IvarListnfABIPtrTy.
1127 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00001128
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001129 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001130 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001131 unsigned long int offset);
1132
Fariborz Jahanianda320092009-01-29 19:24:30 +00001133 /// GetOrEmitProtocol - Get the protocol object for the given
1134 /// declaration, emitting it if necessary. The return value has type
1135 /// ProtocolPtrTy.
1136 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
1137
1138 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1139 /// object for the given declaration, emitting it if needed. These
1140 /// forward references will be filled in with empty bodies if no
1141 /// definition is seen. The return value has type ProtocolPtrTy.
1142 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
1143
1144 /// EmitProtocolList - Generate the list of referenced
1145 /// protocols. The return value has type ProtocolListPtrTy.
1146 llvm::Constant *EmitProtocolList(const std::string &Name,
1147 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001148 ObjCProtocolDecl::protocol_iterator end);
1149
1150 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1151 QualType ResultType,
1152 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00001153 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001154 QualType Arg0Ty,
1155 bool IsSuper,
1156 const CallArgList &CallArgs);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001157
1158 /// GetClassGlobal - Return the global variable for the Objective-C
1159 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001160 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
1161
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001162 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001163 /// for the given class reference.
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001164 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001165 const ObjCInterfaceDecl *ID);
1166
1167 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1168 /// for the given super class reference.
1169 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1170 const ObjCInterfaceDecl *ID);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001171
1172 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1173 /// meta-data
1174 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
1175 const ObjCInterfaceDecl *ID);
1176
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001177 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1178 /// the given ivar.
1179 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001180 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00001181 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001182 const ObjCIvarDecl *Ivar);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001183
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001184 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1185 /// for the given selector.
1186 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbare588b992009-03-01 04:46:24 +00001187
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001188 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001189 /// interface. The return value has type EHTypePtrTy.
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001190 llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
1191 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001192
1193 const char *getMetaclassSymbolPrefix() const {
1194 return "OBJC_METACLASS_$_";
1195 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001196
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001197 const char *getClassSymbolPrefix() const {
1198 return "OBJC_CLASS_$_";
1199 }
1200
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001201 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001202 uint32_t &InstanceStart,
1203 uint32_t &InstanceSize);
1204
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001205public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001206 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001207 // FIXME. All stubs for now!
1208 virtual llvm::Function *ModuleInitFunction();
1209
1210 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
1211 QualType ResultType,
1212 Selector Sel,
1213 llvm::Value *Receiver,
1214 bool IsClassMessage,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001215 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001216
1217 virtual CodeGen::RValue
1218 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
1219 QualType ResultType,
1220 Selector Sel,
1221 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001222 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001223 llvm::Value *Receiver,
1224 bool IsClassMessage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001225 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001226
1227 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001228 const ObjCInterfaceDecl *ID);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001229
1230 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001231 { return EmitSelector(Builder, Sel); }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001232
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001233 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001234
1235 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001236 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001237 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001238
Chris Lattner74391b42009-03-22 21:03:39 +00001239 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001240 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001241 }
Chris Lattner74391b42009-03-22 21:03:39 +00001242 virtual llvm::Constant *GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001243 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001244 }
Chris Lattner74391b42009-03-22 21:03:39 +00001245 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001246 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001247 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001248
1249 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00001250 const Stmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001251 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001252 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001253 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001254 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001255 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001256 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001257 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001258 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001259 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001260 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001261 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001262 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001263 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1264 QualType ObjectTy,
1265 llvm::Value *BaseValue,
1266 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001267 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001268 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001269 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001270 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001271};
1272
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001273} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001274
1275/* *** Helper Functions *** */
1276
1277/// getConstantGEP() - Help routine to construct simple GEPs.
1278static llvm::Constant *getConstantGEP(llvm::Constant *C,
1279 unsigned idx0,
1280 unsigned idx1) {
1281 llvm::Value *Idxs[] = {
1282 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
1283 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
1284 };
1285 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
1286}
1287
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001288/// hasObjCExceptionAttribute - Return true if this class or any super
1289/// class has the __objc_exception__ attribute.
1290static bool hasObjCExceptionAttribute(const ObjCInterfaceDecl *OID) {
Daniel Dunbarb11fa0d2009-04-13 21:08:27 +00001291 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001292 return true;
1293 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
1294 return hasObjCExceptionAttribute(Super);
1295 return false;
1296}
1297
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001298/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001299
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001300CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
1301 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001302{
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001303 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001304 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001305}
1306
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001307/// GetClass - Return a reference to the class for the given interface
1308/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001309llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001310 const ObjCInterfaceDecl *ID) {
1311 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001312}
1313
1314/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001315llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001316 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001317}
1318
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001319/// Generate a constant CFString object.
1320/*
1321 struct __builtin_CFString {
1322 const int *isa; // point to __CFConstantStringClassReference
1323 int flags;
1324 const char *str;
1325 long length;
1326 };
1327*/
1328
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001329llvm::Constant *CGObjCCommonMac::GenerateConstantString(
Steve Naroff33fdb732009-03-31 16:53:37 +00001330 const ObjCStringLiteral *SL) {
Steve Naroff8d4141f2009-04-01 13:55:36 +00001331 return CGM.GetAddrOfConstantCFString(SL->getString());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001332}
1333
1334/// Generates a message send where the super is the receiver. This is
1335/// a message send to self with special delivery semantics indicating
1336/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001337CodeGen::RValue
1338CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001339 QualType ResultType,
1340 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001341 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001342 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001343 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001344 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001345 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001346 // Create and init a super structure; this is a (receiver, class)
1347 // pair we will pass to objc_msgSendSuper.
1348 llvm::Value *ObjCSuper =
1349 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
1350 llvm::Value *ReceiverAsObject =
1351 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
1352 CGF.Builder.CreateStore(ReceiverAsObject,
1353 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001354
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001355 // If this is a class message the metaclass is passed as the target.
1356 llvm::Value *Target;
1357 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001358 if (isCategoryImpl) {
1359 // Message sent to 'super' in a class method defined in a category
1360 // implementation requires an odd treatment.
1361 // If we are in a class method, we must retrieve the
1362 // _metaclass_ for the current class, pointed at by
1363 // the class's "isa" pointer. The following assumes that
1364 // isa" is the first ivar in a class (which it must be).
1365 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1366 Target = CGF.Builder.CreateStructGEP(Target, 0);
1367 Target = CGF.Builder.CreateLoad(Target);
1368 }
1369 else {
1370 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1371 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1372 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1373 Target = Super;
1374 }
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001375 } else {
1376 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1377 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001378 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
1379 // and ObjCTypes types.
1380 const llvm::Type *ClassTy =
1381 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001382 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001383 CGF.Builder.CreateStore(Target,
1384 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
1385
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001386 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001387 ObjCSuper, ObjCTypes.SuperPtrCTy,
1388 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001389}
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001390
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001391/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001392CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001393 QualType ResultType,
1394 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001395 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001396 bool IsClassMessage,
1397 const CallArgList &CallArgs) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001398 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001399 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001400 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001401}
1402
1403CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001404 QualType ResultType,
1405 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001406 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001407 QualType Arg0Ty,
1408 bool IsSuper,
1409 const CallArgList &CallArgs) {
1410 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001411 if (!IsSuper)
1412 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001413 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
1414 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
1415 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001416 CGF.getContext().getObjCSelType()));
1417 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001418
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001419 CodeGenTypes &Types = CGM.getTypes();
1420 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
Fariborz Jahanian65257ca2009-04-29 22:47:27 +00001421 // FIXME. vararg flag must be true when this API is used for 64bit code gen.
1422 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001423
1424 llvm::Constant *Fn;
Daniel Dunbar88b53962009-02-02 22:03:45 +00001425 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Daniel Dunbar5669e572008-10-17 03:24:53 +00001426 Fn = ObjCTypes.getSendStretFn(IsSuper);
1427 } else if (ResultType->isFloatingType()) {
1428 // FIXME: Sadly, this is wrong. This actually depends on the
1429 // architecture. This happens to be right for x86-32 though.
1430 Fn = ObjCTypes.getSendFpretFn(IsSuper);
1431 } else {
1432 Fn = ObjCTypes.getSendFn(IsSuper);
1433 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +00001434 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +00001435 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001436}
1437
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001438llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001439 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001440 // FIXME: I don't understand why gcc generates this, or where it is
1441 // resolved. Investigate. Its also wasteful to look this up over and
1442 // over.
1443 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1444
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001445 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
1446 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001447}
1448
Fariborz Jahanianda320092009-01-29 19:24:30 +00001449void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001450 // FIXME: We shouldn't need this, the protocol decl should contain
1451 // enough information to tell us whether this was a declaration or a
1452 // definition.
1453 DefinedProtocols.insert(PD->getIdentifier());
1454
1455 // If we have generated a forward reference to this protocol, emit
1456 // it now. Otherwise do nothing, the protocol objects are lazily
1457 // emitted.
1458 if (Protocols.count(PD->getIdentifier()))
1459 GetOrEmitProtocol(PD);
1460}
1461
Fariborz Jahanianda320092009-01-29 19:24:30 +00001462llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001463 if (DefinedProtocols.count(PD->getIdentifier()))
1464 return GetOrEmitProtocol(PD);
1465 return GetOrEmitProtocolRef(PD);
1466}
1467
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001468/*
1469 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1470 struct _objc_protocol {
1471 struct _objc_protocol_extension *isa;
1472 char *protocol_name;
1473 struct _objc_protocol_list *protocol_list;
1474 struct _objc__method_prototype_list *instance_methods;
1475 struct _objc__method_prototype_list *class_methods
1476 };
1477
1478 See EmitProtocolExtension().
1479*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001480llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1481 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1482
1483 // Early exit if a defining object has already been generated.
1484 if (Entry && Entry->hasInitializer())
1485 return Entry;
1486
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001487 // FIXME: I don't understand why gcc generates this, or where it is
1488 // resolved. Investigate. Its also wasteful to look this up over and
1489 // over.
1490 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1491
Chris Lattner8ec03f52008-11-24 03:54:41 +00001492 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001493
1494 // Construct method lists.
1495 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1496 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00001497 for (ObjCProtocolDecl::instmeth_iterator
1498 i = PD->instmeth_begin(CGM.getContext()),
1499 e = PD->instmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001500 ObjCMethodDecl *MD = *i;
1501 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1502 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1503 OptInstanceMethods.push_back(C);
1504 } else {
1505 InstanceMethods.push_back(C);
1506 }
1507 }
1508
Douglas Gregor6ab35242009-04-09 21:40:53 +00001509 for (ObjCProtocolDecl::classmeth_iterator
1510 i = PD->classmeth_begin(CGM.getContext()),
1511 e = PD->classmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001512 ObjCMethodDecl *MD = *i;
1513 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1514 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1515 OptClassMethods.push_back(C);
1516 } else {
1517 ClassMethods.push_back(C);
1518 }
1519 }
1520
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001521 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001522 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001523 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001524 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001525 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001526 PD->protocol_begin(),
1527 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001528 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001529 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
1530 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001531 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1532 InstanceMethods);
1533 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001534 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
1535 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001536 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1537 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001538 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
1539 Values);
1540
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001541 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001542 // Already created, fix the linkage and update the initializer.
1543 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001544 Entry->setInitializer(Init);
1545 } else {
1546 Entry =
1547 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
1548 llvm::GlobalValue::InternalLinkage,
1549 Init,
1550 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
1551 &CGM.getModule());
1552 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001553 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001554 UsedGlobals.push_back(Entry);
1555 // FIXME: Is this necessary? Why only for protocol?
1556 Entry->setAlignment(4);
1557 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001558
1559 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001560}
1561
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001562llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001563 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1564
1565 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001566 // We use the initializer as a marker of whether this is a forward
1567 // reference or not. At module finalization we add the empty
1568 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001569 Entry =
1570 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001571 llvm::GlobalValue::ExternalLinkage,
1572 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001573 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001574 &CGM.getModule());
1575 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001576 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001577 UsedGlobals.push_back(Entry);
1578 // FIXME: Is this necessary? Why only for protocol?
1579 Entry->setAlignment(4);
1580 }
1581
1582 return Entry;
1583}
1584
1585/*
1586 struct _objc_protocol_extension {
1587 uint32_t size;
1588 struct objc_method_description_list *optional_instance_methods;
1589 struct objc_method_description_list *optional_class_methods;
1590 struct objc_property_list *instance_properties;
1591 };
1592*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001593llvm::Constant *
1594CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1595 const ConstantVector &OptInstanceMethods,
1596 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001597 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001598 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001599 std::vector<llvm::Constant*> Values(4);
1600 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001601 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001602 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1603 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001604 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1605 OptInstanceMethods);
1606 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001607 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1608 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001609 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1610 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001611 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1612 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001613 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001614
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001615 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001616 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1617 Values[3]->isNullValue())
1618 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1619
1620 llvm::Constant *Init =
1621 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001622
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001623 // No special section, but goes in llvm.used
1624 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
1625 Init,
1626 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001627}
1628
1629/*
1630 struct objc_protocol_list {
1631 struct objc_protocol_list *next;
1632 long count;
1633 Protocol *list[];
1634 };
1635*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001636llvm::Constant *
1637CGObjCMac::EmitProtocolList(const std::string &Name,
1638 ObjCProtocolDecl::protocol_iterator begin,
1639 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001640 std::vector<llvm::Constant*> ProtocolRefs;
1641
Daniel Dunbardbc933702008-08-21 21:57:41 +00001642 for (; begin != end; ++begin)
1643 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001644
1645 // Just return null for empty protocol lists
1646 if (ProtocolRefs.empty())
1647 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1648
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001649 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001650 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1651
1652 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001653 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001654 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1655 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1656 Values[2] =
1657 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1658 ProtocolRefs.size()),
1659 ProtocolRefs);
1660
1661 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1662 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001663 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001664 4, false);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001665 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1666}
1667
1668/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001669 struct _objc_property {
1670 const char * const name;
1671 const char * const attributes;
1672 };
1673
1674 struct _objc_property_list {
1675 uint32_t entsize; // sizeof (struct _objc_property)
1676 uint32_t prop_count;
1677 struct _objc_property[prop_count];
1678 };
1679*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001680llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1681 const Decl *Container,
1682 const ObjCContainerDecl *OCD,
1683 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001684 std::vector<llvm::Constant*> Properties, Prop(2);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001685 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(CGM.getContext()),
1686 E = OCD->prop_end(CGM.getContext()); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00001687 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001688 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001689 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001690 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1691 Prop));
1692 }
1693
1694 // Return null for empty list.
1695 if (Properties.empty())
1696 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1697
1698 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001699 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001700 std::vector<llvm::Constant*> Values(3);
1701 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1702 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1703 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1704 Properties.size());
1705 Values[2] = llvm::ConstantArray::get(AT, Properties);
1706 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1707
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001708 llvm::GlobalVariable *GV =
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001709 CreateMetadataVar(Name, Init,
1710 (ObjCABI == 2) ? "__DATA, __objc_const" :
1711 "__OBJC,__property,regular,no_dead_strip",
1712 (ObjCABI == 2) ? 8 : 4,
1713 true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001714 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001715}
1716
1717/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001718 struct objc_method_description_list {
1719 int count;
1720 struct objc_method_description list[];
1721 };
1722*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001723llvm::Constant *
1724CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1725 std::vector<llvm::Constant*> Desc(2);
1726 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1727 ObjCTypes.SelectorPtrTy);
1728 Desc[1] = GetMethodVarType(MD);
1729 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1730 Desc);
1731}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001732
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001733llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1734 const char *Section,
1735 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001736 // Return null for empty list.
1737 if (Methods.empty())
1738 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1739
1740 std::vector<llvm::Constant*> Values(2);
1741 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1742 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1743 Methods.size());
1744 Values[1] = llvm::ConstantArray::get(AT, Methods);
1745 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1746
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001747 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001748 return llvm::ConstantExpr::getBitCast(GV,
1749 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001750}
1751
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001752/*
1753 struct _objc_category {
1754 char *category_name;
1755 char *class_name;
1756 struct _objc_method_list *instance_methods;
1757 struct _objc_method_list *class_methods;
1758 struct _objc_protocol_list *protocols;
1759 uint32_t size; // <rdar://4585769>
1760 struct _objc_property_list *instance_properties;
1761 };
1762 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001763void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001764 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001765
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001766 // FIXME: This is poor design, the OCD should have a pointer to the
1767 // category decl. Additionally, note that Category can be null for
1768 // the @implementation w/o an @interface case. Sema should just
1769 // create one for us as it does for @implementation so everyone else
1770 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001771 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001772 const ObjCCategoryDecl *Category =
1773 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001774 std::string ExtName(Interface->getNameAsString() + "_" +
1775 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001776
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001777 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001778 for (ObjCCategoryImplDecl::instmeth_iterator
1779 i = OCD->instmeth_begin(CGM.getContext()),
1780 e = OCD->instmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001781 // Instance methods should always be defined.
1782 InstanceMethods.push_back(GetMethodConstant(*i));
1783 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001784 for (ObjCCategoryImplDecl::classmeth_iterator
1785 i = OCD->classmeth_begin(CGM.getContext()),
1786 e = OCD->classmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001787 // Class methods should always be defined.
1788 ClassMethods.push_back(GetMethodConstant(*i));
1789 }
1790
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001791 std::vector<llvm::Constant*> Values(7);
1792 Values[0] = GetClassName(OCD->getIdentifier());
1793 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00001794 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001795 Values[2] =
1796 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1797 ExtName,
1798 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001799 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001800 Values[3] =
1801 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001802 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001803 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001804 if (Category) {
1805 Values[4] =
1806 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1807 Category->protocol_begin(),
1808 Category->protocol_end());
1809 } else {
1810 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1811 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001812 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001813
1814 // If there is no category @interface then there can be no properties.
1815 if (Category) {
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001816 Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001817 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001818 } else {
1819 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1820 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001821
1822 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1823 Values);
1824
1825 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001826 CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init,
1827 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001828 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001829 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001830}
1831
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001832// FIXME: Get from somewhere?
1833enum ClassFlags {
1834 eClassFlags_Factory = 0x00001,
1835 eClassFlags_Meta = 0x00002,
1836 // <rdr://5142207>
1837 eClassFlags_HasCXXStructors = 0x02000,
1838 eClassFlags_Hidden = 0x20000,
1839 eClassFlags_ABI2_Hidden = 0x00010,
1840 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1841};
1842
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001843/*
1844 struct _objc_class {
1845 Class isa;
1846 Class super_class;
1847 const char *name;
1848 long version;
1849 long info;
1850 long instance_size;
1851 struct _objc_ivar_list *ivars;
1852 struct _objc_method_list *methods;
1853 struct _objc_cache *cache;
1854 struct _objc_protocol_list *protocols;
1855 // Objective-C 1.0 extensions (<rdr://4585769>)
1856 const char *ivar_layout;
1857 struct _objc_class_ext *ext;
1858 };
1859
1860 See EmitClassExtension();
1861 */
1862void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001863 DefinedSymbols.insert(ID->getIdentifier());
1864
Chris Lattner8ec03f52008-11-24 03:54:41 +00001865 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001866 // FIXME: Gross
1867 ObjCInterfaceDecl *Interface =
1868 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001869 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001870 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001871 Interface->protocol_begin(),
1872 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001873 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar2bebbf02009-05-03 10:46:44 +00001874 unsigned Size =
1875 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001876
1877 // FIXME: Set CXX-structors flag.
Daniel Dunbar04d40782009-04-14 06:00:08 +00001878 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001879 Flags |= eClassFlags_Hidden;
1880
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001881 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001882 for (ObjCImplementationDecl::instmeth_iterator
1883 i = ID->instmeth_begin(CGM.getContext()),
1884 e = ID->instmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001885 // Instance methods should always be defined.
1886 InstanceMethods.push_back(GetMethodConstant(*i));
1887 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001888 for (ObjCImplementationDecl::classmeth_iterator
1889 i = ID->classmeth_begin(CGM.getContext()),
1890 e = ID->classmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001891 // Class methods should always be defined.
1892 ClassMethods.push_back(GetMethodConstant(*i));
1893 }
1894
Douglas Gregor653f1b12009-04-23 01:02:12 +00001895 for (ObjCImplementationDecl::propimpl_iterator
1896 i = ID->propimpl_begin(CGM.getContext()),
1897 e = ID->propimpl_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001898 ObjCPropertyImplDecl *PID = *i;
1899
1900 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1901 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1902
1903 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1904 if (llvm::Constant *C = GetMethodConstant(MD))
1905 InstanceMethods.push_back(C);
1906 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1907 if (llvm::Constant *C = GetMethodConstant(MD))
1908 InstanceMethods.push_back(C);
1909 }
1910 }
1911
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001912 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar5384b092009-05-03 08:56:52 +00001913 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001914 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001915 // Record a reference to the super class.
1916 LazySymbols.insert(Super->getIdentifier());
1917
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001918 Values[ 1] =
1919 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1920 ObjCTypes.ClassPtrTy);
1921 } else {
1922 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1923 }
1924 Values[ 2] = GetClassName(ID->getIdentifier());
1925 // Version is always 0.
1926 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1927 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1928 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001929 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001930 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001931 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001932 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001933 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001934 // cache is always NULL.
1935 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1936 Values[ 9] = Protocols;
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00001937 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001938 Values[11] = EmitClassExtension(ID);
1939 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1940 Values);
1941
1942 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001943 CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init,
1944 "__OBJC,__class,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001945 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001946 DefinedClasses.push_back(GV);
1947}
1948
1949llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1950 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001951 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001952 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001953 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001954
Daniel Dunbar04d40782009-04-14 06:00:08 +00001955 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001956 Flags |= eClassFlags_Hidden;
1957
1958 std::vector<llvm::Constant*> Values(12);
1959 // The isa for the metaclass is the root of the hierarchy.
1960 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1961 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1962 Root = Super;
1963 Values[ 0] =
1964 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1965 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001966 // The super class for the metaclass is emitted as the name of the
1967 // super class. The runtime fixes this up to point to the
1968 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001969 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1970 Values[ 1] =
1971 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1972 ObjCTypes.ClassPtrTy);
1973 } else {
1974 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1975 }
1976 Values[ 2] = GetClassName(ID->getIdentifier());
1977 // Version is always 0.
1978 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1979 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1980 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001981 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001982 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001983 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001984 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001985 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001986 // cache is always NULL.
1987 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1988 Values[ 9] = Protocols;
1989 // ivar_layout for metaclass is always NULL.
1990 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1991 // The class extension is always unused for metaclasses.
1992 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1993 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1994 Values);
1995
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001996 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001997 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001998
1999 // Check for a forward reference.
2000 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2001 if (GV) {
2002 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2003 "Forward metaclass reference has incorrect type.");
2004 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2005 GV->setInitializer(Init);
2006 } else {
2007 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
2008 llvm::GlobalValue::InternalLinkage,
2009 Init, Name,
2010 &CGM.getModule());
2011 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002012 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002013 GV->setAlignment(4);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002014 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002015
2016 return GV;
2017}
2018
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002019llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002020 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002021
2022 // FIXME: Should we look these up somewhere other than the
2023 // module. Its a bit silly since we only generate these while
2024 // processing an implementation, so exactly one pointer would work
2025 // if know when we entered/exitted an implementation block.
2026
2027 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002028 // Previously, metaclass with internal linkage may have been defined.
2029 // pass 'true' as 2nd argument so it is returned.
2030 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002031 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2032 "Forward metaclass reference has incorrect type.");
2033 return GV;
2034 } else {
2035 // Generate as an external reference to keep a consistent
2036 // module. This will be patched up when we emit the metaclass.
2037 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
2038 llvm::GlobalValue::ExternalLinkage,
2039 0,
2040 Name,
2041 &CGM.getModule());
2042 }
2043}
2044
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002045/*
2046 struct objc_class_ext {
2047 uint32_t size;
2048 const char *weak_ivar_layout;
2049 struct _objc_property_list *properties;
2050 };
2051*/
2052llvm::Constant *
2053CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
2054 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002055 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002056
2057 std::vector<llvm::Constant*> Values(3);
2058 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002059 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002060 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002061 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002062
2063 // Return null if no extension bits are used.
2064 if (Values[1]->isNullValue() && Values[2]->isNullValue())
2065 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
2066
2067 llvm::Constant *Init =
2068 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002069 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002070 Init, "__OBJC,__class_ext,regular,no_dead_strip",
2071 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002072}
2073
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00002074/// getInterfaceDeclForIvar - Get the interface declaration node where
2075/// this ivar is declared in.
2076/// FIXME. Ideally, this info should be in the ivar node. But currently
2077/// it is not and prevailing wisdom is that ASTs should not have more
2078/// info than is absolutely needed, even though this info reflects the
2079/// source language.
2080///
2081static const ObjCInterfaceDecl *getInterfaceDeclForIvar(
2082 const ObjCInterfaceDecl *OI,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002083 const ObjCIvarDecl *IVD,
2084 ASTContext &Context) {
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00002085 if (!OI)
2086 return 0;
2087 assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface");
2088 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
2089 E = OI->ivar_end(); I != E; ++I)
2090 if ((*I)->getIdentifier() == IVD->getIdentifier())
2091 return OI;
Fariborz Jahanian5a4b4532009-03-31 17:00:52 +00002092 // look into properties.
Douglas Gregor6ab35242009-04-09 21:40:53 +00002093 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(Context),
2094 E = OI->prop_end(Context); I != E; ++I) {
Fariborz Jahanian5a4b4532009-03-31 17:00:52 +00002095 ObjCPropertyDecl *PDecl = (*I);
2096 if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl())
2097 if (IV->getIdentifier() == IVD->getIdentifier())
2098 return OI;
2099 }
Douglas Gregor6ab35242009-04-09 21:40:53 +00002100 return getInterfaceDeclForIvar(OI->getSuperClass(), IVD, Context);
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00002101}
2102
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002103/*
2104 struct objc_ivar {
2105 char *ivar_name;
2106 char *ivar_type;
2107 int ivar_offset;
2108 };
2109
2110 struct objc_ivar_list {
2111 int ivar_count;
2112 struct objc_ivar list[count];
2113 };
2114 */
2115llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002116 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002117 std::vector<llvm::Constant*> Ivars, Ivar(3);
2118
2119 // When emitting the root class GCC emits ivar entries for the
2120 // actual class structure. It is not clear if we need to follow this
2121 // behavior; for now lets try and get away with not doing it. If so,
2122 // the cleanest solution would be to make up an ObjCInterfaceDecl
2123 // for the class.
2124 if (ForClass)
2125 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002126
2127 ObjCInterfaceDecl *OID =
2128 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002129
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002130 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
2131 GetNamedIvarList(OID, OIvars);
2132
2133 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2134 ObjCIvarDecl *IVD = OIvars[i];
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002135 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2136 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00002137 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002138 ComputeIvarBaseOffset(CGM, OID, IVD));
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002139 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002140 }
2141
2142 // Return null for empty list.
2143 if (Ivars.empty())
2144 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
2145
2146 std::vector<llvm::Constant*> Values(2);
2147 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
2148 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
2149 Ivars.size());
2150 Values[1] = llvm::ConstantArray::get(AT, Ivars);
2151 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2152
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002153 llvm::GlobalVariable *GV;
2154 if (ForClass)
2155 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(),
Daniel Dunbar58a29122009-03-09 22:18:41 +00002156 Init, "__OBJC,__class_vars,regular,no_dead_strip",
2157 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002158 else
2159 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_"
2160 + ID->getNameAsString(),
2161 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002162 4, true);
2163 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002164}
2165
2166/*
2167 struct objc_method {
2168 SEL method_name;
2169 char *method_types;
2170 void *method;
2171 };
2172
2173 struct objc_method_list {
2174 struct objc_method_list *obsolete;
2175 int count;
2176 struct objc_method methods_list[count];
2177 };
2178*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002179
2180/// GetMethodConstant - Return a struct objc_method constant for the
2181/// given method if it has been defined. The result is null if the
2182/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002183llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002184 // FIXME: Use DenseMap::lookup
2185 llvm::Function *Fn = MethodDefinitions[MD];
2186 if (!Fn)
2187 return 0;
2188
2189 std::vector<llvm::Constant*> Method(3);
2190 Method[0] =
2191 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2192 ObjCTypes.SelectorPtrTy);
2193 Method[1] = GetMethodVarType(MD);
2194 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
2195 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
2196}
2197
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002198llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
2199 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002200 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002201 // Return null for empty list.
2202 if (Methods.empty())
2203 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
2204
2205 std::vector<llvm::Constant*> Values(3);
2206 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
2207 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
2208 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
2209 Methods.size());
2210 Values[2] = llvm::ConstantArray::get(AT, Methods);
2211 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2212
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002213 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002214 return llvm::ConstantExpr::getBitCast(GV,
2215 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002216}
2217
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002218llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00002219 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002220 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002221 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002222
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002223 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002224 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002225 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002226 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002227 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002228 llvm::GlobalValue::InternalLinkage,
2229 Name,
2230 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002231 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002232
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002233 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002234}
2235
Daniel Dunbar48fa0642009-04-19 02:03:42 +00002236/// GetFieldBaseOffset - return the field's byte offset.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002237uint64_t CGObjCCommonMac::GetFieldBaseOffset(const ObjCInterfaceDecl *OI,
2238 const llvm::StructLayout *Layout,
Chris Lattnercd0ee142009-03-31 08:33:16 +00002239 const FieldDecl *Field) {
Daniel Dunbar97776872009-04-22 07:32:20 +00002240 // Is this a C struct?
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00002241 if (!OI)
2242 return Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
Daniel Dunbar97776872009-04-22 07:32:20 +00002243 return ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002244}
2245
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002246llvm::GlobalVariable *
2247CGObjCCommonMac::CreateMetadataVar(const std::string &Name,
2248 llvm::Constant *Init,
2249 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002250 unsigned Align,
2251 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002252 const llvm::Type *Ty = Init->getType();
2253 llvm::GlobalVariable *GV =
2254 new llvm::GlobalVariable(Ty, false,
2255 llvm::GlobalValue::InternalLinkage,
2256 Init,
2257 Name,
2258 &CGM.getModule());
2259 if (Section)
2260 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002261 if (Align)
2262 GV->setAlignment(Align);
2263 if (AddToUsed)
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002264 UsedGlobals.push_back(GV);
2265 return GV;
2266}
2267
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002268llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002269 // Abuse this interface function as a place to finalize.
2270 FinishModule();
2271
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002272 return NULL;
2273}
2274
Chris Lattner74391b42009-03-22 21:03:39 +00002275llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002276 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002277}
2278
Chris Lattner74391b42009-03-22 21:03:39 +00002279llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002280 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002281}
2282
Chris Lattner74391b42009-03-22 21:03:39 +00002283llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002284 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002285}
2286
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002287/*
2288
2289Objective-C setjmp-longjmp (sjlj) Exception Handling
2290--
2291
2292The basic framework for a @try-catch-finally is as follows:
2293{
2294 objc_exception_data d;
2295 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002296 bool _call_try_exit = true;
2297
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002298 objc_exception_try_enter(&d);
2299 if (!setjmp(d.jmp_buf)) {
2300 ... try body ...
2301 } else {
2302 // exception path
2303 id _caught = objc_exception_extract(&d);
2304
2305 // enter new try scope for handlers
2306 if (!setjmp(d.jmp_buf)) {
2307 ... match exception and execute catch blocks ...
2308
2309 // fell off end, rethrow.
2310 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00002311 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002312 } else {
2313 // exception in catch block
2314 _rethrow = objc_exception_extract(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002315 _call_try_exit = false;
2316 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002317 }
2318 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002319 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002320
2321finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002322 if (_call_try_exit)
2323 objc_exception_try_exit(&d);
2324
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002325 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002326 ... dispatch to finally destination ...
2327
2328finally_rethrow:
2329 objc_exception_throw(_rethrow);
2330
2331finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002332}
2333
2334This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00002335uses _rethrow to determine if objc_exception_try_exit should be called
2336and if the object should be rethrown. This breaks in the face of
2337throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002338
2339We specialize this framework for a few particular circumstances:
2340
2341 - If there are no catch blocks, then we avoid emitting the second
2342 exception handling context.
2343
2344 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2345 e)) we avoid emitting the code to rethrow an uncaught exception.
2346
2347 - FIXME: If there is no @finally block we can do a few more
2348 simplifications.
2349
2350Rethrows and Jumps-Through-Finally
2351--
2352
2353Support for implicit rethrows and jumping through the finally block is
2354handled by storing the current exception-handling context in
2355ObjCEHStack.
2356
Daniel Dunbar898d5082008-09-30 01:06:03 +00002357In order to implement proper @finally semantics, we support one basic
2358mechanism for jumping through the finally block to an arbitrary
2359destination. Constructs which generate exits from a @try or @catch
2360block use this mechanism to implement the proper semantics by chaining
2361jumps, as necessary.
2362
2363This mechanism works like the one used for indirect goto: we
2364arbitrarily assign an ID to each destination and store the ID for the
2365destination in a variable prior to entering the finally block. At the
2366end of the finally block we simply create a switch to the proper
2367destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002368
2369Code gen for @synchronized(expr) stmt;
2370Effectively generating code for:
2371objc_sync_enter(expr);
2372@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002373*/
2374
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002375void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2376 const Stmt &S) {
2377 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002378 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00002379 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002380 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar55e87422008-11-11 02:29:29 +00002381 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
2382 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
2383 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar1c566672009-02-24 01:43:46 +00002384
2385 // For @synchronized, call objc_sync_enter(sync.expr). The
2386 // evaluation of the expression must occur before we enter the
2387 // @synchronized. We can safely avoid a temp here because jumps into
2388 // @synchronized are illegal & this will dominate uses.
2389 llvm::Value *SyncArg = 0;
2390 if (!isTry) {
2391 SyncArg =
2392 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2393 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00002394 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002395 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002396
2397 // Push an EH context entry, used for handling rethrows and jumps
2398 // through finally.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002399 CGF.PushCleanupBlock(FinallyBlock);
2400
Anders Carlsson273558f2009-02-07 21:37:21 +00002401 CGF.ObjCEHValueStack.push_back(0);
2402
Daniel Dunbar898d5082008-09-30 01:06:03 +00002403 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00002404 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2405 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002406 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
2407 "_rethrow");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002408 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
2409 "_call_try_exit");
2410 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr);
2411
Anders Carlsson80f25672008-09-09 17:59:25 +00002412 // Enter a new try block and call setjmp.
Chris Lattner34b02a12009-04-22 02:26:14 +00002413 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Anders Carlsson80f25672008-09-09 17:59:25 +00002414 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
2415 "jmpbufarray");
2416 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
Chris Lattner34b02a12009-04-22 02:26:14 +00002417 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlsson80f25672008-09-09 17:59:25 +00002418 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00002419
Daniel Dunbar55e87422008-11-11 02:29:29 +00002420 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2421 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002422 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002423 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002424
2425 // Emit the @try block.
2426 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002427 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
2428 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002429 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002430
2431 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002432 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002433
2434 // Retrieve the exception object. We may emit multiple blocks but
2435 // nothing can cross this so the value is already in SSA form.
Chris Lattner34b02a12009-04-22 02:26:14 +00002436 llvm::Value *Caught =
2437 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2438 ExceptionData, "caught");
Anders Carlsson273558f2009-02-07 21:37:21 +00002439 CGF.ObjCEHValueStack.back() = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002440 if (!isTry)
2441 {
2442 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002443 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002444 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002445 }
2446 else if (const ObjCAtCatchStmt* CatchStmt =
2447 cast<ObjCAtTryStmt>(S).getCatchStmts())
2448 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00002449 // Enter a new exception try block (in case a @catch block throws
2450 // an exception).
Chris Lattner34b02a12009-04-22 02:26:14 +00002451 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002452
Chris Lattner34b02a12009-04-22 02:26:14 +00002453 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlsson80f25672008-09-09 17:59:25 +00002454 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002455 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00002456
Daniel Dunbar55e87422008-11-11 02:29:29 +00002457 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
2458 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002459 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002460
2461 CGF.EmitBlock(CatchBlock);
2462
Daniel Dunbar55e40722008-09-27 07:03:52 +00002463 // Handle catch list. As a special case we check if everything is
2464 // matched and avoid generating code for falling off the end if
2465 // so.
2466 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00002467 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00002468 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00002469
Steve Naroff7ba138a2009-03-03 19:52:17 +00002470 const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Daniel Dunbar129271a2008-09-27 07:36:24 +00002471 const PointerType *PT = 0;
2472
Anders Carlsson80f25672008-09-09 17:59:25 +00002473 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00002474 if (!CatchParam) {
2475 AllMatched = true;
2476 } else {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002477 PT = CatchParam->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002478
Daniel Dunbar97f61d12008-09-27 22:21:14 +00002479 // catch(id e) always matches.
2480 // FIXME: For the time being we also match id<X>; this should
2481 // be rejected by Sema instead.
Steve Naroff389bf462009-02-12 17:52:19 +00002482 if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) ||
Steve Naroff7ba138a2009-03-03 19:52:17 +00002483 CatchParam->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00002484 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00002485 }
2486
Daniel Dunbar55e40722008-09-27 07:03:52 +00002487 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00002488 if (CatchParam) {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002489 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002490 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002491 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002492 }
Anders Carlsson1452f552008-09-11 08:21:54 +00002493
Anders Carlssondde0a942008-09-11 09:15:33 +00002494 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002495 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002496 break;
2497 }
2498
Daniel Dunbar129271a2008-09-27 07:36:24 +00002499 assert(PT && "Unexpected non-pointer type in @catch");
2500 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00002501 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002502 assert(ObjCType && "Catch parameter must have Objective-C type!");
2503
2504 // Check if the @catch block matches the exception object.
2505 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
2506
Chris Lattner34b02a12009-04-22 02:26:14 +00002507 llvm::Value *Match =
2508 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
2509 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00002510
Daniel Dunbar55e87422008-11-11 02:29:29 +00002511 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00002512
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002513 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002514 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002515
2516 // Emit the @catch block.
2517 CGF.EmitBlock(MatchedBlock);
Steve Naroff7ba138a2009-03-03 19:52:17 +00002518 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002519 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002520
2521 llvm::Value *Tmp =
Steve Naroff7ba138a2009-03-03 19:52:17 +00002522 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002523 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002524 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002525
2526 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002527 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002528
2529 CGF.EmitBlock(NextCatchBlock);
2530 }
2531
Daniel Dunbar55e40722008-09-27 07:03:52 +00002532 if (!AllMatched) {
2533 // None of the handlers caught the exception, so store it to be
2534 // rethrown at the end of the @finally block.
2535 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002536 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002537 }
2538
2539 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002540 CGF.EmitBlock(CatchHandler);
Chris Lattner34b02a12009-04-22 02:26:14 +00002541 CGF.Builder.CreateStore(
2542 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2543 ExceptionData),
Daniel Dunbar55e40722008-09-27 07:03:52 +00002544 RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002545 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002546 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002547 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00002548 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002549 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002550 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson80f25672008-09-09 17:59:25 +00002551 }
2552
Daniel Dunbar898d5082008-09-30 01:06:03 +00002553 // Pop the exception-handling stack entry. It is important to do
2554 // this now, because the code in the @finally block is not in this
2555 // context.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002556 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2557
Anders Carlsson273558f2009-02-07 21:37:21 +00002558 CGF.ObjCEHValueStack.pop_back();
2559
Anders Carlsson80f25672008-09-09 17:59:25 +00002560 // Emit the @finally block.
2561 CGF.EmitBlock(FinallyBlock);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002562 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2563
2564 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2565
2566 CGF.EmitBlock(FinallyExit);
Chris Lattner34b02a12009-04-22 02:26:14 +00002567 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002568
2569 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002570 if (isTry) {
2571 if (const ObjCAtFinallyStmt* FinallyStmt =
2572 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2573 CGF.EmitStmt(FinallyStmt->getFinallyBody());
Daniel Dunbar1c566672009-02-24 01:43:46 +00002574 } else {
2575 // Emit objc_sync_exit(expr); as finally's sole statement for
2576 // @synchronized.
Chris Lattnerbbccd612009-04-22 02:38:11 +00002577 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002578 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002579
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002580 // Emit the switch block
2581 if (Info.SwitchBlock)
2582 CGF.EmitBlock(Info.SwitchBlock);
2583 if (Info.EndBlock)
2584 CGF.EmitBlock(Info.EndBlock);
2585
Daniel Dunbar898d5082008-09-30 01:06:03 +00002586 CGF.EmitBlock(FinallyRethrow);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002587 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbar898d5082008-09-30 01:06:03 +00002588 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002589 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00002590
2591 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002592}
2593
2594void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002595 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002596 llvm::Value *ExceptionAsObject;
2597
2598 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2599 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2600 ExceptionAsObject =
2601 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2602 } else {
Anders Carlsson273558f2009-02-07 21:37:21 +00002603 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002604 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00002605 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002606 }
2607
Chris Lattnerbbccd612009-04-22 02:38:11 +00002608 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002609 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002610
2611 // Clear the insertion point to indicate we are in unreachable code.
2612 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002613}
2614
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002615/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002616/// object: objc_read_weak (id *src)
2617///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002618llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002619 llvm::Value *AddrWeakObj)
2620{
Eli Friedman8339b352009-03-07 03:57:15 +00002621 const llvm::Type* DestTy =
2622 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002623 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00002624 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002625 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00002626 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002627 return read_weak;
2628}
2629
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002630/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2631/// objc_assign_weak (id src, id *dst)
2632///
2633void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2634 llvm::Value *src, llvm::Value *dst)
2635{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002636 const llvm::Type * SrcTy = src->getType();
2637 if (!isa<llvm::PointerType>(SrcTy)) {
2638 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2639 assert(Size <= 8 && "does not support size > 8");
2640 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2641 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002642 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2643 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002644 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2645 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00002646 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002647 src, dst, "weakassign");
2648 return;
2649}
2650
Fariborz Jahanian58626502008-11-19 00:59:10 +00002651/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2652/// objc_assign_global (id src, id *dst)
2653///
2654void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2655 llvm::Value *src, llvm::Value *dst)
2656{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002657 const llvm::Type * SrcTy = src->getType();
2658 if (!isa<llvm::PointerType>(SrcTy)) {
2659 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2660 assert(Size <= 8 && "does not support size > 8");
2661 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2662 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002663 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2664 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002665 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2666 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002667 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00002668 src, dst, "globalassign");
2669 return;
2670}
2671
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002672/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2673/// objc_assign_ivar (id src, id *dst)
2674///
2675void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2676 llvm::Value *src, llvm::Value *dst)
2677{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002678 const llvm::Type * SrcTy = src->getType();
2679 if (!isa<llvm::PointerType>(SrcTy)) {
2680 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2681 assert(Size <= 8 && "does not support size > 8");
2682 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2683 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002684 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2685 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002686 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2687 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002688 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(),
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002689 src, dst, "assignivar");
2690 return;
2691}
2692
Fariborz Jahanian58626502008-11-19 00:59:10 +00002693/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2694/// objc_assign_strongCast (id src, id *dst)
2695///
2696void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2697 llvm::Value *src, llvm::Value *dst)
2698{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002699 const llvm::Type * SrcTy = src->getType();
2700 if (!isa<llvm::PointerType>(SrcTy)) {
2701 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2702 assert(Size <= 8 && "does not support size > 8");
2703 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2704 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002705 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2706 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002707 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2708 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002709 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00002710 src, dst, "weakassign");
2711 return;
2712}
2713
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002714/// EmitObjCValueForIvar - Code Gen for ivar reference.
2715///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002716LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2717 QualType ObjectTy,
2718 llvm::Value *BaseValue,
2719 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002720 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00002721 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00002722 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
2723 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002724}
2725
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002726llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00002727 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002728 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00002729 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002730 return llvm::ConstantInt::get(
2731 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2732 Offset);
2733}
2734
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002735/* *** Private Interface *** */
2736
2737/// EmitImageInfo - Emit the image info marker used to encode some module
2738/// level information.
2739///
2740/// See: <rdr://4810609&4810587&4810587>
2741/// struct IMAGE_INFO {
2742/// unsigned version;
2743/// unsigned flags;
2744/// };
2745enum ImageInfoFlags {
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002746 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what
2747 // this implies.
2748 eImageInfo_GarbageCollected = (1 << 1),
2749 eImageInfo_GCOnly = (1 << 2),
2750 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
2751
2752 // A flag indicating that the module has no instances of an
2753 // @synthesize of a superclass variable. <rdar://problem/6803242>
2754 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002755};
2756
2757void CGObjCMac::EmitImageInfo() {
2758 unsigned version = 0; // Version is unused?
2759 unsigned flags = 0;
2760
2761 // FIXME: Fix and continue?
2762 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2763 flags |= eImageInfo_GarbageCollected;
2764 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2765 flags |= eImageInfo_GCOnly;
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002766
2767 // We never allow @synthesize of a superclass property.
2768 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002769
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002770 // Emitted as int[2];
2771 llvm::Constant *values[2] = {
2772 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2773 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2774 };
2775 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002776
2777 const char *Section;
2778 if (ObjCABI == 1)
2779 Section = "__OBJC, __image_info,regular";
2780 else
2781 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002782 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002783 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
2784 llvm::ConstantArray::get(AT, values, 2),
2785 Section,
2786 0,
2787 true);
2788 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002789}
2790
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002791
2792// struct objc_module {
2793// unsigned long version;
2794// unsigned long size;
2795// const char *name;
2796// Symtab symtab;
2797// };
2798
2799// FIXME: Get from somewhere
2800static const int ModuleVersion = 7;
2801
2802void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002803 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002804
2805 std::vector<llvm::Constant*> Values(4);
2806 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2807 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002808 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002809 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002810 Values[3] = EmitModuleSymbols();
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002811 CreateMetadataVar("\01L_OBJC_MODULES",
2812 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
2813 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002814 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002815}
2816
2817llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002818 unsigned NumClasses = DefinedClasses.size();
2819 unsigned NumCategories = DefinedCategories.size();
2820
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002821 // Return null if no symbols were defined.
2822 if (!NumClasses && !NumCategories)
2823 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2824
2825 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002826 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2827 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2828 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2829 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2830
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002831 // The runtime expects exactly the list of defined classes followed
2832 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002833 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002834 for (unsigned i=0; i<NumClasses; i++)
2835 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2836 ObjCTypes.Int8PtrTy);
2837 for (unsigned i=0; i<NumCategories; i++)
2838 Symbols[NumClasses + i] =
2839 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2840 ObjCTypes.Int8PtrTy);
2841
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002842 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002843 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002844 NumClasses + NumCategories),
2845 Symbols);
2846
2847 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2848
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002849 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002850 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
2851 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002852 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002853 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2854}
2855
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002856llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002857 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002858 LazySymbols.insert(ID->getIdentifier());
2859
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002860 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2861
2862 if (!Entry) {
2863 llvm::Constant *Casted =
2864 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2865 ObjCTypes.ClassPtrTy);
2866 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002867 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
2868 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002869 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002870 }
2871
2872 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002873}
2874
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002875llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002876 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2877
2878 if (!Entry) {
2879 llvm::Constant *Casted =
2880 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2881 ObjCTypes.SelectorPtrTy);
2882 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002883 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
2884 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002885 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002886 }
2887
2888 return Builder.CreateLoad(Entry, false, "tmp");
2889}
2890
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002891llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002892 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002893
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002894 if (!Entry)
2895 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
2896 llvm::ConstantArray::get(Ident->getName()),
2897 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00002898 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002899
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002900 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002901}
2902
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00002903/// GetIvarLayoutName - Returns a unique constant for the given
2904/// ivar layout bitmap.
2905llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
2906 const ObjCCommonTypesHelper &ObjCTypes) {
2907 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
2908}
2909
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002910void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCInterfaceDecl *OI,
2911 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002912 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00002913 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002914 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +00002915 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002916 bool IsUnion = (RD && RD->isUnion());
2917 uint64_t MaxUnionIvarSize = 0;
2918 uint64_t MaxSkippedUnionIvarSize = 0;
2919 FieldDecl *MaxField = 0;
2920 FieldDecl *MaxSkippedField = 0;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002921 FieldDecl *LastFieldBitfield = 0;
2922
Chris Lattnerf1690852009-03-31 08:48:01 +00002923 unsigned base = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002924 if (RecFields.empty())
2925 return;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002926 if (IsUnion)
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002927 base = BytePos + GetFieldBaseOffset(OI, Layout, RecFields[0]);
Chris Lattnerf1690852009-03-31 08:48:01 +00002928 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
2929 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
2930
2931 llvm::SmallVector<FieldDecl*, 16> TmpRecFields;
2932
2933 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002934 FieldDecl *Field = RecFields[i];
2935 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002936 if (!Field->getIdentifier() || Field->isBitField()) {
2937 LastFieldBitfield = Field;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002938 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002939 }
2940 LastFieldBitfield = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002941 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00002942 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002943 if (FQT->isUnionType())
2944 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002945
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002946 const RecordType *RT = FQT->getAsRecordType();
2947 const RecordDecl *RD = RT->getDecl();
Daniel Dunbarb02532a2009-04-19 23:41:48 +00002948 // FIXME - Find a more efficient way of passing records down.
Douglas Gregor6ab35242009-04-09 21:40:53 +00002949 TmpRecFields.append(RD->field_begin(CGM.getContext()),
2950 RD->field_end(CGM.getContext()));
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00002951 const llvm::Type *Ty = CGM.getTypes().ConvertType(FQT);
2952 const llvm::StructLayout *RecLayout =
2953 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
2954
2955 BuildAggrIvarLayout(0, RecLayout, RD, TmpRecFields,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002956 BytePos + GetFieldBaseOffset(OI, Layout, Field),
Fariborz Jahanian81adc052009-04-24 16:17:09 +00002957 ForStrongLayout, HasUnion);
Chris Lattnerf1690852009-03-31 08:48:01 +00002958 TmpRecFields.clear();
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002959 continue;
2960 }
Chris Lattnerf1690852009-03-31 08:48:01 +00002961
2962 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002963 const ConstantArrayType *CArray =
2964 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002965 uint64_t ElCount = CArray->getSize().getZExtValue();
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002966 assert(CArray && "only array with know element size is supported");
2967 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00002968 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
2969 const ConstantArrayType *CArray =
2970 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002971 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00002972 FQT = CArray->getElementType();
2973 }
2974
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002975 assert(!FQT->isUnionType() &&
2976 "layout for array of unions not supported");
2977 if (FQT->isRecordType()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00002978 int OldIndex = IvarsInfo.size() - 1;
2979 int OldSkIndex = SkipIvars.size() -1;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002980
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002981 // FIXME - Use a common routine with the above!
2982 const RecordType *RT = FQT->getAsRecordType();
2983 const RecordDecl *RD = RT->getDecl();
2984 // FIXME - Find a more efficiant way of passing records down.
Douglas Gregor6ab35242009-04-09 21:40:53 +00002985 TmpRecFields.append(RD->field_begin(CGM.getContext()),
2986 RD->field_end(CGM.getContext()));
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00002987 const llvm::Type *Ty = CGM.getTypes().ConvertType(FQT);
2988 const llvm::StructLayout *RecLayout =
2989 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Chris Lattnerf1690852009-03-31 08:48:01 +00002990
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00002991 BuildAggrIvarLayout(0, RecLayout, RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00002992 TmpRecFields,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002993 BytePos + GetFieldBaseOffset(OI, Layout, Field),
Fariborz Jahanian81adc052009-04-24 16:17:09 +00002994 ForStrongLayout, HasUnion);
Chris Lattnerf1690852009-03-31 08:48:01 +00002995 TmpRecFields.clear();
2996
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002997 // Replicate layout information for each array element. Note that
2998 // one element is already done.
2999 uint64_t ElIx = 1;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003000 for (int FirstIndex = IvarsInfo.size() - 1,
3001 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003002 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003003 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3004 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3005 IvarsInfo[i].ivar_size));
3006 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3007 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3008 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003009 }
3010 continue;
3011 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003012 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003013 // At this point, we are done with Record/Union and array there of.
3014 // For other arrays we are down to its element type.
3015 QualType::GCAttrTypes GCAttr = QualType::GCNone;
3016 do {
3017 if (FQT.isObjCGCStrong() || FQT.isObjCGCWeak()) {
3018 GCAttr = FQT.isObjCGCStrong() ? QualType::Strong : QualType::Weak;
3019 break;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003020 } else if (CGM.getContext().isObjCObjectPointerType(FQT)) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003021 GCAttr = QualType::Strong;
3022 break;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003023 } else if (const PointerType *PT = FQT->getAsPointerType()) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003024 FQT = PT->getPointeeType();
Daniel Dunbar487993b2009-05-03 13:32:01 +00003025 } else {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003026 break;
3027 }
3028 } while (true);
Chris Lattnerf1690852009-03-31 08:48:01 +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 Dunbar8b2926c2009-05-03 13:44:42 +00003040 IvarsInfo.push_back(GC_IVAR(BytePos + GetFieldBaseOffset(OI, Layout,
3041 Field),
3042 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003043 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003044 } else if ((ForStrongLayout &&
3045 (GCAttr == QualType::GCNone || GCAttr == QualType::Weak))
3046 || (!ForStrongLayout && GCAttr != QualType::Weak)) {
3047 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003048 // FIXME: Why the asymmetry? We divide by word size in bits on
3049 // other side.
3050 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003051 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003052 MaxSkippedUnionIvarSize = UnionIvarSize;
3053 MaxSkippedField = Field;
3054 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003055 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003056 // FIXME: Why the asymmetry, we divide by byte size in bits here?
3057 SkipIvars.push_back(GC_IVAR(BytePos + GetFieldBaseOffset(OI, Layout,
3058 Field),
3059 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003060 }
3061 }
3062 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003063 if (LastFieldBitfield) {
3064 // Last field was a bitfield. Must update skip info.
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003065 Expr *BitWidth = LastFieldBitfield->getBitWidth();
3066 uint64_t BitFieldSize =
Eli Friedman9a901bb2009-04-26 19:19:15 +00003067 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Daniel Dunbar487993b2009-05-03 13:32:01 +00003068 GC_IVAR skivar;
3069 skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout,
3070 LastFieldBitfield);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003071 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3072 + ((BitFieldSize % ByteSizeInBits) != 0);
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003073 SkipIvars.push_back(skivar);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003074 }
3075
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003076 if (MaxField)
3077 IvarsInfo.push_back(GC_IVAR(BytePos + GetFieldBaseOffset(OI, Layout,
3078 MaxField),
3079 MaxUnionIvarSize));
3080 if (MaxSkippedField)
3081 SkipIvars.push_back(GC_IVAR(BytePos + GetFieldBaseOffset(OI, Layout,
3082 MaxSkippedField),
3083 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003084}
3085
3086/// BuildIvarLayout - Builds ivar layout bitmap for the class
3087/// implementation for the __strong or __weak case.
3088/// The layout map displays which words in ivar list must be skipped
3089/// and which must be scanned by GC (see below). String is built of bytes.
3090/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3091/// of words to skip and right nibble is count of words to scan. So, each
3092/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3093/// represented by a 0x00 byte which also ends the string.
3094/// 1. when ForStrongLayout is true, following ivars are scanned:
3095/// - id, Class
3096/// - object *
3097/// - __strong anything
3098///
3099/// 2. When ForStrongLayout is false, following ivars are scanned:
3100/// - __weak anything
3101///
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003102llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003103 const ObjCImplementationDecl *OMD,
3104 bool ForStrongLayout) {
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003105 bool hasUnion = false;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003106
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003107 unsigned int WordsToScan, WordsToSkip;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003108 const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
3109 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
3110 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003111
Chris Lattnerf1690852009-03-31 08:48:01 +00003112 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003113 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003114 CGM.getContext().CollectObjCIvars(OI, RecFields);
3115 if (RecFields.empty())
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003116 return llvm::Constant::getNullValue(PtrTy);
Chris Lattnerf1690852009-03-31 08:48:01 +00003117
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003118 SkipIvars.clear();
3119 IvarsInfo.clear();
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00003120
Daniel Dunbar84ad77a2009-04-22 09:39:34 +00003121 const llvm::StructLayout *Layout =
3122 CGM.getTargetData().getStructLayout(GetConcreteClassStruct(CGM, OI));
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003123 BuildAggrIvarLayout(OI, Layout, 0, RecFields, 0, ForStrongLayout, hasUnion);
3124 if (IvarsInfo.empty())
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003125 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003126
3127 // Sort on byte position in case we encounterred a union nested in
3128 // the ivar list.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003129 if (hasUnion && !IvarsInfo.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003130 std::sort(IvarsInfo.begin(), IvarsInfo.end());
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003131 if (hasUnion && !SkipIvars.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003132 std::sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003133
3134 // Build the string of skip/scan nibbles
Fariborz Jahanian8c2f2d12009-04-24 17:15:27 +00003135 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003136 unsigned int WordSize =
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003137 CGM.getTypes().getTargetData().getTypePaddedSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003138 if (IvarsInfo[0].ivar_bytepos == 0) {
3139 WordsToSkip = 0;
3140 WordsToScan = IvarsInfo[0].ivar_size;
3141 }
3142 else {
3143 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3144 WordsToScan = IvarsInfo[0].ivar_size;
3145 }
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003146 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++)
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003147 {
3148 unsigned int TailPrevGCObjC =
3149 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
3150 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC)
3151 {
3152 // consecutive 'scanned' object pointers.
3153 WordsToScan += IvarsInfo[i].ivar_size;
3154 }
3155 else
3156 {
3157 // Skip over 'gc'able object pointer which lay over each other.
3158 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3159 continue;
3160 // Must skip over 1 or more words. We save current skip/scan values
3161 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003162 SKIP_SCAN SkScan;
3163 SkScan.skip = WordsToSkip;
3164 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003165 SkipScanIvars.push_back(SkScan);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003166
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003167 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003168 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3169 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003170 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003171 WordsToSkip = 0;
3172 WordsToScan = IvarsInfo[i].ivar_size;
3173 }
3174 }
3175 if (WordsToScan > 0)
3176 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003177 SKIP_SCAN SkScan;
3178 SkScan.skip = WordsToSkip;
3179 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003180 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003181 }
3182
3183 bool BytesSkipped = false;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003184 if (!SkipIvars.empty())
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003185 {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003186 unsigned int LastIndex = SkipIvars.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003187 int LastByteSkipped =
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003188 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
3189 LastIndex = IvarsInfo.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003190 int LastByteScanned =
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003191 IvarsInfo[LastIndex].ivar_bytepos +
3192 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003193 BytesSkipped = (LastByteSkipped > LastByteScanned);
3194 // Compute number of bytes to skip at the tail end of the last ivar scanned.
3195 if (BytesSkipped)
3196 {
3197 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003198 SKIP_SCAN SkScan;
3199 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3200 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003201 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003202 }
3203 }
3204 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3205 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003206 int SkipScan = SkipScanIvars.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003207 for (int i = 0; i <= SkipScan; i++)
3208 {
3209 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3210 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3211 // 0xM0 followed by 0x0N detected.
3212 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3213 for (int j = i+1; j < SkipScan; j++)
3214 SkipScanIvars[j] = SkipScanIvars[j+1];
3215 --SkipScan;
3216 }
3217 }
3218
3219 // Generate the string.
3220 std::string BitMap;
3221 for (int i = 0; i <= SkipScan; i++)
3222 {
3223 unsigned char byte;
3224 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3225 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3226 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3227 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
3228
3229 if (skip_small > 0 || skip_big > 0)
3230 BytesSkipped = true;
3231 // first skip big.
3232 for (unsigned int ix = 0; ix < skip_big; ix++)
3233 BitMap += (unsigned char)(0xf0);
3234
3235 // next (skip small, scan)
3236 if (skip_small)
3237 {
3238 byte = skip_small << 4;
3239 if (scan_big > 0)
3240 {
3241 byte |= 0xf;
3242 --scan_big;
3243 }
3244 else if (scan_small)
3245 {
3246 byte |= scan_small;
3247 scan_small = 0;
3248 }
3249 BitMap += byte;
3250 }
3251 // next scan big
3252 for (unsigned int ix = 0; ix < scan_big; ix++)
3253 BitMap += (unsigned char)(0x0f);
3254 // last scan small
3255 if (scan_small)
3256 {
3257 byte = scan_small;
3258 BitMap += byte;
3259 }
3260 }
3261 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003262 unsigned char zero = 0;
3263 BitMap += zero;
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003264
3265 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
3266 printf("\n%s ivar layout for class '%s': ",
3267 ForStrongLayout ? "strong" : "weak",
3268 OMD->getClassInterface()->getNameAsCString());
3269 const unsigned char *s = (unsigned char*)BitMap.c_str();
3270 for (unsigned i = 0; i < BitMap.size(); i++)
3271 if (!(s[i] & 0xf0))
3272 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3273 else
3274 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3275 printf("\n");
3276 }
3277
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003278 // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as
3279 // final layout.
3280 if (ForStrongLayout && !BytesSkipped)
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003281 return llvm::Constant::getNullValue(PtrTy);
3282 llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3283 llvm::ConstantArray::get(BitMap.c_str()),
3284 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003285 1, true);
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003286 return getConstantGEP(Entry, 0, 0);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003287}
3288
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003289llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003290 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3291
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003292 // FIXME: Avoid std::string copying.
3293 if (!Entry)
3294 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
3295 llvm::ConstantArray::get(Sel.getAsString()),
3296 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003297 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003298
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003299 return getConstantGEP(Entry, 0, 0);
3300}
3301
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003302// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003303llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003304 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3305}
3306
3307// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003308llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003309 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
3310}
3311
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003312llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003313 std::string TypeStr;
3314 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3315
3316 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003317
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003318 if (!Entry)
3319 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
3320 llvm::ConstantArray::get(TypeStr),
3321 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003322 1, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003323
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003324 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003325}
3326
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003327llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003328 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003329 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3330 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00003331
3332 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3333
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003334 if (!Entry)
3335 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
3336 llvm::ConstantArray::get(TypeStr),
3337 "__TEXT,__cstring,cstring_literals",
3338 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00003339
3340 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003341}
3342
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003343// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003344llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003345 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
3346
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003347 if (!Entry)
3348 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
3349 llvm::ConstantArray::get(Ident->getName()),
3350 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003351 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003352
3353 return getConstantGEP(Entry, 0, 0);
3354}
3355
3356// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003357// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003358llvm::Constant *
3359 CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3360 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003361 std::string TypeStr;
3362 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003363 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3364}
3365
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003366void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
3367 const ObjCContainerDecl *CD,
3368 std::string &NameOut) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00003369 NameOut = '\01';
3370 NameOut += (D->isInstanceMethod() ? '-' : '+');
Chris Lattner077bf5e2008-11-24 03:33:13 +00003371 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003372 assert (CD && "Missing container decl in GetNameForMethod");
3373 NameOut += CD->getNameAsString();
Fariborz Jahanian1e9aef32009-04-16 18:34:20 +00003374 if (const ObjCCategoryImplDecl *CID =
3375 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) {
3376 NameOut += '(';
3377 NameOut += CID->getNameAsString();
3378 NameOut+= ')';
3379 }
Chris Lattner077bf5e2008-11-24 03:33:13 +00003380 NameOut += ' ';
3381 NameOut += D->getSelector().getAsString();
3382 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003383}
3384
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003385void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003386 EmitModuleInfo();
3387
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003388 // Emit the dummy bodies for any protocols which were referenced but
3389 // never defined.
3390 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
3391 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
3392 if (i->second->hasInitializer())
3393 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003394
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003395 std::vector<llvm::Constant*> Values(5);
3396 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
3397 Values[1] = GetClassName(i->first);
3398 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
3399 Values[3] = Values[4] =
3400 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
3401 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
3402 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
3403 Values));
3404 }
3405
3406 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003407 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003408 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003409 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003410 }
3411
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003412 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003413 llvm::GlobalValue *GV =
3414 new llvm::GlobalVariable(AT, false,
3415 llvm::GlobalValue::AppendingLinkage,
3416 llvm::ConstantArray::get(AT, Used),
3417 "llvm.used",
3418 &CGM.getModule());
3419
3420 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003421
3422 // Add assembler directives to add lazy undefined symbol references
3423 // for classes which are referenced but not defined. This is
3424 // important for correct linker interaction.
3425
3426 // FIXME: Uh, this isn't particularly portable.
3427 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00003428
3429 if (!CGM.getModule().getModuleInlineAsm().empty())
3430 s << "\n";
3431
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003432 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
3433 e = LazySymbols.end(); i != e; ++i) {
3434 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
3435 }
3436 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
3437 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003438 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003439 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
3440 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00003441
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003442 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003443}
3444
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003445CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003446 : CGObjCCommonMac(cgm),
3447 ObjCTypes(cgm)
3448{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003449 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003450 ObjCABI = 2;
3451}
3452
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003453/* *** */
3454
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003455ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
3456: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003457{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003458 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3459 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003460
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003461 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003462 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003463 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003464 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003465 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
3466
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003467 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00003468 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003469 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003470
3471 // FIXME: It would be nice to unify this with the opaque type, so
3472 // that the IR comes out a bit cleaner.
3473 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
3474 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003475
3476 // I'm not sure I like this. The implicit coordination is a bit
3477 // gross. We should solve this in a reasonable fashion because this
3478 // is a pretty common task (match some runtime data structure with
3479 // an LLVM data structure).
3480
3481 // FIXME: This is leaked.
3482 // FIXME: Merge with rewriter code?
3483
3484 // struct _objc_super {
3485 // id self;
3486 // Class cls;
3487 // }
3488 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3489 SourceLocation(),
3490 &Ctx.Idents.get("_objc_super"));
Douglas Gregor6ab35242009-04-09 21:40:53 +00003491 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3492 Ctx.getObjCIdType(), 0, false));
3493 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3494 Ctx.getObjCClassType(), 0, false));
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003495 RD->completeDefinition(Ctx);
3496
3497 SuperCTy = Ctx.getTagDeclType(RD);
3498 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
3499
3500 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003501 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
3502
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003503 // struct _prop_t {
3504 // char *name;
3505 // char *attributes;
3506 // }
Chris Lattner1c02f862009-04-22 02:53:24 +00003507 PropertyTy = llvm::StructType::get(Int8PtrTy, Int8PtrTy, NULL);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003508 CGM.getModule().addTypeName("struct._prop_t",
3509 PropertyTy);
3510
3511 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003512 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003513 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003514 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003515 // }
3516 PropertyListTy = llvm::StructType::get(IntTy,
3517 IntTy,
3518 llvm::ArrayType::get(PropertyTy, 0),
3519 NULL);
3520 CGM.getModule().addTypeName("struct._prop_list_t",
3521 PropertyListTy);
3522 // struct _prop_list_t *
3523 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
3524
3525 // struct _objc_method {
3526 // SEL _cmd;
3527 // char *method_type;
3528 // char *_imp;
3529 // }
3530 MethodTy = llvm::StructType::get(SelectorPtrTy,
3531 Int8PtrTy,
3532 Int8PtrTy,
3533 NULL);
3534 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003535
3536 // struct _objc_cache *
3537 CacheTy = llvm::OpaqueType::get();
3538 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
3539 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003540}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003541
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003542ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
3543 : ObjCCommonTypesHelper(cgm)
3544{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003545 // struct _objc_method_description {
3546 // SEL name;
3547 // char *types;
3548 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003549 MethodDescriptionTy =
3550 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003551 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003552 NULL);
3553 CGM.getModule().addTypeName("struct._objc_method_description",
3554 MethodDescriptionTy);
3555
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003556 // struct _objc_method_description_list {
3557 // int count;
3558 // struct _objc_method_description[1];
3559 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003560 MethodDescriptionListTy =
3561 llvm::StructType::get(IntTy,
3562 llvm::ArrayType::get(MethodDescriptionTy, 0),
3563 NULL);
3564 CGM.getModule().addTypeName("struct._objc_method_description_list",
3565 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003566
3567 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003568 MethodDescriptionListPtrTy =
3569 llvm::PointerType::getUnqual(MethodDescriptionListTy);
3570
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003571 // Protocol description structures
3572
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003573 // struct _objc_protocol_extension {
3574 // uint32_t size; // sizeof(struct _objc_protocol_extension)
3575 // struct _objc_method_description_list *optional_instance_methods;
3576 // struct _objc_method_description_list *optional_class_methods;
3577 // struct _objc_property_list *instance_properties;
3578 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003579 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003580 llvm::StructType::get(IntTy,
3581 MethodDescriptionListPtrTy,
3582 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003583 PropertyListPtrTy,
3584 NULL);
3585 CGM.getModule().addTypeName("struct._objc_protocol_extension",
3586 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003587
3588 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003589 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
3590
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003591 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003592
3593 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
3594 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3595
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003596 const llvm::Type *T =
3597 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
3598 LongTy,
3599 llvm::ArrayType::get(ProtocolTyHolder, 0),
3600 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003601 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
3602
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003603 // struct _objc_protocol {
3604 // struct _objc_protocol_extension *isa;
3605 // char *protocol_name;
3606 // struct _objc_protocol **_objc_protocol_list;
3607 // struct _objc_method_description_list *instance_methods;
3608 // struct _objc_method_description_list *class_methods;
3609 // }
3610 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003611 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003612 llvm::PointerType::getUnqual(ProtocolListTyHolder),
3613 MethodDescriptionListPtrTy,
3614 MethodDescriptionListPtrTy,
3615 NULL);
3616 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
3617
3618 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
3619 CGM.getModule().addTypeName("struct._objc_protocol_list",
3620 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003621 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003622 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
3623
3624 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003625 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003626 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003627
3628 // Class description structures
3629
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003630 // struct _objc_ivar {
3631 // char *ivar_name;
3632 // char *ivar_type;
3633 // int ivar_offset;
3634 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003635 IvarTy = llvm::StructType::get(Int8PtrTy,
3636 Int8PtrTy,
3637 IntTy,
3638 NULL);
3639 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
3640
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003641 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003642 IvarListTy = llvm::OpaqueType::get();
3643 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
3644 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
3645
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003646 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003647 MethodListTy = llvm::OpaqueType::get();
3648 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
3649 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
3650
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003651 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003652 ClassExtensionTy =
3653 llvm::StructType::get(IntTy,
3654 Int8PtrTy,
3655 PropertyListPtrTy,
3656 NULL);
3657 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
3658 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
3659
3660 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3661
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003662 // struct _objc_class {
3663 // Class isa;
3664 // Class super_class;
3665 // char *name;
3666 // long version;
3667 // long info;
3668 // long instance_size;
3669 // struct _objc_ivar_list *ivars;
3670 // struct _objc_method_list *methods;
3671 // struct _objc_cache *cache;
3672 // struct _objc_protocol_list *protocols;
3673 // char *ivar_layout;
3674 // struct _objc_class_ext *ext;
3675 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003676 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3677 llvm::PointerType::getUnqual(ClassTyHolder),
3678 Int8PtrTy,
3679 LongTy,
3680 LongTy,
3681 LongTy,
3682 IvarListPtrTy,
3683 MethodListPtrTy,
3684 CachePtrTy,
3685 ProtocolListPtrTy,
3686 Int8PtrTy,
3687 ClassExtensionPtrTy,
3688 NULL);
3689 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
3690
3691 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
3692 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
3693 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
3694
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003695 // struct _objc_category {
3696 // char *category_name;
3697 // char *class_name;
3698 // struct _objc_method_list *instance_method;
3699 // struct _objc_method_list *class_method;
3700 // uint32_t size; // sizeof(struct _objc_category)
3701 // struct _objc_property_list *instance_properties;// category's @property
3702 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003703 CategoryTy = llvm::StructType::get(Int8PtrTy,
3704 Int8PtrTy,
3705 MethodListPtrTy,
3706 MethodListPtrTy,
3707 ProtocolListPtrTy,
3708 IntTy,
3709 PropertyListPtrTy,
3710 NULL);
3711 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
3712
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003713 // Global metadata structures
3714
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003715 // struct _objc_symtab {
3716 // long sel_ref_cnt;
3717 // SEL *refs;
3718 // short cls_def_cnt;
3719 // short cat_def_cnt;
3720 // char *defs[cls_def_cnt + cat_def_cnt];
3721 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003722 SymtabTy = llvm::StructType::get(LongTy,
3723 SelectorPtrTy,
3724 ShortTy,
3725 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003726 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003727 NULL);
3728 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
3729 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
3730
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003731 // struct _objc_module {
3732 // long version;
3733 // long size; // sizeof(struct _objc_module)
3734 // char *name;
3735 // struct _objc_symtab* symtab;
3736 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003737 ModuleTy =
3738 llvm::StructType::get(LongTy,
3739 LongTy,
3740 Int8PtrTy,
3741 SymtabPtrTy,
3742 NULL);
3743 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003744
Anders Carlsson2abd89c2008-08-31 04:05:03 +00003745
Anders Carlsson124526b2008-09-09 10:10:21 +00003746 // FIXME: This is the size of the setjmp buffer and should be
3747 // target specific. 18 is what's used on 32-bit X86.
3748 uint64_t SetJmpBufferSize = 18;
3749
3750 // Exceptions
3751 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00003752 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00003753
3754 ExceptionDataTy =
3755 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
3756 SetJmpBufferSize),
3757 StackPtrTy, NULL);
3758 CGM.getModule().addTypeName("struct._objc_exception_data",
3759 ExceptionDataTy);
3760
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003761}
3762
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003763ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003764: ObjCCommonTypesHelper(cgm)
3765{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003766 // struct _method_list_t {
3767 // uint32_t entsize; // sizeof(struct _objc_method)
3768 // uint32_t method_count;
3769 // struct _objc_method method_list[method_count];
3770 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003771 MethodListnfABITy = llvm::StructType::get(IntTy,
3772 IntTy,
3773 llvm::ArrayType::get(MethodTy, 0),
3774 NULL);
3775 CGM.getModule().addTypeName("struct.__method_list_t",
3776 MethodListnfABITy);
3777 // struct method_list_t *
3778 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003779
3780 // struct _protocol_t {
3781 // id isa; // NULL
3782 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003783 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003784 // const struct method_list_t * const instance_methods;
3785 // const struct method_list_t * const class_methods;
3786 // const struct method_list_t *optionalInstanceMethods;
3787 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003788 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003789 // const uint32_t size; // sizeof(struct _protocol_t)
3790 // const uint32_t flags; // = 0
3791 // }
3792
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003793 // Holder for struct _protocol_list_t *
3794 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3795
3796 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3797 Int8PtrTy,
3798 llvm::PointerType::getUnqual(
3799 ProtocolListTyHolder),
3800 MethodListnfABIPtrTy,
3801 MethodListnfABIPtrTy,
3802 MethodListnfABIPtrTy,
3803 MethodListnfABIPtrTy,
3804 PropertyListPtrTy,
3805 IntTy,
3806 IntTy,
3807 NULL);
3808 CGM.getModule().addTypeName("struct._protocol_t",
3809 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003810
3811 // struct _protocol_t*
3812 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003813
Fariborz Jahanianda320092009-01-29 19:24:30 +00003814 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003815 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00003816 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003817 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003818 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3819 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003820 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003821 NULL);
3822 CGM.getModule().addTypeName("struct._objc_protocol_list",
3823 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003824 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3825 ProtocolListnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003826
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003827 // struct _objc_protocol_list*
3828 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003829
3830 // struct _ivar_t {
3831 // unsigned long int *offset; // pointer to ivar offset location
3832 // char *name;
3833 // char *type;
3834 // uint32_t alignment;
3835 // uint32_t size;
3836 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003837 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3838 Int8PtrTy,
3839 Int8PtrTy,
3840 IntTy,
3841 IntTy,
3842 NULL);
3843 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3844
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003845 // struct _ivar_list_t {
3846 // uint32 entsize; // sizeof(struct _ivar_t)
3847 // uint32 count;
3848 // struct _iver_t list[count];
3849 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003850 IvarListnfABITy = llvm::StructType::get(IntTy,
3851 IntTy,
3852 llvm::ArrayType::get(
3853 IvarnfABITy, 0),
3854 NULL);
3855 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3856
3857 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003858
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003859 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003860 // uint32_t const flags;
3861 // uint32_t const instanceStart;
3862 // uint32_t const instanceSize;
3863 // uint32_t const reserved; // only when building for 64bit targets
3864 // const uint8_t * const ivarLayout;
3865 // const char *const name;
3866 // const struct _method_list_t * const baseMethods;
3867 // const struct _objc_protocol_list *const baseProtocols;
3868 // const struct _ivar_list_t *const ivars;
3869 // const uint8_t * const weakIvarLayout;
3870 // const struct _prop_list_t * const properties;
3871 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003872
3873 // FIXME. Add 'reserved' field in 64bit abi mode!
3874 ClassRonfABITy = llvm::StructType::get(IntTy,
3875 IntTy,
3876 IntTy,
3877 Int8PtrTy,
3878 Int8PtrTy,
3879 MethodListnfABIPtrTy,
3880 ProtocolListnfABIPtrTy,
3881 IvarListnfABIPtrTy,
3882 Int8PtrTy,
3883 PropertyListPtrTy,
3884 NULL);
3885 CGM.getModule().addTypeName("struct._class_ro_t",
3886 ClassRonfABITy);
3887
3888 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3889 std::vector<const llvm::Type*> Params;
3890 Params.push_back(ObjectPtrTy);
3891 Params.push_back(SelectorPtrTy);
3892 ImpnfABITy = llvm::PointerType::getUnqual(
3893 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3894
3895 // struct _class_t {
3896 // struct _class_t *isa;
3897 // struct _class_t * const superclass;
3898 // void *cache;
3899 // IMP *vtable;
3900 // struct class_ro_t *ro;
3901 // }
3902
3903 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3904 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3905 llvm::PointerType::getUnqual(ClassTyHolder),
3906 CachePtrTy,
3907 llvm::PointerType::getUnqual(ImpnfABITy),
3908 llvm::PointerType::getUnqual(
3909 ClassRonfABITy),
3910 NULL);
3911 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3912
3913 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3914 ClassnfABITy);
3915
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003916 // LLVM for struct _class_t *
3917 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3918
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003919 // struct _category_t {
3920 // const char * const name;
3921 // struct _class_t *const cls;
3922 // const struct _method_list_t * const instance_methods;
3923 // const struct _method_list_t * const class_methods;
3924 // const struct _protocol_list_t * const protocols;
3925 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003926 // }
3927 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003928 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003929 MethodListnfABIPtrTy,
3930 MethodListnfABIPtrTy,
3931 ProtocolListnfABIPtrTy,
3932 PropertyListPtrTy,
3933 NULL);
3934 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003935
3936 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003937 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3938 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003939
3940 // MessageRefTy - LLVM for:
3941 // struct _message_ref_t {
3942 // IMP messenger;
3943 // SEL name;
3944 // };
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003945
3946 // First the clang type for struct _message_ref_t
3947 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3948 SourceLocation(),
3949 &Ctx.Idents.get("_message_ref_t"));
Douglas Gregor6ab35242009-04-09 21:40:53 +00003950 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3951 Ctx.VoidPtrTy, 0, false));
3952 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3953 Ctx.getObjCSelType(), 0, false));
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003954 RD->completeDefinition(Ctx);
3955
3956 MessageRefCTy = Ctx.getTagDeclType(RD);
3957 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
3958 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003959
3960 // MessageRefPtrTy - LLVM for struct _message_ref_t*
3961 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
3962
3963 // SuperMessageRefTy - LLVM for:
3964 // struct _super_message_ref_t {
3965 // SUPER_IMP messenger;
3966 // SEL name;
3967 // };
3968 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy,
3969 SelectorPtrTy,
3970 NULL);
3971 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
3972
3973 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
3974 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
3975
Daniel Dunbare588b992009-03-01 04:46:24 +00003976
3977 // struct objc_typeinfo {
3978 // const void** vtable; // objc_ehtype_vtable + 2
3979 // const char* name; // c++ typeinfo string
3980 // Class cls;
3981 // };
3982 EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy),
3983 Int8PtrTy,
3984 ClassnfABIPtrTy,
3985 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00003986 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Daniel Dunbare588b992009-03-01 04:46:24 +00003987 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003988}
3989
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003990llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3991 FinishNonFragileABIModule();
3992
3993 return NULL;
3994}
3995
3996void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3997 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003998
3999 // Build list of all implemented classe addresses in array
4000 // L_OBJC_LABEL_CLASS_$.
4001 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
4002 // list of 'nonlazy' implementations (defined as those with a +load{}
4003 // method!!).
4004 unsigned NumClasses = DefinedClasses.size();
4005 if (NumClasses) {
4006 std::vector<llvm::Constant*> Symbols(NumClasses);
4007 for (unsigned i=0; i<NumClasses; i++)
4008 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
4009 ObjCTypes.Int8PtrTy);
4010 llvm::Constant* Init =
4011 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
4012 NumClasses),
4013 Symbols);
4014
4015 llvm::GlobalVariable *GV =
4016 new llvm::GlobalVariable(Init->getType(), false,
4017 llvm::GlobalValue::InternalLinkage,
4018 Init,
4019 "\01L_OBJC_LABEL_CLASS_$",
4020 &CGM.getModule());
Daniel Dunbar58a29122009-03-09 22:18:41 +00004021 GV->setAlignment(8);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004022 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
4023 UsedGlobals.push_back(GV);
4024 }
4025
4026 // Build list of all implemented category addresses in array
4027 // L_OBJC_LABEL_CATEGORY_$.
4028 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
4029 // list of 'nonlazy' category implementations (defined as those with a +load{}
4030 // method!!).
4031 unsigned NumCategory = DefinedCategories.size();
4032 if (NumCategory) {
4033 std::vector<llvm::Constant*> Symbols(NumCategory);
4034 for (unsigned i=0; i<NumCategory; i++)
4035 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
4036 ObjCTypes.Int8PtrTy);
4037 llvm::Constant* Init =
4038 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
4039 NumCategory),
4040 Symbols);
4041
4042 llvm::GlobalVariable *GV =
4043 new llvm::GlobalVariable(Init->getType(), false,
4044 llvm::GlobalValue::InternalLinkage,
4045 Init,
4046 "\01L_OBJC_LABEL_CATEGORY_$",
4047 &CGM.getModule());
Daniel Dunbar58a29122009-03-09 22:18:41 +00004048 GV->setAlignment(8);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004049 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
4050 UsedGlobals.push_back(GV);
4051 }
4052
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004053 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
4054 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
4055 std::vector<llvm::Constant*> Values(2);
4056 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
Fariborz Jahanian067986e2009-02-24 21:08:09 +00004057 unsigned int flags = 0;
Fariborz Jahanian66a5c2c2009-02-24 23:34:44 +00004058 // FIXME: Fix and continue?
4059 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
4060 flags |= eImageInfo_GarbageCollected;
4061 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
4062 flags |= eImageInfo_GCOnly;
Fariborz Jahanian067986e2009-02-24 21:08:09 +00004063 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004064 llvm::Constant* Init = llvm::ConstantArray::get(
4065 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
4066 Values);
4067 llvm::GlobalVariable *IMGV =
4068 new llvm::GlobalVariable(Init->getType(), false,
4069 llvm::GlobalValue::InternalLinkage,
4070 Init,
4071 "\01L_OBJC_IMAGE_INFO",
4072 &CGM.getModule());
4073 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
Daniel Dunbar325f7582009-04-23 08:03:21 +00004074 IMGV->setConstant(true);
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004075 UsedGlobals.push_back(IMGV);
4076
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004077 std::vector<llvm::Constant*> Used;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004078
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004079 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
4080 e = UsedGlobals.end(); i != e; ++i) {
4081 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
4082 }
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004083
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004084 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
4085 llvm::GlobalValue *GV =
4086 new llvm::GlobalVariable(AT, false,
4087 llvm::GlobalValue::AppendingLinkage,
4088 llvm::ConstantArray::get(AT, Used),
4089 "llvm.used",
4090 &CGM.getModule());
4091
4092 GV->setSection("llvm.metadata");
4093
4094}
4095
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004096// Metadata flags
4097enum MetaDataDlags {
4098 CLS = 0x0,
4099 CLS_META = 0x1,
4100 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004101 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004102 CLS_EXCEPTION = 0x20
4103};
4104/// BuildClassRoTInitializer - generate meta-data for:
4105/// struct _class_ro_t {
4106/// uint32_t const flags;
4107/// uint32_t const instanceStart;
4108/// uint32_t const instanceSize;
4109/// uint32_t const reserved; // only when building for 64bit targets
4110/// const uint8_t * const ivarLayout;
4111/// const char *const name;
4112/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004113/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004114/// const struct _ivar_list_t *const ivars;
4115/// const uint8_t * const weakIvarLayout;
4116/// const struct _prop_list_t * const properties;
4117/// }
4118///
4119llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
4120 unsigned flags,
4121 unsigned InstanceStart,
4122 unsigned InstanceSize,
4123 const ObjCImplementationDecl *ID) {
4124 std::string ClassName = ID->getNameAsString();
4125 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
4126 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4127 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4128 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
4129 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004130 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4131 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004132 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004133 // const struct _method_list_t * const baseMethods;
4134 std::vector<llvm::Constant*> Methods;
4135 std::string MethodListName("\01l_OBJC_$_");
4136 if (flags & CLS_META) {
4137 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004138 for (ObjCImplementationDecl::classmeth_iterator
4139 i = ID->classmeth_begin(CGM.getContext()),
4140 e = ID->classmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004141 // Class methods should always be defined.
4142 Methods.push_back(GetMethodConstant(*i));
4143 }
4144 } else {
4145 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004146 for (ObjCImplementationDecl::instmeth_iterator
4147 i = ID->instmeth_begin(CGM.getContext()),
4148 e = ID->instmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004149 // Instance methods should always be defined.
4150 Methods.push_back(GetMethodConstant(*i));
4151 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00004152 for (ObjCImplementationDecl::propimpl_iterator
4153 i = ID->propimpl_begin(CGM.getContext()),
4154 e = ID->propimpl_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004155 ObjCPropertyImplDecl *PID = *i;
4156
4157 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4158 ObjCPropertyDecl *PD = PID->getPropertyDecl();
4159
4160 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4161 if (llvm::Constant *C = GetMethodConstant(MD))
4162 Methods.push_back(C);
4163 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4164 if (llvm::Constant *C = GetMethodConstant(MD))
4165 Methods.push_back(C);
4166 }
4167 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004168 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004169 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004170 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004171
4172 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4173 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
4174 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
4175 + OID->getNameAsString(),
4176 OID->protocol_begin(),
4177 OID->protocol_end());
4178
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004179 if (flags & CLS_META)
4180 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4181 else
4182 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004183 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4184 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004185 if (flags & CLS_META)
4186 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
4187 else
4188 Values[ 9] =
4189 EmitPropertyList(
4190 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
4191 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004192 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
4193 Values);
4194 llvm::GlobalVariable *CLASS_RO_GV =
4195 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
4196 llvm::GlobalValue::InternalLinkage,
4197 Init,
4198 (flags & CLS_META) ?
4199 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4200 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
4201 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004202 CLASS_RO_GV->setAlignment(
4203 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004204 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004205 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004206
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004207}
4208
4209/// BuildClassMetaData - This routine defines that to-level meta-data
4210/// for the given ClassName for:
4211/// struct _class_t {
4212/// struct _class_t *isa;
4213/// struct _class_t * const superclass;
4214/// void *cache;
4215/// IMP *vtable;
4216/// struct class_ro_t *ro;
4217/// }
4218///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004219llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
4220 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004221 llvm::Constant *IsAGV,
4222 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004223 llvm::Constant *ClassRoGV,
4224 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004225 std::vector<llvm::Constant*> Values(5);
4226 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004227 Values[1] = SuperClassGV
4228 ? SuperClassGV
4229 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004230 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4231 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4232 Values[4] = ClassRoGV; // &CLASS_RO_GV
4233 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
4234 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004235 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4236 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004237 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004238 GV->setAlignment(
4239 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004240 if (HiddenVisibility)
4241 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004242 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004243}
4244
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004245void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004246 uint32_t &InstanceStart,
4247 uint32_t &InstanceSize) {
Daniel Dunbar97776872009-04-22 07:32:20 +00004248 // Find first and last (non-padding) ivars in this interface.
4249
4250 // FIXME: Use iterator.
4251 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004252 GetNamedIvarList(OID->getClassInterface(), OIvars);
Daniel Dunbar97776872009-04-22 07:32:20 +00004253
4254 if (OIvars.empty()) {
4255 InstanceStart = InstanceSize = 0;
4256 return;
Daniel Dunbard4ae6c02009-04-22 04:39:47 +00004257 }
Daniel Dunbar97776872009-04-22 07:32:20 +00004258
4259 const ObjCIvarDecl *First = OIvars.front();
4260 const ObjCIvarDecl *Last = OIvars.back();
4261
4262 InstanceStart = ComputeIvarBaseOffset(CGM, OID, First);
4263 const llvm::Type *FieldTy =
4264 CGM.getTypes().ConvertTypeForMem(Last->getType());
4265 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004266// FIXME. This breaks compatibility with llvm-gcc-4.2 (but makes it compatible
4267// with gcc-4.2). We postpone this for now.
4268#if 0
4269 if (Last->isBitField()) {
4270 Expr *BitWidth = Last->getBitWidth();
4271 uint64_t BitFieldSize =
Eli Friedman9a901bb2009-04-26 19:19:15 +00004272 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004273 Size = (BitFieldSize / 8) + ((BitFieldSize % 8) != 0);
4274 }
4275#endif
Daniel Dunbar97776872009-04-22 07:32:20 +00004276 InstanceSize = ComputeIvarBaseOffset(CGM, OID, Last) + Size;
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004277}
4278
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004279void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4280 std::string ClassName = ID->getNameAsString();
4281 if (!ObjCEmptyCacheVar) {
4282 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004283 ObjCTypes.CacheTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004284 false,
4285 llvm::GlobalValue::ExternalLinkage,
4286 0,
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004287 "_objc_empty_cache",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004288 &CGM.getModule());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004289
4290 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004291 ObjCTypes.ImpnfABITy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004292 false,
4293 llvm::GlobalValue::ExternalLinkage,
4294 0,
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004295 "_objc_empty_vtable",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004296 &CGM.getModule());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004297 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004298 assert(ID->getClassInterface() &&
4299 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004300 // FIXME: Is this correct (that meta class size is never computed)?
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004301 uint32_t InstanceStart =
4302 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
4303 uint32_t InstanceSize = InstanceStart;
4304 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004305 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4306 std::string ObjCClassName(getClassSymbolPrefix());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004307
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004308 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004309
Daniel Dunbar04d40782009-04-14 06:00:08 +00004310 bool classIsHidden =
4311 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004312 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004313 flags |= OBJC2_CLS_HIDDEN;
4314 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004315 // class is root
4316 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004317 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004318 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004319 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004320 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004321 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4322 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4323 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004324 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004325 // work on super class metadata symbol.
4326 std::string SuperClassName =
4327 ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004328 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004329 }
4330 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4331 InstanceStart,
4332 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004333 std::string TClassName = ObjCMetaClassName + ClassName;
4334 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004335 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4336 classIsHidden);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004337
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004338 // Metadata for the class
4339 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004340 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004341 flags |= OBJC2_CLS_HIDDEN;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004342
4343 if (hasObjCExceptionAttribute(ID->getClassInterface()))
4344 flags |= CLS_EXCEPTION;
4345
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004346 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004347 flags |= CLS_ROOT;
4348 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00004349 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004350 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004351 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004352 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004353 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004354 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004355 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004356 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004357 InstanceStart,
4358 InstanceSize,
4359 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004360
4361 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004362 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004363 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4364 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004365 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004366
4367 // Force the definition of the EHType if necessary.
4368 if (flags & CLS_EXCEPTION)
4369 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004370}
4371
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004372/// GenerateProtocolRef - This routine is called to generate code for
4373/// a protocol reference expression; as in:
4374/// @code
4375/// @protocol(Proto1);
4376/// @endcode
4377/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4378/// which will hold address of the protocol meta-data.
4379///
4380llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
4381 const ObjCProtocolDecl *PD) {
4382
Fariborz Jahanian960cd062009-04-10 18:47:34 +00004383 // This routine is called for @protocol only. So, we must build definition
4384 // of protocol's meta-data (not a reference to it!)
4385 //
4386 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004387 ObjCTypes.ExternalProtocolPtrTy);
4388
4389 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
4390 ProtocolName += PD->getNameAsCString();
4391
4392 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
4393 if (PTGV)
4394 return Builder.CreateLoad(PTGV, false, "tmp");
4395 PTGV = new llvm::GlobalVariable(
4396 Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00004397 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004398 Init,
4399 ProtocolName,
4400 &CGM.getModule());
4401 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
4402 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4403 UsedGlobals.push_back(PTGV);
4404 return Builder.CreateLoad(PTGV, false, "tmp");
4405}
4406
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004407/// GenerateCategory - Build metadata for a category implementation.
4408/// struct _category_t {
4409/// const char * const name;
4410/// struct _class_t *const cls;
4411/// const struct _method_list_t * const instance_methods;
4412/// const struct _method_list_t * const class_methods;
4413/// const struct _protocol_list_t * const protocols;
4414/// const struct _prop_list_t * const properties;
4415/// }
4416///
4417void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
4418{
4419 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004420 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
4421 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004422 "_$_" + OCD->getNameAsString());
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004423 std::string ExtClassName(getClassSymbolPrefix() +
4424 Interface->getNameAsString());
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004425
4426 std::vector<llvm::Constant*> Values(6);
4427 Values[0] = GetClassName(OCD->getIdentifier());
4428 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004429 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004430 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004431 std::vector<llvm::Constant*> Methods;
4432 std::string MethodListName(Prefix);
4433 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
4434 "_$_" + OCD->getNameAsString();
4435
Douglas Gregor653f1b12009-04-23 01:02:12 +00004436 for (ObjCCategoryImplDecl::instmeth_iterator
4437 i = OCD->instmeth_begin(CGM.getContext()),
4438 e = OCD->instmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004439 // Instance methods should always be defined.
4440 Methods.push_back(GetMethodConstant(*i));
4441 }
4442
4443 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004444 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004445 Methods);
4446
4447 MethodListName = Prefix;
4448 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
4449 OCD->getNameAsString();
4450 Methods.clear();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004451 for (ObjCCategoryImplDecl::classmeth_iterator
4452 i = OCD->classmeth_begin(CGM.getContext()),
4453 e = OCD->classmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004454 // Class methods should always be defined.
4455 Methods.push_back(GetMethodConstant(*i));
4456 }
4457
4458 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004459 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004460 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004461 const ObjCCategoryDecl *Category =
4462 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004463 if (Category) {
4464 std::string ExtName(Interface->getNameAsString() + "_$_" +
4465 OCD->getNameAsString());
4466 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
4467 + Interface->getNameAsString() + "_$_"
4468 + Category->getNameAsString(),
4469 Category->protocol_begin(),
4470 Category->protocol_end());
4471 Values[5] =
4472 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
4473 OCD, Category, ObjCTypes);
4474 }
4475 else {
4476 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4477 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
4478 }
4479
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004480 llvm::Constant *Init =
4481 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
4482 Values);
4483 llvm::GlobalVariable *GCATV
4484 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
4485 false,
4486 llvm::GlobalValue::InternalLinkage,
4487 Init,
4488 ExtCatName,
4489 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004490 GCATV->setAlignment(
4491 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004492 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004493 UsedGlobals.push_back(GCATV);
4494 DefinedCategories.push_back(GCATV);
4495}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004496
4497/// GetMethodConstant - Return a struct objc_method constant for the
4498/// given method if it has been defined. The result is null if the
4499/// method has not been defined. The return value has type MethodPtrTy.
4500llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
4501 const ObjCMethodDecl *MD) {
4502 // FIXME: Use DenseMap::lookup
4503 llvm::Function *Fn = MethodDefinitions[MD];
4504 if (!Fn)
4505 return 0;
4506
4507 std::vector<llvm::Constant*> Method(3);
4508 Method[0] =
4509 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4510 ObjCTypes.SelectorPtrTy);
4511 Method[1] = GetMethodVarType(MD);
4512 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
4513 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
4514}
4515
4516/// EmitMethodList - Build meta-data for method declarations
4517/// struct _method_list_t {
4518/// uint32_t entsize; // sizeof(struct _objc_method)
4519/// uint32_t method_count;
4520/// struct _objc_method method_list[method_count];
4521/// }
4522///
4523llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
4524 const std::string &Name,
4525 const char *Section,
4526 const ConstantVector &Methods) {
4527 // Return null for empty list.
4528 if (Methods.empty())
4529 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
4530
4531 std::vector<llvm::Constant*> Values(3);
4532 // sizeof(struct _objc_method)
4533 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
4534 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4535 // method_count
4536 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
4537 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
4538 Methods.size());
4539 Values[2] = llvm::ConstantArray::get(AT, Methods);
4540 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4541
4542 llvm::GlobalVariable *GV =
4543 new llvm::GlobalVariable(Init->getType(), false,
4544 llvm::GlobalValue::InternalLinkage,
4545 Init,
4546 Name,
4547 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004548 GV->setAlignment(
4549 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004550 GV->setSection(Section);
4551 UsedGlobals.push_back(GV);
4552 return llvm::ConstantExpr::getBitCast(GV,
4553 ObjCTypes.MethodListnfABIPtrTy);
4554}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004555
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004556/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
4557/// the given ivar.
4558///
4559llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004560 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004561 const ObjCIvarDecl *Ivar) {
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00004562 std::string Name = "OBJC_IVAR_$_" +
Douglas Gregor6ab35242009-04-09 21:40:53 +00004563 getInterfaceDeclForIvar(ID, Ivar, CGM.getContext())->getNameAsString() +
4564 '.' + Ivar->getNameAsString();
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004565 llvm::GlobalVariable *IvarOffsetGV =
4566 CGM.getModule().getGlobalVariable(Name);
4567 if (!IvarOffsetGV)
4568 IvarOffsetGV =
4569 new llvm::GlobalVariable(ObjCTypes.LongTy,
4570 false,
4571 llvm::GlobalValue::ExternalLinkage,
4572 0,
4573 Name,
4574 &CGM.getModule());
4575 return IvarOffsetGV;
4576}
4577
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004578llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004579 const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004580 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004581 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00004582 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
4583 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
4584 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004585 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004586 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00004587
4588 // FIXME: This matches gcc, but shouldn't the visibility be set on
4589 // the use as well (i.e., in ObjCIvarOffsetVariable).
4590 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
4591 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
4592 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004593 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00004594 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00004595 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004596 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004597 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004598}
4599
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004600/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00004601/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004602/// IvarListnfABIPtrTy.
4603/// struct _ivar_t {
4604/// unsigned long int *offset; // pointer to ivar offset location
4605/// char *name;
4606/// char *type;
4607/// uint32_t alignment;
4608/// uint32_t size;
4609/// }
4610/// struct _ivar_list_t {
4611/// uint32 entsize; // sizeof(struct _ivar_t)
4612/// uint32 count;
4613/// struct _iver_t list[count];
4614/// }
4615///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004616
4617void CGObjCCommonMac::GetNamedIvarList(const ObjCInterfaceDecl *OID,
4618 llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const {
4619 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
4620 E = OID->ivar_end(); I != E; ++I) {
4621 // Ignore unnamed bit-fields.
4622 if (!(*I)->getDeclName())
4623 continue;
4624
4625 Res.push_back(*I);
4626 }
4627
4628 for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(CGM.getContext()),
4629 E = OID->prop_end(CGM.getContext()); I != E; ++I)
4630 if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl())
4631 Res.push_back(IV);
4632}
4633
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004634llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
4635 const ObjCImplementationDecl *ID) {
4636
4637 std::vector<llvm::Constant*> Ivars, Ivar(5);
4638
4639 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4640 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
4641
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004642 // FIXME. Consolidate this with similar code in GenerateClass.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00004643
Daniel Dunbar91636d62009-04-20 00:33:43 +00004644 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00004645 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004646 GetNamedIvarList(OID, OIvars);
Fariborz Jahanian99eee362009-04-01 19:37:34 +00004647
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004648 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
4649 ObjCIvarDecl *IVD = OIvars[i];
Daniel Dunbar3eec8aa2009-04-20 05:53:40 +00004650 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004651 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004652 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
4653 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004654 const llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004655 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004656 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
4657 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004658 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004659 Align = llvm::Log2_32(Align);
4660 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00004661 // NOTE. Size of a bitfield does not match gcc's, because of the
4662 // way bitfields are treated special in each. But I am told that
4663 // 'size' for bitfield ivars is ignored by the runtime so it does
4664 // not matter. If it matters, there is enough info to get the
4665 // bitfield right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004666 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4667 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
4668 }
4669 // Return null for empty list.
4670 if (Ivars.empty())
4671 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4672 std::vector<llvm::Constant*> Values(3);
4673 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
4674 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4675 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
4676 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
4677 Ivars.size());
4678 Values[2] = llvm::ConstantArray::get(AT, Ivars);
4679 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4680 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4681 llvm::GlobalVariable *GV =
4682 new llvm::GlobalVariable(Init->getType(), false,
4683 llvm::GlobalValue::InternalLinkage,
4684 Init,
4685 Prefix + OID->getNameAsString(),
4686 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004687 GV->setAlignment(
4688 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004689 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004690
4691 UsedGlobals.push_back(GV);
4692 return llvm::ConstantExpr::getBitCast(GV,
4693 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004694}
4695
4696llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4697 const ObjCProtocolDecl *PD) {
4698 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4699
4700 if (!Entry) {
4701 // We use the initializer as a marker of whether this is a forward
4702 // reference or not. At module finalization we add the empty
4703 // contents for protocols which were referenced but never defined.
4704 Entry =
4705 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
4706 llvm::GlobalValue::ExternalLinkage,
4707 0,
4708 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
4709 &CGM.getModule());
4710 Entry->setSection("__DATA,__datacoal_nt,coalesced");
4711 UsedGlobals.push_back(Entry);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004712 }
4713
4714 return Entry;
4715}
4716
4717/// GetOrEmitProtocol - Generate the protocol meta-data:
4718/// @code
4719/// struct _protocol_t {
4720/// id isa; // NULL
4721/// const char * const protocol_name;
4722/// const struct _protocol_list_t * protocol_list; // super protocols
4723/// const struct method_list_t * const instance_methods;
4724/// const struct method_list_t * const class_methods;
4725/// const struct method_list_t *optionalInstanceMethods;
4726/// const struct method_list_t *optionalClassMethods;
4727/// const struct _prop_list_t * properties;
4728/// const uint32_t size; // sizeof(struct _protocol_t)
4729/// const uint32_t flags; // = 0
4730/// }
4731/// @endcode
4732///
4733
4734llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4735 const ObjCProtocolDecl *PD) {
4736 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4737
4738 // Early exit if a defining object has already been generated.
4739 if (Entry && Entry->hasInitializer())
4740 return Entry;
4741
4742 const char *ProtocolName = PD->getNameAsCString();
4743
4744 // Construct method lists.
4745 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4746 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00004747 for (ObjCProtocolDecl::instmeth_iterator
4748 i = PD->instmeth_begin(CGM.getContext()),
4749 e = PD->instmeth_end(CGM.getContext());
4750 i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004751 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004752 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004753 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4754 OptInstanceMethods.push_back(C);
4755 } else {
4756 InstanceMethods.push_back(C);
4757 }
4758 }
4759
Douglas Gregor6ab35242009-04-09 21:40:53 +00004760 for (ObjCProtocolDecl::classmeth_iterator
4761 i = PD->classmeth_begin(CGM.getContext()),
4762 e = PD->classmeth_end(CGM.getContext());
4763 i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004764 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004765 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004766 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4767 OptClassMethods.push_back(C);
4768 } else {
4769 ClassMethods.push_back(C);
4770 }
4771 }
4772
4773 std::vector<llvm::Constant*> Values(10);
4774 // isa is NULL
4775 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
4776 Values[1] = GetClassName(PD->getIdentifier());
4777 Values[2] = EmitProtocolList(
4778 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4779 PD->protocol_begin(),
4780 PD->protocol_end());
4781
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004782 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004783 + PD->getNameAsString(),
4784 "__DATA, __objc_const",
4785 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004786 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004787 + PD->getNameAsString(),
4788 "__DATA, __objc_const",
4789 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004790 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004791 + PD->getNameAsString(),
4792 "__DATA, __objc_const",
4793 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004794 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004795 + PD->getNameAsString(),
4796 "__DATA, __objc_const",
4797 OptClassMethods);
4798 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4799 0, PD, ObjCTypes);
4800 uint32_t Size =
4801 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
4802 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4803 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
4804 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
4805 Values);
4806
4807 if (Entry) {
4808 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00004809 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004810 Entry->setInitializer(Init);
4811 } else {
4812 Entry =
4813 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004814 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004815 Init,
4816 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
4817 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004818 Entry->setAlignment(
4819 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004820 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004821 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004822 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
4823
4824 // Use this protocol meta-data to build protocol list table in section
4825 // __DATA, __objc_protolist
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004826 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004827 ObjCTypes.ProtocolnfABIPtrTy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004828 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004829 Entry,
4830 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
4831 +ProtocolName,
4832 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004833 PTGV->setAlignment(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004834 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00004835 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004836 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4837 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004838 return Entry;
4839}
4840
4841/// EmitProtocolList - Generate protocol list meta-data:
4842/// @code
4843/// struct _protocol_list_t {
4844/// long protocol_count; // Note, this is 32/64 bit
4845/// struct _protocol_t[protocol_count];
4846/// }
4847/// @endcode
4848///
4849llvm::Constant *
4850CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4851 ObjCProtocolDecl::protocol_iterator begin,
4852 ObjCProtocolDecl::protocol_iterator end) {
4853 std::vector<llvm::Constant*> ProtocolRefs;
4854
Fariborz Jahanianda320092009-01-29 19:24:30 +00004855 // Just return null for empty protocol lists
Daniel Dunbar948e2582009-02-15 07:36:20 +00004856 if (begin == end)
Fariborz Jahanianda320092009-01-29 19:24:30 +00004857 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4858
Daniel Dunbar948e2582009-02-15 07:36:20 +00004859 // FIXME: We shouldn't need to do this lookup here, should we?
Fariborz Jahanianda320092009-01-29 19:24:30 +00004860 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4861 if (GV)
Daniel Dunbar948e2582009-02-15 07:36:20 +00004862 return llvm::ConstantExpr::getBitCast(GV,
4863 ObjCTypes.ProtocolListnfABIPtrTy);
4864
4865 for (; begin != end; ++begin)
4866 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4867
Fariborz Jahanianda320092009-01-29 19:24:30 +00004868 // This list is null terminated.
4869 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004870 ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004871
4872 std::vector<llvm::Constant*> Values(2);
4873 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4874 Values[1] =
Daniel Dunbar948e2582009-02-15 07:36:20 +00004875 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004876 ProtocolRefs.size()),
4877 ProtocolRefs);
4878
4879 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4880 GV = new llvm::GlobalVariable(Init->getType(), false,
4881 llvm::GlobalValue::InternalLinkage,
4882 Init,
4883 Name,
4884 &CGM.getModule());
4885 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004886 GV->setAlignment(
4887 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004888 UsedGlobals.push_back(GV);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004889 return llvm::ConstantExpr::getBitCast(GV,
4890 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004891}
4892
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004893/// GetMethodDescriptionConstant - This routine build following meta-data:
4894/// struct _objc_method {
4895/// SEL _cmd;
4896/// char *method_type;
4897/// char *_imp;
4898/// }
4899
4900llvm::Constant *
4901CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4902 std::vector<llvm::Constant*> Desc(3);
4903 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4904 ObjCTypes.SelectorPtrTy);
4905 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004906 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004907 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4908 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4909}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004910
4911/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4912/// This code gen. amounts to generating code for:
4913/// @code
4914/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4915/// @encode
4916///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004917LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004918 CodeGen::CodeGenFunction &CGF,
4919 QualType ObjectTy,
4920 llvm::Value *BaseValue,
4921 const ObjCIvarDecl *Ivar,
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004922 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00004923 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00004924 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
4925 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004926}
4927
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004928llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
4929 CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00004930 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004931 const ObjCIvarDecl *Ivar) {
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00004932 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),
4933 false, "ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004934}
4935
Fariborz Jahanian46551122009-02-04 00:22:57 +00004936CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4937 CodeGen::CodeGenFunction &CGF,
4938 QualType ResultType,
4939 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004940 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00004941 QualType Arg0Ty,
4942 bool IsSuper,
4943 const CallArgList &CallArgs) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004944 // FIXME. Even though IsSuper is passes. This function doese not
4945 // handle calls to 'super' receivers.
4946 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004947 llvm::Value *Arg0 = Receiver;
4948 if (!IsSuper)
4949 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004950
4951 // Find the message function name.
Fariborz Jahanianef163782009-02-05 01:13:09 +00004952 // FIXME. This is too much work to get the ABI-specific result type
4953 // needed to find the message name.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004954 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4955 llvm::SmallVector<QualType, 16>());
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00004956 llvm::Constant *Fn = 0;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004957 std::string Name("\01l_");
4958 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004959#if 0
4960 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004961 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00004962 Fn = ObjCTypes.getMessageSendIdStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004963 // FIXME. Is there a better way of getting these names.
4964 // They are available in RuntimeFunctions vector pair.
4965 Name += "objc_msgSendId_stret_fixup";
4966 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004967 else
4968#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004969 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00004970 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004971 Name += "objc_msgSendSuper2_stret_fixup";
4972 }
4973 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004974 {
Chris Lattner1c02f862009-04-22 02:53:24 +00004975 Fn = ObjCTypes.getMessageSendStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004976 Name += "objc_msgSend_stret_fixup";
4977 }
4978 }
Fariborz Jahanian5b2bad02009-04-30 16:31:11 +00004979 else if (!IsSuper && ResultType->isFloatingType()) {
4980 if (const BuiltinType *BT = ResultType->getAsBuiltinType()) {
4981 BuiltinType::Kind k = BT->getKind();
4982 if (k == BuiltinType::LongDouble) {
4983 Fn = ObjCTypes.getMessageSendFpretFixupFn();
4984 Name += "objc_msgSend_fpret_fixup";
4985 }
4986 else {
4987 Fn = ObjCTypes.getMessageSendFixupFn();
4988 Name += "objc_msgSend_fixup";
4989 }
4990 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004991 }
4992 else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004993#if 0
4994// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004995 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00004996 Fn = ObjCTypes.getMessageSendIdFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004997 Name += "objc_msgSendId_fixup";
4998 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004999 else
5000#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005001 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005002 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005003 Name += "objc_msgSendSuper2_fixup";
5004 }
5005 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005006 {
Chris Lattner1c02f862009-04-22 02:53:24 +00005007 Fn = ObjCTypes.getMessageSendFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005008 Name += "objc_msgSend_fixup";
5009 }
5010 }
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005011 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005012 Name += '_';
5013 std::string SelName(Sel.getAsString());
5014 // Replace all ':' in selector name with '_' ouch!
5015 for(unsigned i = 0; i < SelName.size(); i++)
5016 if (SelName[i] == ':')
5017 SelName[i] = '_';
5018 Name += SelName;
5019 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5020 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005021 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005022 std::vector<llvm::Constant*> Values(2);
5023 Values[0] = Fn;
5024 Values[1] = GetMethodVarName(Sel);
5025 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
5026 GV = new llvm::GlobalVariable(Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005027 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005028 Init,
5029 Name,
5030 &CGM.getModule());
5031 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005032 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005033 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005034 }
5035 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005036
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005037 CallArgList ActualArgs;
5038 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
5039 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
5040 ObjCTypes.MessageRefCPtrTy));
5041 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005042 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
5043 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5044 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00005045 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005046 Callee = CGF.Builder.CreateBitCast(Callee,
5047 llvm::PointerType::getUnqual(FTy));
5048 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005049}
5050
5051/// Generate code for a message send expression in the nonfragile abi.
5052CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
5053 CodeGen::CodeGenFunction &CGF,
5054 QualType ResultType,
5055 Selector Sel,
5056 llvm::Value *Receiver,
5057 bool IsClassMessage,
5058 const CallArgList &CallArgs) {
Fariborz Jahanian46551122009-02-04 00:22:57 +00005059 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005060 Receiver, CGF.getContext().getObjCIdType(),
Fariborz Jahanian46551122009-02-04 00:22:57 +00005061 false, CallArgs);
5062}
5063
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005064llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005065CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005066 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5067
Daniel Dunbardfff2302009-03-02 05:18:14 +00005068 if (!GV) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005069 GV = new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
5070 llvm::GlobalValue::ExternalLinkage,
5071 0, Name, &CGM.getModule());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005072 }
5073
5074 return GV;
5075}
5076
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005077llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005078 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005079 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
5080
5081 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005082 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005083 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005084 Entry =
5085 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5086 llvm::GlobalValue::InternalLinkage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005087 ClassGV,
Daniel Dunbar11394522009-04-18 08:51:00 +00005088 "\01L_OBJC_CLASSLIST_REFERENCES_$_",
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005089 &CGM.getModule());
5090 Entry->setAlignment(
5091 CGM.getTargetData().getPrefTypeAlignment(
5092 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005093 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
5094 UsedGlobals.push_back(Entry);
5095 }
5096
5097 return Builder.CreateLoad(Entry, false, "tmp");
5098}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005099
Daniel Dunbar11394522009-04-18 08:51:00 +00005100llvm::Value *
5101CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
5102 const ObjCInterfaceDecl *ID) {
5103 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
5104
5105 if (!Entry) {
5106 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5107 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5108 Entry =
5109 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5110 llvm::GlobalValue::InternalLinkage,
5111 ClassGV,
5112 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
5113 &CGM.getModule());
5114 Entry->setAlignment(
5115 CGM.getTargetData().getPrefTypeAlignment(
5116 ObjCTypes.ClassnfABIPtrTy));
5117 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005118 UsedGlobals.push_back(Entry);
5119 }
5120
5121 return Builder.CreateLoad(Entry, false, "tmp");
5122}
5123
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005124/// EmitMetaClassRef - Return a Value * of the address of _class_t
5125/// meta-data
5126///
5127llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5128 const ObjCInterfaceDecl *ID) {
5129 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5130 if (Entry)
5131 return Builder.CreateLoad(Entry, false, "tmp");
5132
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005133 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005134 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005135 Entry =
5136 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5137 llvm::GlobalValue::InternalLinkage,
5138 MetaClassGV,
5139 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
5140 &CGM.getModule());
5141 Entry->setAlignment(
5142 CGM.getTargetData().getPrefTypeAlignment(
5143 ObjCTypes.ClassnfABIPtrTy));
5144
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005145 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005146 UsedGlobals.push_back(Entry);
5147
5148 return Builder.CreateLoad(Entry, false, "tmp");
5149}
5150
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005151/// GetClass - Return a reference to the class for the given interface
5152/// decl.
5153llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5154 const ObjCInterfaceDecl *ID) {
5155 return EmitClassRef(Builder, ID);
5156}
5157
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005158/// Generates a message send where the super is the receiver. This is
5159/// a message send to self with special delivery semantics indicating
5160/// which class's method should be called.
5161CodeGen::RValue
5162CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
5163 QualType ResultType,
5164 Selector Sel,
5165 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005166 bool isCategoryImpl,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005167 llvm::Value *Receiver,
5168 bool IsClassMessage,
5169 const CodeGen::CallArgList &CallArgs) {
5170 // ...
5171 // Create and init a super structure; this is a (receiver, class)
5172 // pair we will pass to objc_msgSendSuper.
5173 llvm::Value *ObjCSuper =
5174 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
5175
5176 llvm::Value *ReceiverAsObject =
5177 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5178 CGF.Builder.CreateStore(ReceiverAsObject,
5179 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
5180
5181 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005182 llvm::Value *Target;
5183 if (IsClassMessage) {
5184 if (isCategoryImpl) {
5185 // Message sent to "super' in a class method defined in
5186 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005187 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005188 Target = CGF.Builder.CreateStructGEP(Target, 0);
5189 Target = CGF.Builder.CreateLoad(Target);
5190 }
5191 else
5192 Target = EmitMetaClassRef(CGF.Builder, Class);
5193 }
5194 else
Daniel Dunbar11394522009-04-18 08:51:00 +00005195 Target = EmitSuperClassRef(CGF.Builder, Class);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005196
5197 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
5198 // and ObjCTypes types.
5199 const llvm::Type *ClassTy =
5200 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5201 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5202 CGF.Builder.CreateStore(Target,
5203 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
5204
5205 return EmitMessageSend(CGF, ResultType, Sel,
5206 ObjCSuper, ObjCTypes.SuperPtrCTy,
5207 true, CallArgs);
5208}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005209
5210llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
5211 Selector Sel) {
5212 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
5213
5214 if (!Entry) {
5215 llvm::Constant *Casted =
5216 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5217 ObjCTypes.SelectorPtrTy);
5218 Entry =
5219 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
5220 llvm::GlobalValue::InternalLinkage,
5221 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
5222 &CGM.getModule());
5223 Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip");
5224 UsedGlobals.push_back(Entry);
5225 }
5226
5227 return Builder.CreateLoad(Entry, false, "tmp");
5228}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005229/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
5230/// objc_assign_ivar (id src, id *dst)
5231///
5232void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
5233 llvm::Value *src, llvm::Value *dst)
5234{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005235 const llvm::Type * SrcTy = src->getType();
5236 if (!isa<llvm::PointerType>(SrcTy)) {
5237 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5238 assert(Size <= 8 && "does not support size > 8");
5239 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5240 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005241 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5242 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005243 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5244 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005245 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005246 src, dst, "assignivar");
5247 return;
5248}
5249
5250/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5251/// objc_assign_strongCast (id src, id *dst)
5252///
5253void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
5254 CodeGen::CodeGenFunction &CGF,
5255 llvm::Value *src, llvm::Value *dst)
5256{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005257 const llvm::Type * SrcTy = src->getType();
5258 if (!isa<llvm::PointerType>(SrcTy)) {
5259 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5260 assert(Size <= 8 && "does not support size > 8");
5261 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5262 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005263 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5264 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005265 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5266 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005267 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005268 src, dst, "weakassign");
5269 return;
5270}
5271
5272/// EmitObjCWeakRead - Code gen for loading value of a __weak
5273/// object: objc_read_weak (id *src)
5274///
5275llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
5276 CodeGen::CodeGenFunction &CGF,
5277 llvm::Value *AddrWeakObj)
5278{
Eli Friedman8339b352009-03-07 03:57:15 +00005279 const llvm::Type* DestTy =
5280 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005281 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005282 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005283 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005284 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005285 return read_weak;
5286}
5287
5288/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5289/// objc_assign_weak (id src, id *dst)
5290///
5291void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
5292 llvm::Value *src, llvm::Value *dst)
5293{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005294 const llvm::Type * SrcTy = src->getType();
5295 if (!isa<llvm::PointerType>(SrcTy)) {
5296 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5297 assert(Size <= 8 && "does not support size > 8");
5298 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5299 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005300 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5301 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005302 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5303 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005304 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005305 src, dst, "weakassign");
5306 return;
5307}
5308
5309/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5310/// objc_assign_global (id src, id *dst)
5311///
5312void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
5313 llvm::Value *src, llvm::Value *dst)
5314{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005315 const llvm::Type * SrcTy = src->getType();
5316 if (!isa<llvm::PointerType>(SrcTy)) {
5317 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5318 assert(Size <= 8 && "does not support size > 8");
5319 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5320 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005321 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5322 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005323 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5324 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005325 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005326 src, dst, "globalassign");
5327 return;
5328}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005329
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005330void
5331CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5332 const Stmt &S) {
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005333 bool isTry = isa<ObjCAtTryStmt>(S);
5334 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
5335 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005336 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005337 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005338 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005339 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
5340
5341 // For @synchronized, call objc_sync_enter(sync.expr). The
5342 // evaluation of the expression must occur before we enter the
5343 // @synchronized. We can safely avoid a temp here because jumps into
5344 // @synchronized are illegal & this will dominate uses.
5345 llvm::Value *SyncArg = 0;
5346 if (!isTry) {
5347 SyncArg =
5348 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
5349 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005350 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005351 }
5352
5353 // Push an EH context entry, used for handling rethrows and jumps
5354 // through finally.
5355 CGF.PushCleanupBlock(FinallyBlock);
5356
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005357 CGF.setInvokeDest(TryHandler);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005358
5359 CGF.EmitBlock(TryBlock);
5360 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
5361 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
5362 CGF.EmitBranchThroughCleanup(FinallyEnd);
5363
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005364 // Emit the exception handler.
5365
5366 CGF.EmitBlock(TryHandler);
5367
5368 llvm::Value *llvm_eh_exception =
5369 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
5370 llvm::Value *llvm_eh_selector_i64 =
5371 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64);
5372 llvm::Value *llvm_eh_typeid_for_i64 =
5373 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64);
5374 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5375 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow");
5376
5377 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
5378 SelectorArgs.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005379 SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005380
5381 // Construct the lists of (type, catch body) to handle.
Daniel Dunbarede8de92009-03-06 00:01:21 +00005382 llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005383 bool HasCatchAll = false;
5384 if (isTry) {
5385 if (const ObjCAtCatchStmt* CatchStmt =
5386 cast<ObjCAtTryStmt>(S).getCatchStmts()) {
5387 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005388 const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Steve Naroff7ba138a2009-03-03 19:52:17 +00005389 Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody()));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005390
5391 // catch(...) always matches.
Steve Naroff7ba138a2009-03-03 19:52:17 +00005392 if (!CatchDecl) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005393 // Use i8* null here to signal this is a catch all, not a cleanup.
5394 llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
5395 SelectorArgs.push_back(Null);
5396 HasCatchAll = true;
5397 break;
5398 }
5399
Daniel Dunbarede8de92009-03-06 00:01:21 +00005400 if (CGF.getContext().isObjCIdType(CatchDecl->getType()) ||
5401 CatchDecl->getType()->isObjCQualifiedIdType()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005402 llvm::Value *IDEHType =
5403 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5404 if (!IDEHType)
5405 IDEHType =
5406 new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5407 llvm::GlobalValue::ExternalLinkage,
5408 0, "OBJC_EHTYPE_id", &CGM.getModule());
5409 SelectorArgs.push_back(IDEHType);
5410 HasCatchAll = true;
5411 break;
5412 }
5413
5414 // All other types should be Objective-C interface pointer types.
Daniel Dunbarede8de92009-03-06 00:01:21 +00005415 const PointerType *PT = CatchDecl->getType()->getAsPointerType();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005416 assert(PT && "Invalid @catch type.");
5417 const ObjCInterfaceType *IT =
5418 PT->getPointeeType()->getAsObjCInterfaceType();
5419 assert(IT && "Invalid @catch type.");
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005420 llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005421 SelectorArgs.push_back(EHType);
5422 }
5423 }
5424 }
5425
5426 // We use a cleanup unless there was already a catch all.
5427 if (!HasCatchAll) {
5428 SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
Daniel Dunbarede8de92009-03-06 00:01:21 +00005429 Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005430 }
5431
5432 llvm::Value *Selector =
5433 CGF.Builder.CreateCall(llvm_eh_selector_i64,
5434 SelectorArgs.begin(), SelectorArgs.end(),
5435 "selector");
5436 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005437 const ParmVarDecl *CatchParam = Handlers[i].first;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005438 const Stmt *CatchBody = Handlers[i].second;
5439
5440 llvm::BasicBlock *Next = 0;
5441
5442 // The last handler always matches.
5443 if (i + 1 != e) {
5444 assert(CatchParam && "Only last handler can be a catch all.");
5445
5446 llvm::BasicBlock *Match = CGF.createBasicBlock("match");
5447 Next = CGF.createBasicBlock("catch.next");
5448 llvm::Value *Id =
5449 CGF.Builder.CreateCall(llvm_eh_typeid_for_i64,
5450 CGF.Builder.CreateBitCast(SelectorArgs[i+2],
5451 ObjCTypes.Int8PtrTy));
5452 CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id),
5453 Match, Next);
5454
5455 CGF.EmitBlock(Match);
5456 }
5457
5458 if (CatchBody) {
5459 llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end");
5460 llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler");
5461
5462 // Cleanups must call objc_end_catch.
5463 //
5464 // FIXME: It seems incorrect for objc_begin_catch to be inside
5465 // this context, but this matches gcc.
5466 CGF.PushCleanupBlock(MatchEnd);
5467 CGF.setInvokeDest(MatchHandler);
5468
5469 llvm::Value *ExcObject =
Chris Lattner8a569112009-04-22 02:15:23 +00005470 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005471
5472 // Bind the catch parameter if it exists.
5473 if (CatchParam) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005474 ExcObject =
5475 CGF.Builder.CreateBitCast(ExcObject,
5476 CGF.ConvertType(CatchParam->getType()));
5477 // CatchParam is a ParmVarDecl because of the grammar
5478 // construction used to handle this, but for codegen purposes
5479 // we treat this as a local decl.
5480 CGF.EmitLocalBlockVarDecl(*CatchParam);
5481 CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005482 }
5483
5484 CGF.ObjCEHValueStack.push_back(ExcObject);
5485 CGF.EmitStmt(CatchBody);
5486 CGF.ObjCEHValueStack.pop_back();
5487
5488 CGF.EmitBranchThroughCleanup(FinallyEnd);
5489
5490 CGF.EmitBlock(MatchHandler);
5491
5492 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5493 // We are required to emit this call to satisfy LLVM, even
5494 // though we don't use the result.
5495 llvm::SmallVector<llvm::Value*, 8> Args;
5496 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005497 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005498 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
5499 0));
5500 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5501 CGF.Builder.CreateStore(Exc, RethrowPtr);
5502 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5503
5504 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5505
5506 CGF.EmitBlock(MatchEnd);
5507
5508 // Unfortunately, we also have to generate another EH frame here
5509 // in case this throws.
5510 llvm::BasicBlock *MatchEndHandler =
5511 CGF.createBasicBlock("match.end.handler");
5512 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattner8a569112009-04-22 02:15:23 +00005513 CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005514 Cont, MatchEndHandler,
5515 Args.begin(), Args.begin());
5516
5517 CGF.EmitBlock(Cont);
5518 if (Info.SwitchBlock)
5519 CGF.EmitBlock(Info.SwitchBlock);
5520 if (Info.EndBlock)
5521 CGF.EmitBlock(Info.EndBlock);
5522
5523 CGF.EmitBlock(MatchEndHandler);
5524 Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5525 // We are required to emit this call to satisfy LLVM, even
5526 // though we don't use the result.
5527 Args.clear();
5528 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005529 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005530 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
5531 0));
5532 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5533 CGF.Builder.CreateStore(Exc, RethrowPtr);
5534 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5535
5536 if (Next)
5537 CGF.EmitBlock(Next);
5538 } else {
5539 assert(!Next && "catchup should be last handler.");
5540
5541 CGF.Builder.CreateStore(Exc, RethrowPtr);
5542 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5543 }
5544 }
5545
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005546 // Pop the cleanup entry, the @finally is outside this cleanup
5547 // scope.
5548 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5549 CGF.setInvokeDest(PrevLandingPad);
5550
5551 CGF.EmitBlock(FinallyBlock);
5552
5553 if (isTry) {
5554 if (const ObjCAtFinallyStmt* FinallyStmt =
5555 cast<ObjCAtTryStmt>(S).getFinallyStmt())
5556 CGF.EmitStmt(FinallyStmt->getFinallyBody());
5557 } else {
5558 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
5559 // @synchronized.
Chris Lattnerbbccd612009-04-22 02:38:11 +00005560 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005561 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005562
5563 if (Info.SwitchBlock)
5564 CGF.EmitBlock(Info.SwitchBlock);
5565 if (Info.EndBlock)
5566 CGF.EmitBlock(Info.EndBlock);
5567
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005568 // Branch around the rethrow code.
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005569 CGF.EmitBranch(FinallyEnd);
5570
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005571 CGF.EmitBlock(FinallyRethrow);
Chris Lattner8a569112009-04-22 02:15:23 +00005572 CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005573 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005574 CGF.Builder.CreateUnreachable();
5575
5576 CGF.EmitBlock(FinallyEnd);
5577}
5578
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005579/// EmitThrowStmt - Generate code for a throw statement.
5580void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
5581 const ObjCAtThrowStmt &S) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005582 llvm::Value *Exception;
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005583 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005584 Exception = CGF.EmitScalarExpr(ThrowExpr);
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005585 } else {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005586 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
5587 "Unexpected rethrow outside @catch block.");
5588 Exception = CGF.ObjCEHValueStack.back();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005589 }
5590
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005591 llvm::Value *ExceptionAsObject =
5592 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
5593 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
5594 if (InvokeDest) {
5595 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattnerbbccd612009-04-22 02:38:11 +00005596 CGF.Builder.CreateInvoke(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005597 Cont, InvokeDest,
5598 &ExceptionAsObject, &ExceptionAsObject + 1);
5599 CGF.EmitBlock(Cont);
5600 } else
Chris Lattnerbbccd612009-04-22 02:38:11 +00005601 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005602 CGF.Builder.CreateUnreachable();
5603
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005604 // Clear the insertion point to indicate we are in unreachable code.
5605 CGF.Builder.ClearInsertionPoint();
5606}
Daniel Dunbare588b992009-03-01 04:46:24 +00005607
5608llvm::Value *
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005609CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
5610 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00005611 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00005612
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005613 // If we don't need a definition, return the entry if found or check
5614 // if we use an external reference.
5615 if (!ForDefinition) {
5616 if (Entry)
5617 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00005618
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005619 // If this type (or a super class) has the __objc_exception__
5620 // attribute, emit an external reference.
5621 if (hasObjCExceptionAttribute(ID))
5622 return Entry =
5623 new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5624 llvm::GlobalValue::ExternalLinkage,
5625 0,
5626 (std::string("OBJC_EHTYPE_$_") +
5627 ID->getIdentifier()->getName()),
5628 &CGM.getModule());
5629 }
5630
5631 // Otherwise we need to either make a new entry or fill in the
5632 // initializer.
5633 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005634 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00005635 std::string VTableName = "objc_ehtype_vtable";
5636 llvm::GlobalVariable *VTableGV =
5637 CGM.getModule().getGlobalVariable(VTableName);
5638 if (!VTableGV)
5639 VTableGV = new llvm::GlobalVariable(ObjCTypes.Int8PtrTy, false,
5640 llvm::GlobalValue::ExternalLinkage,
5641 0, VTableName, &CGM.getModule());
5642
5643 llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2);
5644
5645 std::vector<llvm::Constant*> Values(3);
5646 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
5647 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005648 Values[2] = GetClassGlobal(ClassName);
Daniel Dunbare588b992009-03-01 04:46:24 +00005649 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
5650
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005651 if (Entry) {
5652 Entry->setInitializer(Init);
5653 } else {
5654 Entry = new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5655 llvm::GlobalValue::WeakAnyLinkage,
5656 Init,
5657 (std::string("OBJC_EHTYPE_$_") +
5658 ID->getIdentifier()->getName()),
5659 &CGM.getModule());
5660 }
5661
Daniel Dunbar04d40782009-04-14 06:00:08 +00005662 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005663 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005664 Entry->setAlignment(8);
5665
5666 if (ForDefinition) {
5667 Entry->setSection("__DATA,__objc_const");
5668 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
5669 } else {
5670 Entry->setSection("__DATA,__datacoal_nt,coalesced");
5671 }
Daniel Dunbare588b992009-03-01 04:46:24 +00005672
5673 return Entry;
5674}
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005675
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005676/* *** */
5677
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005678CodeGen::CGObjCRuntime *
5679CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00005680 return new CGObjCMac(CGM);
5681}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005682
5683CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005684CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005685 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005686}