blob: ecaff8ac02fcf2ce068191c432d80f16ca640cf1 [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:
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000712 unsigned int ivar_bytepos;
713 unsigned int ivar_size;
714 GC_IVAR() : ivar_bytepos(0), ivar_size(0) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000715
716 // Allow sorting based on byte pos.
717 bool operator<(const GC_IVAR &b) const {
718 return ivar_bytepos < b.ivar_bytepos;
719 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000720 };
721
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000722 class SKIP_SCAN {
723 public:
724 unsigned int skip;
725 unsigned int scan;
726 SKIP_SCAN() : skip(0), scan(0) {}
727 };
728
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000729protected:
730 CodeGen::CodeGenModule &CGM;
731 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000732 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000733
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000734 // gc ivar layout bitmap calculation helper caches.
735 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
736 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000737
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000738 /// LazySymbols - Symbols to generate a lazy reference for. See
739 /// DefinedSymbols and FinishModule().
740 std::set<IdentifierInfo*> LazySymbols;
741
742 /// DefinedSymbols - External symbols which are defined by this
743 /// module. The symbols in this list and LazySymbols are used to add
744 /// special linker symbols which ensure that Objective-C modules are
745 /// linked properly.
746 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000747
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000748 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000749 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000750
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000751 /// MethodVarNames - uniqued method variable names.
752 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000753
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000754 /// MethodVarTypes - uniqued method type signatures. We have to use
755 /// a StringMap here because have no other unique reference.
756 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000757
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000758 /// MethodDefinitions - map of methods which have been defined in
759 /// this translation unit.
760 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000761
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000762 /// PropertyNames - uniqued method variable names.
763 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000764
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000765 /// ClassReferences - uniqued class references.
766 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000767
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000768 /// SelectorReferences - uniqued selector references.
769 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000770
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000771 /// Protocols - Protocols for which an objc_protocol structure has
772 /// been emitted. Forward declarations are handled by creating an
773 /// empty structure whose initializer is filled in when/if defined.
774 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000775
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000776 /// DefinedProtocols - Protocols which have actually been
777 /// defined. We should not need this, see FIXME in GenerateProtocol.
778 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000779
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000780 /// DefinedClasses - List of defined classes.
781 std::vector<llvm::GlobalValue*> DefinedClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000782
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000783 /// DefinedCategories - List of defined categories.
784 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000785
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000786 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000787 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000788 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000789
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000790 /// GetNameForMethod - Return a name for the given method.
791 /// \param[out] NameOut - The return value.
792 void GetNameForMethod(const ObjCMethodDecl *OMD,
793 const ObjCContainerDecl *CD,
794 std::string &NameOut);
795
796 /// GetMethodVarName - Return a unique constant for the given
797 /// selector's name. The return value has type char *.
798 llvm::Constant *GetMethodVarName(Selector Sel);
799 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
800 llvm::Constant *GetMethodVarName(const std::string &Name);
801
802 /// GetMethodVarType - Return a unique constant for the given
803 /// selector's name. The return value has type char *.
804
805 // FIXME: This is a horrible name.
806 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000807 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000808
809 /// GetPropertyName - Return a unique constant for the given
810 /// name. The return value has type char *.
811 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
812
813 // FIXME: This can be dropped once string functions are unified.
814 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
815 const Decl *Container);
816
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000817 /// GetClassName - Return a unique constant for the given selector's
818 /// name. The return value has type char *.
819 llvm::Constant *GetClassName(IdentifierInfo *Ident);
820
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000821 /// BuildIvarLayout - Builds ivar layout bitmap for the class
822 /// implementation for the __strong or __weak case.
823 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000824 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
825 bool ForStrongLayout);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000826
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000827 void BuildAggrIvarLayout(const ObjCInterfaceDecl *OI,
828 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000829 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +0000830 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000831 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000832 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000833
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000834 /// GetIvarLayoutName - Returns a unique constant for the given
835 /// ivar layout bitmap.
836 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
837 const ObjCCommonTypesHelper &ObjCTypes);
838
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000839 /// EmitPropertyList - Emit the given property list. The return
840 /// value has type PropertyListPtrTy.
841 llvm::Constant *EmitPropertyList(const std::string &Name,
842 const Decl *Container,
843 const ObjCContainerDecl *OCD,
844 const ObjCCommonTypesHelper &ObjCTypes);
845
Fariborz Jahanianda320092009-01-29 19:24:30 +0000846 /// GetProtocolRef - Return a reference to the internal protocol
847 /// description, creating an empty one if it has not been
848 /// defined. The return value has type ProtocolPtrTy.
849 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000850
Chris Lattnercd0ee142009-03-31 08:33:16 +0000851 /// GetFieldBaseOffset - return's field byte offset.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000852 uint64_t GetFieldBaseOffset(const ObjCInterfaceDecl *OI,
853 const llvm::StructLayout *Layout,
Chris Lattnercd0ee142009-03-31 08:33:16 +0000854 const FieldDecl *Field);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000855
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000856 /// CreateMetadataVar - Create a global variable with internal
857 /// linkage for use by the Objective-C runtime.
858 ///
859 /// This is a convenience wrapper which not only creates the
860 /// variable, but also sets the section and alignment and adds the
861 /// global to the UsedGlobals list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000862 ///
863 /// \param Name - The variable name.
864 /// \param Init - The variable initializer; this is also used to
865 /// define the type of the variable.
866 /// \param Section - The section the variable should go into, or 0.
867 /// \param Align - The alignment for the variable, or 0.
868 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000869 /// "llvm.used".
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000870 llvm::GlobalVariable *CreateMetadataVar(const std::string &Name,
871 llvm::Constant *Init,
872 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000873 unsigned Align,
874 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000875
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000876 /// GetNamedIvarList - Return the list of ivars in the interface
877 /// itself (not including super classes and not including unnamed
878 /// bitfields).
879 ///
880 /// For the non-fragile ABI, this also includes synthesized property
881 /// ivars.
882 void GetNamedIvarList(const ObjCInterfaceDecl *OID,
883 llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const;
884
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000885public:
886 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
887 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000888
Steve Naroff33fdb732009-03-31 16:53:37 +0000889 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000890
891 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
892 const ObjCContainerDecl *CD=0);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000893
894 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
895
896 /// GetOrEmitProtocol - Get the protocol object for the given
897 /// declaration, emitting it if necessary. The return value has type
898 /// ProtocolPtrTy.
899 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
900
901 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
902 /// object for the given declaration, emitting it if needed. These
903 /// forward references will be filled in with empty bodies if no
904 /// definition is seen. The return value has type ProtocolPtrTy.
905 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000906};
907
908class CGObjCMac : public CGObjCCommonMac {
909private:
910 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000911 /// EmitImageInfo - Emit the image info marker used to encode some module
912 /// level information.
913 void EmitImageInfo();
914
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000915 /// EmitModuleInfo - Another marker encoding module level
916 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000917 void EmitModuleInfo();
918
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000919 /// EmitModuleSymols - Emit module symbols, the list of defined
920 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000921 llvm::Constant *EmitModuleSymbols();
922
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000923 /// FinishModule - Write out global data structures at the end of
924 /// processing a translation unit.
925 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000926
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000927 /// EmitClassExtension - Generate the class extension structure used
928 /// to store the weak ivar layout and properties. The return value
929 /// has type ClassExtensionPtrTy.
930 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
931
932 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
933 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000934 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000935 const ObjCInterfaceDecl *ID);
936
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000937 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000938 QualType ResultType,
939 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000940 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000941 QualType Arg0Ty,
942 bool IsSuper,
943 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000944
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000945 /// EmitIvarList - Emit the ivar list for the given
946 /// implementation. If ForClass is true the list of class ivars
947 /// (i.e. metaclass ivars) is emitted, otherwise the list of
948 /// interface ivars will be emitted. The return value has type
949 /// IvarListPtrTy.
950 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000951 bool ForClass);
952
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000953 /// EmitMetaClass - Emit a forward reference to the class structure
954 /// for the metaclass of the given interface. The return value has
955 /// type ClassPtrTy.
956 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
957
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000958 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000959 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000960 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
961 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000962 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000963
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000964 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000965
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000966 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000967
968 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000969 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000970 llvm::Constant *EmitMethodList(const std::string &Name,
971 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000972 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000973
974 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000975 /// method declarations.
976 /// - TypeName: The name for the type containing the methods.
977 /// - IsProtocol: True iff these methods are for a protocol.
978 /// - ClassMethds: True iff these are class methods.
979 /// - Required: When true, only "required" methods are
980 /// listed. Similarly, when false only "optional" methods are
981 /// listed. For classes this should always be true.
982 /// - begin, end: The method list to output.
983 ///
984 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000985 llvm::Constant *EmitMethodDescList(const std::string &Name,
986 const char *Section,
987 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000988
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000989 /// GetOrEmitProtocol - Get the protocol object for the given
990 /// declaration, emitting it if necessary. The return value has type
991 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000992 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000993
994 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
995 /// object for the given declaration, emitting it if needed. These
996 /// forward references will be filled in with empty bodies if no
997 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000998 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000999
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001000 /// EmitProtocolExtension - Generate the protocol extension
1001 /// structure used to store optional instance and class methods, and
1002 /// protocol properties. The return value has type
1003 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001004 llvm::Constant *
1005 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1006 const ConstantVector &OptInstanceMethods,
1007 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001008
1009 /// EmitProtocolList - Generate the list of referenced
1010 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +00001011 llvm::Constant *EmitProtocolList(const std::string &Name,
1012 ObjCProtocolDecl::protocol_iterator begin,
1013 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001014
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001015 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1016 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001017 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001018
Fariborz Jahanianda320092009-01-29 19:24:30 +00001019 public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001020 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001021
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001022 virtual llvm::Function *ModuleInitFunction();
1023
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001024 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001025 QualType ResultType,
1026 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001027 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001028 bool IsClassMessage,
1029 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001030
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001031 virtual CodeGen::RValue
1032 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001033 QualType ResultType,
1034 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001035 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001036 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001037 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001038 bool IsClassMessage,
1039 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001040
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001041 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001042 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001043
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001044 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001045
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001046 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001047
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001048 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001049
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001050 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001051 const ObjCProtocolDecl *PD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00001052
Chris Lattner74391b42009-03-22 21:03:39 +00001053 virtual llvm::Constant *GetPropertyGetFunction();
1054 virtual llvm::Constant *GetPropertySetFunction();
1055 virtual llvm::Constant *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001056
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001057 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1058 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001059 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1060 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001061 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001062 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001063 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1064 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001065 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1066 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001067 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1068 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001069 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1070 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001071
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001072 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1073 QualType ObjectTy,
1074 llvm::Value *BaseValue,
1075 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001076 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001077 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001078 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001079 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001080};
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001081
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001082class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001083private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001084 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001085 llvm::GlobalVariable* ObjCEmptyCacheVar;
1086 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001087
Daniel Dunbar11394522009-04-18 08:51:00 +00001088 /// SuperClassReferences - uniqued super class references.
1089 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
1090
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001091 /// MetaClassReferences - uniqued meta class references.
1092 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001093
1094 /// EHTypeReferences - uniqued class ehtype references.
1095 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001096
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001097 /// FinishNonFragileABIModule - Write out global data structures at the end of
1098 /// processing a translation unit.
1099 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001100
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00001101 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1102 unsigned InstanceStart,
1103 unsigned InstanceSize,
1104 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001105 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
1106 llvm::Constant *IsAGV,
1107 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001108 llvm::Constant *ClassRoGV,
1109 bool HiddenVisibility);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001110
1111 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
1112
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001113 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
1114
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001115 /// EmitMethodList - Emit the method list for the given
1116 /// implementation. The return value has type MethodListnfABITy.
1117 llvm::Constant *EmitMethodList(const std::string &Name,
1118 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001119 const ConstantVector &Methods);
1120 /// EmitIvarList - Emit the ivar list for the given
1121 /// implementation. If ForClass is true the list of class ivars
1122 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1123 /// interface ivars will be emitted. The return value has type
1124 /// IvarListnfABIPtrTy.
1125 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00001126
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001127 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001128 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001129 unsigned long int offset);
1130
Fariborz Jahanianda320092009-01-29 19:24:30 +00001131 /// GetOrEmitProtocol - Get the protocol object for the given
1132 /// declaration, emitting it if necessary. The return value has type
1133 /// ProtocolPtrTy.
1134 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
1135
1136 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1137 /// object for the given declaration, emitting it if needed. These
1138 /// forward references will be filled in with empty bodies if no
1139 /// definition is seen. The return value has type ProtocolPtrTy.
1140 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
1141
1142 /// EmitProtocolList - Generate the list of referenced
1143 /// protocols. The return value has type ProtocolListPtrTy.
1144 llvm::Constant *EmitProtocolList(const std::string &Name,
1145 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001146 ObjCProtocolDecl::protocol_iterator end);
1147
1148 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1149 QualType ResultType,
1150 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00001151 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001152 QualType Arg0Ty,
1153 bool IsSuper,
1154 const CallArgList &CallArgs);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001155
1156 /// GetClassGlobal - Return the global variable for the Objective-C
1157 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001158 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
1159
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001160 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001161 /// for the given class reference.
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001162 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001163 const ObjCInterfaceDecl *ID);
1164
1165 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1166 /// for the given super class reference.
1167 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1168 const ObjCInterfaceDecl *ID);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001169
1170 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1171 /// meta-data
1172 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
1173 const ObjCInterfaceDecl *ID);
1174
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001175 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1176 /// the given ivar.
1177 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001178 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00001179 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001180 const ObjCIvarDecl *Ivar);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001181
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001182 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1183 /// for the given selector.
1184 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbare588b992009-03-01 04:46:24 +00001185
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001186 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001187 /// interface. The return value has type EHTypePtrTy.
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001188 llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
1189 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001190
1191 const char *getMetaclassSymbolPrefix() const {
1192 return "OBJC_METACLASS_$_";
1193 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001194
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001195 const char *getClassSymbolPrefix() const {
1196 return "OBJC_CLASS_$_";
1197 }
1198
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001199 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001200 uint32_t &InstanceStart,
1201 uint32_t &InstanceSize);
1202
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001203public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001204 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001205 // FIXME. All stubs for now!
1206 virtual llvm::Function *ModuleInitFunction();
1207
1208 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
1209 QualType ResultType,
1210 Selector Sel,
1211 llvm::Value *Receiver,
1212 bool IsClassMessage,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001213 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001214
1215 virtual CodeGen::RValue
1216 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
1217 QualType ResultType,
1218 Selector Sel,
1219 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001220 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001221 llvm::Value *Receiver,
1222 bool IsClassMessage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001223 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001224
1225 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001226 const ObjCInterfaceDecl *ID);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001227
1228 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001229 { return EmitSelector(Builder, Sel); }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001230
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001231 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001232
1233 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001234 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001235 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001236
Chris Lattner74391b42009-03-22 21:03:39 +00001237 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001238 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001239 }
Chris Lattner74391b42009-03-22 21:03:39 +00001240 virtual llvm::Constant *GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001241 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001242 }
Chris Lattner74391b42009-03-22 21:03:39 +00001243 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001244 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001245 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001246
1247 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00001248 const Stmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001249 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001250 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001251 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001252 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001253 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001254 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001255 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001256 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001257 virtual void EmitObjCIvarAssign(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 EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001260 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001261 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1262 QualType ObjectTy,
1263 llvm::Value *BaseValue,
1264 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001265 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001266 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001267 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001268 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001269};
1270
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001271} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001272
1273/* *** Helper Functions *** */
1274
1275/// getConstantGEP() - Help routine to construct simple GEPs.
1276static llvm::Constant *getConstantGEP(llvm::Constant *C,
1277 unsigned idx0,
1278 unsigned idx1) {
1279 llvm::Value *Idxs[] = {
1280 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
1281 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
1282 };
1283 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
1284}
1285
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001286/// hasObjCExceptionAttribute - Return true if this class or any super
1287/// class has the __objc_exception__ attribute.
1288static bool hasObjCExceptionAttribute(const ObjCInterfaceDecl *OID) {
Daniel Dunbarb11fa0d2009-04-13 21:08:27 +00001289 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001290 return true;
1291 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
1292 return hasObjCExceptionAttribute(Super);
1293 return false;
1294}
1295
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001296/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001297
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001298CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
1299 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001300{
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001301 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001302 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001303}
1304
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001305/// GetClass - Return a reference to the class for the given interface
1306/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001307llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001308 const ObjCInterfaceDecl *ID) {
1309 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001310}
1311
1312/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001313llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001314 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001315}
1316
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001317/// Generate a constant CFString object.
1318/*
1319 struct __builtin_CFString {
1320 const int *isa; // point to __CFConstantStringClassReference
1321 int flags;
1322 const char *str;
1323 long length;
1324 };
1325*/
1326
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001327llvm::Constant *CGObjCCommonMac::GenerateConstantString(
Steve Naroff33fdb732009-03-31 16:53:37 +00001328 const ObjCStringLiteral *SL) {
Steve Naroff8d4141f2009-04-01 13:55:36 +00001329 return CGM.GetAddrOfConstantCFString(SL->getString());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001330}
1331
1332/// Generates a message send where the super is the receiver. This is
1333/// a message send to self with special delivery semantics indicating
1334/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001335CodeGen::RValue
1336CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001337 QualType ResultType,
1338 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001339 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001340 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001341 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001342 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001343 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001344 // Create and init a super structure; this is a (receiver, class)
1345 // pair we will pass to objc_msgSendSuper.
1346 llvm::Value *ObjCSuper =
1347 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
1348 llvm::Value *ReceiverAsObject =
1349 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
1350 CGF.Builder.CreateStore(ReceiverAsObject,
1351 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001352
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001353 // If this is a class message the metaclass is passed as the target.
1354 llvm::Value *Target;
1355 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001356 if (isCategoryImpl) {
1357 // Message sent to 'super' in a class method defined in a category
1358 // implementation requires an odd treatment.
1359 // If we are in a class method, we must retrieve the
1360 // _metaclass_ for the current class, pointed at by
1361 // the class's "isa" pointer. The following assumes that
1362 // isa" is the first ivar in a class (which it must be).
1363 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1364 Target = CGF.Builder.CreateStructGEP(Target, 0);
1365 Target = CGF.Builder.CreateLoad(Target);
1366 }
1367 else {
1368 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1369 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1370 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1371 Target = Super;
1372 }
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001373 } else {
1374 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1375 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001376 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
1377 // and ObjCTypes types.
1378 const llvm::Type *ClassTy =
1379 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001380 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001381 CGF.Builder.CreateStore(Target,
1382 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
1383
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001384 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001385 ObjCSuper, ObjCTypes.SuperPtrCTy,
1386 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001387}
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001388
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001389/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001390CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001391 QualType ResultType,
1392 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001393 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001394 bool IsClassMessage,
1395 const CallArgList &CallArgs) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001396 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001397 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001398 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001399}
1400
1401CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001402 QualType ResultType,
1403 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001404 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001405 QualType Arg0Ty,
1406 bool IsSuper,
1407 const CallArgList &CallArgs) {
1408 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001409 if (!IsSuper)
1410 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001411 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
1412 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
1413 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001414 CGF.getContext().getObjCSelType()));
1415 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001416
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001417 CodeGenTypes &Types = CGM.getTypes();
1418 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
Fariborz Jahanian65257ca2009-04-29 22:47:27 +00001419 // FIXME. vararg flag must be true when this API is used for 64bit code gen.
1420 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001421
1422 llvm::Constant *Fn;
Daniel Dunbar88b53962009-02-02 22:03:45 +00001423 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Daniel Dunbar5669e572008-10-17 03:24:53 +00001424 Fn = ObjCTypes.getSendStretFn(IsSuper);
1425 } else if (ResultType->isFloatingType()) {
1426 // FIXME: Sadly, this is wrong. This actually depends on the
1427 // architecture. This happens to be right for x86-32 though.
1428 Fn = ObjCTypes.getSendFpretFn(IsSuper);
1429 } else {
1430 Fn = ObjCTypes.getSendFn(IsSuper);
1431 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +00001432 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +00001433 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001434}
1435
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001436llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001437 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001438 // FIXME: I don't understand why gcc generates this, or where it is
1439 // resolved. Investigate. Its also wasteful to look this up over and
1440 // over.
1441 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1442
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001443 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
1444 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001445}
1446
Fariborz Jahanianda320092009-01-29 19:24:30 +00001447void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001448 // FIXME: We shouldn't need this, the protocol decl should contain
1449 // enough information to tell us whether this was a declaration or a
1450 // definition.
1451 DefinedProtocols.insert(PD->getIdentifier());
1452
1453 // If we have generated a forward reference to this protocol, emit
1454 // it now. Otherwise do nothing, the protocol objects are lazily
1455 // emitted.
1456 if (Protocols.count(PD->getIdentifier()))
1457 GetOrEmitProtocol(PD);
1458}
1459
Fariborz Jahanianda320092009-01-29 19:24:30 +00001460llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001461 if (DefinedProtocols.count(PD->getIdentifier()))
1462 return GetOrEmitProtocol(PD);
1463 return GetOrEmitProtocolRef(PD);
1464}
1465
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001466/*
1467 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1468 struct _objc_protocol {
1469 struct _objc_protocol_extension *isa;
1470 char *protocol_name;
1471 struct _objc_protocol_list *protocol_list;
1472 struct _objc__method_prototype_list *instance_methods;
1473 struct _objc__method_prototype_list *class_methods
1474 };
1475
1476 See EmitProtocolExtension().
1477*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001478llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1479 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1480
1481 // Early exit if a defining object has already been generated.
1482 if (Entry && Entry->hasInitializer())
1483 return Entry;
1484
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001485 // FIXME: I don't understand why gcc generates this, or where it is
1486 // resolved. Investigate. Its also wasteful to look this up over and
1487 // over.
1488 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1489
Chris Lattner8ec03f52008-11-24 03:54:41 +00001490 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001491
1492 // Construct method lists.
1493 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1494 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00001495 for (ObjCProtocolDecl::instmeth_iterator
1496 i = PD->instmeth_begin(CGM.getContext()),
1497 e = PD->instmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001498 ObjCMethodDecl *MD = *i;
1499 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1500 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1501 OptInstanceMethods.push_back(C);
1502 } else {
1503 InstanceMethods.push_back(C);
1504 }
1505 }
1506
Douglas Gregor6ab35242009-04-09 21:40:53 +00001507 for (ObjCProtocolDecl::classmeth_iterator
1508 i = PD->classmeth_begin(CGM.getContext()),
1509 e = PD->classmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001510 ObjCMethodDecl *MD = *i;
1511 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1512 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1513 OptClassMethods.push_back(C);
1514 } else {
1515 ClassMethods.push_back(C);
1516 }
1517 }
1518
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001519 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001520 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001521 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001522 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001523 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001524 PD->protocol_begin(),
1525 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001526 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001527 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
1528 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001529 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1530 InstanceMethods);
1531 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001532 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
1533 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001534 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1535 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001536 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
1537 Values);
1538
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001539 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001540 // Already created, fix the linkage and update the initializer.
1541 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001542 Entry->setInitializer(Init);
1543 } else {
1544 Entry =
1545 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
1546 llvm::GlobalValue::InternalLinkage,
1547 Init,
1548 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
1549 &CGM.getModule());
1550 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001551 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001552 UsedGlobals.push_back(Entry);
1553 // FIXME: Is this necessary? Why only for protocol?
1554 Entry->setAlignment(4);
1555 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001556
1557 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001558}
1559
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001560llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001561 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1562
1563 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001564 // We use the initializer as a marker of whether this is a forward
1565 // reference or not. At module finalization we add the empty
1566 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001567 Entry =
1568 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001569 llvm::GlobalValue::ExternalLinkage,
1570 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001571 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001572 &CGM.getModule());
1573 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001574 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001575 UsedGlobals.push_back(Entry);
1576 // FIXME: Is this necessary? Why only for protocol?
1577 Entry->setAlignment(4);
1578 }
1579
1580 return Entry;
1581}
1582
1583/*
1584 struct _objc_protocol_extension {
1585 uint32_t size;
1586 struct objc_method_description_list *optional_instance_methods;
1587 struct objc_method_description_list *optional_class_methods;
1588 struct objc_property_list *instance_properties;
1589 };
1590*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001591llvm::Constant *
1592CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1593 const ConstantVector &OptInstanceMethods,
1594 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001595 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001596 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001597 std::vector<llvm::Constant*> Values(4);
1598 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001599 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001600 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1601 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001602 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1603 OptInstanceMethods);
1604 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001605 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1606 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001607 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1608 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001609 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1610 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001611 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001612
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001613 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001614 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1615 Values[3]->isNullValue())
1616 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1617
1618 llvm::Constant *Init =
1619 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001620
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001621 // No special section, but goes in llvm.used
1622 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
1623 Init,
1624 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001625}
1626
1627/*
1628 struct objc_protocol_list {
1629 struct objc_protocol_list *next;
1630 long count;
1631 Protocol *list[];
1632 };
1633*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001634llvm::Constant *
1635CGObjCMac::EmitProtocolList(const std::string &Name,
1636 ObjCProtocolDecl::protocol_iterator begin,
1637 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001638 std::vector<llvm::Constant*> ProtocolRefs;
1639
Daniel Dunbardbc933702008-08-21 21:57:41 +00001640 for (; begin != end; ++begin)
1641 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001642
1643 // Just return null for empty protocol lists
1644 if (ProtocolRefs.empty())
1645 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1646
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001647 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001648 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1649
1650 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001651 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001652 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1653 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1654 Values[2] =
1655 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1656 ProtocolRefs.size()),
1657 ProtocolRefs);
1658
1659 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1660 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001661 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001662 4, false);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001663 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1664}
1665
1666/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001667 struct _objc_property {
1668 const char * const name;
1669 const char * const attributes;
1670 };
1671
1672 struct _objc_property_list {
1673 uint32_t entsize; // sizeof (struct _objc_property)
1674 uint32_t prop_count;
1675 struct _objc_property[prop_count];
1676 };
1677*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001678llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1679 const Decl *Container,
1680 const ObjCContainerDecl *OCD,
1681 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001682 std::vector<llvm::Constant*> Properties, Prop(2);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001683 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(CGM.getContext()),
1684 E = OCD->prop_end(CGM.getContext()); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00001685 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001686 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001687 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001688 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1689 Prop));
1690 }
1691
1692 // Return null for empty list.
1693 if (Properties.empty())
1694 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1695
1696 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001697 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001698 std::vector<llvm::Constant*> Values(3);
1699 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1700 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1701 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1702 Properties.size());
1703 Values[2] = llvm::ConstantArray::get(AT, Properties);
1704 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1705
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001706 llvm::GlobalVariable *GV =
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001707 CreateMetadataVar(Name, Init,
1708 (ObjCABI == 2) ? "__DATA, __objc_const" :
1709 "__OBJC,__property,regular,no_dead_strip",
1710 (ObjCABI == 2) ? 8 : 4,
1711 true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001712 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001713}
1714
1715/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001716 struct objc_method_description_list {
1717 int count;
1718 struct objc_method_description list[];
1719 };
1720*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001721llvm::Constant *
1722CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1723 std::vector<llvm::Constant*> Desc(2);
1724 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1725 ObjCTypes.SelectorPtrTy);
1726 Desc[1] = GetMethodVarType(MD);
1727 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1728 Desc);
1729}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001730
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001731llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1732 const char *Section,
1733 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001734 // Return null for empty list.
1735 if (Methods.empty())
1736 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1737
1738 std::vector<llvm::Constant*> Values(2);
1739 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1740 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1741 Methods.size());
1742 Values[1] = llvm::ConstantArray::get(AT, Methods);
1743 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1744
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001745 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001746 return llvm::ConstantExpr::getBitCast(GV,
1747 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001748}
1749
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001750/*
1751 struct _objc_category {
1752 char *category_name;
1753 char *class_name;
1754 struct _objc_method_list *instance_methods;
1755 struct _objc_method_list *class_methods;
1756 struct _objc_protocol_list *protocols;
1757 uint32_t size; // <rdar://4585769>
1758 struct _objc_property_list *instance_properties;
1759 };
1760 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001761void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001762 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001763
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001764 // FIXME: This is poor design, the OCD should have a pointer to the
1765 // category decl. Additionally, note that Category can be null for
1766 // the @implementation w/o an @interface case. Sema should just
1767 // create one for us as it does for @implementation so everyone else
1768 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001769 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001770 const ObjCCategoryDecl *Category =
1771 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001772 std::string ExtName(Interface->getNameAsString() + "_" +
1773 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001774
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001775 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001776 for (ObjCCategoryImplDecl::instmeth_iterator
1777 i = OCD->instmeth_begin(CGM.getContext()),
1778 e = OCD->instmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001779 // Instance methods should always be defined.
1780 InstanceMethods.push_back(GetMethodConstant(*i));
1781 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001782 for (ObjCCategoryImplDecl::classmeth_iterator
1783 i = OCD->classmeth_begin(CGM.getContext()),
1784 e = OCD->classmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001785 // Class methods should always be defined.
1786 ClassMethods.push_back(GetMethodConstant(*i));
1787 }
1788
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001789 std::vector<llvm::Constant*> Values(7);
1790 Values[0] = GetClassName(OCD->getIdentifier());
1791 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00001792 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001793 Values[2] =
1794 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1795 ExtName,
1796 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001797 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001798 Values[3] =
1799 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001800 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001801 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001802 if (Category) {
1803 Values[4] =
1804 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1805 Category->protocol_begin(),
1806 Category->protocol_end());
1807 } else {
1808 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1809 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001810 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001811
1812 // If there is no category @interface then there can be no properties.
1813 if (Category) {
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001814 Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001815 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001816 } else {
1817 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1818 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001819
1820 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1821 Values);
1822
1823 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001824 CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init,
1825 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001826 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001827 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001828}
1829
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001830// FIXME: Get from somewhere?
1831enum ClassFlags {
1832 eClassFlags_Factory = 0x00001,
1833 eClassFlags_Meta = 0x00002,
1834 // <rdr://5142207>
1835 eClassFlags_HasCXXStructors = 0x02000,
1836 eClassFlags_Hidden = 0x20000,
1837 eClassFlags_ABI2_Hidden = 0x00010,
1838 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1839};
1840
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001841/*
1842 struct _objc_class {
1843 Class isa;
1844 Class super_class;
1845 const char *name;
1846 long version;
1847 long info;
1848 long instance_size;
1849 struct _objc_ivar_list *ivars;
1850 struct _objc_method_list *methods;
1851 struct _objc_cache *cache;
1852 struct _objc_protocol_list *protocols;
1853 // Objective-C 1.0 extensions (<rdr://4585769>)
1854 const char *ivar_layout;
1855 struct _objc_class_ext *ext;
1856 };
1857
1858 See EmitClassExtension();
1859 */
1860void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001861 DefinedSymbols.insert(ID->getIdentifier());
1862
Chris Lattner8ec03f52008-11-24 03:54:41 +00001863 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001864 // FIXME: Gross
1865 ObjCInterfaceDecl *Interface =
1866 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001867 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001868 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001869 Interface->protocol_begin(),
1870 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001871 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar2bebbf02009-05-03 10:46:44 +00001872 unsigned Size =
1873 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001874
1875 // FIXME: Set CXX-structors flag.
Daniel Dunbar04d40782009-04-14 06:00:08 +00001876 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001877 Flags |= eClassFlags_Hidden;
1878
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001879 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001880 for (ObjCImplementationDecl::instmeth_iterator
1881 i = ID->instmeth_begin(CGM.getContext()),
1882 e = ID->instmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001883 // Instance methods should always be defined.
1884 InstanceMethods.push_back(GetMethodConstant(*i));
1885 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001886 for (ObjCImplementationDecl::classmeth_iterator
1887 i = ID->classmeth_begin(CGM.getContext()),
1888 e = ID->classmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001889 // Class methods should always be defined.
1890 ClassMethods.push_back(GetMethodConstant(*i));
1891 }
1892
Douglas Gregor653f1b12009-04-23 01:02:12 +00001893 for (ObjCImplementationDecl::propimpl_iterator
1894 i = ID->propimpl_begin(CGM.getContext()),
1895 e = ID->propimpl_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001896 ObjCPropertyImplDecl *PID = *i;
1897
1898 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1899 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1900
1901 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1902 if (llvm::Constant *C = GetMethodConstant(MD))
1903 InstanceMethods.push_back(C);
1904 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1905 if (llvm::Constant *C = GetMethodConstant(MD))
1906 InstanceMethods.push_back(C);
1907 }
1908 }
1909
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001910 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar5384b092009-05-03 08:56:52 +00001911 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001912 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001913 // Record a reference to the super class.
1914 LazySymbols.insert(Super->getIdentifier());
1915
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001916 Values[ 1] =
1917 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1918 ObjCTypes.ClassPtrTy);
1919 } else {
1920 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1921 }
1922 Values[ 2] = GetClassName(ID->getIdentifier());
1923 // Version is always 0.
1924 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1925 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1926 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001927 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001928 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001929 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001930 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001931 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001932 // cache is always NULL.
1933 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1934 Values[ 9] = Protocols;
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00001935 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001936 Values[11] = EmitClassExtension(ID);
1937 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1938 Values);
1939
1940 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001941 CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init,
1942 "__OBJC,__class,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001943 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001944 DefinedClasses.push_back(GV);
1945}
1946
1947llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1948 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001949 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001950 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001951 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001952
Daniel Dunbar04d40782009-04-14 06:00:08 +00001953 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001954 Flags |= eClassFlags_Hidden;
1955
1956 std::vector<llvm::Constant*> Values(12);
1957 // The isa for the metaclass is the root of the hierarchy.
1958 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1959 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1960 Root = Super;
1961 Values[ 0] =
1962 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1963 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001964 // The super class for the metaclass is emitted as the name of the
1965 // super class. The runtime fixes this up to point to the
1966 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001967 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1968 Values[ 1] =
1969 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1970 ObjCTypes.ClassPtrTy);
1971 } else {
1972 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1973 }
1974 Values[ 2] = GetClassName(ID->getIdentifier());
1975 // Version is always 0.
1976 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1977 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1978 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001979 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001980 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001981 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001982 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001983 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001984 // cache is always NULL.
1985 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1986 Values[ 9] = Protocols;
1987 // ivar_layout for metaclass is always NULL.
1988 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1989 // The class extension is always unused for metaclasses.
1990 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1991 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1992 Values);
1993
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001994 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001995 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001996
1997 // Check for a forward reference.
1998 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1999 if (GV) {
2000 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2001 "Forward metaclass reference has incorrect type.");
2002 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2003 GV->setInitializer(Init);
2004 } else {
2005 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
2006 llvm::GlobalValue::InternalLinkage,
2007 Init, Name,
2008 &CGM.getModule());
2009 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002010 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002011 GV->setAlignment(4);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002012 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002013
2014 return GV;
2015}
2016
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002017llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002018 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002019
2020 // FIXME: Should we look these up somewhere other than the
2021 // module. Its a bit silly since we only generate these while
2022 // processing an implementation, so exactly one pointer would work
2023 // if know when we entered/exitted an implementation block.
2024
2025 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002026 // Previously, metaclass with internal linkage may have been defined.
2027 // pass 'true' as 2nd argument so it is returned.
2028 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002029 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2030 "Forward metaclass reference has incorrect type.");
2031 return GV;
2032 } else {
2033 // Generate as an external reference to keep a consistent
2034 // module. This will be patched up when we emit the metaclass.
2035 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
2036 llvm::GlobalValue::ExternalLinkage,
2037 0,
2038 Name,
2039 &CGM.getModule());
2040 }
2041}
2042
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002043/*
2044 struct objc_class_ext {
2045 uint32_t size;
2046 const char *weak_ivar_layout;
2047 struct _objc_property_list *properties;
2048 };
2049*/
2050llvm::Constant *
2051CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
2052 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002053 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002054
2055 std::vector<llvm::Constant*> Values(3);
2056 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002057 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002058 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002059 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002060
2061 // Return null if no extension bits are used.
2062 if (Values[1]->isNullValue() && Values[2]->isNullValue())
2063 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
2064
2065 llvm::Constant *Init =
2066 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002067 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002068 Init, "__OBJC,__class_ext,regular,no_dead_strip",
2069 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002070}
2071
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00002072/// getInterfaceDeclForIvar - Get the interface declaration node where
2073/// this ivar is declared in.
2074/// FIXME. Ideally, this info should be in the ivar node. But currently
2075/// it is not and prevailing wisdom is that ASTs should not have more
2076/// info than is absolutely needed, even though this info reflects the
2077/// source language.
2078///
2079static const ObjCInterfaceDecl *getInterfaceDeclForIvar(
2080 const ObjCInterfaceDecl *OI,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002081 const ObjCIvarDecl *IVD,
2082 ASTContext &Context) {
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00002083 if (!OI)
2084 return 0;
2085 assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface");
2086 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
2087 E = OI->ivar_end(); I != E; ++I)
2088 if ((*I)->getIdentifier() == IVD->getIdentifier())
2089 return OI;
Fariborz Jahanian5a4b4532009-03-31 17:00:52 +00002090 // look into properties.
Douglas Gregor6ab35242009-04-09 21:40:53 +00002091 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(Context),
2092 E = OI->prop_end(Context); I != E; ++I) {
Fariborz Jahanian5a4b4532009-03-31 17:00:52 +00002093 ObjCPropertyDecl *PDecl = (*I);
2094 if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl())
2095 if (IV->getIdentifier() == IVD->getIdentifier())
2096 return OI;
2097 }
Douglas Gregor6ab35242009-04-09 21:40:53 +00002098 return getInterfaceDeclForIvar(OI->getSuperClass(), IVD, Context);
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00002099}
2100
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002101/*
2102 struct objc_ivar {
2103 char *ivar_name;
2104 char *ivar_type;
2105 int ivar_offset;
2106 };
2107
2108 struct objc_ivar_list {
2109 int ivar_count;
2110 struct objc_ivar list[count];
2111 };
2112 */
2113llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002114 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002115 std::vector<llvm::Constant*> Ivars, Ivar(3);
2116
2117 // When emitting the root class GCC emits ivar entries for the
2118 // actual class structure. It is not clear if we need to follow this
2119 // behavior; for now lets try and get away with not doing it. If so,
2120 // the cleanest solution would be to make up an ObjCInterfaceDecl
2121 // for the class.
2122 if (ForClass)
2123 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002124
2125 ObjCInterfaceDecl *OID =
2126 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002127
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002128 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
2129 GetNamedIvarList(OID, OIvars);
2130
2131 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2132 ObjCIvarDecl *IVD = OIvars[i];
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002133 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2134 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00002135 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002136 ComputeIvarBaseOffset(CGM, OID, IVD));
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002137 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002138 }
2139
2140 // Return null for empty list.
2141 if (Ivars.empty())
2142 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
2143
2144 std::vector<llvm::Constant*> Values(2);
2145 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
2146 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
2147 Ivars.size());
2148 Values[1] = llvm::ConstantArray::get(AT, Ivars);
2149 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2150
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002151 llvm::GlobalVariable *GV;
2152 if (ForClass)
2153 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(),
Daniel Dunbar58a29122009-03-09 22:18:41 +00002154 Init, "__OBJC,__class_vars,regular,no_dead_strip",
2155 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002156 else
2157 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_"
2158 + ID->getNameAsString(),
2159 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002160 4, true);
2161 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002162}
2163
2164/*
2165 struct objc_method {
2166 SEL method_name;
2167 char *method_types;
2168 void *method;
2169 };
2170
2171 struct objc_method_list {
2172 struct objc_method_list *obsolete;
2173 int count;
2174 struct objc_method methods_list[count];
2175 };
2176*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002177
2178/// GetMethodConstant - Return a struct objc_method constant for the
2179/// given method if it has been defined. The result is null if the
2180/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002181llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002182 // FIXME: Use DenseMap::lookup
2183 llvm::Function *Fn = MethodDefinitions[MD];
2184 if (!Fn)
2185 return 0;
2186
2187 std::vector<llvm::Constant*> Method(3);
2188 Method[0] =
2189 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2190 ObjCTypes.SelectorPtrTy);
2191 Method[1] = GetMethodVarType(MD);
2192 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
2193 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
2194}
2195
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002196llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
2197 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002198 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002199 // Return null for empty list.
2200 if (Methods.empty())
2201 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
2202
2203 std::vector<llvm::Constant*> Values(3);
2204 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
2205 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
2206 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
2207 Methods.size());
2208 Values[2] = llvm::ConstantArray::get(AT, Methods);
2209 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2210
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002211 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002212 return llvm::ConstantExpr::getBitCast(GV,
2213 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002214}
2215
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002216llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00002217 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002218 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002219 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002220
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002221 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002222 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002223 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002224 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002225 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002226 llvm::GlobalValue::InternalLinkage,
2227 Name,
2228 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002229 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002230
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002231 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002232}
2233
Daniel Dunbar48fa0642009-04-19 02:03:42 +00002234/// GetFieldBaseOffset - return the field's byte offset.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002235uint64_t CGObjCCommonMac::GetFieldBaseOffset(const ObjCInterfaceDecl *OI,
2236 const llvm::StructLayout *Layout,
Chris Lattnercd0ee142009-03-31 08:33:16 +00002237 const FieldDecl *Field) {
Daniel Dunbar97776872009-04-22 07:32:20 +00002238 // Is this a C struct?
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00002239 if (!OI)
2240 return Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
Daniel Dunbar97776872009-04-22 07:32:20 +00002241 return ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002242}
2243
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002244llvm::GlobalVariable *
2245CGObjCCommonMac::CreateMetadataVar(const std::string &Name,
2246 llvm::Constant *Init,
2247 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002248 unsigned Align,
2249 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002250 const llvm::Type *Ty = Init->getType();
2251 llvm::GlobalVariable *GV =
2252 new llvm::GlobalVariable(Ty, false,
2253 llvm::GlobalValue::InternalLinkage,
2254 Init,
2255 Name,
2256 &CGM.getModule());
2257 if (Section)
2258 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002259 if (Align)
2260 GV->setAlignment(Align);
2261 if (AddToUsed)
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002262 UsedGlobals.push_back(GV);
2263 return GV;
2264}
2265
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002266llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002267 // Abuse this interface function as a place to finalize.
2268 FinishModule();
2269
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002270 return NULL;
2271}
2272
Chris Lattner74391b42009-03-22 21:03:39 +00002273llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002274 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002275}
2276
Chris Lattner74391b42009-03-22 21:03:39 +00002277llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002278 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002279}
2280
Chris Lattner74391b42009-03-22 21:03:39 +00002281llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002282 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002283}
2284
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002285/*
2286
2287Objective-C setjmp-longjmp (sjlj) Exception Handling
2288--
2289
2290The basic framework for a @try-catch-finally is as follows:
2291{
2292 objc_exception_data d;
2293 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002294 bool _call_try_exit = true;
2295
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002296 objc_exception_try_enter(&d);
2297 if (!setjmp(d.jmp_buf)) {
2298 ... try body ...
2299 } else {
2300 // exception path
2301 id _caught = objc_exception_extract(&d);
2302
2303 // enter new try scope for handlers
2304 if (!setjmp(d.jmp_buf)) {
2305 ... match exception and execute catch blocks ...
2306
2307 // fell off end, rethrow.
2308 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00002309 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002310 } else {
2311 // exception in catch block
2312 _rethrow = objc_exception_extract(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002313 _call_try_exit = false;
2314 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002315 }
2316 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002317 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002318
2319finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002320 if (_call_try_exit)
2321 objc_exception_try_exit(&d);
2322
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002323 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002324 ... dispatch to finally destination ...
2325
2326finally_rethrow:
2327 objc_exception_throw(_rethrow);
2328
2329finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002330}
2331
2332This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00002333uses _rethrow to determine if objc_exception_try_exit should be called
2334and if the object should be rethrown. This breaks in the face of
2335throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002336
2337We specialize this framework for a few particular circumstances:
2338
2339 - If there are no catch blocks, then we avoid emitting the second
2340 exception handling context.
2341
2342 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2343 e)) we avoid emitting the code to rethrow an uncaught exception.
2344
2345 - FIXME: If there is no @finally block we can do a few more
2346 simplifications.
2347
2348Rethrows and Jumps-Through-Finally
2349--
2350
2351Support for implicit rethrows and jumping through the finally block is
2352handled by storing the current exception-handling context in
2353ObjCEHStack.
2354
Daniel Dunbar898d5082008-09-30 01:06:03 +00002355In order to implement proper @finally semantics, we support one basic
2356mechanism for jumping through the finally block to an arbitrary
2357destination. Constructs which generate exits from a @try or @catch
2358block use this mechanism to implement the proper semantics by chaining
2359jumps, as necessary.
2360
2361This mechanism works like the one used for indirect goto: we
2362arbitrarily assign an ID to each destination and store the ID for the
2363destination in a variable prior to entering the finally block. At the
2364end of the finally block we simply create a switch to the proper
2365destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002366
2367Code gen for @synchronized(expr) stmt;
2368Effectively generating code for:
2369objc_sync_enter(expr);
2370@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002371*/
2372
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002373void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2374 const Stmt &S) {
2375 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002376 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00002377 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002378 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar55e87422008-11-11 02:29:29 +00002379 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
2380 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
2381 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar1c566672009-02-24 01:43:46 +00002382
2383 // For @synchronized, call objc_sync_enter(sync.expr). The
2384 // evaluation of the expression must occur before we enter the
2385 // @synchronized. We can safely avoid a temp here because jumps into
2386 // @synchronized are illegal & this will dominate uses.
2387 llvm::Value *SyncArg = 0;
2388 if (!isTry) {
2389 SyncArg =
2390 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2391 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00002392 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002393 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002394
2395 // Push an EH context entry, used for handling rethrows and jumps
2396 // through finally.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002397 CGF.PushCleanupBlock(FinallyBlock);
2398
Anders Carlsson273558f2009-02-07 21:37:21 +00002399 CGF.ObjCEHValueStack.push_back(0);
2400
Daniel Dunbar898d5082008-09-30 01:06:03 +00002401 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00002402 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2403 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002404 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
2405 "_rethrow");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002406 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
2407 "_call_try_exit");
2408 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr);
2409
Anders Carlsson80f25672008-09-09 17:59:25 +00002410 // Enter a new try block and call setjmp.
Chris Lattner34b02a12009-04-22 02:26:14 +00002411 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Anders Carlsson80f25672008-09-09 17:59:25 +00002412 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
2413 "jmpbufarray");
2414 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
Chris Lattner34b02a12009-04-22 02:26:14 +00002415 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlsson80f25672008-09-09 17:59:25 +00002416 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00002417
Daniel Dunbar55e87422008-11-11 02:29:29 +00002418 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2419 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002420 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002421 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002422
2423 // Emit the @try block.
2424 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002425 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
2426 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002427 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002428
2429 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002430 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002431
2432 // Retrieve the exception object. We may emit multiple blocks but
2433 // nothing can cross this so the value is already in SSA form.
Chris Lattner34b02a12009-04-22 02:26:14 +00002434 llvm::Value *Caught =
2435 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2436 ExceptionData, "caught");
Anders Carlsson273558f2009-02-07 21:37:21 +00002437 CGF.ObjCEHValueStack.back() = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002438 if (!isTry)
2439 {
2440 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002441 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002442 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002443 }
2444 else if (const ObjCAtCatchStmt* CatchStmt =
2445 cast<ObjCAtTryStmt>(S).getCatchStmts())
2446 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00002447 // Enter a new exception try block (in case a @catch block throws
2448 // an exception).
Chris Lattner34b02a12009-04-22 02:26:14 +00002449 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002450
Chris Lattner34b02a12009-04-22 02:26:14 +00002451 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlsson80f25672008-09-09 17:59:25 +00002452 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002453 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00002454
Daniel Dunbar55e87422008-11-11 02:29:29 +00002455 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
2456 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002457 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002458
2459 CGF.EmitBlock(CatchBlock);
2460
Daniel Dunbar55e40722008-09-27 07:03:52 +00002461 // Handle catch list. As a special case we check if everything is
2462 // matched and avoid generating code for falling off the end if
2463 // so.
2464 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00002465 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00002466 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00002467
Steve Naroff7ba138a2009-03-03 19:52:17 +00002468 const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Daniel Dunbar129271a2008-09-27 07:36:24 +00002469 const PointerType *PT = 0;
2470
Anders Carlsson80f25672008-09-09 17:59:25 +00002471 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00002472 if (!CatchParam) {
2473 AllMatched = true;
2474 } else {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002475 PT = CatchParam->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002476
Daniel Dunbar97f61d12008-09-27 22:21:14 +00002477 // catch(id e) always matches.
2478 // FIXME: For the time being we also match id<X>; this should
2479 // be rejected by Sema instead.
Steve Naroff389bf462009-02-12 17:52:19 +00002480 if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) ||
Steve Naroff7ba138a2009-03-03 19:52:17 +00002481 CatchParam->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00002482 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00002483 }
2484
Daniel Dunbar55e40722008-09-27 07:03:52 +00002485 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00002486 if (CatchParam) {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002487 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002488 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002489 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002490 }
Anders Carlsson1452f552008-09-11 08:21:54 +00002491
Anders Carlssondde0a942008-09-11 09:15:33 +00002492 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002493 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002494 break;
2495 }
2496
Daniel Dunbar129271a2008-09-27 07:36:24 +00002497 assert(PT && "Unexpected non-pointer type in @catch");
2498 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00002499 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002500 assert(ObjCType && "Catch parameter must have Objective-C type!");
2501
2502 // Check if the @catch block matches the exception object.
2503 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
2504
Chris Lattner34b02a12009-04-22 02:26:14 +00002505 llvm::Value *Match =
2506 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
2507 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00002508
Daniel Dunbar55e87422008-11-11 02:29:29 +00002509 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00002510
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002511 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002512 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002513
2514 // Emit the @catch block.
2515 CGF.EmitBlock(MatchedBlock);
Steve Naroff7ba138a2009-03-03 19:52:17 +00002516 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002517 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002518
2519 llvm::Value *Tmp =
Steve Naroff7ba138a2009-03-03 19:52:17 +00002520 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002521 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002522 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002523
2524 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002525 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002526
2527 CGF.EmitBlock(NextCatchBlock);
2528 }
2529
Daniel Dunbar55e40722008-09-27 07:03:52 +00002530 if (!AllMatched) {
2531 // None of the handlers caught the exception, so store it to be
2532 // rethrown at the end of the @finally block.
2533 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002534 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002535 }
2536
2537 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002538 CGF.EmitBlock(CatchHandler);
Chris Lattner34b02a12009-04-22 02:26:14 +00002539 CGF.Builder.CreateStore(
2540 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2541 ExceptionData),
Daniel Dunbar55e40722008-09-27 07:03:52 +00002542 RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002543 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002544 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002545 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00002546 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002547 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002548 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson80f25672008-09-09 17:59:25 +00002549 }
2550
Daniel Dunbar898d5082008-09-30 01:06:03 +00002551 // Pop the exception-handling stack entry. It is important to do
2552 // this now, because the code in the @finally block is not in this
2553 // context.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002554 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2555
Anders Carlsson273558f2009-02-07 21:37:21 +00002556 CGF.ObjCEHValueStack.pop_back();
2557
Anders Carlsson80f25672008-09-09 17:59:25 +00002558 // Emit the @finally block.
2559 CGF.EmitBlock(FinallyBlock);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002560 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2561
2562 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2563
2564 CGF.EmitBlock(FinallyExit);
Chris Lattner34b02a12009-04-22 02:26:14 +00002565 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002566
2567 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002568 if (isTry) {
2569 if (const ObjCAtFinallyStmt* FinallyStmt =
2570 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2571 CGF.EmitStmt(FinallyStmt->getFinallyBody());
Daniel Dunbar1c566672009-02-24 01:43:46 +00002572 } else {
2573 // Emit objc_sync_exit(expr); as finally's sole statement for
2574 // @synchronized.
Chris Lattnerbbccd612009-04-22 02:38:11 +00002575 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002576 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002577
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002578 // Emit the switch block
2579 if (Info.SwitchBlock)
2580 CGF.EmitBlock(Info.SwitchBlock);
2581 if (Info.EndBlock)
2582 CGF.EmitBlock(Info.EndBlock);
2583
Daniel Dunbar898d5082008-09-30 01:06:03 +00002584 CGF.EmitBlock(FinallyRethrow);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002585 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbar898d5082008-09-30 01:06:03 +00002586 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002587 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00002588
2589 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002590}
2591
2592void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002593 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002594 llvm::Value *ExceptionAsObject;
2595
2596 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2597 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2598 ExceptionAsObject =
2599 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2600 } else {
Anders Carlsson273558f2009-02-07 21:37:21 +00002601 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002602 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00002603 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002604 }
2605
Chris Lattnerbbccd612009-04-22 02:38:11 +00002606 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002607 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002608
2609 // Clear the insertion point to indicate we are in unreachable code.
2610 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002611}
2612
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002613/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002614/// object: objc_read_weak (id *src)
2615///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002616llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002617 llvm::Value *AddrWeakObj)
2618{
Eli Friedman8339b352009-03-07 03:57:15 +00002619 const llvm::Type* DestTy =
2620 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002621 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00002622 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002623 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00002624 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002625 return read_weak;
2626}
2627
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002628/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2629/// objc_assign_weak (id src, id *dst)
2630///
2631void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2632 llvm::Value *src, llvm::Value *dst)
2633{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002634 const llvm::Type * SrcTy = src->getType();
2635 if (!isa<llvm::PointerType>(SrcTy)) {
2636 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2637 assert(Size <= 8 && "does not support size > 8");
2638 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2639 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002640 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2641 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002642 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2643 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00002644 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002645 src, dst, "weakassign");
2646 return;
2647}
2648
Fariborz Jahanian58626502008-11-19 00:59:10 +00002649/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2650/// objc_assign_global (id src, id *dst)
2651///
2652void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2653 llvm::Value *src, llvm::Value *dst)
2654{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002655 const llvm::Type * SrcTy = src->getType();
2656 if (!isa<llvm::PointerType>(SrcTy)) {
2657 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2658 assert(Size <= 8 && "does not support size > 8");
2659 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2660 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002661 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2662 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002663 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2664 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002665 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00002666 src, dst, "globalassign");
2667 return;
2668}
2669
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002670/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2671/// objc_assign_ivar (id src, id *dst)
2672///
2673void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2674 llvm::Value *src, llvm::Value *dst)
2675{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002676 const llvm::Type * SrcTy = src->getType();
2677 if (!isa<llvm::PointerType>(SrcTy)) {
2678 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2679 assert(Size <= 8 && "does not support size > 8");
2680 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2681 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002682 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2683 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002684 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2685 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002686 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(),
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002687 src, dst, "assignivar");
2688 return;
2689}
2690
Fariborz Jahanian58626502008-11-19 00:59:10 +00002691/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2692/// objc_assign_strongCast (id src, id *dst)
2693///
2694void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2695 llvm::Value *src, llvm::Value *dst)
2696{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002697 const llvm::Type * SrcTy = src->getType();
2698 if (!isa<llvm::PointerType>(SrcTy)) {
2699 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2700 assert(Size <= 8 && "does not support size > 8");
2701 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2702 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002703 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2704 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002705 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2706 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002707 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00002708 src, dst, "weakassign");
2709 return;
2710}
2711
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002712/// EmitObjCValueForIvar - Code Gen for ivar reference.
2713///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002714LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2715 QualType ObjectTy,
2716 llvm::Value *BaseValue,
2717 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002718 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00002719 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00002720 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
2721 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002722}
2723
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002724llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00002725 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002726 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00002727 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002728 return llvm::ConstantInt::get(
2729 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2730 Offset);
2731}
2732
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002733/* *** Private Interface *** */
2734
2735/// EmitImageInfo - Emit the image info marker used to encode some module
2736/// level information.
2737///
2738/// See: <rdr://4810609&4810587&4810587>
2739/// struct IMAGE_INFO {
2740/// unsigned version;
2741/// unsigned flags;
2742/// };
2743enum ImageInfoFlags {
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002744 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what
2745 // this implies.
2746 eImageInfo_GarbageCollected = (1 << 1),
2747 eImageInfo_GCOnly = (1 << 2),
2748 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
2749
2750 // A flag indicating that the module has no instances of an
2751 // @synthesize of a superclass variable. <rdar://problem/6803242>
2752 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002753};
2754
2755void CGObjCMac::EmitImageInfo() {
2756 unsigned version = 0; // Version is unused?
2757 unsigned flags = 0;
2758
2759 // FIXME: Fix and continue?
2760 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2761 flags |= eImageInfo_GarbageCollected;
2762 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2763 flags |= eImageInfo_GCOnly;
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002764
2765 // We never allow @synthesize of a superclass property.
2766 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002767
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002768 // Emitted as int[2];
2769 llvm::Constant *values[2] = {
2770 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2771 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2772 };
2773 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002774
2775 const char *Section;
2776 if (ObjCABI == 1)
2777 Section = "__OBJC, __image_info,regular";
2778 else
2779 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002780 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002781 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
2782 llvm::ConstantArray::get(AT, values, 2),
2783 Section,
2784 0,
2785 true);
2786 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002787}
2788
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002789
2790// struct objc_module {
2791// unsigned long version;
2792// unsigned long size;
2793// const char *name;
2794// Symtab symtab;
2795// };
2796
2797// FIXME: Get from somewhere
2798static const int ModuleVersion = 7;
2799
2800void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002801 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002802
2803 std::vector<llvm::Constant*> Values(4);
2804 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2805 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002806 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002807 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002808 Values[3] = EmitModuleSymbols();
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002809 CreateMetadataVar("\01L_OBJC_MODULES",
2810 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
2811 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002812 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002813}
2814
2815llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002816 unsigned NumClasses = DefinedClasses.size();
2817 unsigned NumCategories = DefinedCategories.size();
2818
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002819 // Return null if no symbols were defined.
2820 if (!NumClasses && !NumCategories)
2821 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2822
2823 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002824 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2825 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2826 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2827 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2828
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002829 // The runtime expects exactly the list of defined classes followed
2830 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002831 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002832 for (unsigned i=0; i<NumClasses; i++)
2833 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2834 ObjCTypes.Int8PtrTy);
2835 for (unsigned i=0; i<NumCategories; i++)
2836 Symbols[NumClasses + i] =
2837 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2838 ObjCTypes.Int8PtrTy);
2839
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002840 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002841 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002842 NumClasses + NumCategories),
2843 Symbols);
2844
2845 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2846
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002847 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002848 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
2849 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002850 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002851 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2852}
2853
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002854llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002855 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002856 LazySymbols.insert(ID->getIdentifier());
2857
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002858 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2859
2860 if (!Entry) {
2861 llvm::Constant *Casted =
2862 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2863 ObjCTypes.ClassPtrTy);
2864 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002865 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
2866 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002867 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002868 }
2869
2870 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002871}
2872
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002873llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002874 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2875
2876 if (!Entry) {
2877 llvm::Constant *Casted =
2878 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2879 ObjCTypes.SelectorPtrTy);
2880 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002881 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
2882 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002883 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002884 }
2885
2886 return Builder.CreateLoad(Entry, false, "tmp");
2887}
2888
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002889llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002890 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002891
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002892 if (!Entry)
2893 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
2894 llvm::ConstantArray::get(Ident->getName()),
2895 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00002896 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002897
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002898 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002899}
2900
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00002901/// GetIvarLayoutName - Returns a unique constant for the given
2902/// ivar layout bitmap.
2903llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
2904 const ObjCCommonTypesHelper &ObjCTypes) {
2905 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
2906}
2907
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002908void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCInterfaceDecl *OI,
2909 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002910 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00002911 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002912 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +00002913 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002914 bool IsUnion = (RD && RD->isUnion());
2915 uint64_t MaxUnionIvarSize = 0;
2916 uint64_t MaxSkippedUnionIvarSize = 0;
2917 FieldDecl *MaxField = 0;
2918 FieldDecl *MaxSkippedField = 0;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002919 FieldDecl *LastFieldBitfield = 0;
2920
Chris Lattnerf1690852009-03-31 08:48:01 +00002921 unsigned base = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002922 if (RecFields.empty())
2923 return;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002924 if (IsUnion)
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002925 base = BytePos + GetFieldBaseOffset(OI, Layout, RecFields[0]);
Chris Lattnerf1690852009-03-31 08:48:01 +00002926 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
2927 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
2928
2929 llvm::SmallVector<FieldDecl*, 16> TmpRecFields;
2930
2931 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002932 FieldDecl *Field = RecFields[i];
2933 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002934 if (!Field->getIdentifier() || Field->isBitField()) {
2935 LastFieldBitfield = Field;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002936 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002937 }
2938 LastFieldBitfield = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002939 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00002940 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002941 if (FQT->isUnionType())
2942 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002943
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002944 const RecordType *RT = FQT->getAsRecordType();
2945 const RecordDecl *RD = RT->getDecl();
Daniel Dunbarb02532a2009-04-19 23:41:48 +00002946 // FIXME - Find a more efficient way of passing records down.
Douglas Gregor6ab35242009-04-09 21:40:53 +00002947 TmpRecFields.append(RD->field_begin(CGM.getContext()),
2948 RD->field_end(CGM.getContext()));
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00002949 const llvm::Type *Ty = CGM.getTypes().ConvertType(FQT);
2950 const llvm::StructLayout *RecLayout =
2951 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
2952
2953 BuildAggrIvarLayout(0, RecLayout, RD, TmpRecFields,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002954 BytePos + GetFieldBaseOffset(OI, Layout, Field),
Fariborz Jahanian81adc052009-04-24 16:17:09 +00002955 ForStrongLayout, HasUnion);
Chris Lattnerf1690852009-03-31 08:48:01 +00002956 TmpRecFields.clear();
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002957 continue;
2958 }
Chris Lattnerf1690852009-03-31 08:48:01 +00002959
2960 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002961 const ConstantArrayType *CArray =
2962 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002963 uint64_t ElCount = CArray->getSize().getZExtValue();
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002964 assert(CArray && "only array with know element size is supported");
2965 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00002966 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
2967 const ConstantArrayType *CArray =
2968 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002969 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00002970 FQT = CArray->getElementType();
2971 }
2972
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002973 assert(!FQT->isUnionType() &&
2974 "layout for array of unions not supported");
2975 if (FQT->isRecordType()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00002976 int OldIndex = IvarsInfo.size() - 1;
2977 int OldSkIndex = SkipIvars.size() -1;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002978
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002979 // FIXME - Use a common routine with the above!
2980 const RecordType *RT = FQT->getAsRecordType();
2981 const RecordDecl *RD = RT->getDecl();
2982 // FIXME - Find a more efficiant way of passing records down.
Douglas Gregor6ab35242009-04-09 21:40:53 +00002983 TmpRecFields.append(RD->field_begin(CGM.getContext()),
2984 RD->field_end(CGM.getContext()));
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00002985 const llvm::Type *Ty = CGM.getTypes().ConvertType(FQT);
2986 const llvm::StructLayout *RecLayout =
2987 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Chris Lattnerf1690852009-03-31 08:48:01 +00002988
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00002989 BuildAggrIvarLayout(0, RecLayout, RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00002990 TmpRecFields,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002991 BytePos + GetFieldBaseOffset(OI, Layout, Field),
Fariborz Jahanian81adc052009-04-24 16:17:09 +00002992 ForStrongLayout, HasUnion);
Chris Lattnerf1690852009-03-31 08:48:01 +00002993 TmpRecFields.clear();
2994
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002995 // Replicate layout information for each array element. Note that
2996 // one element is already done.
2997 uint64_t ElIx = 1;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00002998 for (int FirstIndex = IvarsInfo.size() - 1,
2999 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003000 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003001 for (int i = OldIndex+1; i <= FirstIndex; ++i) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003002 GC_IVAR gcivar;
3003 gcivar.ivar_bytepos = IvarsInfo[i].ivar_bytepos + Size*ElIx;
3004 gcivar.ivar_size = IvarsInfo[i].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003005 IvarsInfo.push_back(gcivar);
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003006 }
3007
Chris Lattnerf1690852009-03-31 08:48:01 +00003008 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003009 GC_IVAR skivar;
3010 skivar.ivar_bytepos = SkipIvars[i].ivar_bytepos + Size*ElIx;
3011 skivar.ivar_size = SkipIvars[i].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003012 SkipIvars.push_back(skivar);
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003013 }
3014 }
3015 continue;
3016 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003017 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003018 // At this point, we are done with Record/Union and array there of.
3019 // For other arrays we are down to its element type.
3020 QualType::GCAttrTypes GCAttr = QualType::GCNone;
3021 do {
3022 if (FQT.isObjCGCStrong() || FQT.isObjCGCWeak()) {
3023 GCAttr = FQT.isObjCGCStrong() ? QualType::Strong : QualType::Weak;
3024 break;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003025 } else if (CGM.getContext().isObjCObjectPointerType(FQT)) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003026 GCAttr = QualType::Strong;
3027 break;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003028 } else if (const PointerType *PT = FQT->getAsPointerType()) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003029 FQT = PT->getPointeeType();
Daniel Dunbar487993b2009-05-03 13:32:01 +00003030 } else {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003031 break;
3032 }
3033 } while (true);
Chris Lattnerf1690852009-03-31 08:48:01 +00003034
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003035 if ((ForStrongLayout && GCAttr == QualType::Strong)
3036 || (!ForStrongLayout && GCAttr == QualType::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003037 if (IsUnion) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003038 uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType())
3039 / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003040 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003041 MaxUnionIvarSize = UnionIvarSize;
3042 MaxField = Field;
3043 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003044 } else {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003045 GC_IVAR gcivar;
3046 gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field);
3047 gcivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) /
3048 WordSizeInBits;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003049 IvarsInfo.push_back(gcivar);
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003050 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003051 } else if ((ForStrongLayout &&
3052 (GCAttr == QualType::GCNone || GCAttr == QualType::Weak))
3053 || (!ForStrongLayout && GCAttr != QualType::Weak)) {
3054 if (IsUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003055 uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType());
Daniel Dunbar487993b2009-05-03 13:32:01 +00003056 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003057 MaxSkippedUnionIvarSize = UnionIvarSize;
3058 MaxSkippedField = Field;
3059 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003060 } else {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003061 GC_IVAR skivar;
3062 skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field);
3063 skivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) /
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003064 ByteSizeInBits;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003065 SkipIvars.push_back(skivar);
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003066 }
3067 }
3068 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003069 if (LastFieldBitfield) {
3070 // Last field was a bitfield. Must update skip info.
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003071 Expr *BitWidth = LastFieldBitfield->getBitWidth();
3072 uint64_t BitFieldSize =
Eli Friedman9a901bb2009-04-26 19:19:15 +00003073 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Daniel Dunbar487993b2009-05-03 13:32:01 +00003074 GC_IVAR skivar;
3075 skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout,
3076 LastFieldBitfield);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003077 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3078 + ((BitFieldSize % ByteSizeInBits) != 0);
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003079 SkipIvars.push_back(skivar);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003080 }
3081
Chris Lattnerf1690852009-03-31 08:48:01 +00003082 if (MaxField) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003083 GC_IVAR gcivar;
3084 gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, MaxField);
3085 gcivar.ivar_size = MaxUnionIvarSize;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003086 IvarsInfo.push_back(gcivar);
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003087 }
Chris Lattnerf1690852009-03-31 08:48:01 +00003088
3089 if (MaxSkippedField) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003090 GC_IVAR skivar;
3091 skivar.ivar_bytepos = BytePos +
3092 GetFieldBaseOffset(OI, Layout, MaxSkippedField);
3093 skivar.ivar_size = MaxSkippedUnionIvarSize;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003094 SkipIvars.push_back(skivar);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003095 }
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003096}
3097
3098/// BuildIvarLayout - Builds ivar layout bitmap for the class
3099/// implementation for the __strong or __weak case.
3100/// The layout map displays which words in ivar list must be skipped
3101/// and which must be scanned by GC (see below). String is built of bytes.
3102/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3103/// of words to skip and right nibble is count of words to scan. So, each
3104/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3105/// represented by a 0x00 byte which also ends the string.
3106/// 1. when ForStrongLayout is true, following ivars are scanned:
3107/// - id, Class
3108/// - object *
3109/// - __strong anything
3110///
3111/// 2. When ForStrongLayout is false, following ivars are scanned:
3112/// - __weak anything
3113///
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003114llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003115 const ObjCImplementationDecl *OMD,
3116 bool ForStrongLayout) {
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003117 bool hasUnion = false;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003118
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003119 unsigned int WordsToScan, WordsToSkip;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003120 const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
3121 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
3122 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003123
Chris Lattnerf1690852009-03-31 08:48:01 +00003124 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003125 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003126 CGM.getContext().CollectObjCIvars(OI, RecFields);
3127 if (RecFields.empty())
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003128 return llvm::Constant::getNullValue(PtrTy);
Chris Lattnerf1690852009-03-31 08:48:01 +00003129
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003130 SkipIvars.clear();
3131 IvarsInfo.clear();
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00003132
Daniel Dunbar84ad77a2009-04-22 09:39:34 +00003133 const llvm::StructLayout *Layout =
3134 CGM.getTargetData().getStructLayout(GetConcreteClassStruct(CGM, OI));
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003135 BuildAggrIvarLayout(OI, Layout, 0, RecFields, 0, ForStrongLayout, hasUnion);
3136 if (IvarsInfo.empty())
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003137 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003138
3139 // Sort on byte position in case we encounterred a union nested in
3140 // the ivar list.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003141 if (hasUnion && !IvarsInfo.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003142 std::sort(IvarsInfo.begin(), IvarsInfo.end());
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003143 if (hasUnion && !SkipIvars.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003144 std::sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003145
3146 // Build the string of skip/scan nibbles
Fariborz Jahanian8c2f2d12009-04-24 17:15:27 +00003147 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003148 unsigned int WordSize =
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003149 CGM.getTypes().getTargetData().getTypePaddedSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003150 if (IvarsInfo[0].ivar_bytepos == 0) {
3151 WordsToSkip = 0;
3152 WordsToScan = IvarsInfo[0].ivar_size;
3153 }
3154 else {
3155 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3156 WordsToScan = IvarsInfo[0].ivar_size;
3157 }
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003158 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++)
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003159 {
3160 unsigned int TailPrevGCObjC =
3161 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
3162 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC)
3163 {
3164 // consecutive 'scanned' object pointers.
3165 WordsToScan += IvarsInfo[i].ivar_size;
3166 }
3167 else
3168 {
3169 // Skip over 'gc'able object pointer which lay over each other.
3170 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3171 continue;
3172 // Must skip over 1 or more words. We save current skip/scan values
3173 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003174 SKIP_SCAN SkScan;
3175 SkScan.skip = WordsToSkip;
3176 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003177 SkipScanIvars.push_back(SkScan);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003178
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003179 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003180 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3181 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003182 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003183 WordsToSkip = 0;
3184 WordsToScan = IvarsInfo[i].ivar_size;
3185 }
3186 }
3187 if (WordsToScan > 0)
3188 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003189 SKIP_SCAN SkScan;
3190 SkScan.skip = WordsToSkip;
3191 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003192 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003193 }
3194
3195 bool BytesSkipped = false;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003196 if (!SkipIvars.empty())
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003197 {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003198 unsigned int LastIndex = SkipIvars.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003199 int LastByteSkipped =
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003200 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
3201 LastIndex = IvarsInfo.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003202 int LastByteScanned =
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003203 IvarsInfo[LastIndex].ivar_bytepos +
3204 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003205 BytesSkipped = (LastByteSkipped > LastByteScanned);
3206 // Compute number of bytes to skip at the tail end of the last ivar scanned.
3207 if (BytesSkipped)
3208 {
3209 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003210 SKIP_SCAN SkScan;
3211 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3212 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003213 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003214 }
3215 }
3216 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3217 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003218 int SkipScan = SkipScanIvars.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003219 for (int i = 0; i <= SkipScan; i++)
3220 {
3221 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3222 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3223 // 0xM0 followed by 0x0N detected.
3224 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3225 for (int j = i+1; j < SkipScan; j++)
3226 SkipScanIvars[j] = SkipScanIvars[j+1];
3227 --SkipScan;
3228 }
3229 }
3230
3231 // Generate the string.
3232 std::string BitMap;
3233 for (int i = 0; i <= SkipScan; i++)
3234 {
3235 unsigned char byte;
3236 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3237 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3238 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3239 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
3240
3241 if (skip_small > 0 || skip_big > 0)
3242 BytesSkipped = true;
3243 // first skip big.
3244 for (unsigned int ix = 0; ix < skip_big; ix++)
3245 BitMap += (unsigned char)(0xf0);
3246
3247 // next (skip small, scan)
3248 if (skip_small)
3249 {
3250 byte = skip_small << 4;
3251 if (scan_big > 0)
3252 {
3253 byte |= 0xf;
3254 --scan_big;
3255 }
3256 else if (scan_small)
3257 {
3258 byte |= scan_small;
3259 scan_small = 0;
3260 }
3261 BitMap += byte;
3262 }
3263 // next scan big
3264 for (unsigned int ix = 0; ix < scan_big; ix++)
3265 BitMap += (unsigned char)(0x0f);
3266 // last scan small
3267 if (scan_small)
3268 {
3269 byte = scan_small;
3270 BitMap += byte;
3271 }
3272 }
3273 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003274 unsigned char zero = 0;
3275 BitMap += zero;
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003276
3277 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
3278 printf("\n%s ivar layout for class '%s': ",
3279 ForStrongLayout ? "strong" : "weak",
3280 OMD->getClassInterface()->getNameAsCString());
3281 const unsigned char *s = (unsigned char*)BitMap.c_str();
3282 for (unsigned i = 0; i < BitMap.size(); i++)
3283 if (!(s[i] & 0xf0))
3284 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3285 else
3286 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3287 printf("\n");
3288 }
3289
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003290 // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as
3291 // final layout.
3292 if (ForStrongLayout && !BytesSkipped)
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003293 return llvm::Constant::getNullValue(PtrTy);
3294 llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3295 llvm::ConstantArray::get(BitMap.c_str()),
3296 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003297 1, true);
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003298 return getConstantGEP(Entry, 0, 0);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003299}
3300
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003301llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003302 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3303
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003304 // FIXME: Avoid std::string copying.
3305 if (!Entry)
3306 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
3307 llvm::ConstantArray::get(Sel.getAsString()),
3308 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003309 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003310
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003311 return getConstantGEP(Entry, 0, 0);
3312}
3313
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003314// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003315llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003316 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3317}
3318
3319// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003320llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003321 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
3322}
3323
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003324llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003325 std::string TypeStr;
3326 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3327
3328 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003329
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003330 if (!Entry)
3331 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
3332 llvm::ConstantArray::get(TypeStr),
3333 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003334 1, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003335
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003336 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003337}
3338
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003339llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003340 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003341 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3342 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00003343
3344 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3345
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003346 if (!Entry)
3347 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
3348 llvm::ConstantArray::get(TypeStr),
3349 "__TEXT,__cstring,cstring_literals",
3350 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00003351
3352 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003353}
3354
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003355// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003356llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003357 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
3358
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003359 if (!Entry)
3360 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
3361 llvm::ConstantArray::get(Ident->getName()),
3362 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003363 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003364
3365 return getConstantGEP(Entry, 0, 0);
3366}
3367
3368// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003369// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003370llvm::Constant *
3371 CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3372 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003373 std::string TypeStr;
3374 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003375 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3376}
3377
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003378void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
3379 const ObjCContainerDecl *CD,
3380 std::string &NameOut) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00003381 NameOut = '\01';
3382 NameOut += (D->isInstanceMethod() ? '-' : '+');
Chris Lattner077bf5e2008-11-24 03:33:13 +00003383 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003384 assert (CD && "Missing container decl in GetNameForMethod");
3385 NameOut += CD->getNameAsString();
Fariborz Jahanian1e9aef32009-04-16 18:34:20 +00003386 if (const ObjCCategoryImplDecl *CID =
3387 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) {
3388 NameOut += '(';
3389 NameOut += CID->getNameAsString();
3390 NameOut+= ')';
3391 }
Chris Lattner077bf5e2008-11-24 03:33:13 +00003392 NameOut += ' ';
3393 NameOut += D->getSelector().getAsString();
3394 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003395}
3396
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003397void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003398 EmitModuleInfo();
3399
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003400 // Emit the dummy bodies for any protocols which were referenced but
3401 // never defined.
3402 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
3403 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
3404 if (i->second->hasInitializer())
3405 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003406
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003407 std::vector<llvm::Constant*> Values(5);
3408 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
3409 Values[1] = GetClassName(i->first);
3410 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
3411 Values[3] = Values[4] =
3412 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
3413 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
3414 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
3415 Values));
3416 }
3417
3418 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003419 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003420 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003421 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003422 }
3423
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003424 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003425 llvm::GlobalValue *GV =
3426 new llvm::GlobalVariable(AT, false,
3427 llvm::GlobalValue::AppendingLinkage,
3428 llvm::ConstantArray::get(AT, Used),
3429 "llvm.used",
3430 &CGM.getModule());
3431
3432 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003433
3434 // Add assembler directives to add lazy undefined symbol references
3435 // for classes which are referenced but not defined. This is
3436 // important for correct linker interaction.
3437
3438 // FIXME: Uh, this isn't particularly portable.
3439 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00003440
3441 if (!CGM.getModule().getModuleInlineAsm().empty())
3442 s << "\n";
3443
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003444 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
3445 e = LazySymbols.end(); i != e; ++i) {
3446 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
3447 }
3448 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
3449 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003450 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003451 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
3452 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00003453
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003454 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003455}
3456
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003457CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003458 : CGObjCCommonMac(cgm),
3459 ObjCTypes(cgm)
3460{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003461 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003462 ObjCABI = 2;
3463}
3464
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003465/* *** */
3466
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003467ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
3468: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003469{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003470 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3471 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003472
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003473 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003474 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003475 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003476 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003477 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
3478
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003479 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00003480 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003481 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003482
3483 // FIXME: It would be nice to unify this with the opaque type, so
3484 // that the IR comes out a bit cleaner.
3485 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
3486 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003487
3488 // I'm not sure I like this. The implicit coordination is a bit
3489 // gross. We should solve this in a reasonable fashion because this
3490 // is a pretty common task (match some runtime data structure with
3491 // an LLVM data structure).
3492
3493 // FIXME: This is leaked.
3494 // FIXME: Merge with rewriter code?
3495
3496 // struct _objc_super {
3497 // id self;
3498 // Class cls;
3499 // }
3500 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3501 SourceLocation(),
3502 &Ctx.Idents.get("_objc_super"));
Douglas Gregor6ab35242009-04-09 21:40:53 +00003503 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3504 Ctx.getObjCIdType(), 0, false));
3505 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3506 Ctx.getObjCClassType(), 0, false));
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003507 RD->completeDefinition(Ctx);
3508
3509 SuperCTy = Ctx.getTagDeclType(RD);
3510 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
3511
3512 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003513 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
3514
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003515 // struct _prop_t {
3516 // char *name;
3517 // char *attributes;
3518 // }
Chris Lattner1c02f862009-04-22 02:53:24 +00003519 PropertyTy = llvm::StructType::get(Int8PtrTy, Int8PtrTy, NULL);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003520 CGM.getModule().addTypeName("struct._prop_t",
3521 PropertyTy);
3522
3523 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003524 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003525 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003526 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003527 // }
3528 PropertyListTy = llvm::StructType::get(IntTy,
3529 IntTy,
3530 llvm::ArrayType::get(PropertyTy, 0),
3531 NULL);
3532 CGM.getModule().addTypeName("struct._prop_list_t",
3533 PropertyListTy);
3534 // struct _prop_list_t *
3535 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
3536
3537 // struct _objc_method {
3538 // SEL _cmd;
3539 // char *method_type;
3540 // char *_imp;
3541 // }
3542 MethodTy = llvm::StructType::get(SelectorPtrTy,
3543 Int8PtrTy,
3544 Int8PtrTy,
3545 NULL);
3546 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003547
3548 // struct _objc_cache *
3549 CacheTy = llvm::OpaqueType::get();
3550 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
3551 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003552}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003553
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003554ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
3555 : ObjCCommonTypesHelper(cgm)
3556{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003557 // struct _objc_method_description {
3558 // SEL name;
3559 // char *types;
3560 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003561 MethodDescriptionTy =
3562 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003563 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003564 NULL);
3565 CGM.getModule().addTypeName("struct._objc_method_description",
3566 MethodDescriptionTy);
3567
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003568 // struct _objc_method_description_list {
3569 // int count;
3570 // struct _objc_method_description[1];
3571 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003572 MethodDescriptionListTy =
3573 llvm::StructType::get(IntTy,
3574 llvm::ArrayType::get(MethodDescriptionTy, 0),
3575 NULL);
3576 CGM.getModule().addTypeName("struct._objc_method_description_list",
3577 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003578
3579 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003580 MethodDescriptionListPtrTy =
3581 llvm::PointerType::getUnqual(MethodDescriptionListTy);
3582
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003583 // Protocol description structures
3584
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003585 // struct _objc_protocol_extension {
3586 // uint32_t size; // sizeof(struct _objc_protocol_extension)
3587 // struct _objc_method_description_list *optional_instance_methods;
3588 // struct _objc_method_description_list *optional_class_methods;
3589 // struct _objc_property_list *instance_properties;
3590 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003591 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003592 llvm::StructType::get(IntTy,
3593 MethodDescriptionListPtrTy,
3594 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003595 PropertyListPtrTy,
3596 NULL);
3597 CGM.getModule().addTypeName("struct._objc_protocol_extension",
3598 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003599
3600 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003601 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
3602
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003603 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003604
3605 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
3606 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3607
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003608 const llvm::Type *T =
3609 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
3610 LongTy,
3611 llvm::ArrayType::get(ProtocolTyHolder, 0),
3612 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003613 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
3614
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003615 // struct _objc_protocol {
3616 // struct _objc_protocol_extension *isa;
3617 // char *protocol_name;
3618 // struct _objc_protocol **_objc_protocol_list;
3619 // struct _objc_method_description_list *instance_methods;
3620 // struct _objc_method_description_list *class_methods;
3621 // }
3622 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003623 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003624 llvm::PointerType::getUnqual(ProtocolListTyHolder),
3625 MethodDescriptionListPtrTy,
3626 MethodDescriptionListPtrTy,
3627 NULL);
3628 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
3629
3630 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
3631 CGM.getModule().addTypeName("struct._objc_protocol_list",
3632 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003633 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003634 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
3635
3636 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003637 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003638 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003639
3640 // Class description structures
3641
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003642 // struct _objc_ivar {
3643 // char *ivar_name;
3644 // char *ivar_type;
3645 // int ivar_offset;
3646 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003647 IvarTy = llvm::StructType::get(Int8PtrTy,
3648 Int8PtrTy,
3649 IntTy,
3650 NULL);
3651 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
3652
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003653 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003654 IvarListTy = llvm::OpaqueType::get();
3655 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
3656 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
3657
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003658 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003659 MethodListTy = llvm::OpaqueType::get();
3660 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
3661 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
3662
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003663 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003664 ClassExtensionTy =
3665 llvm::StructType::get(IntTy,
3666 Int8PtrTy,
3667 PropertyListPtrTy,
3668 NULL);
3669 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
3670 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
3671
3672 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3673
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003674 // struct _objc_class {
3675 // Class isa;
3676 // Class super_class;
3677 // char *name;
3678 // long version;
3679 // long info;
3680 // long instance_size;
3681 // struct _objc_ivar_list *ivars;
3682 // struct _objc_method_list *methods;
3683 // struct _objc_cache *cache;
3684 // struct _objc_protocol_list *protocols;
3685 // char *ivar_layout;
3686 // struct _objc_class_ext *ext;
3687 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003688 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3689 llvm::PointerType::getUnqual(ClassTyHolder),
3690 Int8PtrTy,
3691 LongTy,
3692 LongTy,
3693 LongTy,
3694 IvarListPtrTy,
3695 MethodListPtrTy,
3696 CachePtrTy,
3697 ProtocolListPtrTy,
3698 Int8PtrTy,
3699 ClassExtensionPtrTy,
3700 NULL);
3701 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
3702
3703 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
3704 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
3705 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
3706
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003707 // struct _objc_category {
3708 // char *category_name;
3709 // char *class_name;
3710 // struct _objc_method_list *instance_method;
3711 // struct _objc_method_list *class_method;
3712 // uint32_t size; // sizeof(struct _objc_category)
3713 // struct _objc_property_list *instance_properties;// category's @property
3714 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003715 CategoryTy = llvm::StructType::get(Int8PtrTy,
3716 Int8PtrTy,
3717 MethodListPtrTy,
3718 MethodListPtrTy,
3719 ProtocolListPtrTy,
3720 IntTy,
3721 PropertyListPtrTy,
3722 NULL);
3723 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
3724
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003725 // Global metadata structures
3726
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003727 // struct _objc_symtab {
3728 // long sel_ref_cnt;
3729 // SEL *refs;
3730 // short cls_def_cnt;
3731 // short cat_def_cnt;
3732 // char *defs[cls_def_cnt + cat_def_cnt];
3733 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003734 SymtabTy = llvm::StructType::get(LongTy,
3735 SelectorPtrTy,
3736 ShortTy,
3737 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003738 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003739 NULL);
3740 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
3741 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
3742
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003743 // struct _objc_module {
3744 // long version;
3745 // long size; // sizeof(struct _objc_module)
3746 // char *name;
3747 // struct _objc_symtab* symtab;
3748 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003749 ModuleTy =
3750 llvm::StructType::get(LongTy,
3751 LongTy,
3752 Int8PtrTy,
3753 SymtabPtrTy,
3754 NULL);
3755 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003756
Anders Carlsson2abd89c2008-08-31 04:05:03 +00003757
Anders Carlsson124526b2008-09-09 10:10:21 +00003758 // FIXME: This is the size of the setjmp buffer and should be
3759 // target specific. 18 is what's used on 32-bit X86.
3760 uint64_t SetJmpBufferSize = 18;
3761
3762 // Exceptions
3763 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00003764 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00003765
3766 ExceptionDataTy =
3767 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
3768 SetJmpBufferSize),
3769 StackPtrTy, NULL);
3770 CGM.getModule().addTypeName("struct._objc_exception_data",
3771 ExceptionDataTy);
3772
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003773}
3774
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003775ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003776: ObjCCommonTypesHelper(cgm)
3777{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003778 // struct _method_list_t {
3779 // uint32_t entsize; // sizeof(struct _objc_method)
3780 // uint32_t method_count;
3781 // struct _objc_method method_list[method_count];
3782 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003783 MethodListnfABITy = llvm::StructType::get(IntTy,
3784 IntTy,
3785 llvm::ArrayType::get(MethodTy, 0),
3786 NULL);
3787 CGM.getModule().addTypeName("struct.__method_list_t",
3788 MethodListnfABITy);
3789 // struct method_list_t *
3790 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003791
3792 // struct _protocol_t {
3793 // id isa; // NULL
3794 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003795 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003796 // const struct method_list_t * const instance_methods;
3797 // const struct method_list_t * const class_methods;
3798 // const struct method_list_t *optionalInstanceMethods;
3799 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003800 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003801 // const uint32_t size; // sizeof(struct _protocol_t)
3802 // const uint32_t flags; // = 0
3803 // }
3804
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003805 // Holder for struct _protocol_list_t *
3806 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3807
3808 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3809 Int8PtrTy,
3810 llvm::PointerType::getUnqual(
3811 ProtocolListTyHolder),
3812 MethodListnfABIPtrTy,
3813 MethodListnfABIPtrTy,
3814 MethodListnfABIPtrTy,
3815 MethodListnfABIPtrTy,
3816 PropertyListPtrTy,
3817 IntTy,
3818 IntTy,
3819 NULL);
3820 CGM.getModule().addTypeName("struct._protocol_t",
3821 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003822
3823 // struct _protocol_t*
3824 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003825
Fariborz Jahanianda320092009-01-29 19:24:30 +00003826 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003827 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00003828 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003829 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003830 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3831 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003832 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003833 NULL);
3834 CGM.getModule().addTypeName("struct._objc_protocol_list",
3835 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003836 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3837 ProtocolListnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003838
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003839 // struct _objc_protocol_list*
3840 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003841
3842 // struct _ivar_t {
3843 // unsigned long int *offset; // pointer to ivar offset location
3844 // char *name;
3845 // char *type;
3846 // uint32_t alignment;
3847 // uint32_t size;
3848 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003849 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3850 Int8PtrTy,
3851 Int8PtrTy,
3852 IntTy,
3853 IntTy,
3854 NULL);
3855 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3856
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003857 // struct _ivar_list_t {
3858 // uint32 entsize; // sizeof(struct _ivar_t)
3859 // uint32 count;
3860 // struct _iver_t list[count];
3861 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003862 IvarListnfABITy = llvm::StructType::get(IntTy,
3863 IntTy,
3864 llvm::ArrayType::get(
3865 IvarnfABITy, 0),
3866 NULL);
3867 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3868
3869 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003870
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003871 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003872 // uint32_t const flags;
3873 // uint32_t const instanceStart;
3874 // uint32_t const instanceSize;
3875 // uint32_t const reserved; // only when building for 64bit targets
3876 // const uint8_t * const ivarLayout;
3877 // const char *const name;
3878 // const struct _method_list_t * const baseMethods;
3879 // const struct _objc_protocol_list *const baseProtocols;
3880 // const struct _ivar_list_t *const ivars;
3881 // const uint8_t * const weakIvarLayout;
3882 // const struct _prop_list_t * const properties;
3883 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003884
3885 // FIXME. Add 'reserved' field in 64bit abi mode!
3886 ClassRonfABITy = llvm::StructType::get(IntTy,
3887 IntTy,
3888 IntTy,
3889 Int8PtrTy,
3890 Int8PtrTy,
3891 MethodListnfABIPtrTy,
3892 ProtocolListnfABIPtrTy,
3893 IvarListnfABIPtrTy,
3894 Int8PtrTy,
3895 PropertyListPtrTy,
3896 NULL);
3897 CGM.getModule().addTypeName("struct._class_ro_t",
3898 ClassRonfABITy);
3899
3900 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3901 std::vector<const llvm::Type*> Params;
3902 Params.push_back(ObjectPtrTy);
3903 Params.push_back(SelectorPtrTy);
3904 ImpnfABITy = llvm::PointerType::getUnqual(
3905 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3906
3907 // struct _class_t {
3908 // struct _class_t *isa;
3909 // struct _class_t * const superclass;
3910 // void *cache;
3911 // IMP *vtable;
3912 // struct class_ro_t *ro;
3913 // }
3914
3915 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3916 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3917 llvm::PointerType::getUnqual(ClassTyHolder),
3918 CachePtrTy,
3919 llvm::PointerType::getUnqual(ImpnfABITy),
3920 llvm::PointerType::getUnqual(
3921 ClassRonfABITy),
3922 NULL);
3923 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3924
3925 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3926 ClassnfABITy);
3927
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003928 // LLVM for struct _class_t *
3929 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3930
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003931 // struct _category_t {
3932 // const char * const name;
3933 // struct _class_t *const cls;
3934 // const struct _method_list_t * const instance_methods;
3935 // const struct _method_list_t * const class_methods;
3936 // const struct _protocol_list_t * const protocols;
3937 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003938 // }
3939 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003940 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003941 MethodListnfABIPtrTy,
3942 MethodListnfABIPtrTy,
3943 ProtocolListnfABIPtrTy,
3944 PropertyListPtrTy,
3945 NULL);
3946 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003947
3948 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003949 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3950 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003951
3952 // MessageRefTy - LLVM for:
3953 // struct _message_ref_t {
3954 // IMP messenger;
3955 // SEL name;
3956 // };
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003957
3958 // First the clang type for struct _message_ref_t
3959 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3960 SourceLocation(),
3961 &Ctx.Idents.get("_message_ref_t"));
Douglas Gregor6ab35242009-04-09 21:40:53 +00003962 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3963 Ctx.VoidPtrTy, 0, false));
3964 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3965 Ctx.getObjCSelType(), 0, false));
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003966 RD->completeDefinition(Ctx);
3967
3968 MessageRefCTy = Ctx.getTagDeclType(RD);
3969 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
3970 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003971
3972 // MessageRefPtrTy - LLVM for struct _message_ref_t*
3973 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
3974
3975 // SuperMessageRefTy - LLVM for:
3976 // struct _super_message_ref_t {
3977 // SUPER_IMP messenger;
3978 // SEL name;
3979 // };
3980 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy,
3981 SelectorPtrTy,
3982 NULL);
3983 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
3984
3985 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
3986 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
3987
Daniel Dunbare588b992009-03-01 04:46:24 +00003988
3989 // struct objc_typeinfo {
3990 // const void** vtable; // objc_ehtype_vtable + 2
3991 // const char* name; // c++ typeinfo string
3992 // Class cls;
3993 // };
3994 EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy),
3995 Int8PtrTy,
3996 ClassnfABIPtrTy,
3997 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00003998 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Daniel Dunbare588b992009-03-01 04:46:24 +00003999 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004000}
4001
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004002llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
4003 FinishNonFragileABIModule();
4004
4005 return NULL;
4006}
4007
4008void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4009 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004010
4011 // Build list of all implemented classe addresses in array
4012 // L_OBJC_LABEL_CLASS_$.
4013 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
4014 // list of 'nonlazy' implementations (defined as those with a +load{}
4015 // method!!).
4016 unsigned NumClasses = DefinedClasses.size();
4017 if (NumClasses) {
4018 std::vector<llvm::Constant*> Symbols(NumClasses);
4019 for (unsigned i=0; i<NumClasses; i++)
4020 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
4021 ObjCTypes.Int8PtrTy);
4022 llvm::Constant* Init =
4023 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
4024 NumClasses),
4025 Symbols);
4026
4027 llvm::GlobalVariable *GV =
4028 new llvm::GlobalVariable(Init->getType(), false,
4029 llvm::GlobalValue::InternalLinkage,
4030 Init,
4031 "\01L_OBJC_LABEL_CLASS_$",
4032 &CGM.getModule());
Daniel Dunbar58a29122009-03-09 22:18:41 +00004033 GV->setAlignment(8);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004034 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
4035 UsedGlobals.push_back(GV);
4036 }
4037
4038 // Build list of all implemented category addresses in array
4039 // L_OBJC_LABEL_CATEGORY_$.
4040 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
4041 // list of 'nonlazy' category implementations (defined as those with a +load{}
4042 // method!!).
4043 unsigned NumCategory = DefinedCategories.size();
4044 if (NumCategory) {
4045 std::vector<llvm::Constant*> Symbols(NumCategory);
4046 for (unsigned i=0; i<NumCategory; i++)
4047 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
4048 ObjCTypes.Int8PtrTy);
4049 llvm::Constant* Init =
4050 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
4051 NumCategory),
4052 Symbols);
4053
4054 llvm::GlobalVariable *GV =
4055 new llvm::GlobalVariable(Init->getType(), false,
4056 llvm::GlobalValue::InternalLinkage,
4057 Init,
4058 "\01L_OBJC_LABEL_CATEGORY_$",
4059 &CGM.getModule());
Daniel Dunbar58a29122009-03-09 22:18:41 +00004060 GV->setAlignment(8);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004061 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
4062 UsedGlobals.push_back(GV);
4063 }
4064
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004065 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
4066 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
4067 std::vector<llvm::Constant*> Values(2);
4068 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
Fariborz Jahanian067986e2009-02-24 21:08:09 +00004069 unsigned int flags = 0;
Fariborz Jahanian66a5c2c2009-02-24 23:34:44 +00004070 // FIXME: Fix and continue?
4071 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
4072 flags |= eImageInfo_GarbageCollected;
4073 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
4074 flags |= eImageInfo_GCOnly;
Fariborz Jahanian067986e2009-02-24 21:08:09 +00004075 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004076 llvm::Constant* Init = llvm::ConstantArray::get(
4077 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
4078 Values);
4079 llvm::GlobalVariable *IMGV =
4080 new llvm::GlobalVariable(Init->getType(), false,
4081 llvm::GlobalValue::InternalLinkage,
4082 Init,
4083 "\01L_OBJC_IMAGE_INFO",
4084 &CGM.getModule());
4085 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
Daniel Dunbar325f7582009-04-23 08:03:21 +00004086 IMGV->setConstant(true);
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004087 UsedGlobals.push_back(IMGV);
4088
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004089 std::vector<llvm::Constant*> Used;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004090
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004091 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
4092 e = UsedGlobals.end(); i != e; ++i) {
4093 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
4094 }
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004095
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004096 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
4097 llvm::GlobalValue *GV =
4098 new llvm::GlobalVariable(AT, false,
4099 llvm::GlobalValue::AppendingLinkage,
4100 llvm::ConstantArray::get(AT, Used),
4101 "llvm.used",
4102 &CGM.getModule());
4103
4104 GV->setSection("llvm.metadata");
4105
4106}
4107
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004108// Metadata flags
4109enum MetaDataDlags {
4110 CLS = 0x0,
4111 CLS_META = 0x1,
4112 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004113 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004114 CLS_EXCEPTION = 0x20
4115};
4116/// BuildClassRoTInitializer - generate meta-data for:
4117/// struct _class_ro_t {
4118/// uint32_t const flags;
4119/// uint32_t const instanceStart;
4120/// uint32_t const instanceSize;
4121/// uint32_t const reserved; // only when building for 64bit targets
4122/// const uint8_t * const ivarLayout;
4123/// const char *const name;
4124/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004125/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004126/// const struct _ivar_list_t *const ivars;
4127/// const uint8_t * const weakIvarLayout;
4128/// const struct _prop_list_t * const properties;
4129/// }
4130///
4131llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
4132 unsigned flags,
4133 unsigned InstanceStart,
4134 unsigned InstanceSize,
4135 const ObjCImplementationDecl *ID) {
4136 std::string ClassName = ID->getNameAsString();
4137 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
4138 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4139 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4140 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
4141 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004142 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4143 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004144 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004145 // const struct _method_list_t * const baseMethods;
4146 std::vector<llvm::Constant*> Methods;
4147 std::string MethodListName("\01l_OBJC_$_");
4148 if (flags & CLS_META) {
4149 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004150 for (ObjCImplementationDecl::classmeth_iterator
4151 i = ID->classmeth_begin(CGM.getContext()),
4152 e = ID->classmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004153 // Class methods should always be defined.
4154 Methods.push_back(GetMethodConstant(*i));
4155 }
4156 } else {
4157 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004158 for (ObjCImplementationDecl::instmeth_iterator
4159 i = ID->instmeth_begin(CGM.getContext()),
4160 e = ID->instmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004161 // Instance methods should always be defined.
4162 Methods.push_back(GetMethodConstant(*i));
4163 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00004164 for (ObjCImplementationDecl::propimpl_iterator
4165 i = ID->propimpl_begin(CGM.getContext()),
4166 e = ID->propimpl_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004167 ObjCPropertyImplDecl *PID = *i;
4168
4169 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4170 ObjCPropertyDecl *PD = PID->getPropertyDecl();
4171
4172 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4173 if (llvm::Constant *C = GetMethodConstant(MD))
4174 Methods.push_back(C);
4175 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4176 if (llvm::Constant *C = GetMethodConstant(MD))
4177 Methods.push_back(C);
4178 }
4179 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004180 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004181 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004182 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004183
4184 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4185 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
4186 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
4187 + OID->getNameAsString(),
4188 OID->protocol_begin(),
4189 OID->protocol_end());
4190
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004191 if (flags & CLS_META)
4192 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4193 else
4194 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004195 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4196 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004197 if (flags & CLS_META)
4198 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
4199 else
4200 Values[ 9] =
4201 EmitPropertyList(
4202 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
4203 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004204 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
4205 Values);
4206 llvm::GlobalVariable *CLASS_RO_GV =
4207 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
4208 llvm::GlobalValue::InternalLinkage,
4209 Init,
4210 (flags & CLS_META) ?
4211 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4212 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
4213 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004214 CLASS_RO_GV->setAlignment(
4215 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004216 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004217 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004218
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004219}
4220
4221/// BuildClassMetaData - This routine defines that to-level meta-data
4222/// for the given ClassName for:
4223/// struct _class_t {
4224/// struct _class_t *isa;
4225/// struct _class_t * const superclass;
4226/// void *cache;
4227/// IMP *vtable;
4228/// struct class_ro_t *ro;
4229/// }
4230///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004231llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
4232 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004233 llvm::Constant *IsAGV,
4234 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004235 llvm::Constant *ClassRoGV,
4236 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004237 std::vector<llvm::Constant*> Values(5);
4238 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004239 Values[1] = SuperClassGV
4240 ? SuperClassGV
4241 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004242 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4243 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4244 Values[4] = ClassRoGV; // &CLASS_RO_GV
4245 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
4246 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004247 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4248 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004249 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004250 GV->setAlignment(
4251 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004252 if (HiddenVisibility)
4253 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004254 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004255}
4256
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004257void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004258 uint32_t &InstanceStart,
4259 uint32_t &InstanceSize) {
Daniel Dunbar97776872009-04-22 07:32:20 +00004260 // Find first and last (non-padding) ivars in this interface.
4261
4262 // FIXME: Use iterator.
4263 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004264 GetNamedIvarList(OID->getClassInterface(), OIvars);
Daniel Dunbar97776872009-04-22 07:32:20 +00004265
4266 if (OIvars.empty()) {
4267 InstanceStart = InstanceSize = 0;
4268 return;
Daniel Dunbard4ae6c02009-04-22 04:39:47 +00004269 }
Daniel Dunbar97776872009-04-22 07:32:20 +00004270
4271 const ObjCIvarDecl *First = OIvars.front();
4272 const ObjCIvarDecl *Last = OIvars.back();
4273
4274 InstanceStart = ComputeIvarBaseOffset(CGM, OID, First);
4275 const llvm::Type *FieldTy =
4276 CGM.getTypes().ConvertTypeForMem(Last->getType());
4277 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004278// FIXME. This breaks compatibility with llvm-gcc-4.2 (but makes it compatible
4279// with gcc-4.2). We postpone this for now.
4280#if 0
4281 if (Last->isBitField()) {
4282 Expr *BitWidth = Last->getBitWidth();
4283 uint64_t BitFieldSize =
Eli Friedman9a901bb2009-04-26 19:19:15 +00004284 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004285 Size = (BitFieldSize / 8) + ((BitFieldSize % 8) != 0);
4286 }
4287#endif
Daniel Dunbar97776872009-04-22 07:32:20 +00004288 InstanceSize = ComputeIvarBaseOffset(CGM, OID, Last) + Size;
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004289}
4290
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004291void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4292 std::string ClassName = ID->getNameAsString();
4293 if (!ObjCEmptyCacheVar) {
4294 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004295 ObjCTypes.CacheTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004296 false,
4297 llvm::GlobalValue::ExternalLinkage,
4298 0,
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004299 "_objc_empty_cache",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004300 &CGM.getModule());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004301
4302 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004303 ObjCTypes.ImpnfABITy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004304 false,
4305 llvm::GlobalValue::ExternalLinkage,
4306 0,
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004307 "_objc_empty_vtable",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004308 &CGM.getModule());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004309 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004310 assert(ID->getClassInterface() &&
4311 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004312 // FIXME: Is this correct (that meta class size is never computed)?
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004313 uint32_t InstanceStart =
4314 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
4315 uint32_t InstanceSize = InstanceStart;
4316 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004317 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4318 std::string ObjCClassName(getClassSymbolPrefix());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004319
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004320 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004321
Daniel Dunbar04d40782009-04-14 06:00:08 +00004322 bool classIsHidden =
4323 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004324 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004325 flags |= OBJC2_CLS_HIDDEN;
4326 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004327 // class is root
4328 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004329 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004330 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004331 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004332 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004333 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4334 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4335 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004336 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004337 // work on super class metadata symbol.
4338 std::string SuperClassName =
4339 ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004340 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004341 }
4342 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4343 InstanceStart,
4344 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004345 std::string TClassName = ObjCMetaClassName + ClassName;
4346 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004347 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4348 classIsHidden);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004349
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004350 // Metadata for the class
4351 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004352 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004353 flags |= OBJC2_CLS_HIDDEN;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004354
4355 if (hasObjCExceptionAttribute(ID->getClassInterface()))
4356 flags |= CLS_EXCEPTION;
4357
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004358 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004359 flags |= CLS_ROOT;
4360 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00004361 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004362 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004363 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004364 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004365 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004366 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004367 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004368 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004369 InstanceStart,
4370 InstanceSize,
4371 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004372
4373 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004374 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004375 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4376 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004377 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004378
4379 // Force the definition of the EHType if necessary.
4380 if (flags & CLS_EXCEPTION)
4381 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004382}
4383
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004384/// GenerateProtocolRef - This routine is called to generate code for
4385/// a protocol reference expression; as in:
4386/// @code
4387/// @protocol(Proto1);
4388/// @endcode
4389/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4390/// which will hold address of the protocol meta-data.
4391///
4392llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
4393 const ObjCProtocolDecl *PD) {
4394
Fariborz Jahanian960cd062009-04-10 18:47:34 +00004395 // This routine is called for @protocol only. So, we must build definition
4396 // of protocol's meta-data (not a reference to it!)
4397 //
4398 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004399 ObjCTypes.ExternalProtocolPtrTy);
4400
4401 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
4402 ProtocolName += PD->getNameAsCString();
4403
4404 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
4405 if (PTGV)
4406 return Builder.CreateLoad(PTGV, false, "tmp");
4407 PTGV = new llvm::GlobalVariable(
4408 Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00004409 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004410 Init,
4411 ProtocolName,
4412 &CGM.getModule());
4413 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
4414 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4415 UsedGlobals.push_back(PTGV);
4416 return Builder.CreateLoad(PTGV, false, "tmp");
4417}
4418
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004419/// GenerateCategory - Build metadata for a category implementation.
4420/// struct _category_t {
4421/// const char * const name;
4422/// struct _class_t *const cls;
4423/// const struct _method_list_t * const instance_methods;
4424/// const struct _method_list_t * const class_methods;
4425/// const struct _protocol_list_t * const protocols;
4426/// const struct _prop_list_t * const properties;
4427/// }
4428///
4429void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
4430{
4431 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004432 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
4433 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004434 "_$_" + OCD->getNameAsString());
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004435 std::string ExtClassName(getClassSymbolPrefix() +
4436 Interface->getNameAsString());
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004437
4438 std::vector<llvm::Constant*> Values(6);
4439 Values[0] = GetClassName(OCD->getIdentifier());
4440 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004441 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004442 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004443 std::vector<llvm::Constant*> Methods;
4444 std::string MethodListName(Prefix);
4445 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
4446 "_$_" + OCD->getNameAsString();
4447
Douglas Gregor653f1b12009-04-23 01:02:12 +00004448 for (ObjCCategoryImplDecl::instmeth_iterator
4449 i = OCD->instmeth_begin(CGM.getContext()),
4450 e = OCD->instmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004451 // Instance methods should always be defined.
4452 Methods.push_back(GetMethodConstant(*i));
4453 }
4454
4455 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004456 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004457 Methods);
4458
4459 MethodListName = Prefix;
4460 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
4461 OCD->getNameAsString();
4462 Methods.clear();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004463 for (ObjCCategoryImplDecl::classmeth_iterator
4464 i = OCD->classmeth_begin(CGM.getContext()),
4465 e = OCD->classmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004466 // Class methods should always be defined.
4467 Methods.push_back(GetMethodConstant(*i));
4468 }
4469
4470 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004471 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004472 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004473 const ObjCCategoryDecl *Category =
4474 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004475 if (Category) {
4476 std::string ExtName(Interface->getNameAsString() + "_$_" +
4477 OCD->getNameAsString());
4478 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
4479 + Interface->getNameAsString() + "_$_"
4480 + Category->getNameAsString(),
4481 Category->protocol_begin(),
4482 Category->protocol_end());
4483 Values[5] =
4484 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
4485 OCD, Category, ObjCTypes);
4486 }
4487 else {
4488 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4489 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
4490 }
4491
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004492 llvm::Constant *Init =
4493 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
4494 Values);
4495 llvm::GlobalVariable *GCATV
4496 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
4497 false,
4498 llvm::GlobalValue::InternalLinkage,
4499 Init,
4500 ExtCatName,
4501 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004502 GCATV->setAlignment(
4503 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004504 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004505 UsedGlobals.push_back(GCATV);
4506 DefinedCategories.push_back(GCATV);
4507}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004508
4509/// GetMethodConstant - Return a struct objc_method constant for the
4510/// given method if it has been defined. The result is null if the
4511/// method has not been defined. The return value has type MethodPtrTy.
4512llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
4513 const ObjCMethodDecl *MD) {
4514 // FIXME: Use DenseMap::lookup
4515 llvm::Function *Fn = MethodDefinitions[MD];
4516 if (!Fn)
4517 return 0;
4518
4519 std::vector<llvm::Constant*> Method(3);
4520 Method[0] =
4521 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4522 ObjCTypes.SelectorPtrTy);
4523 Method[1] = GetMethodVarType(MD);
4524 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
4525 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
4526}
4527
4528/// EmitMethodList - Build meta-data for method declarations
4529/// struct _method_list_t {
4530/// uint32_t entsize; // sizeof(struct _objc_method)
4531/// uint32_t method_count;
4532/// struct _objc_method method_list[method_count];
4533/// }
4534///
4535llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
4536 const std::string &Name,
4537 const char *Section,
4538 const ConstantVector &Methods) {
4539 // Return null for empty list.
4540 if (Methods.empty())
4541 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
4542
4543 std::vector<llvm::Constant*> Values(3);
4544 // sizeof(struct _objc_method)
4545 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
4546 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4547 // method_count
4548 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
4549 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
4550 Methods.size());
4551 Values[2] = llvm::ConstantArray::get(AT, Methods);
4552 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4553
4554 llvm::GlobalVariable *GV =
4555 new llvm::GlobalVariable(Init->getType(), false,
4556 llvm::GlobalValue::InternalLinkage,
4557 Init,
4558 Name,
4559 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004560 GV->setAlignment(
4561 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004562 GV->setSection(Section);
4563 UsedGlobals.push_back(GV);
4564 return llvm::ConstantExpr::getBitCast(GV,
4565 ObjCTypes.MethodListnfABIPtrTy);
4566}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004567
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004568/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
4569/// the given ivar.
4570///
4571llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004572 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004573 const ObjCIvarDecl *Ivar) {
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00004574 std::string Name = "OBJC_IVAR_$_" +
Douglas Gregor6ab35242009-04-09 21:40:53 +00004575 getInterfaceDeclForIvar(ID, Ivar, CGM.getContext())->getNameAsString() +
4576 '.' + Ivar->getNameAsString();
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004577 llvm::GlobalVariable *IvarOffsetGV =
4578 CGM.getModule().getGlobalVariable(Name);
4579 if (!IvarOffsetGV)
4580 IvarOffsetGV =
4581 new llvm::GlobalVariable(ObjCTypes.LongTy,
4582 false,
4583 llvm::GlobalValue::ExternalLinkage,
4584 0,
4585 Name,
4586 &CGM.getModule());
4587 return IvarOffsetGV;
4588}
4589
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004590llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004591 const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004592 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004593 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00004594 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
4595 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
4596 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004597 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004598 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00004599
4600 // FIXME: This matches gcc, but shouldn't the visibility be set on
4601 // the use as well (i.e., in ObjCIvarOffsetVariable).
4602 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
4603 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
4604 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004605 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00004606 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00004607 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004608 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004609 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004610}
4611
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004612/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00004613/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004614/// IvarListnfABIPtrTy.
4615/// struct _ivar_t {
4616/// unsigned long int *offset; // pointer to ivar offset location
4617/// char *name;
4618/// char *type;
4619/// uint32_t alignment;
4620/// uint32_t size;
4621/// }
4622/// struct _ivar_list_t {
4623/// uint32 entsize; // sizeof(struct _ivar_t)
4624/// uint32 count;
4625/// struct _iver_t list[count];
4626/// }
4627///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004628
4629void CGObjCCommonMac::GetNamedIvarList(const ObjCInterfaceDecl *OID,
4630 llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const {
4631 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
4632 E = OID->ivar_end(); I != E; ++I) {
4633 // Ignore unnamed bit-fields.
4634 if (!(*I)->getDeclName())
4635 continue;
4636
4637 Res.push_back(*I);
4638 }
4639
4640 for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(CGM.getContext()),
4641 E = OID->prop_end(CGM.getContext()); I != E; ++I)
4642 if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl())
4643 Res.push_back(IV);
4644}
4645
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004646llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
4647 const ObjCImplementationDecl *ID) {
4648
4649 std::vector<llvm::Constant*> Ivars, Ivar(5);
4650
4651 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4652 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
4653
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004654 // FIXME. Consolidate this with similar code in GenerateClass.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00004655
Daniel Dunbar91636d62009-04-20 00:33:43 +00004656 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00004657 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004658 GetNamedIvarList(OID, OIvars);
Fariborz Jahanian99eee362009-04-01 19:37:34 +00004659
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004660 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
4661 ObjCIvarDecl *IVD = OIvars[i];
Daniel Dunbar3eec8aa2009-04-20 05:53:40 +00004662 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004663 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004664 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
4665 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004666 const llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004667 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004668 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
4669 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004670 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004671 Align = llvm::Log2_32(Align);
4672 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00004673 // NOTE. Size of a bitfield does not match gcc's, because of the
4674 // way bitfields are treated special in each. But I am told that
4675 // 'size' for bitfield ivars is ignored by the runtime so it does
4676 // not matter. If it matters, there is enough info to get the
4677 // bitfield right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004678 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4679 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
4680 }
4681 // Return null for empty list.
4682 if (Ivars.empty())
4683 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4684 std::vector<llvm::Constant*> Values(3);
4685 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
4686 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4687 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
4688 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
4689 Ivars.size());
4690 Values[2] = llvm::ConstantArray::get(AT, Ivars);
4691 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4692 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4693 llvm::GlobalVariable *GV =
4694 new llvm::GlobalVariable(Init->getType(), false,
4695 llvm::GlobalValue::InternalLinkage,
4696 Init,
4697 Prefix + OID->getNameAsString(),
4698 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004699 GV->setAlignment(
4700 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004701 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004702
4703 UsedGlobals.push_back(GV);
4704 return llvm::ConstantExpr::getBitCast(GV,
4705 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004706}
4707
4708llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4709 const ObjCProtocolDecl *PD) {
4710 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4711
4712 if (!Entry) {
4713 // We use the initializer as a marker of whether this is a forward
4714 // reference or not. At module finalization we add the empty
4715 // contents for protocols which were referenced but never defined.
4716 Entry =
4717 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
4718 llvm::GlobalValue::ExternalLinkage,
4719 0,
4720 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
4721 &CGM.getModule());
4722 Entry->setSection("__DATA,__datacoal_nt,coalesced");
4723 UsedGlobals.push_back(Entry);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004724 }
4725
4726 return Entry;
4727}
4728
4729/// GetOrEmitProtocol - Generate the protocol meta-data:
4730/// @code
4731/// struct _protocol_t {
4732/// id isa; // NULL
4733/// const char * const protocol_name;
4734/// const struct _protocol_list_t * protocol_list; // super protocols
4735/// const struct method_list_t * const instance_methods;
4736/// const struct method_list_t * const class_methods;
4737/// const struct method_list_t *optionalInstanceMethods;
4738/// const struct method_list_t *optionalClassMethods;
4739/// const struct _prop_list_t * properties;
4740/// const uint32_t size; // sizeof(struct _protocol_t)
4741/// const uint32_t flags; // = 0
4742/// }
4743/// @endcode
4744///
4745
4746llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4747 const ObjCProtocolDecl *PD) {
4748 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4749
4750 // Early exit if a defining object has already been generated.
4751 if (Entry && Entry->hasInitializer())
4752 return Entry;
4753
4754 const char *ProtocolName = PD->getNameAsCString();
4755
4756 // Construct method lists.
4757 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4758 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00004759 for (ObjCProtocolDecl::instmeth_iterator
4760 i = PD->instmeth_begin(CGM.getContext()),
4761 e = PD->instmeth_end(CGM.getContext());
4762 i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004763 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004764 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004765 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4766 OptInstanceMethods.push_back(C);
4767 } else {
4768 InstanceMethods.push_back(C);
4769 }
4770 }
4771
Douglas Gregor6ab35242009-04-09 21:40:53 +00004772 for (ObjCProtocolDecl::classmeth_iterator
4773 i = PD->classmeth_begin(CGM.getContext()),
4774 e = PD->classmeth_end(CGM.getContext());
4775 i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004776 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004777 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004778 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4779 OptClassMethods.push_back(C);
4780 } else {
4781 ClassMethods.push_back(C);
4782 }
4783 }
4784
4785 std::vector<llvm::Constant*> Values(10);
4786 // isa is NULL
4787 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
4788 Values[1] = GetClassName(PD->getIdentifier());
4789 Values[2] = EmitProtocolList(
4790 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4791 PD->protocol_begin(),
4792 PD->protocol_end());
4793
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004794 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004795 + PD->getNameAsString(),
4796 "__DATA, __objc_const",
4797 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004798 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004799 + PD->getNameAsString(),
4800 "__DATA, __objc_const",
4801 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004802 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004803 + PD->getNameAsString(),
4804 "__DATA, __objc_const",
4805 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004806 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004807 + PD->getNameAsString(),
4808 "__DATA, __objc_const",
4809 OptClassMethods);
4810 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4811 0, PD, ObjCTypes);
4812 uint32_t Size =
4813 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
4814 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4815 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
4816 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
4817 Values);
4818
4819 if (Entry) {
4820 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00004821 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004822 Entry->setInitializer(Init);
4823 } else {
4824 Entry =
4825 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004826 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004827 Init,
4828 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
4829 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004830 Entry->setAlignment(
4831 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004832 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004833 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004834 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
4835
4836 // Use this protocol meta-data to build protocol list table in section
4837 // __DATA, __objc_protolist
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004838 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004839 ObjCTypes.ProtocolnfABIPtrTy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004840 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004841 Entry,
4842 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
4843 +ProtocolName,
4844 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004845 PTGV->setAlignment(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004846 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00004847 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004848 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4849 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004850 return Entry;
4851}
4852
4853/// EmitProtocolList - Generate protocol list meta-data:
4854/// @code
4855/// struct _protocol_list_t {
4856/// long protocol_count; // Note, this is 32/64 bit
4857/// struct _protocol_t[protocol_count];
4858/// }
4859/// @endcode
4860///
4861llvm::Constant *
4862CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4863 ObjCProtocolDecl::protocol_iterator begin,
4864 ObjCProtocolDecl::protocol_iterator end) {
4865 std::vector<llvm::Constant*> ProtocolRefs;
4866
Fariborz Jahanianda320092009-01-29 19:24:30 +00004867 // Just return null for empty protocol lists
Daniel Dunbar948e2582009-02-15 07:36:20 +00004868 if (begin == end)
Fariborz Jahanianda320092009-01-29 19:24:30 +00004869 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4870
Daniel Dunbar948e2582009-02-15 07:36:20 +00004871 // FIXME: We shouldn't need to do this lookup here, should we?
Fariborz Jahanianda320092009-01-29 19:24:30 +00004872 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4873 if (GV)
Daniel Dunbar948e2582009-02-15 07:36:20 +00004874 return llvm::ConstantExpr::getBitCast(GV,
4875 ObjCTypes.ProtocolListnfABIPtrTy);
4876
4877 for (; begin != end; ++begin)
4878 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4879
Fariborz Jahanianda320092009-01-29 19:24:30 +00004880 // This list is null terminated.
4881 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004882 ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004883
4884 std::vector<llvm::Constant*> Values(2);
4885 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4886 Values[1] =
Daniel Dunbar948e2582009-02-15 07:36:20 +00004887 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004888 ProtocolRefs.size()),
4889 ProtocolRefs);
4890
4891 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4892 GV = new llvm::GlobalVariable(Init->getType(), false,
4893 llvm::GlobalValue::InternalLinkage,
4894 Init,
4895 Name,
4896 &CGM.getModule());
4897 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004898 GV->setAlignment(
4899 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004900 UsedGlobals.push_back(GV);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004901 return llvm::ConstantExpr::getBitCast(GV,
4902 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004903}
4904
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004905/// GetMethodDescriptionConstant - This routine build following meta-data:
4906/// struct _objc_method {
4907/// SEL _cmd;
4908/// char *method_type;
4909/// char *_imp;
4910/// }
4911
4912llvm::Constant *
4913CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4914 std::vector<llvm::Constant*> Desc(3);
4915 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4916 ObjCTypes.SelectorPtrTy);
4917 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004918 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004919 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4920 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4921}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004922
4923/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4924/// This code gen. amounts to generating code for:
4925/// @code
4926/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4927/// @encode
4928///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004929LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004930 CodeGen::CodeGenFunction &CGF,
4931 QualType ObjectTy,
4932 llvm::Value *BaseValue,
4933 const ObjCIvarDecl *Ivar,
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004934 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00004935 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00004936 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
4937 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004938}
4939
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004940llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
4941 CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00004942 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004943 const ObjCIvarDecl *Ivar) {
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00004944 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),
4945 false, "ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004946}
4947
Fariborz Jahanian46551122009-02-04 00:22:57 +00004948CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4949 CodeGen::CodeGenFunction &CGF,
4950 QualType ResultType,
4951 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004952 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00004953 QualType Arg0Ty,
4954 bool IsSuper,
4955 const CallArgList &CallArgs) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004956 // FIXME. Even though IsSuper is passes. This function doese not
4957 // handle calls to 'super' receivers.
4958 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004959 llvm::Value *Arg0 = Receiver;
4960 if (!IsSuper)
4961 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004962
4963 // Find the message function name.
Fariborz Jahanianef163782009-02-05 01:13:09 +00004964 // FIXME. This is too much work to get the ABI-specific result type
4965 // needed to find the message name.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004966 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4967 llvm::SmallVector<QualType, 16>());
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00004968 llvm::Constant *Fn = 0;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004969 std::string Name("\01l_");
4970 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004971#if 0
4972 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004973 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00004974 Fn = ObjCTypes.getMessageSendIdStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004975 // FIXME. Is there a better way of getting these names.
4976 // They are available in RuntimeFunctions vector pair.
4977 Name += "objc_msgSendId_stret_fixup";
4978 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004979 else
4980#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004981 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00004982 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004983 Name += "objc_msgSendSuper2_stret_fixup";
4984 }
4985 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004986 {
Chris Lattner1c02f862009-04-22 02:53:24 +00004987 Fn = ObjCTypes.getMessageSendStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004988 Name += "objc_msgSend_stret_fixup";
4989 }
4990 }
Fariborz Jahanian5b2bad02009-04-30 16:31:11 +00004991 else if (!IsSuper && ResultType->isFloatingType()) {
4992 if (const BuiltinType *BT = ResultType->getAsBuiltinType()) {
4993 BuiltinType::Kind k = BT->getKind();
4994 if (k == BuiltinType::LongDouble) {
4995 Fn = ObjCTypes.getMessageSendFpretFixupFn();
4996 Name += "objc_msgSend_fpret_fixup";
4997 }
4998 else {
4999 Fn = ObjCTypes.getMessageSendFixupFn();
5000 Name += "objc_msgSend_fixup";
5001 }
5002 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005003 }
5004 else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005005#if 0
5006// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005007 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005008 Fn = ObjCTypes.getMessageSendIdFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005009 Name += "objc_msgSendId_fixup";
5010 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005011 else
5012#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005013 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005014 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005015 Name += "objc_msgSendSuper2_fixup";
5016 }
5017 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005018 {
Chris Lattner1c02f862009-04-22 02:53:24 +00005019 Fn = ObjCTypes.getMessageSendFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005020 Name += "objc_msgSend_fixup";
5021 }
5022 }
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005023 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005024 Name += '_';
5025 std::string SelName(Sel.getAsString());
5026 // Replace all ':' in selector name with '_' ouch!
5027 for(unsigned i = 0; i < SelName.size(); i++)
5028 if (SelName[i] == ':')
5029 SelName[i] = '_';
5030 Name += SelName;
5031 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5032 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005033 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005034 std::vector<llvm::Constant*> Values(2);
5035 Values[0] = Fn;
5036 Values[1] = GetMethodVarName(Sel);
5037 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
5038 GV = new llvm::GlobalVariable(Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005039 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005040 Init,
5041 Name,
5042 &CGM.getModule());
5043 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005044 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005045 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005046 }
5047 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005048
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005049 CallArgList ActualArgs;
5050 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
5051 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
5052 ObjCTypes.MessageRefCPtrTy));
5053 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005054 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
5055 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5056 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00005057 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005058 Callee = CGF.Builder.CreateBitCast(Callee,
5059 llvm::PointerType::getUnqual(FTy));
5060 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005061}
5062
5063/// Generate code for a message send expression in the nonfragile abi.
5064CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
5065 CodeGen::CodeGenFunction &CGF,
5066 QualType ResultType,
5067 Selector Sel,
5068 llvm::Value *Receiver,
5069 bool IsClassMessage,
5070 const CallArgList &CallArgs) {
Fariborz Jahanian46551122009-02-04 00:22:57 +00005071 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005072 Receiver, CGF.getContext().getObjCIdType(),
Fariborz Jahanian46551122009-02-04 00:22:57 +00005073 false, CallArgs);
5074}
5075
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005076llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005077CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005078 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5079
Daniel Dunbardfff2302009-03-02 05:18:14 +00005080 if (!GV) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005081 GV = new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
5082 llvm::GlobalValue::ExternalLinkage,
5083 0, Name, &CGM.getModule());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005084 }
5085
5086 return GV;
5087}
5088
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005089llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005090 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005091 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
5092
5093 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005094 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005095 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005096 Entry =
5097 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5098 llvm::GlobalValue::InternalLinkage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005099 ClassGV,
Daniel Dunbar11394522009-04-18 08:51:00 +00005100 "\01L_OBJC_CLASSLIST_REFERENCES_$_",
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005101 &CGM.getModule());
5102 Entry->setAlignment(
5103 CGM.getTargetData().getPrefTypeAlignment(
5104 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005105 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
5106 UsedGlobals.push_back(Entry);
5107 }
5108
5109 return Builder.CreateLoad(Entry, false, "tmp");
5110}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005111
Daniel Dunbar11394522009-04-18 08:51:00 +00005112llvm::Value *
5113CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
5114 const ObjCInterfaceDecl *ID) {
5115 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
5116
5117 if (!Entry) {
5118 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5119 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5120 Entry =
5121 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5122 llvm::GlobalValue::InternalLinkage,
5123 ClassGV,
5124 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
5125 &CGM.getModule());
5126 Entry->setAlignment(
5127 CGM.getTargetData().getPrefTypeAlignment(
5128 ObjCTypes.ClassnfABIPtrTy));
5129 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005130 UsedGlobals.push_back(Entry);
5131 }
5132
5133 return Builder.CreateLoad(Entry, false, "tmp");
5134}
5135
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005136/// EmitMetaClassRef - Return a Value * of the address of _class_t
5137/// meta-data
5138///
5139llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5140 const ObjCInterfaceDecl *ID) {
5141 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5142 if (Entry)
5143 return Builder.CreateLoad(Entry, false, "tmp");
5144
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005145 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005146 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005147 Entry =
5148 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5149 llvm::GlobalValue::InternalLinkage,
5150 MetaClassGV,
5151 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
5152 &CGM.getModule());
5153 Entry->setAlignment(
5154 CGM.getTargetData().getPrefTypeAlignment(
5155 ObjCTypes.ClassnfABIPtrTy));
5156
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005157 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005158 UsedGlobals.push_back(Entry);
5159
5160 return Builder.CreateLoad(Entry, false, "tmp");
5161}
5162
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005163/// GetClass - Return a reference to the class for the given interface
5164/// decl.
5165llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5166 const ObjCInterfaceDecl *ID) {
5167 return EmitClassRef(Builder, ID);
5168}
5169
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005170/// Generates a message send where the super is the receiver. This is
5171/// a message send to self with special delivery semantics indicating
5172/// which class's method should be called.
5173CodeGen::RValue
5174CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
5175 QualType ResultType,
5176 Selector Sel,
5177 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005178 bool isCategoryImpl,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005179 llvm::Value *Receiver,
5180 bool IsClassMessage,
5181 const CodeGen::CallArgList &CallArgs) {
5182 // ...
5183 // Create and init a super structure; this is a (receiver, class)
5184 // pair we will pass to objc_msgSendSuper.
5185 llvm::Value *ObjCSuper =
5186 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
5187
5188 llvm::Value *ReceiverAsObject =
5189 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5190 CGF.Builder.CreateStore(ReceiverAsObject,
5191 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
5192
5193 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005194 llvm::Value *Target;
5195 if (IsClassMessage) {
5196 if (isCategoryImpl) {
5197 // Message sent to "super' in a class method defined in
5198 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005199 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005200 Target = CGF.Builder.CreateStructGEP(Target, 0);
5201 Target = CGF.Builder.CreateLoad(Target);
5202 }
5203 else
5204 Target = EmitMetaClassRef(CGF.Builder, Class);
5205 }
5206 else
Daniel Dunbar11394522009-04-18 08:51:00 +00005207 Target = EmitSuperClassRef(CGF.Builder, Class);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005208
5209 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
5210 // and ObjCTypes types.
5211 const llvm::Type *ClassTy =
5212 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5213 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5214 CGF.Builder.CreateStore(Target,
5215 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
5216
5217 return EmitMessageSend(CGF, ResultType, Sel,
5218 ObjCSuper, ObjCTypes.SuperPtrCTy,
5219 true, CallArgs);
5220}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005221
5222llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
5223 Selector Sel) {
5224 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
5225
5226 if (!Entry) {
5227 llvm::Constant *Casted =
5228 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5229 ObjCTypes.SelectorPtrTy);
5230 Entry =
5231 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
5232 llvm::GlobalValue::InternalLinkage,
5233 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
5234 &CGM.getModule());
5235 Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip");
5236 UsedGlobals.push_back(Entry);
5237 }
5238
5239 return Builder.CreateLoad(Entry, false, "tmp");
5240}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005241/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
5242/// objc_assign_ivar (id src, id *dst)
5243///
5244void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
5245 llvm::Value *src, llvm::Value *dst)
5246{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005247 const llvm::Type * SrcTy = src->getType();
5248 if (!isa<llvm::PointerType>(SrcTy)) {
5249 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5250 assert(Size <= 8 && "does not support size > 8");
5251 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5252 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005253 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5254 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005255 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5256 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005257 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005258 src, dst, "assignivar");
5259 return;
5260}
5261
5262/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5263/// objc_assign_strongCast (id src, id *dst)
5264///
5265void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
5266 CodeGen::CodeGenFunction &CGF,
5267 llvm::Value *src, llvm::Value *dst)
5268{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005269 const llvm::Type * SrcTy = src->getType();
5270 if (!isa<llvm::PointerType>(SrcTy)) {
5271 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5272 assert(Size <= 8 && "does not support size > 8");
5273 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5274 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005275 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5276 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005277 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5278 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005279 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005280 src, dst, "weakassign");
5281 return;
5282}
5283
5284/// EmitObjCWeakRead - Code gen for loading value of a __weak
5285/// object: objc_read_weak (id *src)
5286///
5287llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
5288 CodeGen::CodeGenFunction &CGF,
5289 llvm::Value *AddrWeakObj)
5290{
Eli Friedman8339b352009-03-07 03:57:15 +00005291 const llvm::Type* DestTy =
5292 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005293 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005294 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005295 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005296 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005297 return read_weak;
5298}
5299
5300/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5301/// objc_assign_weak (id src, id *dst)
5302///
5303void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
5304 llvm::Value *src, llvm::Value *dst)
5305{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005306 const llvm::Type * SrcTy = src->getType();
5307 if (!isa<llvm::PointerType>(SrcTy)) {
5308 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5309 assert(Size <= 8 && "does not support size > 8");
5310 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5311 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005312 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5313 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005314 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5315 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005316 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005317 src, dst, "weakassign");
5318 return;
5319}
5320
5321/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5322/// objc_assign_global (id src, id *dst)
5323///
5324void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
5325 llvm::Value *src, llvm::Value *dst)
5326{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005327 const llvm::Type * SrcTy = src->getType();
5328 if (!isa<llvm::PointerType>(SrcTy)) {
5329 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5330 assert(Size <= 8 && "does not support size > 8");
5331 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5332 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005333 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5334 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005335 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5336 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005337 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005338 src, dst, "globalassign");
5339 return;
5340}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005341
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005342void
5343CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5344 const Stmt &S) {
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005345 bool isTry = isa<ObjCAtTryStmt>(S);
5346 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
5347 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005348 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005349 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005350 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005351 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
5352
5353 // For @synchronized, call objc_sync_enter(sync.expr). The
5354 // evaluation of the expression must occur before we enter the
5355 // @synchronized. We can safely avoid a temp here because jumps into
5356 // @synchronized are illegal & this will dominate uses.
5357 llvm::Value *SyncArg = 0;
5358 if (!isTry) {
5359 SyncArg =
5360 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
5361 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005362 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005363 }
5364
5365 // Push an EH context entry, used for handling rethrows and jumps
5366 // through finally.
5367 CGF.PushCleanupBlock(FinallyBlock);
5368
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005369 CGF.setInvokeDest(TryHandler);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005370
5371 CGF.EmitBlock(TryBlock);
5372 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
5373 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
5374 CGF.EmitBranchThroughCleanup(FinallyEnd);
5375
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005376 // Emit the exception handler.
5377
5378 CGF.EmitBlock(TryHandler);
5379
5380 llvm::Value *llvm_eh_exception =
5381 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
5382 llvm::Value *llvm_eh_selector_i64 =
5383 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64);
5384 llvm::Value *llvm_eh_typeid_for_i64 =
5385 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64);
5386 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5387 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow");
5388
5389 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
5390 SelectorArgs.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005391 SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005392
5393 // Construct the lists of (type, catch body) to handle.
Daniel Dunbarede8de92009-03-06 00:01:21 +00005394 llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005395 bool HasCatchAll = false;
5396 if (isTry) {
5397 if (const ObjCAtCatchStmt* CatchStmt =
5398 cast<ObjCAtTryStmt>(S).getCatchStmts()) {
5399 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005400 const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Steve Naroff7ba138a2009-03-03 19:52:17 +00005401 Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody()));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005402
5403 // catch(...) always matches.
Steve Naroff7ba138a2009-03-03 19:52:17 +00005404 if (!CatchDecl) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005405 // Use i8* null here to signal this is a catch all, not a cleanup.
5406 llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
5407 SelectorArgs.push_back(Null);
5408 HasCatchAll = true;
5409 break;
5410 }
5411
Daniel Dunbarede8de92009-03-06 00:01:21 +00005412 if (CGF.getContext().isObjCIdType(CatchDecl->getType()) ||
5413 CatchDecl->getType()->isObjCQualifiedIdType()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005414 llvm::Value *IDEHType =
5415 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5416 if (!IDEHType)
5417 IDEHType =
5418 new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5419 llvm::GlobalValue::ExternalLinkage,
5420 0, "OBJC_EHTYPE_id", &CGM.getModule());
5421 SelectorArgs.push_back(IDEHType);
5422 HasCatchAll = true;
5423 break;
5424 }
5425
5426 // All other types should be Objective-C interface pointer types.
Daniel Dunbarede8de92009-03-06 00:01:21 +00005427 const PointerType *PT = CatchDecl->getType()->getAsPointerType();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005428 assert(PT && "Invalid @catch type.");
5429 const ObjCInterfaceType *IT =
5430 PT->getPointeeType()->getAsObjCInterfaceType();
5431 assert(IT && "Invalid @catch type.");
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005432 llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005433 SelectorArgs.push_back(EHType);
5434 }
5435 }
5436 }
5437
5438 // We use a cleanup unless there was already a catch all.
5439 if (!HasCatchAll) {
5440 SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
Daniel Dunbarede8de92009-03-06 00:01:21 +00005441 Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005442 }
5443
5444 llvm::Value *Selector =
5445 CGF.Builder.CreateCall(llvm_eh_selector_i64,
5446 SelectorArgs.begin(), SelectorArgs.end(),
5447 "selector");
5448 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005449 const ParmVarDecl *CatchParam = Handlers[i].first;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005450 const Stmt *CatchBody = Handlers[i].second;
5451
5452 llvm::BasicBlock *Next = 0;
5453
5454 // The last handler always matches.
5455 if (i + 1 != e) {
5456 assert(CatchParam && "Only last handler can be a catch all.");
5457
5458 llvm::BasicBlock *Match = CGF.createBasicBlock("match");
5459 Next = CGF.createBasicBlock("catch.next");
5460 llvm::Value *Id =
5461 CGF.Builder.CreateCall(llvm_eh_typeid_for_i64,
5462 CGF.Builder.CreateBitCast(SelectorArgs[i+2],
5463 ObjCTypes.Int8PtrTy));
5464 CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id),
5465 Match, Next);
5466
5467 CGF.EmitBlock(Match);
5468 }
5469
5470 if (CatchBody) {
5471 llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end");
5472 llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler");
5473
5474 // Cleanups must call objc_end_catch.
5475 //
5476 // FIXME: It seems incorrect for objc_begin_catch to be inside
5477 // this context, but this matches gcc.
5478 CGF.PushCleanupBlock(MatchEnd);
5479 CGF.setInvokeDest(MatchHandler);
5480
5481 llvm::Value *ExcObject =
Chris Lattner8a569112009-04-22 02:15:23 +00005482 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005483
5484 // Bind the catch parameter if it exists.
5485 if (CatchParam) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005486 ExcObject =
5487 CGF.Builder.CreateBitCast(ExcObject,
5488 CGF.ConvertType(CatchParam->getType()));
5489 // CatchParam is a ParmVarDecl because of the grammar
5490 // construction used to handle this, but for codegen purposes
5491 // we treat this as a local decl.
5492 CGF.EmitLocalBlockVarDecl(*CatchParam);
5493 CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005494 }
5495
5496 CGF.ObjCEHValueStack.push_back(ExcObject);
5497 CGF.EmitStmt(CatchBody);
5498 CGF.ObjCEHValueStack.pop_back();
5499
5500 CGF.EmitBranchThroughCleanup(FinallyEnd);
5501
5502 CGF.EmitBlock(MatchHandler);
5503
5504 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5505 // We are required to emit this call to satisfy LLVM, even
5506 // though we don't use the result.
5507 llvm::SmallVector<llvm::Value*, 8> Args;
5508 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005509 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005510 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
5511 0));
5512 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5513 CGF.Builder.CreateStore(Exc, RethrowPtr);
5514 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5515
5516 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5517
5518 CGF.EmitBlock(MatchEnd);
5519
5520 // Unfortunately, we also have to generate another EH frame here
5521 // in case this throws.
5522 llvm::BasicBlock *MatchEndHandler =
5523 CGF.createBasicBlock("match.end.handler");
5524 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattner8a569112009-04-22 02:15:23 +00005525 CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005526 Cont, MatchEndHandler,
5527 Args.begin(), Args.begin());
5528
5529 CGF.EmitBlock(Cont);
5530 if (Info.SwitchBlock)
5531 CGF.EmitBlock(Info.SwitchBlock);
5532 if (Info.EndBlock)
5533 CGF.EmitBlock(Info.EndBlock);
5534
5535 CGF.EmitBlock(MatchEndHandler);
5536 Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5537 // We are required to emit this call to satisfy LLVM, even
5538 // though we don't use the result.
5539 Args.clear();
5540 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005541 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005542 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
5543 0));
5544 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5545 CGF.Builder.CreateStore(Exc, RethrowPtr);
5546 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5547
5548 if (Next)
5549 CGF.EmitBlock(Next);
5550 } else {
5551 assert(!Next && "catchup should be last handler.");
5552
5553 CGF.Builder.CreateStore(Exc, RethrowPtr);
5554 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5555 }
5556 }
5557
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005558 // Pop the cleanup entry, the @finally is outside this cleanup
5559 // scope.
5560 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5561 CGF.setInvokeDest(PrevLandingPad);
5562
5563 CGF.EmitBlock(FinallyBlock);
5564
5565 if (isTry) {
5566 if (const ObjCAtFinallyStmt* FinallyStmt =
5567 cast<ObjCAtTryStmt>(S).getFinallyStmt())
5568 CGF.EmitStmt(FinallyStmt->getFinallyBody());
5569 } else {
5570 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
5571 // @synchronized.
Chris Lattnerbbccd612009-04-22 02:38:11 +00005572 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005573 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005574
5575 if (Info.SwitchBlock)
5576 CGF.EmitBlock(Info.SwitchBlock);
5577 if (Info.EndBlock)
5578 CGF.EmitBlock(Info.EndBlock);
5579
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005580 // Branch around the rethrow code.
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005581 CGF.EmitBranch(FinallyEnd);
5582
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005583 CGF.EmitBlock(FinallyRethrow);
Chris Lattner8a569112009-04-22 02:15:23 +00005584 CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005585 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005586 CGF.Builder.CreateUnreachable();
5587
5588 CGF.EmitBlock(FinallyEnd);
5589}
5590
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005591/// EmitThrowStmt - Generate code for a throw statement.
5592void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
5593 const ObjCAtThrowStmt &S) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005594 llvm::Value *Exception;
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005595 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005596 Exception = CGF.EmitScalarExpr(ThrowExpr);
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005597 } else {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005598 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
5599 "Unexpected rethrow outside @catch block.");
5600 Exception = CGF.ObjCEHValueStack.back();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005601 }
5602
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005603 llvm::Value *ExceptionAsObject =
5604 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
5605 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
5606 if (InvokeDest) {
5607 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattnerbbccd612009-04-22 02:38:11 +00005608 CGF.Builder.CreateInvoke(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005609 Cont, InvokeDest,
5610 &ExceptionAsObject, &ExceptionAsObject + 1);
5611 CGF.EmitBlock(Cont);
5612 } else
Chris Lattnerbbccd612009-04-22 02:38:11 +00005613 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005614 CGF.Builder.CreateUnreachable();
5615
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005616 // Clear the insertion point to indicate we are in unreachable code.
5617 CGF.Builder.ClearInsertionPoint();
5618}
Daniel Dunbare588b992009-03-01 04:46:24 +00005619
5620llvm::Value *
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005621CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
5622 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00005623 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00005624
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005625 // If we don't need a definition, return the entry if found or check
5626 // if we use an external reference.
5627 if (!ForDefinition) {
5628 if (Entry)
5629 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00005630
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005631 // If this type (or a super class) has the __objc_exception__
5632 // attribute, emit an external reference.
5633 if (hasObjCExceptionAttribute(ID))
5634 return Entry =
5635 new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5636 llvm::GlobalValue::ExternalLinkage,
5637 0,
5638 (std::string("OBJC_EHTYPE_$_") +
5639 ID->getIdentifier()->getName()),
5640 &CGM.getModule());
5641 }
5642
5643 // Otherwise we need to either make a new entry or fill in the
5644 // initializer.
5645 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005646 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00005647 std::string VTableName = "objc_ehtype_vtable";
5648 llvm::GlobalVariable *VTableGV =
5649 CGM.getModule().getGlobalVariable(VTableName);
5650 if (!VTableGV)
5651 VTableGV = new llvm::GlobalVariable(ObjCTypes.Int8PtrTy, false,
5652 llvm::GlobalValue::ExternalLinkage,
5653 0, VTableName, &CGM.getModule());
5654
5655 llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2);
5656
5657 std::vector<llvm::Constant*> Values(3);
5658 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
5659 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005660 Values[2] = GetClassGlobal(ClassName);
Daniel Dunbare588b992009-03-01 04:46:24 +00005661 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
5662
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005663 if (Entry) {
5664 Entry->setInitializer(Init);
5665 } else {
5666 Entry = new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5667 llvm::GlobalValue::WeakAnyLinkage,
5668 Init,
5669 (std::string("OBJC_EHTYPE_$_") +
5670 ID->getIdentifier()->getName()),
5671 &CGM.getModule());
5672 }
5673
Daniel Dunbar04d40782009-04-14 06:00:08 +00005674 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005675 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005676 Entry->setAlignment(8);
5677
5678 if (ForDefinition) {
5679 Entry->setSection("__DATA,__objc_const");
5680 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
5681 } else {
5682 Entry->setSection("__DATA,__datacoal_nt,coalesced");
5683 }
Daniel Dunbare588b992009-03-01 04:46:24 +00005684
5685 return Entry;
5686}
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005687
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005688/* *** */
5689
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005690CodeGen::CGObjCRuntime *
5691CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00005692 return new CGObjCMac(CGM);
5693}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005694
5695CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005696CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005697 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005698}