blob: a7fc87c24c59a1b4eb3638a00117da1b8274cd67 [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"
Chris Lattner16f00492009-04-26 01:32:48 +000021#include "clang/AST/StmtObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000022#include "clang/Basic/LangOptions.h"
23
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +000024#include "llvm/Intrinsics.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000025#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000026#include "llvm/ADT/DenseSet.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000027#include "llvm/Target/TargetData.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000028#include <sstream>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000029
30using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000031using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000032
Daniel Dunbar97776872009-04-22 07:32:20 +000033// Common CGObjCRuntime functions, these don't belong here, but they
34// don't belong in CGObjCRuntime either so we will live with it for
35// now.
36
Daniel Dunbar84ad77a2009-04-22 09:39:34 +000037const llvm::StructType *
38CGObjCRuntime::GetConcreteClassStruct(CodeGen::CodeGenModule &CGM,
39 const ObjCInterfaceDecl *OID) {
40 assert(!OID->isForwardDecl() && "Invalid interface decl!");
Daniel Dunbar412f59b2009-04-22 10:28:39 +000041 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
42 return cast<llvm::StructType>(CGM.getTypes().ConvertTagDeclType(RD));
Daniel Dunbar84ad77a2009-04-22 09:39:34 +000043}
44
Daniel Dunbara2435782009-04-22 12:00:04 +000045
46/// LookupFieldDeclForIvar - looks up a field decl in the laid out
47/// storage which matches this 'ivar'.
48///
49static const FieldDecl *LookupFieldDeclForIvar(ASTContext &Context,
50 const ObjCInterfaceDecl *OID,
Daniel Dunbara80a0f62009-04-22 17:43:55 +000051 const ObjCIvarDecl *OIVD,
52 const ObjCInterfaceDecl *&Found) {
Daniel Dunbara2435782009-04-22 12:00:04 +000053 assert(!OID->isForwardDecl() && "Invalid interface decl!");
54 const RecordDecl *RecordForDecl = Context.addRecordToClass(OID);
55 assert(RecordForDecl && "lookupFieldDeclForIvar no storage for class");
56 DeclContext::lookup_const_result Lookup =
57 RecordForDecl->lookup(Context, OIVD->getDeclName());
Daniel Dunbara80a0f62009-04-22 17:43:55 +000058
59 if (Lookup.first != Lookup.second) {
60 Found = OID;
61 return cast<FieldDecl>(*Lookup.first);
62 }
63
64 // If lookup failed, try the superclass.
65 //
66 // FIXME: This is slow, we shouldn't need to do this.
67 const ObjCInterfaceDecl *Super = OID->getSuperClass();
68 assert(OID && "field decl not found!");
69 return LookupFieldDeclForIvar(Context, Super, OIVD, Found);
Daniel Dunbara2435782009-04-22 12:00:04 +000070}
71
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000072static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM,
73 const ObjCInterfaceDecl *OID,
74 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +000075 assert(!OID->isForwardDecl() && "Invalid interface decl!");
Daniel Dunbara80a0f62009-04-22 17:43:55 +000076 const ObjCInterfaceDecl *Container;
77 const FieldDecl *Field =
78 LookupFieldDeclForIvar(CGM.getContext(), OID, Ivar, Container);
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000079 const llvm::StructType *STy =
80 CGObjCRuntime::GetConcreteClassStruct(CGM, Container);
Daniel Dunbar97776872009-04-22 07:32:20 +000081 const llvm::StructLayout *Layout =
Daniel Dunbar84ad77a2009-04-22 09:39:34 +000082 CGM.getTargetData().getStructLayout(STy);
Daniel Dunbar97776872009-04-22 07:32:20 +000083 if (!Field->isBitField())
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000084 return Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field)) * 8;
Daniel Dunbar97776872009-04-22 07:32:20 +000085
86 // FIXME. Must be a better way of getting a bitfield base offset.
87 CodeGenTypes::BitFieldInfo BFI = CGM.getTypes().getBitFieldInfo(Field);
88 // FIXME: The "field no" for bitfields is something completely
89 // different; it is the offset in multiples of the base type size!
90 uint64_t Offset = CGM.getTypes().getLLVMFieldNo(Field);
91 const llvm::Type *Ty =
92 CGM.getTypes().ConvertTypeForMemRecursive(Field->getType());
93 Offset *= CGM.getTypes().getTargetData().getTypePaddedSizeInBits(Ty);
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000094 return Offset + BFI.Begin;
95}
96
97uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
98 const ObjCInterfaceDecl *OID,
99 const ObjCIvarDecl *Ivar) {
100 return LookupFieldBitOffset(CGM, OID, Ivar) / 8;
Daniel Dunbar97776872009-04-22 07:32:20 +0000101}
102
103LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
104 const ObjCInterfaceDecl *OID,
105 llvm::Value *BaseValue,
106 const ObjCIvarDecl *Ivar,
107 unsigned CVRQualifiers,
108 llvm::Value *Offset) {
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000109 // We need to compute the bit offset for the bit-field, the offset
110 // is to the byte.
111 uint64_t BitOffset = LookupFieldBitOffset(CGF.CGM, OID, Ivar) % 8;
Daniel Dunbar412f59b2009-04-22 10:28:39 +0000112
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000113 // Compute (type*) ( (char *) BaseValue + Offset)
Daniel Dunbar97776872009-04-22 07:32:20 +0000114 llvm::Type *I8Ptr = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000115 QualType IvarTy = Ivar->getType();
116 const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy);
Daniel Dunbar97776872009-04-22 07:32:20 +0000117 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr);
Daniel Dunbar97776872009-04-22 07:32:20 +0000118 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000119 V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar97776872009-04-22 07:32:20 +0000120
121 if (Ivar->isBitField()) {
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000122 uint64_t BitFieldSize =
123 Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue();
124 return LValue::MakeBitfield(V, BitOffset, BitFieldSize,
Daniel Dunbare38df862009-05-03 07:52:00 +0000125 IvarTy->isSignedIntegerType(),
126 IvarTy.getCVRQualifiers()|CVRQualifiers);
Daniel Dunbar97776872009-04-22 07:32:20 +0000127 }
128
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000129 LValue LV = LValue::MakeAddr(V, IvarTy.getCVRQualifiers()|CVRQualifiers,
130 CGF.CGM.getContext().getObjCGCAttrKind(IvarTy));
Daniel Dunbar97776872009-04-22 07:32:20 +0000131 LValue::SetObjCIvar(LV, true);
132 return LV;
133}
134
135///
136
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000137namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000138
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000139 typedef std::vector<llvm::Constant*> ConstantVector;
140
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000141 // FIXME: We should find a nicer way to make the labels for
142 // metadata, string concatenation is lame.
143
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000144class ObjCCommonTypesHelper {
145protected:
146 CodeGen::CodeGenModule &CGM;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000147
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000148public:
Fariborz Jahanian0a855d02009-03-23 19:10:40 +0000149 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000150 const llvm::Type *Int8PtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000151
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000152 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
153 const llvm::Type *ObjectPtrTy;
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000154
155 /// PtrObjectPtrTy - LLVM type for id *
156 const llvm::Type *PtrObjectPtrTy;
157
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000158 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000159 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000160 /// ProtocolPtrTy - LLVM type for external protocol handles
161 /// (typeof(Protocol))
162 const llvm::Type *ExternalProtocolPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000163
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000164 // SuperCTy - clang type for struct objc_super.
165 QualType SuperCTy;
166 // SuperPtrCTy - clang type for struct objc_super *.
167 QualType SuperPtrCTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000168
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000169 /// SuperTy - LLVM type for struct objc_super.
170 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000171 /// SuperPtrTy - LLVM type for struct objc_super *.
172 const llvm::Type *SuperPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000173
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000174 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
175 /// in GCC parlance).
176 const llvm::StructType *PropertyTy;
177
178 /// PropertyListTy - LLVM type for struct objc_property_list
179 /// (_prop_list_t in GCC parlance).
180 const llvm::StructType *PropertyListTy;
181 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
182 const llvm::Type *PropertyListPtrTy;
183
184 // MethodTy - LLVM type for struct objc_method.
185 const llvm::StructType *MethodTy;
186
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000187 /// CacheTy - LLVM type for struct objc_cache.
188 const llvm::Type *CacheTy;
189 /// CachePtrTy - LLVM type for struct objc_cache *.
190 const llvm::Type *CachePtrTy;
191
Chris Lattner72db6c32009-04-22 02:44:54 +0000192 llvm::Constant *getGetPropertyFn() {
193 CodeGen::CodeGenTypes &Types = CGM.getTypes();
194 ASTContext &Ctx = CGM.getContext();
195 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
196 llvm::SmallVector<QualType,16> Params;
197 QualType IdType = Ctx.getObjCIdType();
198 QualType SelType = Ctx.getObjCSelType();
199 Params.push_back(IdType);
200 Params.push_back(SelType);
201 Params.push_back(Ctx.LongTy);
202 Params.push_back(Ctx.BoolTy);
203 const llvm::FunctionType *FTy =
204 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
205 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
206 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000207
Chris Lattner72db6c32009-04-22 02:44:54 +0000208 llvm::Constant *getSetPropertyFn() {
209 CodeGen::CodeGenTypes &Types = CGM.getTypes();
210 ASTContext &Ctx = CGM.getContext();
211 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
212 llvm::SmallVector<QualType,16> Params;
213 QualType IdType = Ctx.getObjCIdType();
214 QualType SelType = Ctx.getObjCSelType();
215 Params.push_back(IdType);
216 Params.push_back(SelType);
217 Params.push_back(Ctx.LongTy);
218 Params.push_back(IdType);
219 Params.push_back(Ctx.BoolTy);
220 Params.push_back(Ctx.BoolTy);
221 const llvm::FunctionType *FTy =
222 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
223 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
224 }
225
226 llvm::Constant *getEnumerationMutationFn() {
227 // void objc_enumerationMutation (id)
228 std::vector<const llvm::Type*> Args;
229 Args.push_back(ObjectPtrTy);
230 llvm::FunctionType *FTy =
231 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
232 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
233 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000234
235 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000236 llvm::Constant *getGcReadWeakFn() {
237 // id objc_read_weak (id *)
238 std::vector<const llvm::Type*> Args;
239 Args.push_back(ObjectPtrTy->getPointerTo());
240 llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false);
241 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
242 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000243
244 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000245 llvm::Constant *getGcAssignWeakFn() {
246 // id objc_assign_weak (id, id *)
247 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
248 Args.push_back(ObjectPtrTy->getPointerTo());
249 llvm::FunctionType *FTy =
250 llvm::FunctionType::get(ObjectPtrTy, Args, false);
251 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
252 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000253
254 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000255 llvm::Constant *getGcAssignGlobalFn() {
256 // id objc_assign_global(id, id *)
257 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
258 Args.push_back(ObjectPtrTy->getPointerTo());
259 llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false);
260 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
261 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000262
263 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000264 llvm::Constant *getGcAssignIvarFn() {
265 // id objc_assign_ivar(id, id *)
266 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
267 Args.push_back(ObjectPtrTy->getPointerTo());
268 llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false);
269 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
270 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000271
272 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000273 llvm::Constant *getGcAssignStrongCastFn() {
274 // id objc_assign_global(id, id *)
275 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
276 Args.push_back(ObjectPtrTy->getPointerTo());
277 llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false);
278 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
279 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000280
281 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000282 llvm::Constant *getExceptionThrowFn() {
283 // void objc_exception_throw(id)
284 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
285 llvm::FunctionType *FTy =
286 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
287 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
288 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000289
Daniel Dunbar1c566672009-02-24 01:43:46 +0000290 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000291 llvm::Constant *getSyncEnterFn() {
292 // void objc_sync_enter (id)
293 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
294 llvm::FunctionType *FTy =
295 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
296 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
297 }
Daniel Dunbar1c566672009-02-24 01:43:46 +0000298
299 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000300 llvm::Constant *getSyncExitFn() {
301 // void objc_sync_exit (id)
302 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
303 llvm::FunctionType *FTy =
304 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
305 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
306 }
Daniel Dunbar1c566672009-02-24 01:43:46 +0000307
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000308 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
309 ~ObjCCommonTypesHelper(){}
310};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000311
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000312/// ObjCTypesHelper - Helper class that encapsulates lazy
313/// construction of varies types used during ObjC generation.
314class ObjCTypesHelper : public ObjCCommonTypesHelper {
315private:
316
Chris Lattner4176b0c2009-04-22 02:32:31 +0000317 llvm::Constant *getMessageSendFn() {
318 // id objc_msgSend (id, SEL, ...)
319 std::vector<const llvm::Type*> Params;
320 Params.push_back(ObjectPtrTy);
321 Params.push_back(SelectorPtrTy);
322 return
323 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
324 Params, true),
325 "objc_msgSend");
326 }
327
328 llvm::Constant *getMessageSendStretFn() {
329 // id objc_msgSend_stret (id, SEL, ...)
330 std::vector<const llvm::Type*> Params;
331 Params.push_back(ObjectPtrTy);
332 Params.push_back(SelectorPtrTy);
333 return
334 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
335 Params, true),
336 "objc_msgSend_stret");
337
338 }
339
340 llvm::Constant *getMessageSendFpretFn() {
341 // FIXME: This should be long double on x86_64?
342 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
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::DoubleTy,
348 Params,
349 true),
350 "objc_msgSend_fpret");
351
352 }
353
354 llvm::Constant *getMessageSendSuperFn() {
355 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
356 std::vector<const llvm::Type*> Params;
357 Params.push_back(SuperPtrTy);
358 Params.push_back(SelectorPtrTy);
359 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
360 Params, true),
361 "objc_msgSendSuper");
362 }
363 llvm::Constant *getMessageSendSuperStretFn() {
364 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
365 // SEL op, ...)
366 std::vector<const llvm::Type*> Params;
367 Params.push_back(Int8PtrTy);
368 Params.push_back(SuperPtrTy);
369 Params.push_back(SelectorPtrTy);
370 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
371 Params, true),
372 "objc_msgSendSuper_stret");
373 }
374
375 llvm::Constant *getMessageSendSuperFpretFn() {
376 // There is no objc_msgSendSuper_fpret? How can that work?
377 return getMessageSendSuperFn();
378 }
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000379
380public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000381 /// SymtabTy - LLVM type for struct objc_symtab.
382 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000383 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
384 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000385 /// ModuleTy - LLVM type for struct objc_module.
386 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000387
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000388 /// ProtocolTy - LLVM type for struct objc_protocol.
389 const llvm::StructType *ProtocolTy;
390 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
391 const llvm::Type *ProtocolPtrTy;
392 /// ProtocolExtensionTy - LLVM type for struct
393 /// objc_protocol_extension.
394 const llvm::StructType *ProtocolExtensionTy;
395 /// ProtocolExtensionTy - LLVM type for struct
396 /// objc_protocol_extension *.
397 const llvm::Type *ProtocolExtensionPtrTy;
398 /// MethodDescriptionTy - LLVM type for struct
399 /// objc_method_description.
400 const llvm::StructType *MethodDescriptionTy;
401 /// MethodDescriptionListTy - LLVM type for struct
402 /// objc_method_description_list.
403 const llvm::StructType *MethodDescriptionListTy;
404 /// MethodDescriptionListPtrTy - LLVM type for struct
405 /// objc_method_description_list *.
406 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000407 /// ProtocolListTy - LLVM type for struct objc_property_list.
408 const llvm::Type *ProtocolListTy;
409 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
410 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000411 /// CategoryTy - LLVM type for struct objc_category.
412 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000413 /// ClassTy - LLVM type for struct objc_class.
414 const llvm::StructType *ClassTy;
415 /// ClassPtrTy - LLVM type for struct objc_class *.
416 const llvm::Type *ClassPtrTy;
417 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
418 const llvm::StructType *ClassExtensionTy;
419 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
420 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000421 // IvarTy - LLVM type for struct objc_ivar.
422 const llvm::StructType *IvarTy;
423 /// IvarListTy - LLVM type for struct objc_ivar_list.
424 const llvm::Type *IvarListTy;
425 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
426 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000427 /// MethodListTy - LLVM type for struct objc_method_list.
428 const llvm::Type *MethodListTy;
429 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
430 const llvm::Type *MethodListPtrTy;
Anders Carlsson124526b2008-09-09 10:10:21 +0000431
432 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
433 const llvm::Type *ExceptionDataTy;
434
Anders Carlsson124526b2008-09-09 10:10:21 +0000435 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000436 llvm::Constant *getExceptionTryEnterFn() {
437 std::vector<const llvm::Type*> Params;
438 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
439 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
440 Params, false),
441 "objc_exception_try_enter");
442 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000443
444 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000445 llvm::Constant *getExceptionTryExitFn() {
446 std::vector<const llvm::Type*> Params;
447 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
448 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
449 Params, false),
450 "objc_exception_try_exit");
451 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000452
453 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000454 llvm::Constant *getExceptionExtractFn() {
455 std::vector<const llvm::Type*> Params;
456 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
457 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
458 Params, false),
459 "objc_exception_extract");
460
461 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000462
463 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000464 llvm::Constant *getExceptionMatchFn() {
465 std::vector<const llvm::Type*> Params;
466 Params.push_back(ClassPtrTy);
467 Params.push_back(ObjectPtrTy);
468 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
469 Params, false),
470 "objc_exception_match");
471
472 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000473
474 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000475 llvm::Constant *getSetJmpFn() {
476 std::vector<const llvm::Type*> Params;
477 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
478 return
479 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
480 Params, false),
481 "_setjmp");
482
483 }
Chris Lattner10cac6f2008-11-15 21:26:17 +0000484
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000485public:
486 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000487 ~ObjCTypesHelper() {}
Daniel Dunbar5669e572008-10-17 03:24:53 +0000488
489
Chris Lattner74391b42009-03-22 21:03:39 +0000490 llvm::Constant *getSendFn(bool IsSuper) {
Chris Lattner4176b0c2009-04-22 02:32:31 +0000491 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
Daniel Dunbar5669e572008-10-17 03:24:53 +0000492 }
493
Chris Lattner74391b42009-03-22 21:03:39 +0000494 llvm::Constant *getSendStretFn(bool IsSuper) {
Chris Lattner4176b0c2009-04-22 02:32:31 +0000495 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
Daniel Dunbar5669e572008-10-17 03:24:53 +0000496 }
497
Chris Lattner74391b42009-03-22 21:03:39 +0000498 llvm::Constant *getSendFpretFn(bool IsSuper) {
Chris Lattner4176b0c2009-04-22 02:32:31 +0000499 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
Daniel Dunbar5669e572008-10-17 03:24:53 +0000500 }
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000501};
502
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000503/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000504/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000505class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000506public:
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000507
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000508 // MethodListnfABITy - LLVM for struct _method_list_t
509 const llvm::StructType *MethodListnfABITy;
510
511 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
512 const llvm::Type *MethodListnfABIPtrTy;
513
514 // ProtocolnfABITy = LLVM for struct _protocol_t
515 const llvm::StructType *ProtocolnfABITy;
516
Daniel Dunbar948e2582009-02-15 07:36:20 +0000517 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
518 const llvm::Type *ProtocolnfABIPtrTy;
519
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000520 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
521 const llvm::StructType *ProtocolListnfABITy;
522
523 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
524 const llvm::Type *ProtocolListnfABIPtrTy;
525
526 // ClassnfABITy - LLVM for struct _class_t
527 const llvm::StructType *ClassnfABITy;
528
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000529 // ClassnfABIPtrTy - LLVM for struct _class_t*
530 const llvm::Type *ClassnfABIPtrTy;
531
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000532 // IvarnfABITy - LLVM for struct _ivar_t
533 const llvm::StructType *IvarnfABITy;
534
535 // IvarListnfABITy - LLVM for struct _ivar_list_t
536 const llvm::StructType *IvarListnfABITy;
537
538 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
539 const llvm::Type *IvarListnfABIPtrTy;
540
541 // ClassRonfABITy - LLVM for struct _class_ro_t
542 const llvm::StructType *ClassRonfABITy;
543
544 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
545 const llvm::Type *ImpnfABITy;
546
547 // CategorynfABITy - LLVM for struct _category_t
548 const llvm::StructType *CategorynfABITy;
549
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000550 // New types for nonfragile abi messaging.
551
552 // MessageRefTy - LLVM for:
553 // struct _message_ref_t {
554 // IMP messenger;
555 // SEL name;
556 // };
557 const llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000558 // MessageRefCTy - clang type for struct _message_ref_t
559 QualType MessageRefCTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000560
561 // MessageRefPtrTy - LLVM for struct _message_ref_t*
562 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000563 // MessageRefCPtrTy - clang type for struct _message_ref_t*
564 QualType MessageRefCPtrTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000565
Fariborz Jahanianef163782009-02-05 01:13:09 +0000566 // MessengerTy - Type of the messenger (shown as IMP above)
567 const llvm::FunctionType *MessengerTy;
568
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000569 // SuperMessageRefTy - LLVM for:
570 // struct _super_message_ref_t {
571 // SUPER_IMP messenger;
572 // SEL name;
573 // };
574 const llvm::StructType *SuperMessageRefTy;
575
576 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
577 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000578
Chris Lattner1c02f862009-04-22 02:53:24 +0000579 llvm::Constant *getMessageSendFixupFn() {
580 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
581 std::vector<const llvm::Type*> Params;
582 Params.push_back(ObjectPtrTy);
583 Params.push_back(MessageRefPtrTy);
584 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
585 Params, true),
586 "objc_msgSend_fixup");
587 }
588
589 llvm::Constant *getMessageSendFpretFixupFn() {
590 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
591 std::vector<const llvm::Type*> Params;
592 Params.push_back(ObjectPtrTy);
593 Params.push_back(MessageRefPtrTy);
594 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
595 Params, true),
596 "objc_msgSend_fpret_fixup");
597 }
598
599 llvm::Constant *getMessageSendStretFixupFn() {
600 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
601 std::vector<const llvm::Type*> Params;
602 Params.push_back(ObjectPtrTy);
603 Params.push_back(MessageRefPtrTy);
604 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
605 Params, true),
606 "objc_msgSend_stret_fixup");
607 }
608
609 llvm::Constant *getMessageSendIdFixupFn() {
610 // id objc_msgSendId_fixup(id, struct message_ref_t*, ...)
611 std::vector<const llvm::Type*> Params;
612 Params.push_back(ObjectPtrTy);
613 Params.push_back(MessageRefPtrTy);
614 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
615 Params, true),
616 "objc_msgSendId_fixup");
617 }
618
619 llvm::Constant *getMessageSendIdStretFixupFn() {
620 // id objc_msgSendId_stret_fixup(id, struct message_ref_t*, ...)
621 std::vector<const llvm::Type*> Params;
622 Params.push_back(ObjectPtrTy);
623 Params.push_back(MessageRefPtrTy);
624 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
625 Params, true),
626 "objc_msgSendId_stret_fixup");
627 }
628 llvm::Constant *getMessageSendSuper2FixupFn() {
629 // id objc_msgSendSuper2_fixup (struct objc_super *,
630 // struct _super_message_ref_t*, ...)
631 std::vector<const llvm::Type*> Params;
632 Params.push_back(SuperPtrTy);
633 Params.push_back(SuperMessageRefPtrTy);
634 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
635 Params, true),
636 "objc_msgSendSuper2_fixup");
637 }
638
639 llvm::Constant *getMessageSendSuper2StretFixupFn() {
640 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
641 // struct _super_message_ref_t*, ...)
642 std::vector<const llvm::Type*> Params;
643 Params.push_back(SuperPtrTy);
644 Params.push_back(SuperMessageRefPtrTy);
645 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
646 Params, true),
647 "objc_msgSendSuper2_stret_fixup");
648 }
649
650
651
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000652 /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C
653 /// exception personality function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000654 llvm::Value *getEHPersonalityPtr() {
655 llvm::Constant *Personality =
656 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
657 std::vector<const llvm::Type*>(),
658 true),
659 "__objc_personality_v0");
660 return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy);
661 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000662
Chris Lattner8a569112009-04-22 02:15:23 +0000663 llvm::Constant *getUnwindResumeOrRethrowFn() {
664 std::vector<const llvm::Type*> Params;
665 Params.push_back(Int8PtrTy);
666 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
667 Params, false),
668 "_Unwind_Resume_or_Rethrow");
669 }
670
671 llvm::Constant *getObjCEndCatchFn() {
672 std::vector<const llvm::Type*> Params;
673 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
674 Params, false),
675 "objc_end_catch");
676
677 }
678
679 llvm::Constant *getObjCBeginCatchFn() {
680 std::vector<const llvm::Type*> Params;
681 Params.push_back(Int8PtrTy);
682 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
683 Params, false),
684 "objc_begin_catch");
685 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000686
687 const llvm::StructType *EHTypeTy;
688 const llvm::Type *EHTypePtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000689
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000690 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
691 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000692};
693
694class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000695public:
696 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000697 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000698 public:
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000699 unsigned int ivar_bytepos;
700 unsigned int ivar_size;
701 GC_IVAR() : ivar_bytepos(0), ivar_size(0) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000702
703 // Allow sorting based on byte pos.
704 bool operator<(const GC_IVAR &b) const {
705 return ivar_bytepos < b.ivar_bytepos;
706 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000707 };
708
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000709 class SKIP_SCAN {
710 public:
711 unsigned int skip;
712 unsigned int scan;
713 SKIP_SCAN() : skip(0), scan(0) {}
714 };
715
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000716protected:
717 CodeGen::CodeGenModule &CGM;
718 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000719 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000720
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000721 // gc ivar layout bitmap calculation helper caches.
722 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
723 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000724
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000725 /// LazySymbols - Symbols to generate a lazy reference for. See
726 /// DefinedSymbols and FinishModule().
727 std::set<IdentifierInfo*> LazySymbols;
728
729 /// DefinedSymbols - External symbols which are defined by this
730 /// module. The symbols in this list and LazySymbols are used to add
731 /// special linker symbols which ensure that Objective-C modules are
732 /// linked properly.
733 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000734
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000735 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000736 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000737
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000738 /// MethodVarNames - uniqued method variable names.
739 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000740
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000741 /// MethodVarTypes - uniqued method type signatures. We have to use
742 /// a StringMap here because have no other unique reference.
743 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000744
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000745 /// MethodDefinitions - map of methods which have been defined in
746 /// this translation unit.
747 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000748
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000749 /// PropertyNames - uniqued method variable names.
750 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000751
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000752 /// ClassReferences - uniqued class references.
753 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000754
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000755 /// SelectorReferences - uniqued selector references.
756 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000757
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000758 /// Protocols - Protocols for which an objc_protocol structure has
759 /// been emitted. Forward declarations are handled by creating an
760 /// empty structure whose initializer is filled in when/if defined.
761 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000762
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000763 /// DefinedProtocols - Protocols which have actually been
764 /// defined. We should not need this, see FIXME in GenerateProtocol.
765 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000766
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000767 /// DefinedClasses - List of defined classes.
768 std::vector<llvm::GlobalValue*> DefinedClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000769
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000770 /// DefinedCategories - List of defined categories.
771 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000772
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000773 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000774 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000775 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000776
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000777 /// GetNameForMethod - Return a name for the given method.
778 /// \param[out] NameOut - The return value.
779 void GetNameForMethod(const ObjCMethodDecl *OMD,
780 const ObjCContainerDecl *CD,
781 std::string &NameOut);
782
783 /// GetMethodVarName - Return a unique constant for the given
784 /// selector's name. The return value has type char *.
785 llvm::Constant *GetMethodVarName(Selector Sel);
786 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
787 llvm::Constant *GetMethodVarName(const std::string &Name);
788
789 /// GetMethodVarType - Return a unique constant for the given
790 /// selector's name. The return value has type char *.
791
792 // FIXME: This is a horrible name.
793 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000794 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000795
796 /// GetPropertyName - Return a unique constant for the given
797 /// name. The return value has type char *.
798 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
799
800 // FIXME: This can be dropped once string functions are unified.
801 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
802 const Decl *Container);
803
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000804 /// GetClassName - Return a unique constant for the given selector's
805 /// name. The return value has type char *.
806 llvm::Constant *GetClassName(IdentifierInfo *Ident);
807
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000808 /// BuildIvarLayout - Builds ivar layout bitmap for the class
809 /// implementation for the __strong or __weak case.
810 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000811 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
812 bool ForStrongLayout);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000813
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000814 void BuildAggrIvarLayout(const ObjCInterfaceDecl *OI,
815 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000816 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +0000817 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000818 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000819 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000820
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000821 /// GetIvarLayoutName - Returns a unique constant for the given
822 /// ivar layout bitmap.
823 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
824 const ObjCCommonTypesHelper &ObjCTypes);
825
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000826 /// EmitPropertyList - Emit the given property list. The return
827 /// value has type PropertyListPtrTy.
828 llvm::Constant *EmitPropertyList(const std::string &Name,
829 const Decl *Container,
830 const ObjCContainerDecl *OCD,
831 const ObjCCommonTypesHelper &ObjCTypes);
832
Fariborz Jahanianda320092009-01-29 19:24:30 +0000833 /// GetProtocolRef - Return a reference to the internal protocol
834 /// description, creating an empty one if it has not been
835 /// defined. The return value has type ProtocolPtrTy.
836 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000837
Chris Lattnercd0ee142009-03-31 08:33:16 +0000838 /// GetFieldBaseOffset - return's field byte offset.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000839 uint64_t GetFieldBaseOffset(const ObjCInterfaceDecl *OI,
840 const llvm::StructLayout *Layout,
Chris Lattnercd0ee142009-03-31 08:33:16 +0000841 const FieldDecl *Field);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000842
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000843 /// CreateMetadataVar - Create a global variable with internal
844 /// linkage for use by the Objective-C runtime.
845 ///
846 /// This is a convenience wrapper which not only creates the
847 /// variable, but also sets the section and alignment and adds the
848 /// global to the UsedGlobals list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000849 ///
850 /// \param Name - The variable name.
851 /// \param Init - The variable initializer; this is also used to
852 /// define the type of the variable.
853 /// \param Section - The section the variable should go into, or 0.
854 /// \param Align - The alignment for the variable, or 0.
855 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000856 /// "llvm.used".
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000857 llvm::GlobalVariable *CreateMetadataVar(const std::string &Name,
858 llvm::Constant *Init,
859 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000860 unsigned Align,
861 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000862
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000863 /// GetNamedIvarList - Return the list of ivars in the interface
864 /// itself (not including super classes and not including unnamed
865 /// bitfields).
866 ///
867 /// For the non-fragile ABI, this also includes synthesized property
868 /// ivars.
869 void GetNamedIvarList(const ObjCInterfaceDecl *OID,
870 llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const;
871
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000872public:
873 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
874 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000875
Steve Naroff33fdb732009-03-31 16:53:37 +0000876 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000877
878 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
879 const ObjCContainerDecl *CD=0);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000880
881 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
882
883 /// GetOrEmitProtocol - Get the protocol object for the given
884 /// declaration, emitting it if necessary. The return value has type
885 /// ProtocolPtrTy.
886 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
887
888 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
889 /// object for the given declaration, emitting it if needed. These
890 /// forward references will be filled in with empty bodies if no
891 /// definition is seen. The return value has type ProtocolPtrTy.
892 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000893};
894
895class CGObjCMac : public CGObjCCommonMac {
896private:
897 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000898 /// EmitImageInfo - Emit the image info marker used to encode some module
899 /// level information.
900 void EmitImageInfo();
901
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000902 /// EmitModuleInfo - Another marker encoding module level
903 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000904 void EmitModuleInfo();
905
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000906 /// EmitModuleSymols - Emit module symbols, the list of defined
907 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000908 llvm::Constant *EmitModuleSymbols();
909
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000910 /// FinishModule - Write out global data structures at the end of
911 /// processing a translation unit.
912 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000913
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000914 /// EmitClassExtension - Generate the class extension structure used
915 /// to store the weak ivar layout and properties. The return value
916 /// has type ClassExtensionPtrTy.
917 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
918
919 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
920 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000921 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000922 const ObjCInterfaceDecl *ID);
923
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000924 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000925 QualType ResultType,
926 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000927 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000928 QualType Arg0Ty,
929 bool IsSuper,
930 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000931
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000932 /// EmitIvarList - Emit the ivar list for the given
933 /// implementation. If ForClass is true the list of class ivars
934 /// (i.e. metaclass ivars) is emitted, otherwise the list of
935 /// interface ivars will be emitted. The return value has type
936 /// IvarListPtrTy.
937 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000938 bool ForClass);
939
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000940 /// EmitMetaClass - Emit a forward reference to the class structure
941 /// for the metaclass of the given interface. The return value has
942 /// type ClassPtrTy.
943 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
944
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000945 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000946 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000947 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
948 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000949 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000950
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000951 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000952
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000953 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000954
955 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000956 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000957 llvm::Constant *EmitMethodList(const std::string &Name,
958 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000959 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000960
961 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000962 /// method declarations.
963 /// - TypeName: The name for the type containing the methods.
964 /// - IsProtocol: True iff these methods are for a protocol.
965 /// - ClassMethds: True iff these are class methods.
966 /// - Required: When true, only "required" methods are
967 /// listed. Similarly, when false only "optional" methods are
968 /// listed. For classes this should always be true.
969 /// - begin, end: The method list to output.
970 ///
971 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000972 llvm::Constant *EmitMethodDescList(const std::string &Name,
973 const char *Section,
974 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000975
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000976 /// GetOrEmitProtocol - Get the protocol object for the given
977 /// declaration, emitting it if necessary. The return value has type
978 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000979 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000980
981 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
982 /// object for the given declaration, emitting it if needed. These
983 /// forward references will be filled in with empty bodies if no
984 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000985 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000986
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000987 /// EmitProtocolExtension - Generate the protocol extension
988 /// structure used to store optional instance and class methods, and
989 /// protocol properties. The return value has type
990 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000991 llvm::Constant *
992 EmitProtocolExtension(const ObjCProtocolDecl *PD,
993 const ConstantVector &OptInstanceMethods,
994 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000995
996 /// EmitProtocolList - Generate the list of referenced
997 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +0000998 llvm::Constant *EmitProtocolList(const std::string &Name,
999 ObjCProtocolDecl::protocol_iterator begin,
1000 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001001
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001002 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1003 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001004 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001005
Fariborz Jahanianda320092009-01-29 19:24:30 +00001006 public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001007 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001008
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001009 virtual llvm::Function *ModuleInitFunction();
1010
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001011 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001012 QualType ResultType,
1013 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001014 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001015 bool IsClassMessage,
1016 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001017
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001018 virtual CodeGen::RValue
1019 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001020 QualType ResultType,
1021 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001022 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001023 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001024 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001025 bool IsClassMessage,
1026 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001027
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001028 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001029 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001030
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001031 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001032
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001033 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001034
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001035 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001036
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001037 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001038 const ObjCProtocolDecl *PD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00001039
Chris Lattner74391b42009-03-22 21:03:39 +00001040 virtual llvm::Constant *GetPropertyGetFunction();
1041 virtual llvm::Constant *GetPropertySetFunction();
1042 virtual llvm::Constant *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001043
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001044 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1045 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001046 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1047 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001048 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001049 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001050 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1051 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001052 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1053 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001054 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1055 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001056 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1057 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001058
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001059 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1060 QualType ObjectTy,
1061 llvm::Value *BaseValue,
1062 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001063 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001064 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001065 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001066 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001067};
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001068
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001069class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001070private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001071 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001072 llvm::GlobalVariable* ObjCEmptyCacheVar;
1073 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001074
Daniel Dunbar11394522009-04-18 08:51:00 +00001075 /// SuperClassReferences - uniqued super class references.
1076 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
1077
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001078 /// MetaClassReferences - uniqued meta class references.
1079 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001080
1081 /// EHTypeReferences - uniqued class ehtype references.
1082 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001083
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001084 /// FinishNonFragileABIModule - Write out global data structures at the end of
1085 /// processing a translation unit.
1086 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001087
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00001088 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1089 unsigned InstanceStart,
1090 unsigned InstanceSize,
1091 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001092 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
1093 llvm::Constant *IsAGV,
1094 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001095 llvm::Constant *ClassRoGV,
1096 bool HiddenVisibility);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001097
1098 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
1099
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001100 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
1101
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001102 /// EmitMethodList - Emit the method list for the given
1103 /// implementation. The return value has type MethodListnfABITy.
1104 llvm::Constant *EmitMethodList(const std::string &Name,
1105 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001106 const ConstantVector &Methods);
1107 /// EmitIvarList - Emit the ivar list for the given
1108 /// implementation. If ForClass is true the list of class ivars
1109 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1110 /// interface ivars will be emitted. The return value has type
1111 /// IvarListnfABIPtrTy.
1112 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00001113
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001114 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001115 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001116 unsigned long int offset);
1117
Fariborz Jahanianda320092009-01-29 19:24:30 +00001118 /// GetOrEmitProtocol - Get the protocol object for the given
1119 /// declaration, emitting it if necessary. The return value has type
1120 /// ProtocolPtrTy.
1121 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
1122
1123 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1124 /// object for the given declaration, emitting it if needed. These
1125 /// forward references will be filled in with empty bodies if no
1126 /// definition is seen. The return value has type ProtocolPtrTy.
1127 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
1128
1129 /// EmitProtocolList - Generate the list of referenced
1130 /// protocols. The return value has type ProtocolListPtrTy.
1131 llvm::Constant *EmitProtocolList(const std::string &Name,
1132 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001133 ObjCProtocolDecl::protocol_iterator end);
1134
1135 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1136 QualType ResultType,
1137 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00001138 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001139 QualType Arg0Ty,
1140 bool IsSuper,
1141 const CallArgList &CallArgs);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001142
1143 /// GetClassGlobal - Return the global variable for the Objective-C
1144 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001145 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
1146
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001147 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001148 /// for the given class reference.
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001149 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001150 const ObjCInterfaceDecl *ID);
1151
1152 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1153 /// for the given super class reference.
1154 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1155 const ObjCInterfaceDecl *ID);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001156
1157 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1158 /// meta-data
1159 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
1160 const ObjCInterfaceDecl *ID);
1161
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001162 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1163 /// the given ivar.
1164 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001165 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00001166 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001167 const ObjCIvarDecl *Ivar);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001168
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001169 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1170 /// for the given selector.
1171 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbare588b992009-03-01 04:46:24 +00001172
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001173 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001174 /// interface. The return value has type EHTypePtrTy.
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001175 llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
1176 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001177
1178 const char *getMetaclassSymbolPrefix() const {
1179 return "OBJC_METACLASS_$_";
1180 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001181
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001182 const char *getClassSymbolPrefix() const {
1183 return "OBJC_CLASS_$_";
1184 }
1185
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001186 void GetClassSizeInfo(const ObjCInterfaceDecl *OID,
1187 uint32_t &InstanceStart,
1188 uint32_t &InstanceSize);
1189
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001190public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001191 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001192 // FIXME. All stubs for now!
1193 virtual llvm::Function *ModuleInitFunction();
1194
1195 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
1196 QualType ResultType,
1197 Selector Sel,
1198 llvm::Value *Receiver,
1199 bool IsClassMessage,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001200 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001201
1202 virtual CodeGen::RValue
1203 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
1204 QualType ResultType,
1205 Selector Sel,
1206 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001207 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001208 llvm::Value *Receiver,
1209 bool IsClassMessage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001210 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001211
1212 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001213 const ObjCInterfaceDecl *ID);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001214
1215 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001216 { return EmitSelector(Builder, Sel); }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001217
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001218 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001219
1220 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001221 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001222 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001223
Chris Lattner74391b42009-03-22 21:03:39 +00001224 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001225 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001226 }
Chris Lattner74391b42009-03-22 21:03:39 +00001227 virtual llvm::Constant *GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001228 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001229 }
Chris Lattner74391b42009-03-22 21:03:39 +00001230 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001231 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001232 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001233
1234 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00001235 const Stmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001236 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001237 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001238 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001239 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001240 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001241 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001242 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001243 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001244 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001245 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001246 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001247 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001248 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1249 QualType ObjectTy,
1250 llvm::Value *BaseValue,
1251 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001252 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001253 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001254 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001255 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001256};
1257
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001258} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001259
1260/* *** Helper Functions *** */
1261
1262/// getConstantGEP() - Help routine to construct simple GEPs.
1263static llvm::Constant *getConstantGEP(llvm::Constant *C,
1264 unsigned idx0,
1265 unsigned idx1) {
1266 llvm::Value *Idxs[] = {
1267 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
1268 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
1269 };
1270 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
1271}
1272
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001273/// hasObjCExceptionAttribute - Return true if this class or any super
1274/// class has the __objc_exception__ attribute.
1275static bool hasObjCExceptionAttribute(const ObjCInterfaceDecl *OID) {
Daniel Dunbarb11fa0d2009-04-13 21:08:27 +00001276 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001277 return true;
1278 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
1279 return hasObjCExceptionAttribute(Super);
1280 return false;
1281}
1282
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001283/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001284
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001285CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
1286 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001287{
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001288 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001289 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001290}
1291
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001292/// GetClass - Return a reference to the class for the given interface
1293/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001294llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001295 const ObjCInterfaceDecl *ID) {
1296 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001297}
1298
1299/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001300llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001301 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001302}
1303
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001304/// Generate a constant CFString object.
1305/*
1306 struct __builtin_CFString {
1307 const int *isa; // point to __CFConstantStringClassReference
1308 int flags;
1309 const char *str;
1310 long length;
1311 };
1312*/
1313
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001314llvm::Constant *CGObjCCommonMac::GenerateConstantString(
Steve Naroff33fdb732009-03-31 16:53:37 +00001315 const ObjCStringLiteral *SL) {
Steve Naroff8d4141f2009-04-01 13:55:36 +00001316 return CGM.GetAddrOfConstantCFString(SL->getString());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001317}
1318
1319/// Generates a message send where the super is the receiver. This is
1320/// a message send to self with special delivery semantics indicating
1321/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001322CodeGen::RValue
1323CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001324 QualType ResultType,
1325 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001326 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001327 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001328 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001329 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001330 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001331 // Create and init a super structure; this is a (receiver, class)
1332 // pair we will pass to objc_msgSendSuper.
1333 llvm::Value *ObjCSuper =
1334 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
1335 llvm::Value *ReceiverAsObject =
1336 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
1337 CGF.Builder.CreateStore(ReceiverAsObject,
1338 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001339
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001340 // If this is a class message the metaclass is passed as the target.
1341 llvm::Value *Target;
1342 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001343 if (isCategoryImpl) {
1344 // Message sent to 'super' in a class method defined in a category
1345 // implementation requires an odd treatment.
1346 // If we are in a class method, we must retrieve the
1347 // _metaclass_ for the current class, pointed at by
1348 // the class's "isa" pointer. The following assumes that
1349 // isa" is the first ivar in a class (which it must be).
1350 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1351 Target = CGF.Builder.CreateStructGEP(Target, 0);
1352 Target = CGF.Builder.CreateLoad(Target);
1353 }
1354 else {
1355 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1356 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1357 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1358 Target = Super;
1359 }
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001360 } else {
1361 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1362 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001363 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
1364 // and ObjCTypes types.
1365 const llvm::Type *ClassTy =
1366 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001367 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001368 CGF.Builder.CreateStore(Target,
1369 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
1370
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001371 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001372 ObjCSuper, ObjCTypes.SuperPtrCTy,
1373 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001374}
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001375
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001376/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001377CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001378 QualType ResultType,
1379 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001380 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001381 bool IsClassMessage,
1382 const CallArgList &CallArgs) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001383 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001384 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001385 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001386}
1387
1388CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001389 QualType ResultType,
1390 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001391 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001392 QualType Arg0Ty,
1393 bool IsSuper,
1394 const CallArgList &CallArgs) {
1395 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001396 if (!IsSuper)
1397 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001398 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
1399 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
1400 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001401 CGF.getContext().getObjCSelType()));
1402 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001403
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001404 CodeGenTypes &Types = CGM.getTypes();
1405 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
Fariborz Jahanian65257ca2009-04-29 22:47:27 +00001406 // FIXME. vararg flag must be true when this API is used for 64bit code gen.
1407 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001408
1409 llvm::Constant *Fn;
Daniel Dunbar88b53962009-02-02 22:03:45 +00001410 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Daniel Dunbar5669e572008-10-17 03:24:53 +00001411 Fn = ObjCTypes.getSendStretFn(IsSuper);
1412 } else if (ResultType->isFloatingType()) {
1413 // FIXME: Sadly, this is wrong. This actually depends on the
1414 // architecture. This happens to be right for x86-32 though.
1415 Fn = ObjCTypes.getSendFpretFn(IsSuper);
1416 } else {
1417 Fn = ObjCTypes.getSendFn(IsSuper);
1418 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +00001419 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +00001420 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001421}
1422
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001423llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001424 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001425 // FIXME: I don't understand why gcc generates this, or where it is
1426 // resolved. Investigate. Its also wasteful to look this up over and
1427 // over.
1428 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1429
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001430 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
1431 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001432}
1433
Fariborz Jahanianda320092009-01-29 19:24:30 +00001434void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001435 // FIXME: We shouldn't need this, the protocol decl should contain
1436 // enough information to tell us whether this was a declaration or a
1437 // definition.
1438 DefinedProtocols.insert(PD->getIdentifier());
1439
1440 // If we have generated a forward reference to this protocol, emit
1441 // it now. Otherwise do nothing, the protocol objects are lazily
1442 // emitted.
1443 if (Protocols.count(PD->getIdentifier()))
1444 GetOrEmitProtocol(PD);
1445}
1446
Fariborz Jahanianda320092009-01-29 19:24:30 +00001447llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001448 if (DefinedProtocols.count(PD->getIdentifier()))
1449 return GetOrEmitProtocol(PD);
1450 return GetOrEmitProtocolRef(PD);
1451}
1452
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001453/*
1454 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1455 struct _objc_protocol {
1456 struct _objc_protocol_extension *isa;
1457 char *protocol_name;
1458 struct _objc_protocol_list *protocol_list;
1459 struct _objc__method_prototype_list *instance_methods;
1460 struct _objc__method_prototype_list *class_methods
1461 };
1462
1463 See EmitProtocolExtension().
1464*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001465llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1466 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1467
1468 // Early exit if a defining object has already been generated.
1469 if (Entry && Entry->hasInitializer())
1470 return Entry;
1471
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001472 // FIXME: I don't understand why gcc generates this, or where it is
1473 // resolved. Investigate. Its also wasteful to look this up over and
1474 // over.
1475 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1476
Chris Lattner8ec03f52008-11-24 03:54:41 +00001477 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001478
1479 // Construct method lists.
1480 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1481 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00001482 for (ObjCProtocolDecl::instmeth_iterator
1483 i = PD->instmeth_begin(CGM.getContext()),
1484 e = PD->instmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001485 ObjCMethodDecl *MD = *i;
1486 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1487 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1488 OptInstanceMethods.push_back(C);
1489 } else {
1490 InstanceMethods.push_back(C);
1491 }
1492 }
1493
Douglas Gregor6ab35242009-04-09 21:40:53 +00001494 for (ObjCProtocolDecl::classmeth_iterator
1495 i = PD->classmeth_begin(CGM.getContext()),
1496 e = PD->classmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001497 ObjCMethodDecl *MD = *i;
1498 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1499 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1500 OptClassMethods.push_back(C);
1501 } else {
1502 ClassMethods.push_back(C);
1503 }
1504 }
1505
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001506 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001507 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001508 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001509 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001510 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001511 PD->protocol_begin(),
1512 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001513 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001514 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
1515 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001516 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1517 InstanceMethods);
1518 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001519 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
1520 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001521 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1522 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001523 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
1524 Values);
1525
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001526 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001527 // Already created, fix the linkage and update the initializer.
1528 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001529 Entry->setInitializer(Init);
1530 } else {
1531 Entry =
1532 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
1533 llvm::GlobalValue::InternalLinkage,
1534 Init,
1535 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
1536 &CGM.getModule());
1537 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001538 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001539 UsedGlobals.push_back(Entry);
1540 // FIXME: Is this necessary? Why only for protocol?
1541 Entry->setAlignment(4);
1542 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001543
1544 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001545}
1546
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001547llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001548 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1549
1550 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001551 // We use the initializer as a marker of whether this is a forward
1552 // reference or not. At module finalization we add the empty
1553 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001554 Entry =
1555 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001556 llvm::GlobalValue::ExternalLinkage,
1557 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001558 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001559 &CGM.getModule());
1560 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001561 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001562 UsedGlobals.push_back(Entry);
1563 // FIXME: Is this necessary? Why only for protocol?
1564 Entry->setAlignment(4);
1565 }
1566
1567 return Entry;
1568}
1569
1570/*
1571 struct _objc_protocol_extension {
1572 uint32_t size;
1573 struct objc_method_description_list *optional_instance_methods;
1574 struct objc_method_description_list *optional_class_methods;
1575 struct objc_property_list *instance_properties;
1576 };
1577*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001578llvm::Constant *
1579CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1580 const ConstantVector &OptInstanceMethods,
1581 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001582 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001583 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001584 std::vector<llvm::Constant*> Values(4);
1585 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001586 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001587 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1588 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001589 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1590 OptInstanceMethods);
1591 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001592 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1593 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001594 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1595 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001596 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1597 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001598 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001599
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001600 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001601 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1602 Values[3]->isNullValue())
1603 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1604
1605 llvm::Constant *Init =
1606 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001607
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001608 // No special section, but goes in llvm.used
1609 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
1610 Init,
1611 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001612}
1613
1614/*
1615 struct objc_protocol_list {
1616 struct objc_protocol_list *next;
1617 long count;
1618 Protocol *list[];
1619 };
1620*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001621llvm::Constant *
1622CGObjCMac::EmitProtocolList(const std::string &Name,
1623 ObjCProtocolDecl::protocol_iterator begin,
1624 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001625 std::vector<llvm::Constant*> ProtocolRefs;
1626
Daniel Dunbardbc933702008-08-21 21:57:41 +00001627 for (; begin != end; ++begin)
1628 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001629
1630 // Just return null for empty protocol lists
1631 if (ProtocolRefs.empty())
1632 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1633
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001634 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001635 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1636
1637 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001638 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001639 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1640 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1641 Values[2] =
1642 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1643 ProtocolRefs.size()),
1644 ProtocolRefs);
1645
1646 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1647 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001648 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001649 4, false);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001650 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1651}
1652
1653/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001654 struct _objc_property {
1655 const char * const name;
1656 const char * const attributes;
1657 };
1658
1659 struct _objc_property_list {
1660 uint32_t entsize; // sizeof (struct _objc_property)
1661 uint32_t prop_count;
1662 struct _objc_property[prop_count];
1663 };
1664*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001665llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1666 const Decl *Container,
1667 const ObjCContainerDecl *OCD,
1668 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001669 std::vector<llvm::Constant*> Properties, Prop(2);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001670 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(CGM.getContext()),
1671 E = OCD->prop_end(CGM.getContext()); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00001672 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001673 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001674 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001675 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1676 Prop));
1677 }
1678
1679 // Return null for empty list.
1680 if (Properties.empty())
1681 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1682
1683 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001684 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001685 std::vector<llvm::Constant*> Values(3);
1686 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1687 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1688 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1689 Properties.size());
1690 Values[2] = llvm::ConstantArray::get(AT, Properties);
1691 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1692
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001693 llvm::GlobalVariable *GV =
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001694 CreateMetadataVar(Name, Init,
1695 (ObjCABI == 2) ? "__DATA, __objc_const" :
1696 "__OBJC,__property,regular,no_dead_strip",
1697 (ObjCABI == 2) ? 8 : 4,
1698 true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001699 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001700}
1701
1702/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001703 struct objc_method_description_list {
1704 int count;
1705 struct objc_method_description list[];
1706 };
1707*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001708llvm::Constant *
1709CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1710 std::vector<llvm::Constant*> Desc(2);
1711 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1712 ObjCTypes.SelectorPtrTy);
1713 Desc[1] = GetMethodVarType(MD);
1714 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1715 Desc);
1716}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001717
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001718llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1719 const char *Section,
1720 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001721 // Return null for empty list.
1722 if (Methods.empty())
1723 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1724
1725 std::vector<llvm::Constant*> Values(2);
1726 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1727 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1728 Methods.size());
1729 Values[1] = llvm::ConstantArray::get(AT, Methods);
1730 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1731
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001732 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001733 return llvm::ConstantExpr::getBitCast(GV,
1734 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001735}
1736
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001737/*
1738 struct _objc_category {
1739 char *category_name;
1740 char *class_name;
1741 struct _objc_method_list *instance_methods;
1742 struct _objc_method_list *class_methods;
1743 struct _objc_protocol_list *protocols;
1744 uint32_t size; // <rdar://4585769>
1745 struct _objc_property_list *instance_properties;
1746 };
1747 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001748void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001749 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001750
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001751 // FIXME: This is poor design, the OCD should have a pointer to the
1752 // category decl. Additionally, note that Category can be null for
1753 // the @implementation w/o an @interface case. Sema should just
1754 // create one for us as it does for @implementation so everyone else
1755 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001756 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001757 const ObjCCategoryDecl *Category =
1758 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001759 std::string ExtName(Interface->getNameAsString() + "_" +
1760 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001761
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001762 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001763 for (ObjCCategoryImplDecl::instmeth_iterator
1764 i = OCD->instmeth_begin(CGM.getContext()),
1765 e = OCD->instmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001766 // Instance methods should always be defined.
1767 InstanceMethods.push_back(GetMethodConstant(*i));
1768 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001769 for (ObjCCategoryImplDecl::classmeth_iterator
1770 i = OCD->classmeth_begin(CGM.getContext()),
1771 e = OCD->classmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001772 // Class methods should always be defined.
1773 ClassMethods.push_back(GetMethodConstant(*i));
1774 }
1775
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001776 std::vector<llvm::Constant*> Values(7);
1777 Values[0] = GetClassName(OCD->getIdentifier());
1778 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00001779 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001780 Values[2] =
1781 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1782 ExtName,
1783 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001784 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001785 Values[3] =
1786 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001787 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001788 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001789 if (Category) {
1790 Values[4] =
1791 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1792 Category->protocol_begin(),
1793 Category->protocol_end());
1794 } else {
1795 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1796 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001797 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001798
1799 // If there is no category @interface then there can be no properties.
1800 if (Category) {
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001801 Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001802 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001803 } else {
1804 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1805 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001806
1807 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1808 Values);
1809
1810 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001811 CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init,
1812 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001813 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001814 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001815}
1816
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001817// FIXME: Get from somewhere?
1818enum ClassFlags {
1819 eClassFlags_Factory = 0x00001,
1820 eClassFlags_Meta = 0x00002,
1821 // <rdr://5142207>
1822 eClassFlags_HasCXXStructors = 0x02000,
1823 eClassFlags_Hidden = 0x20000,
1824 eClassFlags_ABI2_Hidden = 0x00010,
1825 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1826};
1827
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001828/*
1829 struct _objc_class {
1830 Class isa;
1831 Class super_class;
1832 const char *name;
1833 long version;
1834 long info;
1835 long instance_size;
1836 struct _objc_ivar_list *ivars;
1837 struct _objc_method_list *methods;
1838 struct _objc_cache *cache;
1839 struct _objc_protocol_list *protocols;
1840 // Objective-C 1.0 extensions (<rdr://4585769>)
1841 const char *ivar_layout;
1842 struct _objc_class_ext *ext;
1843 };
1844
1845 See EmitClassExtension();
1846 */
1847void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001848 DefinedSymbols.insert(ID->getIdentifier());
1849
Chris Lattner8ec03f52008-11-24 03:54:41 +00001850 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001851 // FIXME: Gross
1852 ObjCInterfaceDecl *Interface =
1853 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001854 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001855 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001856 Interface->protocol_begin(),
1857 Interface->protocol_end());
Daniel Dunbar1d7e5392009-05-03 08:55:17 +00001858 const llvm::Type *InterfaceTy =
1859 CGObjCRuntime::GetConcreteClassStruct(CGM, Interface);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001860 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001861 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001862
1863 // FIXME: Set CXX-structors flag.
Daniel Dunbar04d40782009-04-14 06:00:08 +00001864 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001865 Flags |= eClassFlags_Hidden;
1866
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001867 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001868 for (ObjCImplementationDecl::instmeth_iterator
1869 i = ID->instmeth_begin(CGM.getContext()),
1870 e = ID->instmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001871 // Instance methods should always be defined.
1872 InstanceMethods.push_back(GetMethodConstant(*i));
1873 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001874 for (ObjCImplementationDecl::classmeth_iterator
1875 i = ID->classmeth_begin(CGM.getContext()),
1876 e = ID->classmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001877 // Class methods should always be defined.
1878 ClassMethods.push_back(GetMethodConstant(*i));
1879 }
1880
Douglas Gregor653f1b12009-04-23 01:02:12 +00001881 for (ObjCImplementationDecl::propimpl_iterator
1882 i = ID->propimpl_begin(CGM.getContext()),
1883 e = ID->propimpl_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001884 ObjCPropertyImplDecl *PID = *i;
1885
1886 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1887 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1888
1889 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1890 if (llvm::Constant *C = GetMethodConstant(MD))
1891 InstanceMethods.push_back(C);
1892 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1893 if (llvm::Constant *C = GetMethodConstant(MD))
1894 InstanceMethods.push_back(C);
1895 }
1896 }
1897
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001898 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar5384b092009-05-03 08:56:52 +00001899 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001900 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001901 // Record a reference to the super class.
1902 LazySymbols.insert(Super->getIdentifier());
1903
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001904 Values[ 1] =
1905 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1906 ObjCTypes.ClassPtrTy);
1907 } else {
1908 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1909 }
1910 Values[ 2] = GetClassName(ID->getIdentifier());
1911 // Version is always 0.
1912 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1913 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1914 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001915 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001916 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001917 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001918 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001919 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001920 // cache is always NULL.
1921 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1922 Values[ 9] = Protocols;
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00001923 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001924 Values[11] = EmitClassExtension(ID);
1925 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1926 Values);
1927
1928 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001929 CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init,
1930 "__OBJC,__class,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001931 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001932 DefinedClasses.push_back(GV);
1933}
1934
1935llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1936 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001937 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001938 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001939 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001940
Daniel Dunbar04d40782009-04-14 06:00:08 +00001941 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001942 Flags |= eClassFlags_Hidden;
1943
1944 std::vector<llvm::Constant*> Values(12);
1945 // The isa for the metaclass is the root of the hierarchy.
1946 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1947 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1948 Root = Super;
1949 Values[ 0] =
1950 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1951 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001952 // The super class for the metaclass is emitted as the name of the
1953 // super class. The runtime fixes this up to point to the
1954 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001955 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1956 Values[ 1] =
1957 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1958 ObjCTypes.ClassPtrTy);
1959 } else {
1960 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1961 }
1962 Values[ 2] = GetClassName(ID->getIdentifier());
1963 // Version is always 0.
1964 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1965 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1966 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001967 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001968 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001969 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001970 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001971 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001972 // cache is always NULL.
1973 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1974 Values[ 9] = Protocols;
1975 // ivar_layout for metaclass is always NULL.
1976 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1977 // The class extension is always unused for metaclasses.
1978 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1979 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1980 Values);
1981
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001982 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001983 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001984
1985 // Check for a forward reference.
1986 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1987 if (GV) {
1988 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1989 "Forward metaclass reference has incorrect type.");
1990 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1991 GV->setInitializer(Init);
1992 } else {
1993 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1994 llvm::GlobalValue::InternalLinkage,
1995 Init, Name,
1996 &CGM.getModule());
1997 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001998 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001999 GV->setAlignment(4);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002000 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002001
2002 return GV;
2003}
2004
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002005llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002006 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002007
2008 // FIXME: Should we look these up somewhere other than the
2009 // module. Its a bit silly since we only generate these while
2010 // processing an implementation, so exactly one pointer would work
2011 // if know when we entered/exitted an implementation block.
2012
2013 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002014 // Previously, metaclass with internal linkage may have been defined.
2015 // pass 'true' as 2nd argument so it is returned.
2016 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002017 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2018 "Forward metaclass reference has incorrect type.");
2019 return GV;
2020 } else {
2021 // Generate as an external reference to keep a consistent
2022 // module. This will be patched up when we emit the metaclass.
2023 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
2024 llvm::GlobalValue::ExternalLinkage,
2025 0,
2026 Name,
2027 &CGM.getModule());
2028 }
2029}
2030
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002031/*
2032 struct objc_class_ext {
2033 uint32_t size;
2034 const char *weak_ivar_layout;
2035 struct _objc_property_list *properties;
2036 };
2037*/
2038llvm::Constant *
2039CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
2040 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002041 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002042
2043 std::vector<llvm::Constant*> Values(3);
2044 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002045 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002046 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002047 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002048
2049 // Return null if no extension bits are used.
2050 if (Values[1]->isNullValue() && Values[2]->isNullValue())
2051 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
2052
2053 llvm::Constant *Init =
2054 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002055 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002056 Init, "__OBJC,__class_ext,regular,no_dead_strip",
2057 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002058}
2059
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00002060/// getInterfaceDeclForIvar - Get the interface declaration node where
2061/// this ivar is declared in.
2062/// FIXME. Ideally, this info should be in the ivar node. But currently
2063/// it is not and prevailing wisdom is that ASTs should not have more
2064/// info than is absolutely needed, even though this info reflects the
2065/// source language.
2066///
2067static const ObjCInterfaceDecl *getInterfaceDeclForIvar(
2068 const ObjCInterfaceDecl *OI,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002069 const ObjCIvarDecl *IVD,
2070 ASTContext &Context) {
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00002071 if (!OI)
2072 return 0;
2073 assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface");
2074 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
2075 E = OI->ivar_end(); I != E; ++I)
2076 if ((*I)->getIdentifier() == IVD->getIdentifier())
2077 return OI;
Fariborz Jahanian5a4b4532009-03-31 17:00:52 +00002078 // look into properties.
Douglas Gregor6ab35242009-04-09 21:40:53 +00002079 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(Context),
2080 E = OI->prop_end(Context); I != E; ++I) {
Fariborz Jahanian5a4b4532009-03-31 17:00:52 +00002081 ObjCPropertyDecl *PDecl = (*I);
2082 if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl())
2083 if (IV->getIdentifier() == IVD->getIdentifier())
2084 return OI;
2085 }
Douglas Gregor6ab35242009-04-09 21:40:53 +00002086 return getInterfaceDeclForIvar(OI->getSuperClass(), IVD, Context);
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00002087}
2088
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002089/*
2090 struct objc_ivar {
2091 char *ivar_name;
2092 char *ivar_type;
2093 int ivar_offset;
2094 };
2095
2096 struct objc_ivar_list {
2097 int ivar_count;
2098 struct objc_ivar list[count];
2099 };
2100 */
2101llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002102 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002103 std::vector<llvm::Constant*> Ivars, Ivar(3);
2104
2105 // When emitting the root class GCC emits ivar entries for the
2106 // actual class structure. It is not clear if we need to follow this
2107 // behavior; for now lets try and get away with not doing it. If so,
2108 // the cleanest solution would be to make up an ObjCInterfaceDecl
2109 // for the class.
2110 if (ForClass)
2111 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002112
2113 ObjCInterfaceDecl *OID =
2114 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002115
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002116 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
2117 GetNamedIvarList(OID, OIvars);
2118
2119 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2120 ObjCIvarDecl *IVD = OIvars[i];
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002121 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2122 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00002123 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002124 ComputeIvarBaseOffset(CGM, OID, IVD));
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002125 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002126 }
2127
2128 // Return null for empty list.
2129 if (Ivars.empty())
2130 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
2131
2132 std::vector<llvm::Constant*> Values(2);
2133 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
2134 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
2135 Ivars.size());
2136 Values[1] = llvm::ConstantArray::get(AT, Ivars);
2137 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2138
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002139 llvm::GlobalVariable *GV;
2140 if (ForClass)
2141 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(),
Daniel Dunbar58a29122009-03-09 22:18:41 +00002142 Init, "__OBJC,__class_vars,regular,no_dead_strip",
2143 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002144 else
2145 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_"
2146 + ID->getNameAsString(),
2147 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002148 4, true);
2149 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002150}
2151
2152/*
2153 struct objc_method {
2154 SEL method_name;
2155 char *method_types;
2156 void *method;
2157 };
2158
2159 struct objc_method_list {
2160 struct objc_method_list *obsolete;
2161 int count;
2162 struct objc_method methods_list[count];
2163 };
2164*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002165
2166/// GetMethodConstant - Return a struct objc_method constant for the
2167/// given method if it has been defined. The result is null if the
2168/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002169llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002170 // FIXME: Use DenseMap::lookup
2171 llvm::Function *Fn = MethodDefinitions[MD];
2172 if (!Fn)
2173 return 0;
2174
2175 std::vector<llvm::Constant*> Method(3);
2176 Method[0] =
2177 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2178 ObjCTypes.SelectorPtrTy);
2179 Method[1] = GetMethodVarType(MD);
2180 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
2181 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
2182}
2183
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002184llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
2185 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002186 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002187 // Return null for empty list.
2188 if (Methods.empty())
2189 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
2190
2191 std::vector<llvm::Constant*> Values(3);
2192 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
2193 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
2194 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
2195 Methods.size());
2196 Values[2] = llvm::ConstantArray::get(AT, Methods);
2197 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2198
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002199 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002200 return llvm::ConstantExpr::getBitCast(GV,
2201 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002202}
2203
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002204llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00002205 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002206 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002207 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002208
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002209 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002210 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002211 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002212 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002213 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002214 llvm::GlobalValue::InternalLinkage,
2215 Name,
2216 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002217 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002218
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002219 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002220}
2221
Daniel Dunbar48fa0642009-04-19 02:03:42 +00002222/// GetFieldBaseOffset - return the field's byte offset.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002223uint64_t CGObjCCommonMac::GetFieldBaseOffset(const ObjCInterfaceDecl *OI,
2224 const llvm::StructLayout *Layout,
Chris Lattnercd0ee142009-03-31 08:33:16 +00002225 const FieldDecl *Field) {
Daniel Dunbar97776872009-04-22 07:32:20 +00002226 // Is this a C struct?
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00002227 if (!OI)
2228 return Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
Daniel Dunbar97776872009-04-22 07:32:20 +00002229 return ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002230}
2231
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002232llvm::GlobalVariable *
2233CGObjCCommonMac::CreateMetadataVar(const std::string &Name,
2234 llvm::Constant *Init,
2235 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002236 unsigned Align,
2237 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002238 const llvm::Type *Ty = Init->getType();
2239 llvm::GlobalVariable *GV =
2240 new llvm::GlobalVariable(Ty, false,
2241 llvm::GlobalValue::InternalLinkage,
2242 Init,
2243 Name,
2244 &CGM.getModule());
2245 if (Section)
2246 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002247 if (Align)
2248 GV->setAlignment(Align);
2249 if (AddToUsed)
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002250 UsedGlobals.push_back(GV);
2251 return GV;
2252}
2253
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002254llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002255 // Abuse this interface function as a place to finalize.
2256 FinishModule();
2257
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002258 return NULL;
2259}
2260
Chris Lattner74391b42009-03-22 21:03:39 +00002261llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002262 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002263}
2264
Chris Lattner74391b42009-03-22 21:03:39 +00002265llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002266 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002267}
2268
Chris Lattner74391b42009-03-22 21:03:39 +00002269llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002270 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002271}
2272
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002273/*
2274
2275Objective-C setjmp-longjmp (sjlj) Exception Handling
2276--
2277
2278The basic framework for a @try-catch-finally is as follows:
2279{
2280 objc_exception_data d;
2281 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002282 bool _call_try_exit = true;
2283
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002284 objc_exception_try_enter(&d);
2285 if (!setjmp(d.jmp_buf)) {
2286 ... try body ...
2287 } else {
2288 // exception path
2289 id _caught = objc_exception_extract(&d);
2290
2291 // enter new try scope for handlers
2292 if (!setjmp(d.jmp_buf)) {
2293 ... match exception and execute catch blocks ...
2294
2295 // fell off end, rethrow.
2296 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00002297 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002298 } else {
2299 // exception in catch block
2300 _rethrow = objc_exception_extract(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002301 _call_try_exit = false;
2302 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002303 }
2304 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002305 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002306
2307finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002308 if (_call_try_exit)
2309 objc_exception_try_exit(&d);
2310
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002311 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002312 ... dispatch to finally destination ...
2313
2314finally_rethrow:
2315 objc_exception_throw(_rethrow);
2316
2317finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002318}
2319
2320This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00002321uses _rethrow to determine if objc_exception_try_exit should be called
2322and if the object should be rethrown. This breaks in the face of
2323throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002324
2325We specialize this framework for a few particular circumstances:
2326
2327 - If there are no catch blocks, then we avoid emitting the second
2328 exception handling context.
2329
2330 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2331 e)) we avoid emitting the code to rethrow an uncaught exception.
2332
2333 - FIXME: If there is no @finally block we can do a few more
2334 simplifications.
2335
2336Rethrows and Jumps-Through-Finally
2337--
2338
2339Support for implicit rethrows and jumping through the finally block is
2340handled by storing the current exception-handling context in
2341ObjCEHStack.
2342
Daniel Dunbar898d5082008-09-30 01:06:03 +00002343In order to implement proper @finally semantics, we support one basic
2344mechanism for jumping through the finally block to an arbitrary
2345destination. Constructs which generate exits from a @try or @catch
2346block use this mechanism to implement the proper semantics by chaining
2347jumps, as necessary.
2348
2349This mechanism works like the one used for indirect goto: we
2350arbitrarily assign an ID to each destination and store the ID for the
2351destination in a variable prior to entering the finally block. At the
2352end of the finally block we simply create a switch to the proper
2353destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002354
2355Code gen for @synchronized(expr) stmt;
2356Effectively generating code for:
2357objc_sync_enter(expr);
2358@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002359*/
2360
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002361void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2362 const Stmt &S) {
2363 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002364 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00002365 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002366 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar55e87422008-11-11 02:29:29 +00002367 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
2368 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
2369 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar1c566672009-02-24 01:43:46 +00002370
2371 // For @synchronized, call objc_sync_enter(sync.expr). The
2372 // evaluation of the expression must occur before we enter the
2373 // @synchronized. We can safely avoid a temp here because jumps into
2374 // @synchronized are illegal & this will dominate uses.
2375 llvm::Value *SyncArg = 0;
2376 if (!isTry) {
2377 SyncArg =
2378 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2379 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00002380 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002381 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002382
2383 // Push an EH context entry, used for handling rethrows and jumps
2384 // through finally.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002385 CGF.PushCleanupBlock(FinallyBlock);
2386
Anders Carlsson273558f2009-02-07 21:37:21 +00002387 CGF.ObjCEHValueStack.push_back(0);
2388
Daniel Dunbar898d5082008-09-30 01:06:03 +00002389 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00002390 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2391 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002392 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
2393 "_rethrow");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002394 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
2395 "_call_try_exit");
2396 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr);
2397
Anders Carlsson80f25672008-09-09 17:59:25 +00002398 // Enter a new try block and call setjmp.
Chris Lattner34b02a12009-04-22 02:26:14 +00002399 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Anders Carlsson80f25672008-09-09 17:59:25 +00002400 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
2401 "jmpbufarray");
2402 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
Chris Lattner34b02a12009-04-22 02:26:14 +00002403 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlsson80f25672008-09-09 17:59:25 +00002404 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00002405
Daniel Dunbar55e87422008-11-11 02:29:29 +00002406 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2407 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002408 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002409 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002410
2411 // Emit the @try block.
2412 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002413 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
2414 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002415 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002416
2417 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002418 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002419
2420 // Retrieve the exception object. We may emit multiple blocks but
2421 // nothing can cross this so the value is already in SSA form.
Chris Lattner34b02a12009-04-22 02:26:14 +00002422 llvm::Value *Caught =
2423 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2424 ExceptionData, "caught");
Anders Carlsson273558f2009-02-07 21:37:21 +00002425 CGF.ObjCEHValueStack.back() = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002426 if (!isTry)
2427 {
2428 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002429 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002430 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002431 }
2432 else if (const ObjCAtCatchStmt* CatchStmt =
2433 cast<ObjCAtTryStmt>(S).getCatchStmts())
2434 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00002435 // Enter a new exception try block (in case a @catch block throws
2436 // an exception).
Chris Lattner34b02a12009-04-22 02:26:14 +00002437 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002438
Chris Lattner34b02a12009-04-22 02:26:14 +00002439 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlsson80f25672008-09-09 17:59:25 +00002440 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002441 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00002442
Daniel Dunbar55e87422008-11-11 02:29:29 +00002443 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
2444 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002445 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002446
2447 CGF.EmitBlock(CatchBlock);
2448
Daniel Dunbar55e40722008-09-27 07:03:52 +00002449 // Handle catch list. As a special case we check if everything is
2450 // matched and avoid generating code for falling off the end if
2451 // so.
2452 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00002453 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00002454 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00002455
Steve Naroff7ba138a2009-03-03 19:52:17 +00002456 const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Daniel Dunbar129271a2008-09-27 07:36:24 +00002457 const PointerType *PT = 0;
2458
Anders Carlsson80f25672008-09-09 17:59:25 +00002459 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00002460 if (!CatchParam) {
2461 AllMatched = true;
2462 } else {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002463 PT = CatchParam->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002464
Daniel Dunbar97f61d12008-09-27 22:21:14 +00002465 // catch(id e) always matches.
2466 // FIXME: For the time being we also match id<X>; this should
2467 // be rejected by Sema instead.
Steve Naroff389bf462009-02-12 17:52:19 +00002468 if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) ||
Steve Naroff7ba138a2009-03-03 19:52:17 +00002469 CatchParam->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00002470 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00002471 }
2472
Daniel Dunbar55e40722008-09-27 07:03:52 +00002473 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00002474 if (CatchParam) {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002475 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002476 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002477 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002478 }
Anders Carlsson1452f552008-09-11 08:21:54 +00002479
Anders Carlssondde0a942008-09-11 09:15:33 +00002480 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002481 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002482 break;
2483 }
2484
Daniel Dunbar129271a2008-09-27 07:36:24 +00002485 assert(PT && "Unexpected non-pointer type in @catch");
2486 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00002487 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002488 assert(ObjCType && "Catch parameter must have Objective-C type!");
2489
2490 // Check if the @catch block matches the exception object.
2491 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
2492
Chris Lattner34b02a12009-04-22 02:26:14 +00002493 llvm::Value *Match =
2494 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
2495 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00002496
Daniel Dunbar55e87422008-11-11 02:29:29 +00002497 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00002498
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002499 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002500 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002501
2502 // Emit the @catch block.
2503 CGF.EmitBlock(MatchedBlock);
Steve Naroff7ba138a2009-03-03 19:52:17 +00002504 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002505 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002506
2507 llvm::Value *Tmp =
Steve Naroff7ba138a2009-03-03 19:52:17 +00002508 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002509 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002510 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002511
2512 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002513 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002514
2515 CGF.EmitBlock(NextCatchBlock);
2516 }
2517
Daniel Dunbar55e40722008-09-27 07:03:52 +00002518 if (!AllMatched) {
2519 // None of the handlers caught the exception, so store it to be
2520 // rethrown at the end of the @finally block.
2521 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002522 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002523 }
2524
2525 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002526 CGF.EmitBlock(CatchHandler);
Chris Lattner34b02a12009-04-22 02:26:14 +00002527 CGF.Builder.CreateStore(
2528 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2529 ExceptionData),
Daniel Dunbar55e40722008-09-27 07:03:52 +00002530 RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002531 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002532 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002533 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00002534 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002535 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002536 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson80f25672008-09-09 17:59:25 +00002537 }
2538
Daniel Dunbar898d5082008-09-30 01:06:03 +00002539 // Pop the exception-handling stack entry. It is important to do
2540 // this now, because the code in the @finally block is not in this
2541 // context.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002542 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2543
Anders Carlsson273558f2009-02-07 21:37:21 +00002544 CGF.ObjCEHValueStack.pop_back();
2545
Anders Carlsson80f25672008-09-09 17:59:25 +00002546 // Emit the @finally block.
2547 CGF.EmitBlock(FinallyBlock);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002548 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2549
2550 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2551
2552 CGF.EmitBlock(FinallyExit);
Chris Lattner34b02a12009-04-22 02:26:14 +00002553 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002554
2555 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002556 if (isTry) {
2557 if (const ObjCAtFinallyStmt* FinallyStmt =
2558 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2559 CGF.EmitStmt(FinallyStmt->getFinallyBody());
Daniel Dunbar1c566672009-02-24 01:43:46 +00002560 } else {
2561 // Emit objc_sync_exit(expr); as finally's sole statement for
2562 // @synchronized.
Chris Lattnerbbccd612009-04-22 02:38:11 +00002563 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002564 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002565
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002566 // Emit the switch block
2567 if (Info.SwitchBlock)
2568 CGF.EmitBlock(Info.SwitchBlock);
2569 if (Info.EndBlock)
2570 CGF.EmitBlock(Info.EndBlock);
2571
Daniel Dunbar898d5082008-09-30 01:06:03 +00002572 CGF.EmitBlock(FinallyRethrow);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002573 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbar898d5082008-09-30 01:06:03 +00002574 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002575 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00002576
2577 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002578}
2579
2580void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002581 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002582 llvm::Value *ExceptionAsObject;
2583
2584 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2585 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2586 ExceptionAsObject =
2587 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2588 } else {
Anders Carlsson273558f2009-02-07 21:37:21 +00002589 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002590 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00002591 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002592 }
2593
Chris Lattnerbbccd612009-04-22 02:38:11 +00002594 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002595 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002596
2597 // Clear the insertion point to indicate we are in unreachable code.
2598 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002599}
2600
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002601/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002602/// object: objc_read_weak (id *src)
2603///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002604llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002605 llvm::Value *AddrWeakObj)
2606{
Eli Friedman8339b352009-03-07 03:57:15 +00002607 const llvm::Type* DestTy =
2608 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002609 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00002610 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002611 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00002612 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002613 return read_weak;
2614}
2615
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002616/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2617/// objc_assign_weak (id src, id *dst)
2618///
2619void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2620 llvm::Value *src, llvm::Value *dst)
2621{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002622 const llvm::Type * SrcTy = src->getType();
2623 if (!isa<llvm::PointerType>(SrcTy)) {
2624 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2625 assert(Size <= 8 && "does not support size > 8");
2626 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2627 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002628 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2629 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002630 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2631 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00002632 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002633 src, dst, "weakassign");
2634 return;
2635}
2636
Fariborz Jahanian58626502008-11-19 00:59:10 +00002637/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2638/// objc_assign_global (id src, id *dst)
2639///
2640void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2641 llvm::Value *src, llvm::Value *dst)
2642{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002643 const llvm::Type * SrcTy = src->getType();
2644 if (!isa<llvm::PointerType>(SrcTy)) {
2645 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2646 assert(Size <= 8 && "does not support size > 8");
2647 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2648 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002649 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2650 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002651 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2652 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002653 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00002654 src, dst, "globalassign");
2655 return;
2656}
2657
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002658/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2659/// objc_assign_ivar (id src, id *dst)
2660///
2661void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2662 llvm::Value *src, llvm::Value *dst)
2663{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002664 const llvm::Type * SrcTy = src->getType();
2665 if (!isa<llvm::PointerType>(SrcTy)) {
2666 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2667 assert(Size <= 8 && "does not support size > 8");
2668 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2669 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002670 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2671 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002672 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2673 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002674 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(),
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002675 src, dst, "assignivar");
2676 return;
2677}
2678
Fariborz Jahanian58626502008-11-19 00:59:10 +00002679/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2680/// objc_assign_strongCast (id src, id *dst)
2681///
2682void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2683 llvm::Value *src, llvm::Value *dst)
2684{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002685 const llvm::Type * SrcTy = src->getType();
2686 if (!isa<llvm::PointerType>(SrcTy)) {
2687 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2688 assert(Size <= 8 && "does not support size > 8");
2689 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2690 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002691 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2692 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002693 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2694 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002695 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00002696 src, dst, "weakassign");
2697 return;
2698}
2699
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002700/// EmitObjCValueForIvar - Code Gen for ivar reference.
2701///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002702LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2703 QualType ObjectTy,
2704 llvm::Value *BaseValue,
2705 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002706 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00002707 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00002708 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
2709 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002710}
2711
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002712llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00002713 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002714 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00002715 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002716 return llvm::ConstantInt::get(
2717 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2718 Offset);
2719}
2720
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002721/* *** Private Interface *** */
2722
2723/// EmitImageInfo - Emit the image info marker used to encode some module
2724/// level information.
2725///
2726/// See: <rdr://4810609&4810587&4810587>
2727/// struct IMAGE_INFO {
2728/// unsigned version;
2729/// unsigned flags;
2730/// };
2731enum ImageInfoFlags {
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002732 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what
2733 // this implies.
2734 eImageInfo_GarbageCollected = (1 << 1),
2735 eImageInfo_GCOnly = (1 << 2),
2736 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
2737
2738 // A flag indicating that the module has no instances of an
2739 // @synthesize of a superclass variable. <rdar://problem/6803242>
2740 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002741};
2742
2743void CGObjCMac::EmitImageInfo() {
2744 unsigned version = 0; // Version is unused?
2745 unsigned flags = 0;
2746
2747 // FIXME: Fix and continue?
2748 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2749 flags |= eImageInfo_GarbageCollected;
2750 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2751 flags |= eImageInfo_GCOnly;
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002752
2753 // We never allow @synthesize of a superclass property.
2754 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002755
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002756 // Emitted as int[2];
2757 llvm::Constant *values[2] = {
2758 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2759 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2760 };
2761 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002762
2763 const char *Section;
2764 if (ObjCABI == 1)
2765 Section = "__OBJC, __image_info,regular";
2766 else
2767 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002768 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002769 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
2770 llvm::ConstantArray::get(AT, values, 2),
2771 Section,
2772 0,
2773 true);
2774 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002775}
2776
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002777
2778// struct objc_module {
2779// unsigned long version;
2780// unsigned long size;
2781// const char *name;
2782// Symtab symtab;
2783// };
2784
2785// FIXME: Get from somewhere
2786static const int ModuleVersion = 7;
2787
2788void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002789 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002790
2791 std::vector<llvm::Constant*> Values(4);
2792 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2793 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002794 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002795 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002796 Values[3] = EmitModuleSymbols();
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002797 CreateMetadataVar("\01L_OBJC_MODULES",
2798 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
2799 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002800 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002801}
2802
2803llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002804 unsigned NumClasses = DefinedClasses.size();
2805 unsigned NumCategories = DefinedCategories.size();
2806
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002807 // Return null if no symbols were defined.
2808 if (!NumClasses && !NumCategories)
2809 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2810
2811 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002812 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2813 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2814 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2815 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2816
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002817 // The runtime expects exactly the list of defined classes followed
2818 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002819 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002820 for (unsigned i=0; i<NumClasses; i++)
2821 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2822 ObjCTypes.Int8PtrTy);
2823 for (unsigned i=0; i<NumCategories; i++)
2824 Symbols[NumClasses + i] =
2825 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2826 ObjCTypes.Int8PtrTy);
2827
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002828 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002829 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002830 NumClasses + NumCategories),
2831 Symbols);
2832
2833 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2834
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002835 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002836 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
2837 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002838 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002839 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2840}
2841
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002842llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002843 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002844 LazySymbols.insert(ID->getIdentifier());
2845
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002846 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2847
2848 if (!Entry) {
2849 llvm::Constant *Casted =
2850 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2851 ObjCTypes.ClassPtrTy);
2852 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002853 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
2854 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002855 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002856 }
2857
2858 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002859}
2860
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002861llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002862 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2863
2864 if (!Entry) {
2865 llvm::Constant *Casted =
2866 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2867 ObjCTypes.SelectorPtrTy);
2868 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002869 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
2870 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002871 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002872 }
2873
2874 return Builder.CreateLoad(Entry, false, "tmp");
2875}
2876
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002877llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002878 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002879
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002880 if (!Entry)
2881 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
2882 llvm::ConstantArray::get(Ident->getName()),
2883 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00002884 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002885
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002886 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002887}
2888
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00002889/// GetIvarLayoutName - Returns a unique constant for the given
2890/// ivar layout bitmap.
2891llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
2892 const ObjCCommonTypesHelper &ObjCTypes) {
2893 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
2894}
2895
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002896void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCInterfaceDecl *OI,
2897 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002898 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00002899 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002900 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +00002901 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002902 bool IsUnion = (RD && RD->isUnion());
2903 uint64_t MaxUnionIvarSize = 0;
2904 uint64_t MaxSkippedUnionIvarSize = 0;
2905 FieldDecl *MaxField = 0;
2906 FieldDecl *MaxSkippedField = 0;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002907 FieldDecl *LastFieldBitfield = 0;
2908
Chris Lattnerf1690852009-03-31 08:48:01 +00002909 unsigned base = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002910 if (RecFields.empty())
2911 return;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002912 if (IsUnion)
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002913 base = BytePos + GetFieldBaseOffset(OI, Layout, RecFields[0]);
Chris Lattnerf1690852009-03-31 08:48:01 +00002914 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
2915 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
2916
2917 llvm::SmallVector<FieldDecl*, 16> TmpRecFields;
2918
2919 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002920 FieldDecl *Field = RecFields[i];
2921 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002922 if (!Field->getIdentifier() || Field->isBitField()) {
2923 LastFieldBitfield = Field;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002924 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002925 }
2926 LastFieldBitfield = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002927 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00002928 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002929 if (FQT->isUnionType())
2930 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002931 else
2932 assert(FQT->isRecordType() &&
2933 "only union/record is supported for ivar layout bitmap");
2934
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002935 const RecordType *RT = FQT->getAsRecordType();
2936 const RecordDecl *RD = RT->getDecl();
Daniel Dunbarb02532a2009-04-19 23:41:48 +00002937 // FIXME - Find a more efficient way of passing records down.
Douglas Gregor6ab35242009-04-09 21:40:53 +00002938 TmpRecFields.append(RD->field_begin(CGM.getContext()),
2939 RD->field_end(CGM.getContext()));
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00002940 const llvm::Type *Ty = CGM.getTypes().ConvertType(FQT);
2941 const llvm::StructLayout *RecLayout =
2942 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
2943
2944 BuildAggrIvarLayout(0, RecLayout, RD, TmpRecFields,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002945 BytePos + GetFieldBaseOffset(OI, Layout, Field),
Fariborz Jahanian81adc052009-04-24 16:17:09 +00002946 ForStrongLayout, HasUnion);
Chris Lattnerf1690852009-03-31 08:48:01 +00002947 TmpRecFields.clear();
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002948 continue;
2949 }
Chris Lattnerf1690852009-03-31 08:48:01 +00002950
2951 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002952 const ConstantArrayType *CArray =
2953 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002954 uint64_t ElCount = CArray->getSize().getZExtValue();
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002955 assert(CArray && "only array with know element size is supported");
2956 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00002957 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
2958 const ConstantArrayType *CArray =
2959 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002960 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00002961 FQT = CArray->getElementType();
2962 }
2963
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002964 assert(!FQT->isUnionType() &&
2965 "layout for array of unions not supported");
2966 if (FQT->isRecordType()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00002967 int OldIndex = IvarsInfo.size() - 1;
2968 int OldSkIndex = SkipIvars.size() -1;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002969
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002970 // FIXME - Use a common routine with the above!
2971 const RecordType *RT = FQT->getAsRecordType();
2972 const RecordDecl *RD = RT->getDecl();
2973 // FIXME - Find a more efficiant way of passing records down.
Douglas Gregor6ab35242009-04-09 21:40:53 +00002974 TmpRecFields.append(RD->field_begin(CGM.getContext()),
2975 RD->field_end(CGM.getContext()));
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00002976 const llvm::Type *Ty = CGM.getTypes().ConvertType(FQT);
2977 const llvm::StructLayout *RecLayout =
2978 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Chris Lattnerf1690852009-03-31 08:48:01 +00002979
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00002980 BuildAggrIvarLayout(0, RecLayout, RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00002981 TmpRecFields,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002982 BytePos + GetFieldBaseOffset(OI, Layout, Field),
Fariborz Jahanian81adc052009-04-24 16:17:09 +00002983 ForStrongLayout, HasUnion);
Chris Lattnerf1690852009-03-31 08:48:01 +00002984 TmpRecFields.clear();
2985
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002986 // Replicate layout information for each array element. Note that
2987 // one element is already done.
2988 uint64_t ElIx = 1;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00002989 for (int FirstIndex = IvarsInfo.size() - 1,
2990 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002991 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002992 for (int i = OldIndex+1; i <= FirstIndex; ++i)
2993 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002994 GC_IVAR gcivar;
2995 gcivar.ivar_bytepos = IvarsInfo[i].ivar_bytepos + Size*ElIx;
2996 gcivar.ivar_size = IvarsInfo[i].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00002997 IvarsInfo.push_back(gcivar);
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002998 }
2999
Chris Lattnerf1690852009-03-31 08:48:01 +00003000 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003001 GC_IVAR skivar;
3002 skivar.ivar_bytepos = SkipIvars[i].ivar_bytepos + Size*ElIx;
3003 skivar.ivar_size = SkipIvars[i].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003004 SkipIvars.push_back(skivar);
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003005 }
3006 }
3007 continue;
3008 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003009 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003010 // At this point, we are done with Record/Union and array there of.
3011 // For other arrays we are down to its element type.
3012 QualType::GCAttrTypes GCAttr = QualType::GCNone;
3013 do {
3014 if (FQT.isObjCGCStrong() || FQT.isObjCGCWeak()) {
3015 GCAttr = FQT.isObjCGCStrong() ? QualType::Strong : QualType::Weak;
3016 break;
3017 }
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003018 else if (CGM.getContext().isObjCObjectPointerType(FQT)) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003019 GCAttr = QualType::Strong;
3020 break;
3021 }
3022 else if (const PointerType *PT = FQT->getAsPointerType()) {
3023 FQT = PT->getPointeeType();
3024 }
3025 else {
3026 break;
3027 }
3028 } while (true);
Chris Lattnerf1690852009-03-31 08:48:01 +00003029
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003030 if ((ForStrongLayout && GCAttr == QualType::Strong)
3031 || (!ForStrongLayout && GCAttr == QualType::Weak)) {
3032 if (IsUnion)
3033 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003034 uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType())
3035 / WordSizeInBits;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003036 if (UnionIvarSize > MaxUnionIvarSize)
3037 {
3038 MaxUnionIvarSize = UnionIvarSize;
3039 MaxField = Field;
3040 }
3041 }
3042 else
3043 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003044 GC_IVAR gcivar;
3045 gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field);
3046 gcivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) /
3047 WordSizeInBits;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003048 IvarsInfo.push_back(gcivar);
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003049 }
3050 }
3051 else if ((ForStrongLayout &&
3052 (GCAttr == QualType::GCNone || GCAttr == QualType::Weak))
3053 || (!ForStrongLayout && GCAttr != QualType::Weak)) {
3054 if (IsUnion)
3055 {
3056 uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType());
3057 if (UnionIvarSize > MaxSkippedUnionIvarSize)
3058 {
3059 MaxSkippedUnionIvarSize = UnionIvarSize;
3060 MaxSkippedField = Field;
3061 }
3062 }
3063 else
3064 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003065 GC_IVAR skivar;
3066 skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field);
3067 skivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) /
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003068 ByteSizeInBits;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003069 SkipIvars.push_back(skivar);
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003070 }
3071 }
3072 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003073 if (LastFieldBitfield) {
3074 // Last field was a bitfield. Must update skip info.
3075 GC_IVAR skivar;
3076 skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout,
3077 LastFieldBitfield);
3078 Expr *BitWidth = LastFieldBitfield->getBitWidth();
3079 uint64_t BitFieldSize =
Eli Friedman9a901bb2009-04-26 19:19:15 +00003080 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003081 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3082 + ((BitFieldSize % ByteSizeInBits) != 0);
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003083 SkipIvars.push_back(skivar);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003084 }
3085
Chris Lattnerf1690852009-03-31 08:48:01 +00003086 if (MaxField) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003087 GC_IVAR gcivar;
3088 gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, MaxField);
3089 gcivar.ivar_size = MaxUnionIvarSize;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003090 IvarsInfo.push_back(gcivar);
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003091 }
Chris Lattnerf1690852009-03-31 08:48:01 +00003092
3093 if (MaxSkippedField) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003094 GC_IVAR skivar;
3095 skivar.ivar_bytepos = BytePos +
3096 GetFieldBaseOffset(OI, Layout, MaxSkippedField);
3097 skivar.ivar_size = MaxSkippedUnionIvarSize;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003098 SkipIvars.push_back(skivar);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003099 }
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003100}
3101
3102/// BuildIvarLayout - Builds ivar layout bitmap for the class
3103/// implementation for the __strong or __weak case.
3104/// The layout map displays which words in ivar list must be skipped
3105/// and which must be scanned by GC (see below). String is built of bytes.
3106/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3107/// of words to skip and right nibble is count of words to scan. So, each
3108/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3109/// represented by a 0x00 byte which also ends the string.
3110/// 1. when ForStrongLayout is true, following ivars are scanned:
3111/// - id, Class
3112/// - object *
3113/// - __strong anything
3114///
3115/// 2. When ForStrongLayout is false, following ivars are scanned:
3116/// - __weak anything
3117///
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003118llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003119 const ObjCImplementationDecl *OMD,
3120 bool ForStrongLayout) {
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003121 bool hasUnion = false;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003122
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003123 unsigned int WordsToScan, WordsToSkip;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003124 const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
3125 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
3126 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003127
Chris Lattnerf1690852009-03-31 08:48:01 +00003128 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003129 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003130 CGM.getContext().CollectObjCIvars(OI, RecFields);
3131 if (RecFields.empty())
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003132 return llvm::Constant::getNullValue(PtrTy);
Chris Lattnerf1690852009-03-31 08:48:01 +00003133
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003134 SkipIvars.clear();
3135 IvarsInfo.clear();
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00003136
Daniel Dunbar84ad77a2009-04-22 09:39:34 +00003137 const llvm::StructLayout *Layout =
3138 CGM.getTargetData().getStructLayout(GetConcreteClassStruct(CGM, OI));
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003139 BuildAggrIvarLayout(OI, Layout, 0, RecFields, 0, ForStrongLayout, hasUnion);
3140 if (IvarsInfo.empty())
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003141 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003142
3143 // Sort on byte position in case we encounterred a union nested in
3144 // the ivar list.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003145 if (hasUnion && !IvarsInfo.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003146 std::sort(IvarsInfo.begin(), IvarsInfo.end());
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003147 if (hasUnion && !SkipIvars.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003148 std::sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003149
3150 // Build the string of skip/scan nibbles
Fariborz Jahanian8c2f2d12009-04-24 17:15:27 +00003151 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003152 unsigned int WordSize =
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003153 CGM.getTypes().getTargetData().getTypePaddedSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003154 if (IvarsInfo[0].ivar_bytepos == 0) {
3155 WordsToSkip = 0;
3156 WordsToScan = IvarsInfo[0].ivar_size;
3157 }
3158 else {
3159 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3160 WordsToScan = IvarsInfo[0].ivar_size;
3161 }
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003162 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++)
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003163 {
3164 unsigned int TailPrevGCObjC =
3165 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
3166 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC)
3167 {
3168 // consecutive 'scanned' object pointers.
3169 WordsToScan += IvarsInfo[i].ivar_size;
3170 }
3171 else
3172 {
3173 // Skip over 'gc'able object pointer which lay over each other.
3174 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3175 continue;
3176 // Must skip over 1 or more words. We save current skip/scan values
3177 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003178 SKIP_SCAN SkScan;
3179 SkScan.skip = WordsToSkip;
3180 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003181 SkipScanIvars.push_back(SkScan);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003182
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003183 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003184 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3185 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003186 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003187 WordsToSkip = 0;
3188 WordsToScan = IvarsInfo[i].ivar_size;
3189 }
3190 }
3191 if (WordsToScan > 0)
3192 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003193 SKIP_SCAN SkScan;
3194 SkScan.skip = WordsToSkip;
3195 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003196 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003197 }
3198
3199 bool BytesSkipped = false;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003200 if (!SkipIvars.empty())
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003201 {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003202 unsigned int LastIndex = SkipIvars.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003203 int LastByteSkipped =
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003204 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
3205 LastIndex = IvarsInfo.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003206 int LastByteScanned =
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003207 IvarsInfo[LastIndex].ivar_bytepos +
3208 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003209 BytesSkipped = (LastByteSkipped > LastByteScanned);
3210 // Compute number of bytes to skip at the tail end of the last ivar scanned.
3211 if (BytesSkipped)
3212 {
3213 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003214 SKIP_SCAN SkScan;
3215 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3216 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003217 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003218 }
3219 }
3220 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3221 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003222 int SkipScan = SkipScanIvars.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003223 for (int i = 0; i <= SkipScan; i++)
3224 {
3225 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3226 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3227 // 0xM0 followed by 0x0N detected.
3228 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3229 for (int j = i+1; j < SkipScan; j++)
3230 SkipScanIvars[j] = SkipScanIvars[j+1];
3231 --SkipScan;
3232 }
3233 }
3234
3235 // Generate the string.
3236 std::string BitMap;
3237 for (int i = 0; i <= SkipScan; i++)
3238 {
3239 unsigned char byte;
3240 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3241 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3242 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3243 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
3244
3245 if (skip_small > 0 || skip_big > 0)
3246 BytesSkipped = true;
3247 // first skip big.
3248 for (unsigned int ix = 0; ix < skip_big; ix++)
3249 BitMap += (unsigned char)(0xf0);
3250
3251 // next (skip small, scan)
3252 if (skip_small)
3253 {
3254 byte = skip_small << 4;
3255 if (scan_big > 0)
3256 {
3257 byte |= 0xf;
3258 --scan_big;
3259 }
3260 else if (scan_small)
3261 {
3262 byte |= scan_small;
3263 scan_small = 0;
3264 }
3265 BitMap += byte;
3266 }
3267 // next scan big
3268 for (unsigned int ix = 0; ix < scan_big; ix++)
3269 BitMap += (unsigned char)(0x0f);
3270 // last scan small
3271 if (scan_small)
3272 {
3273 byte = scan_small;
3274 BitMap += byte;
3275 }
3276 }
3277 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003278 unsigned char zero = 0;
3279 BitMap += zero;
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003280
3281 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
3282 printf("\n%s ivar layout for class '%s': ",
3283 ForStrongLayout ? "strong" : "weak",
3284 OMD->getClassInterface()->getNameAsCString());
3285 const unsigned char *s = (unsigned char*)BitMap.c_str();
3286 for (unsigned i = 0; i < BitMap.size(); i++)
3287 if (!(s[i] & 0xf0))
3288 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3289 else
3290 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3291 printf("\n");
3292 }
3293
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003294 // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as
3295 // final layout.
3296 if (ForStrongLayout && !BytesSkipped)
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003297 return llvm::Constant::getNullValue(PtrTy);
3298 llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3299 llvm::ConstantArray::get(BitMap.c_str()),
3300 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003301 1, true);
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003302 return getConstantGEP(Entry, 0, 0);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003303}
3304
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003305llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003306 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3307
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003308 // FIXME: Avoid std::string copying.
3309 if (!Entry)
3310 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
3311 llvm::ConstantArray::get(Sel.getAsString()),
3312 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003313 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003314
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003315 return getConstantGEP(Entry, 0, 0);
3316}
3317
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003318// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003319llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003320 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3321}
3322
3323// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003324llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003325 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
3326}
3327
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003328llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003329 std::string TypeStr;
3330 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3331
3332 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003333
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003334 if (!Entry)
3335 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
3336 llvm::ConstantArray::get(TypeStr),
3337 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003338 1, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003339
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003340 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003341}
3342
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003343llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003344 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003345 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3346 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00003347
3348 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3349
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003350 if (!Entry)
3351 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
3352 llvm::ConstantArray::get(TypeStr),
3353 "__TEXT,__cstring,cstring_literals",
3354 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00003355
3356 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003357}
3358
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003359// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003360llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003361 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
3362
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003363 if (!Entry)
3364 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
3365 llvm::ConstantArray::get(Ident->getName()),
3366 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003367 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003368
3369 return getConstantGEP(Entry, 0, 0);
3370}
3371
3372// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003373// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003374llvm::Constant *
3375 CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3376 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003377 std::string TypeStr;
3378 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003379 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3380}
3381
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003382void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
3383 const ObjCContainerDecl *CD,
3384 std::string &NameOut) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00003385 NameOut = '\01';
3386 NameOut += (D->isInstanceMethod() ? '-' : '+');
Chris Lattner077bf5e2008-11-24 03:33:13 +00003387 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003388 assert (CD && "Missing container decl in GetNameForMethod");
3389 NameOut += CD->getNameAsString();
Fariborz Jahanian1e9aef32009-04-16 18:34:20 +00003390 if (const ObjCCategoryImplDecl *CID =
3391 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) {
3392 NameOut += '(';
3393 NameOut += CID->getNameAsString();
3394 NameOut+= ')';
3395 }
Chris Lattner077bf5e2008-11-24 03:33:13 +00003396 NameOut += ' ';
3397 NameOut += D->getSelector().getAsString();
3398 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003399}
3400
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003401void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003402 EmitModuleInfo();
3403
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003404 // Emit the dummy bodies for any protocols which were referenced but
3405 // never defined.
3406 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
3407 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
3408 if (i->second->hasInitializer())
3409 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003410
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003411 std::vector<llvm::Constant*> Values(5);
3412 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
3413 Values[1] = GetClassName(i->first);
3414 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
3415 Values[3] = Values[4] =
3416 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
3417 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
3418 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
3419 Values));
3420 }
3421
3422 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003423 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003424 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003425 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003426 }
3427
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003428 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003429 llvm::GlobalValue *GV =
3430 new llvm::GlobalVariable(AT, false,
3431 llvm::GlobalValue::AppendingLinkage,
3432 llvm::ConstantArray::get(AT, Used),
3433 "llvm.used",
3434 &CGM.getModule());
3435
3436 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003437
3438 // Add assembler directives to add lazy undefined symbol references
3439 // for classes which are referenced but not defined. This is
3440 // important for correct linker interaction.
3441
3442 // FIXME: Uh, this isn't particularly portable.
3443 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00003444
3445 if (!CGM.getModule().getModuleInlineAsm().empty())
3446 s << "\n";
3447
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003448 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
3449 e = LazySymbols.end(); i != e; ++i) {
3450 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
3451 }
3452 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
3453 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003454 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003455 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
3456 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00003457
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003458 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003459}
3460
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003461CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003462 : CGObjCCommonMac(cgm),
3463 ObjCTypes(cgm)
3464{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003465 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003466 ObjCABI = 2;
3467}
3468
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003469/* *** */
3470
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003471ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
3472: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003473{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003474 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3475 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003476
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003477 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003478 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003479 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003480 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003481 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
3482
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003483 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00003484 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003485 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003486
3487 // FIXME: It would be nice to unify this with the opaque type, so
3488 // that the IR comes out a bit cleaner.
3489 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
3490 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003491
3492 // I'm not sure I like this. The implicit coordination is a bit
3493 // gross. We should solve this in a reasonable fashion because this
3494 // is a pretty common task (match some runtime data structure with
3495 // an LLVM data structure).
3496
3497 // FIXME: This is leaked.
3498 // FIXME: Merge with rewriter code?
3499
3500 // struct _objc_super {
3501 // id self;
3502 // Class cls;
3503 // }
3504 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3505 SourceLocation(),
3506 &Ctx.Idents.get("_objc_super"));
Douglas Gregor6ab35242009-04-09 21:40:53 +00003507 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3508 Ctx.getObjCIdType(), 0, false));
3509 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3510 Ctx.getObjCClassType(), 0, false));
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003511 RD->completeDefinition(Ctx);
3512
3513 SuperCTy = Ctx.getTagDeclType(RD);
3514 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
3515
3516 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003517 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
3518
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003519 // struct _prop_t {
3520 // char *name;
3521 // char *attributes;
3522 // }
Chris Lattner1c02f862009-04-22 02:53:24 +00003523 PropertyTy = llvm::StructType::get(Int8PtrTy, Int8PtrTy, NULL);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003524 CGM.getModule().addTypeName("struct._prop_t",
3525 PropertyTy);
3526
3527 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003528 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003529 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003530 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003531 // }
3532 PropertyListTy = llvm::StructType::get(IntTy,
3533 IntTy,
3534 llvm::ArrayType::get(PropertyTy, 0),
3535 NULL);
3536 CGM.getModule().addTypeName("struct._prop_list_t",
3537 PropertyListTy);
3538 // struct _prop_list_t *
3539 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
3540
3541 // struct _objc_method {
3542 // SEL _cmd;
3543 // char *method_type;
3544 // char *_imp;
3545 // }
3546 MethodTy = llvm::StructType::get(SelectorPtrTy,
3547 Int8PtrTy,
3548 Int8PtrTy,
3549 NULL);
3550 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003551
3552 // struct _objc_cache *
3553 CacheTy = llvm::OpaqueType::get();
3554 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
3555 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003556}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003557
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003558ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
3559 : ObjCCommonTypesHelper(cgm)
3560{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003561 // struct _objc_method_description {
3562 // SEL name;
3563 // char *types;
3564 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003565 MethodDescriptionTy =
3566 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003567 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003568 NULL);
3569 CGM.getModule().addTypeName("struct._objc_method_description",
3570 MethodDescriptionTy);
3571
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003572 // struct _objc_method_description_list {
3573 // int count;
3574 // struct _objc_method_description[1];
3575 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003576 MethodDescriptionListTy =
3577 llvm::StructType::get(IntTy,
3578 llvm::ArrayType::get(MethodDescriptionTy, 0),
3579 NULL);
3580 CGM.getModule().addTypeName("struct._objc_method_description_list",
3581 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003582
3583 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003584 MethodDescriptionListPtrTy =
3585 llvm::PointerType::getUnqual(MethodDescriptionListTy);
3586
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003587 // Protocol description structures
3588
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003589 // struct _objc_protocol_extension {
3590 // uint32_t size; // sizeof(struct _objc_protocol_extension)
3591 // struct _objc_method_description_list *optional_instance_methods;
3592 // struct _objc_method_description_list *optional_class_methods;
3593 // struct _objc_property_list *instance_properties;
3594 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003595 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003596 llvm::StructType::get(IntTy,
3597 MethodDescriptionListPtrTy,
3598 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003599 PropertyListPtrTy,
3600 NULL);
3601 CGM.getModule().addTypeName("struct._objc_protocol_extension",
3602 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003603
3604 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003605 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
3606
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003607 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003608
3609 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
3610 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3611
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003612 const llvm::Type *T =
3613 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
3614 LongTy,
3615 llvm::ArrayType::get(ProtocolTyHolder, 0),
3616 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003617 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
3618
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003619 // struct _objc_protocol {
3620 // struct _objc_protocol_extension *isa;
3621 // char *protocol_name;
3622 // struct _objc_protocol **_objc_protocol_list;
3623 // struct _objc_method_description_list *instance_methods;
3624 // struct _objc_method_description_list *class_methods;
3625 // }
3626 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003627 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003628 llvm::PointerType::getUnqual(ProtocolListTyHolder),
3629 MethodDescriptionListPtrTy,
3630 MethodDescriptionListPtrTy,
3631 NULL);
3632 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
3633
3634 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
3635 CGM.getModule().addTypeName("struct._objc_protocol_list",
3636 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003637 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003638 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
3639
3640 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003641 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003642 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003643
3644 // Class description structures
3645
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003646 // struct _objc_ivar {
3647 // char *ivar_name;
3648 // char *ivar_type;
3649 // int ivar_offset;
3650 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003651 IvarTy = llvm::StructType::get(Int8PtrTy,
3652 Int8PtrTy,
3653 IntTy,
3654 NULL);
3655 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
3656
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003657 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003658 IvarListTy = llvm::OpaqueType::get();
3659 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
3660 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
3661
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003662 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003663 MethodListTy = llvm::OpaqueType::get();
3664 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
3665 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
3666
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003667 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003668 ClassExtensionTy =
3669 llvm::StructType::get(IntTy,
3670 Int8PtrTy,
3671 PropertyListPtrTy,
3672 NULL);
3673 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
3674 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
3675
3676 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3677
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003678 // struct _objc_class {
3679 // Class isa;
3680 // Class super_class;
3681 // char *name;
3682 // long version;
3683 // long info;
3684 // long instance_size;
3685 // struct _objc_ivar_list *ivars;
3686 // struct _objc_method_list *methods;
3687 // struct _objc_cache *cache;
3688 // struct _objc_protocol_list *protocols;
3689 // char *ivar_layout;
3690 // struct _objc_class_ext *ext;
3691 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003692 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3693 llvm::PointerType::getUnqual(ClassTyHolder),
3694 Int8PtrTy,
3695 LongTy,
3696 LongTy,
3697 LongTy,
3698 IvarListPtrTy,
3699 MethodListPtrTy,
3700 CachePtrTy,
3701 ProtocolListPtrTy,
3702 Int8PtrTy,
3703 ClassExtensionPtrTy,
3704 NULL);
3705 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
3706
3707 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
3708 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
3709 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
3710
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003711 // struct _objc_category {
3712 // char *category_name;
3713 // char *class_name;
3714 // struct _objc_method_list *instance_method;
3715 // struct _objc_method_list *class_method;
3716 // uint32_t size; // sizeof(struct _objc_category)
3717 // struct _objc_property_list *instance_properties;// category's @property
3718 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003719 CategoryTy = llvm::StructType::get(Int8PtrTy,
3720 Int8PtrTy,
3721 MethodListPtrTy,
3722 MethodListPtrTy,
3723 ProtocolListPtrTy,
3724 IntTy,
3725 PropertyListPtrTy,
3726 NULL);
3727 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
3728
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003729 // Global metadata structures
3730
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003731 // struct _objc_symtab {
3732 // long sel_ref_cnt;
3733 // SEL *refs;
3734 // short cls_def_cnt;
3735 // short cat_def_cnt;
3736 // char *defs[cls_def_cnt + cat_def_cnt];
3737 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003738 SymtabTy = llvm::StructType::get(LongTy,
3739 SelectorPtrTy,
3740 ShortTy,
3741 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003742 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003743 NULL);
3744 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
3745 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
3746
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003747 // struct _objc_module {
3748 // long version;
3749 // long size; // sizeof(struct _objc_module)
3750 // char *name;
3751 // struct _objc_symtab* symtab;
3752 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003753 ModuleTy =
3754 llvm::StructType::get(LongTy,
3755 LongTy,
3756 Int8PtrTy,
3757 SymtabPtrTy,
3758 NULL);
3759 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003760
Anders Carlsson2abd89c2008-08-31 04:05:03 +00003761
Anders Carlsson124526b2008-09-09 10:10:21 +00003762 // FIXME: This is the size of the setjmp buffer and should be
3763 // target specific. 18 is what's used on 32-bit X86.
3764 uint64_t SetJmpBufferSize = 18;
3765
3766 // Exceptions
3767 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00003768 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00003769
3770 ExceptionDataTy =
3771 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
3772 SetJmpBufferSize),
3773 StackPtrTy, NULL);
3774 CGM.getModule().addTypeName("struct._objc_exception_data",
3775 ExceptionDataTy);
3776
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003777}
3778
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003779ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003780: ObjCCommonTypesHelper(cgm)
3781{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003782 // struct _method_list_t {
3783 // uint32_t entsize; // sizeof(struct _objc_method)
3784 // uint32_t method_count;
3785 // struct _objc_method method_list[method_count];
3786 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003787 MethodListnfABITy = llvm::StructType::get(IntTy,
3788 IntTy,
3789 llvm::ArrayType::get(MethodTy, 0),
3790 NULL);
3791 CGM.getModule().addTypeName("struct.__method_list_t",
3792 MethodListnfABITy);
3793 // struct method_list_t *
3794 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003795
3796 // struct _protocol_t {
3797 // id isa; // NULL
3798 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003799 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003800 // const struct method_list_t * const instance_methods;
3801 // const struct method_list_t * const class_methods;
3802 // const struct method_list_t *optionalInstanceMethods;
3803 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003804 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003805 // const uint32_t size; // sizeof(struct _protocol_t)
3806 // const uint32_t flags; // = 0
3807 // }
3808
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003809 // Holder for struct _protocol_list_t *
3810 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3811
3812 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3813 Int8PtrTy,
3814 llvm::PointerType::getUnqual(
3815 ProtocolListTyHolder),
3816 MethodListnfABIPtrTy,
3817 MethodListnfABIPtrTy,
3818 MethodListnfABIPtrTy,
3819 MethodListnfABIPtrTy,
3820 PropertyListPtrTy,
3821 IntTy,
3822 IntTy,
3823 NULL);
3824 CGM.getModule().addTypeName("struct._protocol_t",
3825 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003826
3827 // struct _protocol_t*
3828 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003829
Fariborz Jahanianda320092009-01-29 19:24:30 +00003830 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003831 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00003832 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003833 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003834 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3835 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003836 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003837 NULL);
3838 CGM.getModule().addTypeName("struct._objc_protocol_list",
3839 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003840 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3841 ProtocolListnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003842
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003843 // struct _objc_protocol_list*
3844 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003845
3846 // struct _ivar_t {
3847 // unsigned long int *offset; // pointer to ivar offset location
3848 // char *name;
3849 // char *type;
3850 // uint32_t alignment;
3851 // uint32_t size;
3852 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003853 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3854 Int8PtrTy,
3855 Int8PtrTy,
3856 IntTy,
3857 IntTy,
3858 NULL);
3859 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3860
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003861 // struct _ivar_list_t {
3862 // uint32 entsize; // sizeof(struct _ivar_t)
3863 // uint32 count;
3864 // struct _iver_t list[count];
3865 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003866 IvarListnfABITy = llvm::StructType::get(IntTy,
3867 IntTy,
3868 llvm::ArrayType::get(
3869 IvarnfABITy, 0),
3870 NULL);
3871 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3872
3873 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003874
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003875 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003876 // uint32_t const flags;
3877 // uint32_t const instanceStart;
3878 // uint32_t const instanceSize;
3879 // uint32_t const reserved; // only when building for 64bit targets
3880 // const uint8_t * const ivarLayout;
3881 // const char *const name;
3882 // const struct _method_list_t * const baseMethods;
3883 // const struct _objc_protocol_list *const baseProtocols;
3884 // const struct _ivar_list_t *const ivars;
3885 // const uint8_t * const weakIvarLayout;
3886 // const struct _prop_list_t * const properties;
3887 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003888
3889 // FIXME. Add 'reserved' field in 64bit abi mode!
3890 ClassRonfABITy = llvm::StructType::get(IntTy,
3891 IntTy,
3892 IntTy,
3893 Int8PtrTy,
3894 Int8PtrTy,
3895 MethodListnfABIPtrTy,
3896 ProtocolListnfABIPtrTy,
3897 IvarListnfABIPtrTy,
3898 Int8PtrTy,
3899 PropertyListPtrTy,
3900 NULL);
3901 CGM.getModule().addTypeName("struct._class_ro_t",
3902 ClassRonfABITy);
3903
3904 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3905 std::vector<const llvm::Type*> Params;
3906 Params.push_back(ObjectPtrTy);
3907 Params.push_back(SelectorPtrTy);
3908 ImpnfABITy = llvm::PointerType::getUnqual(
3909 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3910
3911 // struct _class_t {
3912 // struct _class_t *isa;
3913 // struct _class_t * const superclass;
3914 // void *cache;
3915 // IMP *vtable;
3916 // struct class_ro_t *ro;
3917 // }
3918
3919 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3920 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3921 llvm::PointerType::getUnqual(ClassTyHolder),
3922 CachePtrTy,
3923 llvm::PointerType::getUnqual(ImpnfABITy),
3924 llvm::PointerType::getUnqual(
3925 ClassRonfABITy),
3926 NULL);
3927 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3928
3929 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3930 ClassnfABITy);
3931
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003932 // LLVM for struct _class_t *
3933 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3934
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003935 // struct _category_t {
3936 // const char * const name;
3937 // struct _class_t *const cls;
3938 // const struct _method_list_t * const instance_methods;
3939 // const struct _method_list_t * const class_methods;
3940 // const struct _protocol_list_t * const protocols;
3941 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003942 // }
3943 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003944 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003945 MethodListnfABIPtrTy,
3946 MethodListnfABIPtrTy,
3947 ProtocolListnfABIPtrTy,
3948 PropertyListPtrTy,
3949 NULL);
3950 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003951
3952 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003953 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3954 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003955
3956 // MessageRefTy - LLVM for:
3957 // struct _message_ref_t {
3958 // IMP messenger;
3959 // SEL name;
3960 // };
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003961
3962 // First the clang type for struct _message_ref_t
3963 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3964 SourceLocation(),
3965 &Ctx.Idents.get("_message_ref_t"));
Douglas Gregor6ab35242009-04-09 21:40:53 +00003966 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3967 Ctx.VoidPtrTy, 0, false));
3968 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3969 Ctx.getObjCSelType(), 0, false));
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003970 RD->completeDefinition(Ctx);
3971
3972 MessageRefCTy = Ctx.getTagDeclType(RD);
3973 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
3974 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003975
3976 // MessageRefPtrTy - LLVM for struct _message_ref_t*
3977 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
3978
3979 // SuperMessageRefTy - LLVM for:
3980 // struct _super_message_ref_t {
3981 // SUPER_IMP messenger;
3982 // SEL name;
3983 // };
3984 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy,
3985 SelectorPtrTy,
3986 NULL);
3987 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
3988
3989 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
3990 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
3991
Daniel Dunbare588b992009-03-01 04:46:24 +00003992
3993 // struct objc_typeinfo {
3994 // const void** vtable; // objc_ehtype_vtable + 2
3995 // const char* name; // c++ typeinfo string
3996 // Class cls;
3997 // };
3998 EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy),
3999 Int8PtrTy,
4000 ClassnfABIPtrTy,
4001 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004002 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Daniel Dunbare588b992009-03-01 04:46:24 +00004003 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004004}
4005
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004006llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
4007 FinishNonFragileABIModule();
4008
4009 return NULL;
4010}
4011
4012void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4013 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004014
4015 // Build list of all implemented classe addresses in array
4016 // L_OBJC_LABEL_CLASS_$.
4017 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
4018 // list of 'nonlazy' implementations (defined as those with a +load{}
4019 // method!!).
4020 unsigned NumClasses = DefinedClasses.size();
4021 if (NumClasses) {
4022 std::vector<llvm::Constant*> Symbols(NumClasses);
4023 for (unsigned i=0; i<NumClasses; i++)
4024 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
4025 ObjCTypes.Int8PtrTy);
4026 llvm::Constant* Init =
4027 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
4028 NumClasses),
4029 Symbols);
4030
4031 llvm::GlobalVariable *GV =
4032 new llvm::GlobalVariable(Init->getType(), false,
4033 llvm::GlobalValue::InternalLinkage,
4034 Init,
4035 "\01L_OBJC_LABEL_CLASS_$",
4036 &CGM.getModule());
Daniel Dunbar58a29122009-03-09 22:18:41 +00004037 GV->setAlignment(8);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004038 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
4039 UsedGlobals.push_back(GV);
4040 }
4041
4042 // Build list of all implemented category addresses in array
4043 // L_OBJC_LABEL_CATEGORY_$.
4044 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
4045 // list of 'nonlazy' category implementations (defined as those with a +load{}
4046 // method!!).
4047 unsigned NumCategory = DefinedCategories.size();
4048 if (NumCategory) {
4049 std::vector<llvm::Constant*> Symbols(NumCategory);
4050 for (unsigned i=0; i<NumCategory; i++)
4051 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
4052 ObjCTypes.Int8PtrTy);
4053 llvm::Constant* Init =
4054 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
4055 NumCategory),
4056 Symbols);
4057
4058 llvm::GlobalVariable *GV =
4059 new llvm::GlobalVariable(Init->getType(), false,
4060 llvm::GlobalValue::InternalLinkage,
4061 Init,
4062 "\01L_OBJC_LABEL_CATEGORY_$",
4063 &CGM.getModule());
Daniel Dunbar58a29122009-03-09 22:18:41 +00004064 GV->setAlignment(8);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004065 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
4066 UsedGlobals.push_back(GV);
4067 }
4068
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004069 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
4070 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
4071 std::vector<llvm::Constant*> Values(2);
4072 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
Fariborz Jahanian067986e2009-02-24 21:08:09 +00004073 unsigned int flags = 0;
Fariborz Jahanian66a5c2c2009-02-24 23:34:44 +00004074 // FIXME: Fix and continue?
4075 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
4076 flags |= eImageInfo_GarbageCollected;
4077 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
4078 flags |= eImageInfo_GCOnly;
Fariborz Jahanian067986e2009-02-24 21:08:09 +00004079 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004080 llvm::Constant* Init = llvm::ConstantArray::get(
4081 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
4082 Values);
4083 llvm::GlobalVariable *IMGV =
4084 new llvm::GlobalVariable(Init->getType(), false,
4085 llvm::GlobalValue::InternalLinkage,
4086 Init,
4087 "\01L_OBJC_IMAGE_INFO",
4088 &CGM.getModule());
4089 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
Daniel Dunbar325f7582009-04-23 08:03:21 +00004090 IMGV->setConstant(true);
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004091 UsedGlobals.push_back(IMGV);
4092
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004093 std::vector<llvm::Constant*> Used;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004094
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004095 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
4096 e = UsedGlobals.end(); i != e; ++i) {
4097 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
4098 }
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004099
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004100 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
4101 llvm::GlobalValue *GV =
4102 new llvm::GlobalVariable(AT, false,
4103 llvm::GlobalValue::AppendingLinkage,
4104 llvm::ConstantArray::get(AT, Used),
4105 "llvm.used",
4106 &CGM.getModule());
4107
4108 GV->setSection("llvm.metadata");
4109
4110}
4111
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004112// Metadata flags
4113enum MetaDataDlags {
4114 CLS = 0x0,
4115 CLS_META = 0x1,
4116 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004117 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004118 CLS_EXCEPTION = 0x20
4119};
4120/// BuildClassRoTInitializer - generate meta-data for:
4121/// struct _class_ro_t {
4122/// uint32_t const flags;
4123/// uint32_t const instanceStart;
4124/// uint32_t const instanceSize;
4125/// uint32_t const reserved; // only when building for 64bit targets
4126/// const uint8_t * const ivarLayout;
4127/// const char *const name;
4128/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004129/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004130/// const struct _ivar_list_t *const ivars;
4131/// const uint8_t * const weakIvarLayout;
4132/// const struct _prop_list_t * const properties;
4133/// }
4134///
4135llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
4136 unsigned flags,
4137 unsigned InstanceStart,
4138 unsigned InstanceSize,
4139 const ObjCImplementationDecl *ID) {
4140 std::string ClassName = ID->getNameAsString();
4141 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
4142 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4143 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4144 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
4145 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004146 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4147 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004148 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004149 // const struct _method_list_t * const baseMethods;
4150 std::vector<llvm::Constant*> Methods;
4151 std::string MethodListName("\01l_OBJC_$_");
4152 if (flags & CLS_META) {
4153 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004154 for (ObjCImplementationDecl::classmeth_iterator
4155 i = ID->classmeth_begin(CGM.getContext()),
4156 e = ID->classmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004157 // Class methods should always be defined.
4158 Methods.push_back(GetMethodConstant(*i));
4159 }
4160 } else {
4161 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004162 for (ObjCImplementationDecl::instmeth_iterator
4163 i = ID->instmeth_begin(CGM.getContext()),
4164 e = ID->instmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004165 // Instance methods should always be defined.
4166 Methods.push_back(GetMethodConstant(*i));
4167 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00004168 for (ObjCImplementationDecl::propimpl_iterator
4169 i = ID->propimpl_begin(CGM.getContext()),
4170 e = ID->propimpl_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004171 ObjCPropertyImplDecl *PID = *i;
4172
4173 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4174 ObjCPropertyDecl *PD = PID->getPropertyDecl();
4175
4176 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4177 if (llvm::Constant *C = GetMethodConstant(MD))
4178 Methods.push_back(C);
4179 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4180 if (llvm::Constant *C = GetMethodConstant(MD))
4181 Methods.push_back(C);
4182 }
4183 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004184 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004185 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004186 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004187
4188 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4189 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
4190 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
4191 + OID->getNameAsString(),
4192 OID->protocol_begin(),
4193 OID->protocol_end());
4194
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004195 if (flags & CLS_META)
4196 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4197 else
4198 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004199 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4200 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004201 if (flags & CLS_META)
4202 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
4203 else
4204 Values[ 9] =
4205 EmitPropertyList(
4206 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
4207 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004208 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
4209 Values);
4210 llvm::GlobalVariable *CLASS_RO_GV =
4211 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
4212 llvm::GlobalValue::InternalLinkage,
4213 Init,
4214 (flags & CLS_META) ?
4215 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4216 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
4217 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004218 CLASS_RO_GV->setAlignment(
4219 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004220 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004221 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004222
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004223}
4224
4225/// BuildClassMetaData - This routine defines that to-level meta-data
4226/// for the given ClassName for:
4227/// struct _class_t {
4228/// struct _class_t *isa;
4229/// struct _class_t * const superclass;
4230/// void *cache;
4231/// IMP *vtable;
4232/// struct class_ro_t *ro;
4233/// }
4234///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004235llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
4236 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004237 llvm::Constant *IsAGV,
4238 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004239 llvm::Constant *ClassRoGV,
4240 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004241 std::vector<llvm::Constant*> Values(5);
4242 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004243 Values[1] = SuperClassGV
4244 ? SuperClassGV
4245 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004246 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4247 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4248 Values[4] = ClassRoGV; // &CLASS_RO_GV
4249 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
4250 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004251 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4252 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004253 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004254 GV->setAlignment(
4255 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004256 if (HiddenVisibility)
4257 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004258 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004259}
4260
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004261void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCInterfaceDecl *OID,
4262 uint32_t &InstanceStart,
4263 uint32_t &InstanceSize) {
Daniel Dunbar97776872009-04-22 07:32:20 +00004264 // Find first and last (non-padding) ivars in this interface.
4265
4266 // FIXME: Use iterator.
4267 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
4268 GetNamedIvarList(OID, OIvars);
4269
4270 if (OIvars.empty()) {
4271 InstanceStart = InstanceSize = 0;
4272 return;
Daniel Dunbard4ae6c02009-04-22 04:39:47 +00004273 }
Daniel Dunbar97776872009-04-22 07:32:20 +00004274
4275 const ObjCIvarDecl *First = OIvars.front();
4276 const ObjCIvarDecl *Last = OIvars.back();
4277
4278 InstanceStart = ComputeIvarBaseOffset(CGM, OID, First);
4279 const llvm::Type *FieldTy =
4280 CGM.getTypes().ConvertTypeForMem(Last->getType());
4281 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004282// FIXME. This breaks compatibility with llvm-gcc-4.2 (but makes it compatible
4283// with gcc-4.2). We postpone this for now.
4284#if 0
4285 if (Last->isBitField()) {
4286 Expr *BitWidth = Last->getBitWidth();
4287 uint64_t BitFieldSize =
Eli Friedman9a901bb2009-04-26 19:19:15 +00004288 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004289 Size = (BitFieldSize / 8) + ((BitFieldSize % 8) != 0);
4290 }
4291#endif
Daniel Dunbar97776872009-04-22 07:32:20 +00004292 InstanceSize = ComputeIvarBaseOffset(CGM, OID, Last) + Size;
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004293}
4294
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004295void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4296 std::string ClassName = ID->getNameAsString();
4297 if (!ObjCEmptyCacheVar) {
4298 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004299 ObjCTypes.CacheTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004300 false,
4301 llvm::GlobalValue::ExternalLinkage,
4302 0,
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004303 "_objc_empty_cache",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004304 &CGM.getModule());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004305
4306 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004307 ObjCTypes.ImpnfABITy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004308 false,
4309 llvm::GlobalValue::ExternalLinkage,
4310 0,
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004311 "_objc_empty_vtable",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004312 &CGM.getModule());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004313 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004314 assert(ID->getClassInterface() &&
4315 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004316 // FIXME: Is this correct (that meta class size is never computed)?
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004317 uint32_t InstanceStart =
4318 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
4319 uint32_t InstanceSize = InstanceStart;
4320 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004321 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4322 std::string ObjCClassName(getClassSymbolPrefix());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004323
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004324 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004325
Daniel Dunbar04d40782009-04-14 06:00:08 +00004326 bool classIsHidden =
4327 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004328 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004329 flags |= OBJC2_CLS_HIDDEN;
4330 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004331 // class is root
4332 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004333 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004334 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004335 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004336 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004337 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4338 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4339 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004340 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004341 // work on super class metadata symbol.
4342 std::string SuperClassName =
4343 ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004344 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004345 }
4346 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4347 InstanceStart,
4348 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004349 std::string TClassName = ObjCMetaClassName + ClassName;
4350 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004351 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4352 classIsHidden);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004353
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004354 // Metadata for the class
4355 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004356 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004357 flags |= OBJC2_CLS_HIDDEN;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004358
4359 if (hasObjCExceptionAttribute(ID->getClassInterface()))
4360 flags |= CLS_EXCEPTION;
4361
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004362 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004363 flags |= CLS_ROOT;
4364 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00004365 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004366 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004367 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004368 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004369 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004370 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004371 GetClassSizeInfo(ID->getClassInterface(), InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004372 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004373 InstanceStart,
4374 InstanceSize,
4375 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004376
4377 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004378 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004379 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4380 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004381 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004382
4383 // Force the definition of the EHType if necessary.
4384 if (flags & CLS_EXCEPTION)
4385 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004386}
4387
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004388/// GenerateProtocolRef - This routine is called to generate code for
4389/// a protocol reference expression; as in:
4390/// @code
4391/// @protocol(Proto1);
4392/// @endcode
4393/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4394/// which will hold address of the protocol meta-data.
4395///
4396llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
4397 const ObjCProtocolDecl *PD) {
4398
Fariborz Jahanian960cd062009-04-10 18:47:34 +00004399 // This routine is called for @protocol only. So, we must build definition
4400 // of protocol's meta-data (not a reference to it!)
4401 //
4402 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004403 ObjCTypes.ExternalProtocolPtrTy);
4404
4405 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
4406 ProtocolName += PD->getNameAsCString();
4407
4408 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
4409 if (PTGV)
4410 return Builder.CreateLoad(PTGV, false, "tmp");
4411 PTGV = new llvm::GlobalVariable(
4412 Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00004413 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004414 Init,
4415 ProtocolName,
4416 &CGM.getModule());
4417 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
4418 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4419 UsedGlobals.push_back(PTGV);
4420 return Builder.CreateLoad(PTGV, false, "tmp");
4421}
4422
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004423/// GenerateCategory - Build metadata for a category implementation.
4424/// struct _category_t {
4425/// const char * const name;
4426/// struct _class_t *const cls;
4427/// const struct _method_list_t * const instance_methods;
4428/// const struct _method_list_t * const class_methods;
4429/// const struct _protocol_list_t * const protocols;
4430/// const struct _prop_list_t * const properties;
4431/// }
4432///
4433void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
4434{
4435 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004436 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
4437 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004438 "_$_" + OCD->getNameAsString());
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004439 std::string ExtClassName(getClassSymbolPrefix() +
4440 Interface->getNameAsString());
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004441
4442 std::vector<llvm::Constant*> Values(6);
4443 Values[0] = GetClassName(OCD->getIdentifier());
4444 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004445 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004446 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004447 std::vector<llvm::Constant*> Methods;
4448 std::string MethodListName(Prefix);
4449 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
4450 "_$_" + OCD->getNameAsString();
4451
Douglas Gregor653f1b12009-04-23 01:02:12 +00004452 for (ObjCCategoryImplDecl::instmeth_iterator
4453 i = OCD->instmeth_begin(CGM.getContext()),
4454 e = OCD->instmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004455 // Instance methods should always be defined.
4456 Methods.push_back(GetMethodConstant(*i));
4457 }
4458
4459 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004460 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004461 Methods);
4462
4463 MethodListName = Prefix;
4464 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
4465 OCD->getNameAsString();
4466 Methods.clear();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004467 for (ObjCCategoryImplDecl::classmeth_iterator
4468 i = OCD->classmeth_begin(CGM.getContext()),
4469 e = OCD->classmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004470 // Class methods should always be defined.
4471 Methods.push_back(GetMethodConstant(*i));
4472 }
4473
4474 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004475 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004476 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004477 const ObjCCategoryDecl *Category =
4478 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004479 if (Category) {
4480 std::string ExtName(Interface->getNameAsString() + "_$_" +
4481 OCD->getNameAsString());
4482 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
4483 + Interface->getNameAsString() + "_$_"
4484 + Category->getNameAsString(),
4485 Category->protocol_begin(),
4486 Category->protocol_end());
4487 Values[5] =
4488 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
4489 OCD, Category, ObjCTypes);
4490 }
4491 else {
4492 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4493 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
4494 }
4495
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004496 llvm::Constant *Init =
4497 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
4498 Values);
4499 llvm::GlobalVariable *GCATV
4500 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
4501 false,
4502 llvm::GlobalValue::InternalLinkage,
4503 Init,
4504 ExtCatName,
4505 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004506 GCATV->setAlignment(
4507 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004508 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004509 UsedGlobals.push_back(GCATV);
4510 DefinedCategories.push_back(GCATV);
4511}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004512
4513/// GetMethodConstant - Return a struct objc_method constant for the
4514/// given method if it has been defined. The result is null if the
4515/// method has not been defined. The return value has type MethodPtrTy.
4516llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
4517 const ObjCMethodDecl *MD) {
4518 // FIXME: Use DenseMap::lookup
4519 llvm::Function *Fn = MethodDefinitions[MD];
4520 if (!Fn)
4521 return 0;
4522
4523 std::vector<llvm::Constant*> Method(3);
4524 Method[0] =
4525 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4526 ObjCTypes.SelectorPtrTy);
4527 Method[1] = GetMethodVarType(MD);
4528 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
4529 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
4530}
4531
4532/// EmitMethodList - Build meta-data for method declarations
4533/// struct _method_list_t {
4534/// uint32_t entsize; // sizeof(struct _objc_method)
4535/// uint32_t method_count;
4536/// struct _objc_method method_list[method_count];
4537/// }
4538///
4539llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
4540 const std::string &Name,
4541 const char *Section,
4542 const ConstantVector &Methods) {
4543 // Return null for empty list.
4544 if (Methods.empty())
4545 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
4546
4547 std::vector<llvm::Constant*> Values(3);
4548 // sizeof(struct _objc_method)
4549 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
4550 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4551 // method_count
4552 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
4553 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
4554 Methods.size());
4555 Values[2] = llvm::ConstantArray::get(AT, Methods);
4556 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4557
4558 llvm::GlobalVariable *GV =
4559 new llvm::GlobalVariable(Init->getType(), false,
4560 llvm::GlobalValue::InternalLinkage,
4561 Init,
4562 Name,
4563 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004564 GV->setAlignment(
4565 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004566 GV->setSection(Section);
4567 UsedGlobals.push_back(GV);
4568 return llvm::ConstantExpr::getBitCast(GV,
4569 ObjCTypes.MethodListnfABIPtrTy);
4570}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004571
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004572/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
4573/// the given ivar.
4574///
4575llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004576 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004577 const ObjCIvarDecl *Ivar) {
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00004578 std::string Name = "OBJC_IVAR_$_" +
Douglas Gregor6ab35242009-04-09 21:40:53 +00004579 getInterfaceDeclForIvar(ID, Ivar, CGM.getContext())->getNameAsString() +
4580 '.' + Ivar->getNameAsString();
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004581 llvm::GlobalVariable *IvarOffsetGV =
4582 CGM.getModule().getGlobalVariable(Name);
4583 if (!IvarOffsetGV)
4584 IvarOffsetGV =
4585 new llvm::GlobalVariable(ObjCTypes.LongTy,
4586 false,
4587 llvm::GlobalValue::ExternalLinkage,
4588 0,
4589 Name,
4590 &CGM.getModule());
4591 return IvarOffsetGV;
4592}
4593
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004594llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004595 const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004596 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004597 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00004598 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
4599 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
4600 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004601 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004602 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00004603
4604 // FIXME: This matches gcc, but shouldn't the visibility be set on
4605 // the use as well (i.e., in ObjCIvarOffsetVariable).
4606 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
4607 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
4608 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004609 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00004610 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00004611 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004612 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004613 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004614}
4615
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004616/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00004617/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004618/// IvarListnfABIPtrTy.
4619/// struct _ivar_t {
4620/// unsigned long int *offset; // pointer to ivar offset location
4621/// char *name;
4622/// char *type;
4623/// uint32_t alignment;
4624/// uint32_t size;
4625/// }
4626/// struct _ivar_list_t {
4627/// uint32 entsize; // sizeof(struct _ivar_t)
4628/// uint32 count;
4629/// struct _iver_t list[count];
4630/// }
4631///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004632
4633void CGObjCCommonMac::GetNamedIvarList(const ObjCInterfaceDecl *OID,
4634 llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const {
4635 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
4636 E = OID->ivar_end(); I != E; ++I) {
4637 // Ignore unnamed bit-fields.
4638 if (!(*I)->getDeclName())
4639 continue;
4640
4641 Res.push_back(*I);
4642 }
4643
4644 for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(CGM.getContext()),
4645 E = OID->prop_end(CGM.getContext()); I != E; ++I)
4646 if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl())
4647 Res.push_back(IV);
4648}
4649
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004650llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
4651 const ObjCImplementationDecl *ID) {
4652
4653 std::vector<llvm::Constant*> Ivars, Ivar(5);
4654
4655 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4656 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
4657
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004658 // FIXME. Consolidate this with similar code in GenerateClass.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00004659
Daniel Dunbar91636d62009-04-20 00:33:43 +00004660 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00004661 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004662 GetNamedIvarList(OID, OIvars);
Fariborz Jahanian99eee362009-04-01 19:37:34 +00004663
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004664 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
4665 ObjCIvarDecl *IVD = OIvars[i];
Daniel Dunbar3eec8aa2009-04-20 05:53:40 +00004666 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar97776872009-04-22 07:32:20 +00004667 ComputeIvarBaseOffset(CGM, OID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004668 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
4669 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004670 const llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004671 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004672 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
4673 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004674 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004675 Align = llvm::Log2_32(Align);
4676 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00004677 // NOTE. Size of a bitfield does not match gcc's, because of the
4678 // way bitfields are treated special in each. But I am told that
4679 // 'size' for bitfield ivars is ignored by the runtime so it does
4680 // not matter. If it matters, there is enough info to get the
4681 // bitfield right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004682 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4683 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
4684 }
4685 // Return null for empty list.
4686 if (Ivars.empty())
4687 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4688 std::vector<llvm::Constant*> Values(3);
4689 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
4690 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4691 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
4692 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
4693 Ivars.size());
4694 Values[2] = llvm::ConstantArray::get(AT, Ivars);
4695 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4696 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4697 llvm::GlobalVariable *GV =
4698 new llvm::GlobalVariable(Init->getType(), false,
4699 llvm::GlobalValue::InternalLinkage,
4700 Init,
4701 Prefix + OID->getNameAsString(),
4702 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004703 GV->setAlignment(
4704 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004705 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004706
4707 UsedGlobals.push_back(GV);
4708 return llvm::ConstantExpr::getBitCast(GV,
4709 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004710}
4711
4712llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4713 const ObjCProtocolDecl *PD) {
4714 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4715
4716 if (!Entry) {
4717 // We use the initializer as a marker of whether this is a forward
4718 // reference or not. At module finalization we add the empty
4719 // contents for protocols which were referenced but never defined.
4720 Entry =
4721 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
4722 llvm::GlobalValue::ExternalLinkage,
4723 0,
4724 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
4725 &CGM.getModule());
4726 Entry->setSection("__DATA,__datacoal_nt,coalesced");
4727 UsedGlobals.push_back(Entry);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004728 }
4729
4730 return Entry;
4731}
4732
4733/// GetOrEmitProtocol - Generate the protocol meta-data:
4734/// @code
4735/// struct _protocol_t {
4736/// id isa; // NULL
4737/// const char * const protocol_name;
4738/// const struct _protocol_list_t * protocol_list; // super protocols
4739/// const struct method_list_t * const instance_methods;
4740/// const struct method_list_t * const class_methods;
4741/// const struct method_list_t *optionalInstanceMethods;
4742/// const struct method_list_t *optionalClassMethods;
4743/// const struct _prop_list_t * properties;
4744/// const uint32_t size; // sizeof(struct _protocol_t)
4745/// const uint32_t flags; // = 0
4746/// }
4747/// @endcode
4748///
4749
4750llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4751 const ObjCProtocolDecl *PD) {
4752 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4753
4754 // Early exit if a defining object has already been generated.
4755 if (Entry && Entry->hasInitializer())
4756 return Entry;
4757
4758 const char *ProtocolName = PD->getNameAsCString();
4759
4760 // Construct method lists.
4761 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4762 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00004763 for (ObjCProtocolDecl::instmeth_iterator
4764 i = PD->instmeth_begin(CGM.getContext()),
4765 e = PD->instmeth_end(CGM.getContext());
4766 i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004767 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004768 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004769 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4770 OptInstanceMethods.push_back(C);
4771 } else {
4772 InstanceMethods.push_back(C);
4773 }
4774 }
4775
Douglas Gregor6ab35242009-04-09 21:40:53 +00004776 for (ObjCProtocolDecl::classmeth_iterator
4777 i = PD->classmeth_begin(CGM.getContext()),
4778 e = PD->classmeth_end(CGM.getContext());
4779 i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004780 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004781 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004782 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4783 OptClassMethods.push_back(C);
4784 } else {
4785 ClassMethods.push_back(C);
4786 }
4787 }
4788
4789 std::vector<llvm::Constant*> Values(10);
4790 // isa is NULL
4791 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
4792 Values[1] = GetClassName(PD->getIdentifier());
4793 Values[2] = EmitProtocolList(
4794 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4795 PD->protocol_begin(),
4796 PD->protocol_end());
4797
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004798 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004799 + PD->getNameAsString(),
4800 "__DATA, __objc_const",
4801 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004802 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004803 + PD->getNameAsString(),
4804 "__DATA, __objc_const",
4805 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004806 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004807 + PD->getNameAsString(),
4808 "__DATA, __objc_const",
4809 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004810 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004811 + PD->getNameAsString(),
4812 "__DATA, __objc_const",
4813 OptClassMethods);
4814 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4815 0, PD, ObjCTypes);
4816 uint32_t Size =
4817 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
4818 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4819 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
4820 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
4821 Values);
4822
4823 if (Entry) {
4824 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00004825 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004826 Entry->setInitializer(Init);
4827 } else {
4828 Entry =
4829 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004830 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004831 Init,
4832 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
4833 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004834 Entry->setAlignment(
4835 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004836 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004837 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004838 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
4839
4840 // Use this protocol meta-data to build protocol list table in section
4841 // __DATA, __objc_protolist
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004842 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004843 ObjCTypes.ProtocolnfABIPtrTy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004844 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004845 Entry,
4846 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
4847 +ProtocolName,
4848 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004849 PTGV->setAlignment(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004850 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00004851 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004852 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4853 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004854 return Entry;
4855}
4856
4857/// EmitProtocolList - Generate protocol list meta-data:
4858/// @code
4859/// struct _protocol_list_t {
4860/// long protocol_count; // Note, this is 32/64 bit
4861/// struct _protocol_t[protocol_count];
4862/// }
4863/// @endcode
4864///
4865llvm::Constant *
4866CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4867 ObjCProtocolDecl::protocol_iterator begin,
4868 ObjCProtocolDecl::protocol_iterator end) {
4869 std::vector<llvm::Constant*> ProtocolRefs;
4870
Fariborz Jahanianda320092009-01-29 19:24:30 +00004871 // Just return null for empty protocol lists
Daniel Dunbar948e2582009-02-15 07:36:20 +00004872 if (begin == end)
Fariborz Jahanianda320092009-01-29 19:24:30 +00004873 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4874
Daniel Dunbar948e2582009-02-15 07:36:20 +00004875 // FIXME: We shouldn't need to do this lookup here, should we?
Fariborz Jahanianda320092009-01-29 19:24:30 +00004876 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4877 if (GV)
Daniel Dunbar948e2582009-02-15 07:36:20 +00004878 return llvm::ConstantExpr::getBitCast(GV,
4879 ObjCTypes.ProtocolListnfABIPtrTy);
4880
4881 for (; begin != end; ++begin)
4882 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4883
Fariborz Jahanianda320092009-01-29 19:24:30 +00004884 // This list is null terminated.
4885 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004886 ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004887
4888 std::vector<llvm::Constant*> Values(2);
4889 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4890 Values[1] =
Daniel Dunbar948e2582009-02-15 07:36:20 +00004891 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004892 ProtocolRefs.size()),
4893 ProtocolRefs);
4894
4895 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4896 GV = new llvm::GlobalVariable(Init->getType(), false,
4897 llvm::GlobalValue::InternalLinkage,
4898 Init,
4899 Name,
4900 &CGM.getModule());
4901 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004902 GV->setAlignment(
4903 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004904 UsedGlobals.push_back(GV);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004905 return llvm::ConstantExpr::getBitCast(GV,
4906 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004907}
4908
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004909/// GetMethodDescriptionConstant - This routine build following meta-data:
4910/// struct _objc_method {
4911/// SEL _cmd;
4912/// char *method_type;
4913/// char *_imp;
4914/// }
4915
4916llvm::Constant *
4917CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4918 std::vector<llvm::Constant*> Desc(3);
4919 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4920 ObjCTypes.SelectorPtrTy);
4921 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004922 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004923 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4924 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4925}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004926
4927/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4928/// This code gen. amounts to generating code for:
4929/// @code
4930/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4931/// @encode
4932///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004933LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004934 CodeGen::CodeGenFunction &CGF,
4935 QualType ObjectTy,
4936 llvm::Value *BaseValue,
4937 const ObjCIvarDecl *Ivar,
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004938 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00004939 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00004940 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
4941 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004942}
4943
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004944llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
4945 CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00004946 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004947 const ObjCIvarDecl *Ivar) {
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00004948 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),
4949 false, "ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004950}
4951
Fariborz Jahanian46551122009-02-04 00:22:57 +00004952CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4953 CodeGen::CodeGenFunction &CGF,
4954 QualType ResultType,
4955 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004956 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00004957 QualType Arg0Ty,
4958 bool IsSuper,
4959 const CallArgList &CallArgs) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004960 // FIXME. Even though IsSuper is passes. This function doese not
4961 // handle calls to 'super' receivers.
4962 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004963 llvm::Value *Arg0 = Receiver;
4964 if (!IsSuper)
4965 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004966
4967 // Find the message function name.
Fariborz Jahanianef163782009-02-05 01:13:09 +00004968 // FIXME. This is too much work to get the ABI-specific result type
4969 // needed to find the message name.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004970 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4971 llvm::SmallVector<QualType, 16>());
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00004972 llvm::Constant *Fn = 0;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004973 std::string Name("\01l_");
4974 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004975#if 0
4976 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004977 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00004978 Fn = ObjCTypes.getMessageSendIdStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004979 // FIXME. Is there a better way of getting these names.
4980 // They are available in RuntimeFunctions vector pair.
4981 Name += "objc_msgSendId_stret_fixup";
4982 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004983 else
4984#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004985 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00004986 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004987 Name += "objc_msgSendSuper2_stret_fixup";
4988 }
4989 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004990 {
Chris Lattner1c02f862009-04-22 02:53:24 +00004991 Fn = ObjCTypes.getMessageSendStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004992 Name += "objc_msgSend_stret_fixup";
4993 }
4994 }
Fariborz Jahanian5b2bad02009-04-30 16:31:11 +00004995 else if (!IsSuper && ResultType->isFloatingType()) {
4996 if (const BuiltinType *BT = ResultType->getAsBuiltinType()) {
4997 BuiltinType::Kind k = BT->getKind();
4998 if (k == BuiltinType::LongDouble) {
4999 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5000 Name += "objc_msgSend_fpret_fixup";
5001 }
5002 else {
5003 Fn = ObjCTypes.getMessageSendFixupFn();
5004 Name += "objc_msgSend_fixup";
5005 }
5006 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005007 }
5008 else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005009#if 0
5010// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005011 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005012 Fn = ObjCTypes.getMessageSendIdFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005013 Name += "objc_msgSendId_fixup";
5014 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005015 else
5016#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005017 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005018 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005019 Name += "objc_msgSendSuper2_fixup";
5020 }
5021 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005022 {
Chris Lattner1c02f862009-04-22 02:53:24 +00005023 Fn = ObjCTypes.getMessageSendFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005024 Name += "objc_msgSend_fixup";
5025 }
5026 }
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005027 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005028 Name += '_';
5029 std::string SelName(Sel.getAsString());
5030 // Replace all ':' in selector name with '_' ouch!
5031 for(unsigned i = 0; i < SelName.size(); i++)
5032 if (SelName[i] == ':')
5033 SelName[i] = '_';
5034 Name += SelName;
5035 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5036 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005037 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005038 std::vector<llvm::Constant*> Values(2);
5039 Values[0] = Fn;
5040 Values[1] = GetMethodVarName(Sel);
5041 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
5042 GV = new llvm::GlobalVariable(Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005043 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005044 Init,
5045 Name,
5046 &CGM.getModule());
5047 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005048 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005049 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005050 }
5051 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005052
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005053 CallArgList ActualArgs;
5054 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
5055 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
5056 ObjCTypes.MessageRefCPtrTy));
5057 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005058 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
5059 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5060 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00005061 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005062 Callee = CGF.Builder.CreateBitCast(Callee,
5063 llvm::PointerType::getUnqual(FTy));
5064 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005065}
5066
5067/// Generate code for a message send expression in the nonfragile abi.
5068CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
5069 CodeGen::CodeGenFunction &CGF,
5070 QualType ResultType,
5071 Selector Sel,
5072 llvm::Value *Receiver,
5073 bool IsClassMessage,
5074 const CallArgList &CallArgs) {
Fariborz Jahanian46551122009-02-04 00:22:57 +00005075 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005076 Receiver, CGF.getContext().getObjCIdType(),
Fariborz Jahanian46551122009-02-04 00:22:57 +00005077 false, CallArgs);
5078}
5079
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005080llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005081CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005082 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5083
Daniel Dunbardfff2302009-03-02 05:18:14 +00005084 if (!GV) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005085 GV = new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
5086 llvm::GlobalValue::ExternalLinkage,
5087 0, Name, &CGM.getModule());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005088 }
5089
5090 return GV;
5091}
5092
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005093llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005094 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005095 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
5096
5097 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005098 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005099 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005100 Entry =
5101 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5102 llvm::GlobalValue::InternalLinkage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005103 ClassGV,
Daniel Dunbar11394522009-04-18 08:51:00 +00005104 "\01L_OBJC_CLASSLIST_REFERENCES_$_",
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005105 &CGM.getModule());
5106 Entry->setAlignment(
5107 CGM.getTargetData().getPrefTypeAlignment(
5108 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005109 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
5110 UsedGlobals.push_back(Entry);
5111 }
5112
5113 return Builder.CreateLoad(Entry, false, "tmp");
5114}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005115
Daniel Dunbar11394522009-04-18 08:51:00 +00005116llvm::Value *
5117CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
5118 const ObjCInterfaceDecl *ID) {
5119 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
5120
5121 if (!Entry) {
5122 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5123 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5124 Entry =
5125 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5126 llvm::GlobalValue::InternalLinkage,
5127 ClassGV,
5128 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
5129 &CGM.getModule());
5130 Entry->setAlignment(
5131 CGM.getTargetData().getPrefTypeAlignment(
5132 ObjCTypes.ClassnfABIPtrTy));
5133 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005134 UsedGlobals.push_back(Entry);
5135 }
5136
5137 return Builder.CreateLoad(Entry, false, "tmp");
5138}
5139
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005140/// EmitMetaClassRef - Return a Value * of the address of _class_t
5141/// meta-data
5142///
5143llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5144 const ObjCInterfaceDecl *ID) {
5145 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5146 if (Entry)
5147 return Builder.CreateLoad(Entry, false, "tmp");
5148
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005149 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005150 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005151 Entry =
5152 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5153 llvm::GlobalValue::InternalLinkage,
5154 MetaClassGV,
5155 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
5156 &CGM.getModule());
5157 Entry->setAlignment(
5158 CGM.getTargetData().getPrefTypeAlignment(
5159 ObjCTypes.ClassnfABIPtrTy));
5160
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005161 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005162 UsedGlobals.push_back(Entry);
5163
5164 return Builder.CreateLoad(Entry, false, "tmp");
5165}
5166
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005167/// GetClass - Return a reference to the class for the given interface
5168/// decl.
5169llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5170 const ObjCInterfaceDecl *ID) {
5171 return EmitClassRef(Builder, ID);
5172}
5173
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005174/// Generates a message send where the super is the receiver. This is
5175/// a message send to self with special delivery semantics indicating
5176/// which class's method should be called.
5177CodeGen::RValue
5178CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
5179 QualType ResultType,
5180 Selector Sel,
5181 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005182 bool isCategoryImpl,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005183 llvm::Value *Receiver,
5184 bool IsClassMessage,
5185 const CodeGen::CallArgList &CallArgs) {
5186 // ...
5187 // Create and init a super structure; this is a (receiver, class)
5188 // pair we will pass to objc_msgSendSuper.
5189 llvm::Value *ObjCSuper =
5190 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
5191
5192 llvm::Value *ReceiverAsObject =
5193 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5194 CGF.Builder.CreateStore(ReceiverAsObject,
5195 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
5196
5197 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005198 llvm::Value *Target;
5199 if (IsClassMessage) {
5200 if (isCategoryImpl) {
5201 // Message sent to "super' in a class method defined in
5202 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005203 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005204 Target = CGF.Builder.CreateStructGEP(Target, 0);
5205 Target = CGF.Builder.CreateLoad(Target);
5206 }
5207 else
5208 Target = EmitMetaClassRef(CGF.Builder, Class);
5209 }
5210 else
Daniel Dunbar11394522009-04-18 08:51:00 +00005211 Target = EmitSuperClassRef(CGF.Builder, Class);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005212
5213 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
5214 // and ObjCTypes types.
5215 const llvm::Type *ClassTy =
5216 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5217 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5218 CGF.Builder.CreateStore(Target,
5219 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
5220
5221 return EmitMessageSend(CGF, ResultType, Sel,
5222 ObjCSuper, ObjCTypes.SuperPtrCTy,
5223 true, CallArgs);
5224}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005225
5226llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
5227 Selector Sel) {
5228 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
5229
5230 if (!Entry) {
5231 llvm::Constant *Casted =
5232 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5233 ObjCTypes.SelectorPtrTy);
5234 Entry =
5235 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
5236 llvm::GlobalValue::InternalLinkage,
5237 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
5238 &CGM.getModule());
5239 Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip");
5240 UsedGlobals.push_back(Entry);
5241 }
5242
5243 return Builder.CreateLoad(Entry, false, "tmp");
5244}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005245/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
5246/// objc_assign_ivar (id src, id *dst)
5247///
5248void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
5249 llvm::Value *src, llvm::Value *dst)
5250{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005251 const llvm::Type * SrcTy = src->getType();
5252 if (!isa<llvm::PointerType>(SrcTy)) {
5253 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5254 assert(Size <= 8 && "does not support size > 8");
5255 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5256 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005257 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5258 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005259 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5260 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005261 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005262 src, dst, "assignivar");
5263 return;
5264}
5265
5266/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5267/// objc_assign_strongCast (id src, id *dst)
5268///
5269void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
5270 CodeGen::CodeGenFunction &CGF,
5271 llvm::Value *src, llvm::Value *dst)
5272{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005273 const llvm::Type * SrcTy = src->getType();
5274 if (!isa<llvm::PointerType>(SrcTy)) {
5275 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5276 assert(Size <= 8 && "does not support size > 8");
5277 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5278 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005279 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5280 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005281 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5282 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005283 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005284 src, dst, "weakassign");
5285 return;
5286}
5287
5288/// EmitObjCWeakRead - Code gen for loading value of a __weak
5289/// object: objc_read_weak (id *src)
5290///
5291llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
5292 CodeGen::CodeGenFunction &CGF,
5293 llvm::Value *AddrWeakObj)
5294{
Eli Friedman8339b352009-03-07 03:57:15 +00005295 const llvm::Type* DestTy =
5296 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005297 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005298 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005299 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005300 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005301 return read_weak;
5302}
5303
5304/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5305/// objc_assign_weak (id src, id *dst)
5306///
5307void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
5308 llvm::Value *src, llvm::Value *dst)
5309{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005310 const llvm::Type * SrcTy = src->getType();
5311 if (!isa<llvm::PointerType>(SrcTy)) {
5312 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5313 assert(Size <= 8 && "does not support size > 8");
5314 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5315 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005316 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5317 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005318 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5319 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005320 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005321 src, dst, "weakassign");
5322 return;
5323}
5324
5325/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5326/// objc_assign_global (id src, id *dst)
5327///
5328void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
5329 llvm::Value *src, llvm::Value *dst)
5330{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005331 const llvm::Type * SrcTy = src->getType();
5332 if (!isa<llvm::PointerType>(SrcTy)) {
5333 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5334 assert(Size <= 8 && "does not support size > 8");
5335 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5336 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005337 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5338 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005339 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5340 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005341 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005342 src, dst, "globalassign");
5343 return;
5344}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005345
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005346void
5347CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5348 const Stmt &S) {
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005349 bool isTry = isa<ObjCAtTryStmt>(S);
5350 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
5351 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005352 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005353 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005354 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005355 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
5356
5357 // For @synchronized, call objc_sync_enter(sync.expr). The
5358 // evaluation of the expression must occur before we enter the
5359 // @synchronized. We can safely avoid a temp here because jumps into
5360 // @synchronized are illegal & this will dominate uses.
5361 llvm::Value *SyncArg = 0;
5362 if (!isTry) {
5363 SyncArg =
5364 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
5365 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005366 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005367 }
5368
5369 // Push an EH context entry, used for handling rethrows and jumps
5370 // through finally.
5371 CGF.PushCleanupBlock(FinallyBlock);
5372
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005373 CGF.setInvokeDest(TryHandler);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005374
5375 CGF.EmitBlock(TryBlock);
5376 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
5377 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
5378 CGF.EmitBranchThroughCleanup(FinallyEnd);
5379
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005380 // Emit the exception handler.
5381
5382 CGF.EmitBlock(TryHandler);
5383
5384 llvm::Value *llvm_eh_exception =
5385 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
5386 llvm::Value *llvm_eh_selector_i64 =
5387 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64);
5388 llvm::Value *llvm_eh_typeid_for_i64 =
5389 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64);
5390 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5391 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow");
5392
5393 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
5394 SelectorArgs.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005395 SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005396
5397 // Construct the lists of (type, catch body) to handle.
Daniel Dunbarede8de92009-03-06 00:01:21 +00005398 llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005399 bool HasCatchAll = false;
5400 if (isTry) {
5401 if (const ObjCAtCatchStmt* CatchStmt =
5402 cast<ObjCAtTryStmt>(S).getCatchStmts()) {
5403 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005404 const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Steve Naroff7ba138a2009-03-03 19:52:17 +00005405 Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody()));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005406
5407 // catch(...) always matches.
Steve Naroff7ba138a2009-03-03 19:52:17 +00005408 if (!CatchDecl) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005409 // Use i8* null here to signal this is a catch all, not a cleanup.
5410 llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
5411 SelectorArgs.push_back(Null);
5412 HasCatchAll = true;
5413 break;
5414 }
5415
Daniel Dunbarede8de92009-03-06 00:01:21 +00005416 if (CGF.getContext().isObjCIdType(CatchDecl->getType()) ||
5417 CatchDecl->getType()->isObjCQualifiedIdType()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005418 llvm::Value *IDEHType =
5419 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5420 if (!IDEHType)
5421 IDEHType =
5422 new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5423 llvm::GlobalValue::ExternalLinkage,
5424 0, "OBJC_EHTYPE_id", &CGM.getModule());
5425 SelectorArgs.push_back(IDEHType);
5426 HasCatchAll = true;
5427 break;
5428 }
5429
5430 // All other types should be Objective-C interface pointer types.
Daniel Dunbarede8de92009-03-06 00:01:21 +00005431 const PointerType *PT = CatchDecl->getType()->getAsPointerType();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005432 assert(PT && "Invalid @catch type.");
5433 const ObjCInterfaceType *IT =
5434 PT->getPointeeType()->getAsObjCInterfaceType();
5435 assert(IT && "Invalid @catch type.");
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005436 llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005437 SelectorArgs.push_back(EHType);
5438 }
5439 }
5440 }
5441
5442 // We use a cleanup unless there was already a catch all.
5443 if (!HasCatchAll) {
5444 SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
Daniel Dunbarede8de92009-03-06 00:01:21 +00005445 Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005446 }
5447
5448 llvm::Value *Selector =
5449 CGF.Builder.CreateCall(llvm_eh_selector_i64,
5450 SelectorArgs.begin(), SelectorArgs.end(),
5451 "selector");
5452 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005453 const ParmVarDecl *CatchParam = Handlers[i].first;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005454 const Stmt *CatchBody = Handlers[i].second;
5455
5456 llvm::BasicBlock *Next = 0;
5457
5458 // The last handler always matches.
5459 if (i + 1 != e) {
5460 assert(CatchParam && "Only last handler can be a catch all.");
5461
5462 llvm::BasicBlock *Match = CGF.createBasicBlock("match");
5463 Next = CGF.createBasicBlock("catch.next");
5464 llvm::Value *Id =
5465 CGF.Builder.CreateCall(llvm_eh_typeid_for_i64,
5466 CGF.Builder.CreateBitCast(SelectorArgs[i+2],
5467 ObjCTypes.Int8PtrTy));
5468 CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id),
5469 Match, Next);
5470
5471 CGF.EmitBlock(Match);
5472 }
5473
5474 if (CatchBody) {
5475 llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end");
5476 llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler");
5477
5478 // Cleanups must call objc_end_catch.
5479 //
5480 // FIXME: It seems incorrect for objc_begin_catch to be inside
5481 // this context, but this matches gcc.
5482 CGF.PushCleanupBlock(MatchEnd);
5483 CGF.setInvokeDest(MatchHandler);
5484
5485 llvm::Value *ExcObject =
Chris Lattner8a569112009-04-22 02:15:23 +00005486 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005487
5488 // Bind the catch parameter if it exists.
5489 if (CatchParam) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005490 ExcObject =
5491 CGF.Builder.CreateBitCast(ExcObject,
5492 CGF.ConvertType(CatchParam->getType()));
5493 // CatchParam is a ParmVarDecl because of the grammar
5494 // construction used to handle this, but for codegen purposes
5495 // we treat this as a local decl.
5496 CGF.EmitLocalBlockVarDecl(*CatchParam);
5497 CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005498 }
5499
5500 CGF.ObjCEHValueStack.push_back(ExcObject);
5501 CGF.EmitStmt(CatchBody);
5502 CGF.ObjCEHValueStack.pop_back();
5503
5504 CGF.EmitBranchThroughCleanup(FinallyEnd);
5505
5506 CGF.EmitBlock(MatchHandler);
5507
5508 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5509 // We are required to emit this call to satisfy LLVM, even
5510 // though we don't use the result.
5511 llvm::SmallVector<llvm::Value*, 8> Args;
5512 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005513 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005514 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
5515 0));
5516 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5517 CGF.Builder.CreateStore(Exc, RethrowPtr);
5518 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5519
5520 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5521
5522 CGF.EmitBlock(MatchEnd);
5523
5524 // Unfortunately, we also have to generate another EH frame here
5525 // in case this throws.
5526 llvm::BasicBlock *MatchEndHandler =
5527 CGF.createBasicBlock("match.end.handler");
5528 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattner8a569112009-04-22 02:15:23 +00005529 CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005530 Cont, MatchEndHandler,
5531 Args.begin(), Args.begin());
5532
5533 CGF.EmitBlock(Cont);
5534 if (Info.SwitchBlock)
5535 CGF.EmitBlock(Info.SwitchBlock);
5536 if (Info.EndBlock)
5537 CGF.EmitBlock(Info.EndBlock);
5538
5539 CGF.EmitBlock(MatchEndHandler);
5540 Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5541 // We are required to emit this call to satisfy LLVM, even
5542 // though we don't use the result.
5543 Args.clear();
5544 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005545 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005546 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
5547 0));
5548 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5549 CGF.Builder.CreateStore(Exc, RethrowPtr);
5550 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5551
5552 if (Next)
5553 CGF.EmitBlock(Next);
5554 } else {
5555 assert(!Next && "catchup should be last handler.");
5556
5557 CGF.Builder.CreateStore(Exc, RethrowPtr);
5558 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5559 }
5560 }
5561
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005562 // Pop the cleanup entry, the @finally is outside this cleanup
5563 // scope.
5564 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5565 CGF.setInvokeDest(PrevLandingPad);
5566
5567 CGF.EmitBlock(FinallyBlock);
5568
5569 if (isTry) {
5570 if (const ObjCAtFinallyStmt* FinallyStmt =
5571 cast<ObjCAtTryStmt>(S).getFinallyStmt())
5572 CGF.EmitStmt(FinallyStmt->getFinallyBody());
5573 } else {
5574 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
5575 // @synchronized.
Chris Lattnerbbccd612009-04-22 02:38:11 +00005576 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005577 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005578
5579 if (Info.SwitchBlock)
5580 CGF.EmitBlock(Info.SwitchBlock);
5581 if (Info.EndBlock)
5582 CGF.EmitBlock(Info.EndBlock);
5583
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005584 // Branch around the rethrow code.
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005585 CGF.EmitBranch(FinallyEnd);
5586
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005587 CGF.EmitBlock(FinallyRethrow);
Chris Lattner8a569112009-04-22 02:15:23 +00005588 CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005589 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005590 CGF.Builder.CreateUnreachable();
5591
5592 CGF.EmitBlock(FinallyEnd);
5593}
5594
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005595/// EmitThrowStmt - Generate code for a throw statement.
5596void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
5597 const ObjCAtThrowStmt &S) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005598 llvm::Value *Exception;
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005599 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005600 Exception = CGF.EmitScalarExpr(ThrowExpr);
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005601 } else {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005602 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
5603 "Unexpected rethrow outside @catch block.");
5604 Exception = CGF.ObjCEHValueStack.back();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005605 }
5606
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005607 llvm::Value *ExceptionAsObject =
5608 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
5609 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
5610 if (InvokeDest) {
5611 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattnerbbccd612009-04-22 02:38:11 +00005612 CGF.Builder.CreateInvoke(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005613 Cont, InvokeDest,
5614 &ExceptionAsObject, &ExceptionAsObject + 1);
5615 CGF.EmitBlock(Cont);
5616 } else
Chris Lattnerbbccd612009-04-22 02:38:11 +00005617 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005618 CGF.Builder.CreateUnreachable();
5619
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005620 // Clear the insertion point to indicate we are in unreachable code.
5621 CGF.Builder.ClearInsertionPoint();
5622}
Daniel Dunbare588b992009-03-01 04:46:24 +00005623
5624llvm::Value *
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005625CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
5626 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00005627 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00005628
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005629 // If we don't need a definition, return the entry if found or check
5630 // if we use an external reference.
5631 if (!ForDefinition) {
5632 if (Entry)
5633 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00005634
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005635 // If this type (or a super class) has the __objc_exception__
5636 // attribute, emit an external reference.
5637 if (hasObjCExceptionAttribute(ID))
5638 return Entry =
5639 new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5640 llvm::GlobalValue::ExternalLinkage,
5641 0,
5642 (std::string("OBJC_EHTYPE_$_") +
5643 ID->getIdentifier()->getName()),
5644 &CGM.getModule());
5645 }
5646
5647 // Otherwise we need to either make a new entry or fill in the
5648 // initializer.
5649 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005650 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00005651 std::string VTableName = "objc_ehtype_vtable";
5652 llvm::GlobalVariable *VTableGV =
5653 CGM.getModule().getGlobalVariable(VTableName);
5654 if (!VTableGV)
5655 VTableGV = new llvm::GlobalVariable(ObjCTypes.Int8PtrTy, false,
5656 llvm::GlobalValue::ExternalLinkage,
5657 0, VTableName, &CGM.getModule());
5658
5659 llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2);
5660
5661 std::vector<llvm::Constant*> Values(3);
5662 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
5663 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005664 Values[2] = GetClassGlobal(ClassName);
Daniel Dunbare588b992009-03-01 04:46:24 +00005665 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
5666
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005667 if (Entry) {
5668 Entry->setInitializer(Init);
5669 } else {
5670 Entry = new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5671 llvm::GlobalValue::WeakAnyLinkage,
5672 Init,
5673 (std::string("OBJC_EHTYPE_$_") +
5674 ID->getIdentifier()->getName()),
5675 &CGM.getModule());
5676 }
5677
Daniel Dunbar04d40782009-04-14 06:00:08 +00005678 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005679 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005680 Entry->setAlignment(8);
5681
5682 if (ForDefinition) {
5683 Entry->setSection("__DATA,__objc_const");
5684 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
5685 } else {
5686 Entry->setSection("__DATA,__datacoal_nt,coalesced");
5687 }
Daniel Dunbare588b992009-03-01 04:46:24 +00005688
5689 return Entry;
5690}
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005691
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005692/* *** */
5693
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005694CodeGen::CGObjCRuntime *
5695CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00005696 return new CGObjCMac(CGM);
5697}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005698
5699CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005700CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005701 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005702}