blob: 61f5e796f0fbd5b38a5e65cb928a30d2d791fc46 [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 Dunbar97776872009-04-22 07:32:20 +000072uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
73 const ObjCInterfaceDecl *OID,
74 const ObjCIvarDecl *Ivar) {
75 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);
79 QualType T = CGM.getContext().getObjCInterfaceType(Container);
80 const llvm::StructType *STy = 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())
84 return Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
85
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);
94 return (Offset + BFI.Begin) / 8;
95}
96
97LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
98 const ObjCInterfaceDecl *OID,
99 llvm::Value *BaseValue,
100 const ObjCIvarDecl *Ivar,
101 unsigned CVRQualifiers,
102 llvm::Value *Offset) {
Daniel Dunbar412f59b2009-04-22 10:28:39 +0000103 // Force generation of the codegen information for this structure.
104 //
105 // FIXME: Remove once we don't use the bit-field lookup map.
106 (void) GetConcreteClassStruct(CGF.CGM, OID);
107
Daniel Dunbar97776872009-04-22 07:32:20 +0000108 // FIXME: For now, we use an implementation based on just computing
109 // the offset and calculating things directly. For optimization
110 // purposes, it would be cleaner to use a GEP on the proper type
111 // since the structure layout is fixed; however for that we need to
112 // be able to walk the class chain for an Ivar.
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000113 const ObjCInterfaceDecl *Container;
Daniel Dunbar97776872009-04-22 07:32:20 +0000114 const FieldDecl *Field =
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000115 LookupFieldDeclForIvar(CGF.CGM.getContext(), OID, Ivar, Container);
Daniel Dunbar97776872009-04-22 07:32:20 +0000116
117 // (char *) BaseValue
118 llvm::Type *I8Ptr = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
119 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr);
120 // (char*)BaseValue + Offset_symbol
121 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
122 // (type *)((char*)BaseValue + Offset_symbol)
123 const llvm::Type *IvarTy =
124 CGF.CGM.getTypes().ConvertTypeForMem(Ivar->getType());
125 llvm::Type *ptrIvarTy = llvm::PointerType::getUnqual(IvarTy);
126 V = CGF.Builder.CreateBitCast(V, ptrIvarTy);
127
128 if (Ivar->isBitField()) {
Daniel Dunbare38df862009-05-03 07:52:00 +0000129 QualType IvarTy = Ivar->getType();
Daniel Dunbar97776872009-04-22 07:32:20 +0000130 CodeGenTypes::BitFieldInfo bitFieldInfo =
131 CGF.CGM.getTypes().getBitFieldInfo(Field);
132 return LValue::MakeBitfield(V, bitFieldInfo.Begin % 8, bitFieldInfo.Size,
Daniel Dunbare38df862009-05-03 07:52:00 +0000133 IvarTy->isSignedIntegerType(),
134 IvarTy.getCVRQualifiers()|CVRQualifiers);
Daniel Dunbar97776872009-04-22 07:32:20 +0000135 }
136
137 LValue LV = LValue::MakeAddr(V,
138 Ivar->getType().getCVRQualifiers()|CVRQualifiers,
139 CGF.CGM.getContext().getObjCGCAttrKind(Ivar->getType()));
140 LValue::SetObjCIvar(LV, true);
141 return LV;
142}
143
144///
145
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000146namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000147
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000148 typedef std::vector<llvm::Constant*> ConstantVector;
149
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000150 // FIXME: We should find a nicer way to make the labels for
151 // metadata, string concatenation is lame.
152
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000153class ObjCCommonTypesHelper {
154protected:
155 CodeGen::CodeGenModule &CGM;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000156
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000157public:
Fariborz Jahanian0a855d02009-03-23 19:10:40 +0000158 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000159 const llvm::Type *Int8PtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000160
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000161 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
162 const llvm::Type *ObjectPtrTy;
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000163
164 /// PtrObjectPtrTy - LLVM type for id *
165 const llvm::Type *PtrObjectPtrTy;
166
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000167 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000168 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000169 /// ProtocolPtrTy - LLVM type for external protocol handles
170 /// (typeof(Protocol))
171 const llvm::Type *ExternalProtocolPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000172
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000173 // SuperCTy - clang type for struct objc_super.
174 QualType SuperCTy;
175 // SuperPtrCTy - clang type for struct objc_super *.
176 QualType SuperPtrCTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000177
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000178 /// SuperTy - LLVM type for struct objc_super.
179 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000180 /// SuperPtrTy - LLVM type for struct objc_super *.
181 const llvm::Type *SuperPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000182
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000183 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
184 /// in GCC parlance).
185 const llvm::StructType *PropertyTy;
186
187 /// PropertyListTy - LLVM type for struct objc_property_list
188 /// (_prop_list_t in GCC parlance).
189 const llvm::StructType *PropertyListTy;
190 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
191 const llvm::Type *PropertyListPtrTy;
192
193 // MethodTy - LLVM type for struct objc_method.
194 const llvm::StructType *MethodTy;
195
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000196 /// CacheTy - LLVM type for struct objc_cache.
197 const llvm::Type *CacheTy;
198 /// CachePtrTy - LLVM type for struct objc_cache *.
199 const llvm::Type *CachePtrTy;
200
Chris Lattner72db6c32009-04-22 02:44:54 +0000201 llvm::Constant *getGetPropertyFn() {
202 CodeGen::CodeGenTypes &Types = CGM.getTypes();
203 ASTContext &Ctx = CGM.getContext();
204 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
205 llvm::SmallVector<QualType,16> Params;
206 QualType IdType = Ctx.getObjCIdType();
207 QualType SelType = Ctx.getObjCSelType();
208 Params.push_back(IdType);
209 Params.push_back(SelType);
210 Params.push_back(Ctx.LongTy);
211 Params.push_back(Ctx.BoolTy);
212 const llvm::FunctionType *FTy =
213 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
214 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
215 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000216
Chris Lattner72db6c32009-04-22 02:44:54 +0000217 llvm::Constant *getSetPropertyFn() {
218 CodeGen::CodeGenTypes &Types = CGM.getTypes();
219 ASTContext &Ctx = CGM.getContext();
220 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
221 llvm::SmallVector<QualType,16> Params;
222 QualType IdType = Ctx.getObjCIdType();
223 QualType SelType = Ctx.getObjCSelType();
224 Params.push_back(IdType);
225 Params.push_back(SelType);
226 Params.push_back(Ctx.LongTy);
227 Params.push_back(IdType);
228 Params.push_back(Ctx.BoolTy);
229 Params.push_back(Ctx.BoolTy);
230 const llvm::FunctionType *FTy =
231 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
232 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
233 }
234
235 llvm::Constant *getEnumerationMutationFn() {
236 // void objc_enumerationMutation (id)
237 std::vector<const llvm::Type*> Args;
238 Args.push_back(ObjectPtrTy);
239 llvm::FunctionType *FTy =
240 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
241 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
242 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000243
244 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000245 llvm::Constant *getGcReadWeakFn() {
246 // id objc_read_weak (id *)
247 std::vector<const llvm::Type*> Args;
248 Args.push_back(ObjectPtrTy->getPointerTo());
249 llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false);
250 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
251 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000252
253 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000254 llvm::Constant *getGcAssignWeakFn() {
255 // id objc_assign_weak (id, id *)
256 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
257 Args.push_back(ObjectPtrTy->getPointerTo());
258 llvm::FunctionType *FTy =
259 llvm::FunctionType::get(ObjectPtrTy, Args, false);
260 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
261 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000262
263 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000264 llvm::Constant *getGcAssignGlobalFn() {
265 // id objc_assign_global(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_global");
270 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000271
272 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000273 llvm::Constant *getGcAssignIvarFn() {
274 // id objc_assign_ivar(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_ivar");
279 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000280
281 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000282 llvm::Constant *getGcAssignStrongCastFn() {
283 // id objc_assign_global(id, id *)
284 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
285 Args.push_back(ObjectPtrTy->getPointerTo());
286 llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false);
287 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
288 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000289
290 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000291 llvm::Constant *getExceptionThrowFn() {
292 // void objc_exception_throw(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_exception_throw");
297 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000298
Daniel Dunbar1c566672009-02-24 01:43:46 +0000299 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000300 llvm::Constant *getSyncEnterFn() {
301 // void objc_sync_enter (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_enter");
306 }
Daniel Dunbar1c566672009-02-24 01:43:46 +0000307
308 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000309 llvm::Constant *getSyncExitFn() {
310 // void objc_sync_exit (id)
311 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
312 llvm::FunctionType *FTy =
313 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
314 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
315 }
Daniel Dunbar1c566672009-02-24 01:43:46 +0000316
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000317 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
318 ~ObjCCommonTypesHelper(){}
319};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000320
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000321/// ObjCTypesHelper - Helper class that encapsulates lazy
322/// construction of varies types used during ObjC generation.
323class ObjCTypesHelper : public ObjCCommonTypesHelper {
324private:
325
Chris Lattner4176b0c2009-04-22 02:32:31 +0000326 llvm::Constant *getMessageSendFn() {
327 // id objc_msgSend (id, SEL, ...)
328 std::vector<const llvm::Type*> Params;
329 Params.push_back(ObjectPtrTy);
330 Params.push_back(SelectorPtrTy);
331 return
332 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
333 Params, true),
334 "objc_msgSend");
335 }
336
337 llvm::Constant *getMessageSendStretFn() {
338 // id objc_msgSend_stret (id, SEL, ...)
339 std::vector<const llvm::Type*> Params;
340 Params.push_back(ObjectPtrTy);
341 Params.push_back(SelectorPtrTy);
342 return
343 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
344 Params, true),
345 "objc_msgSend_stret");
346
347 }
348
349 llvm::Constant *getMessageSendFpretFn() {
350 // FIXME: This should be long double on x86_64?
351 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
352 std::vector<const llvm::Type*> Params;
353 Params.push_back(ObjectPtrTy);
354 Params.push_back(SelectorPtrTy);
355 return
356 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
357 Params,
358 true),
359 "objc_msgSend_fpret");
360
361 }
362
363 llvm::Constant *getMessageSendSuperFn() {
364 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
365 std::vector<const llvm::Type*> Params;
366 Params.push_back(SuperPtrTy);
367 Params.push_back(SelectorPtrTy);
368 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
369 Params, true),
370 "objc_msgSendSuper");
371 }
372 llvm::Constant *getMessageSendSuperStretFn() {
373 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
374 // SEL op, ...)
375 std::vector<const llvm::Type*> Params;
376 Params.push_back(Int8PtrTy);
377 Params.push_back(SuperPtrTy);
378 Params.push_back(SelectorPtrTy);
379 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
380 Params, true),
381 "objc_msgSendSuper_stret");
382 }
383
384 llvm::Constant *getMessageSendSuperFpretFn() {
385 // There is no objc_msgSendSuper_fpret? How can that work?
386 return getMessageSendSuperFn();
387 }
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000388
389public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000390 /// SymtabTy - LLVM type for struct objc_symtab.
391 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000392 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
393 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000394 /// ModuleTy - LLVM type for struct objc_module.
395 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000396
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000397 /// ProtocolTy - LLVM type for struct objc_protocol.
398 const llvm::StructType *ProtocolTy;
399 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
400 const llvm::Type *ProtocolPtrTy;
401 /// ProtocolExtensionTy - LLVM type for struct
402 /// objc_protocol_extension.
403 const llvm::StructType *ProtocolExtensionTy;
404 /// ProtocolExtensionTy - LLVM type for struct
405 /// objc_protocol_extension *.
406 const llvm::Type *ProtocolExtensionPtrTy;
407 /// MethodDescriptionTy - LLVM type for struct
408 /// objc_method_description.
409 const llvm::StructType *MethodDescriptionTy;
410 /// MethodDescriptionListTy - LLVM type for struct
411 /// objc_method_description_list.
412 const llvm::StructType *MethodDescriptionListTy;
413 /// MethodDescriptionListPtrTy - LLVM type for struct
414 /// objc_method_description_list *.
415 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000416 /// ProtocolListTy - LLVM type for struct objc_property_list.
417 const llvm::Type *ProtocolListTy;
418 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
419 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000420 /// CategoryTy - LLVM type for struct objc_category.
421 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000422 /// ClassTy - LLVM type for struct objc_class.
423 const llvm::StructType *ClassTy;
424 /// ClassPtrTy - LLVM type for struct objc_class *.
425 const llvm::Type *ClassPtrTy;
426 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
427 const llvm::StructType *ClassExtensionTy;
428 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
429 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000430 // IvarTy - LLVM type for struct objc_ivar.
431 const llvm::StructType *IvarTy;
432 /// IvarListTy - LLVM type for struct objc_ivar_list.
433 const llvm::Type *IvarListTy;
434 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
435 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000436 /// MethodListTy - LLVM type for struct objc_method_list.
437 const llvm::Type *MethodListTy;
438 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
439 const llvm::Type *MethodListPtrTy;
Anders Carlsson124526b2008-09-09 10:10:21 +0000440
441 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
442 const llvm::Type *ExceptionDataTy;
443
Anders Carlsson124526b2008-09-09 10:10:21 +0000444 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000445 llvm::Constant *getExceptionTryEnterFn() {
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_enter");
451 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000452
453 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000454 llvm::Constant *getExceptionTryExitFn() {
455 std::vector<const llvm::Type*> Params;
456 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
457 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
458 Params, false),
459 "objc_exception_try_exit");
460 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000461
462 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000463 llvm::Constant *getExceptionExtractFn() {
464 std::vector<const llvm::Type*> Params;
465 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
466 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
467 Params, false),
468 "objc_exception_extract");
469
470 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000471
472 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000473 llvm::Constant *getExceptionMatchFn() {
474 std::vector<const llvm::Type*> Params;
475 Params.push_back(ClassPtrTy);
476 Params.push_back(ObjectPtrTy);
477 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
478 Params, false),
479 "objc_exception_match");
480
481 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000482
483 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000484 llvm::Constant *getSetJmpFn() {
485 std::vector<const llvm::Type*> Params;
486 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
487 return
488 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
489 Params, false),
490 "_setjmp");
491
492 }
Chris Lattner10cac6f2008-11-15 21:26:17 +0000493
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000494public:
495 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000496 ~ObjCTypesHelper() {}
Daniel Dunbar5669e572008-10-17 03:24:53 +0000497
498
Chris Lattner74391b42009-03-22 21:03:39 +0000499 llvm::Constant *getSendFn(bool IsSuper) {
Chris Lattner4176b0c2009-04-22 02:32:31 +0000500 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
Daniel Dunbar5669e572008-10-17 03:24:53 +0000501 }
502
Chris Lattner74391b42009-03-22 21:03:39 +0000503 llvm::Constant *getSendStretFn(bool IsSuper) {
Chris Lattner4176b0c2009-04-22 02:32:31 +0000504 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
Daniel Dunbar5669e572008-10-17 03:24:53 +0000505 }
506
Chris Lattner74391b42009-03-22 21:03:39 +0000507 llvm::Constant *getSendFpretFn(bool IsSuper) {
Chris Lattner4176b0c2009-04-22 02:32:31 +0000508 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
Daniel Dunbar5669e572008-10-17 03:24:53 +0000509 }
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000510};
511
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000512/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000513/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000514class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000515public:
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000516
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000517 // MethodListnfABITy - LLVM for struct _method_list_t
518 const llvm::StructType *MethodListnfABITy;
519
520 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
521 const llvm::Type *MethodListnfABIPtrTy;
522
523 // ProtocolnfABITy = LLVM for struct _protocol_t
524 const llvm::StructType *ProtocolnfABITy;
525
Daniel Dunbar948e2582009-02-15 07:36:20 +0000526 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
527 const llvm::Type *ProtocolnfABIPtrTy;
528
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000529 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
530 const llvm::StructType *ProtocolListnfABITy;
531
532 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
533 const llvm::Type *ProtocolListnfABIPtrTy;
534
535 // ClassnfABITy - LLVM for struct _class_t
536 const llvm::StructType *ClassnfABITy;
537
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000538 // ClassnfABIPtrTy - LLVM for struct _class_t*
539 const llvm::Type *ClassnfABIPtrTy;
540
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000541 // IvarnfABITy - LLVM for struct _ivar_t
542 const llvm::StructType *IvarnfABITy;
543
544 // IvarListnfABITy - LLVM for struct _ivar_list_t
545 const llvm::StructType *IvarListnfABITy;
546
547 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
548 const llvm::Type *IvarListnfABIPtrTy;
549
550 // ClassRonfABITy - LLVM for struct _class_ro_t
551 const llvm::StructType *ClassRonfABITy;
552
553 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
554 const llvm::Type *ImpnfABITy;
555
556 // CategorynfABITy - LLVM for struct _category_t
557 const llvm::StructType *CategorynfABITy;
558
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000559 // New types for nonfragile abi messaging.
560
561 // MessageRefTy - LLVM for:
562 // struct _message_ref_t {
563 // IMP messenger;
564 // SEL name;
565 // };
566 const llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000567 // MessageRefCTy - clang type for struct _message_ref_t
568 QualType MessageRefCTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000569
570 // MessageRefPtrTy - LLVM for struct _message_ref_t*
571 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000572 // MessageRefCPtrTy - clang type for struct _message_ref_t*
573 QualType MessageRefCPtrTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000574
Fariborz Jahanianef163782009-02-05 01:13:09 +0000575 // MessengerTy - Type of the messenger (shown as IMP above)
576 const llvm::FunctionType *MessengerTy;
577
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000578 // SuperMessageRefTy - LLVM for:
579 // struct _super_message_ref_t {
580 // SUPER_IMP messenger;
581 // SEL name;
582 // };
583 const llvm::StructType *SuperMessageRefTy;
584
585 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
586 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000587
Chris Lattner1c02f862009-04-22 02:53:24 +0000588 llvm::Constant *getMessageSendFixupFn() {
589 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
590 std::vector<const llvm::Type*> Params;
591 Params.push_back(ObjectPtrTy);
592 Params.push_back(MessageRefPtrTy);
593 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
594 Params, true),
595 "objc_msgSend_fixup");
596 }
597
598 llvm::Constant *getMessageSendFpretFixupFn() {
599 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
600 std::vector<const llvm::Type*> Params;
601 Params.push_back(ObjectPtrTy);
602 Params.push_back(MessageRefPtrTy);
603 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
604 Params, true),
605 "objc_msgSend_fpret_fixup");
606 }
607
608 llvm::Constant *getMessageSendStretFixupFn() {
609 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
610 std::vector<const llvm::Type*> Params;
611 Params.push_back(ObjectPtrTy);
612 Params.push_back(MessageRefPtrTy);
613 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
614 Params, true),
615 "objc_msgSend_stret_fixup");
616 }
617
618 llvm::Constant *getMessageSendIdFixupFn() {
619 // id objc_msgSendId_fixup(id, struct message_ref_t*, ...)
620 std::vector<const llvm::Type*> Params;
621 Params.push_back(ObjectPtrTy);
622 Params.push_back(MessageRefPtrTy);
623 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
624 Params, true),
625 "objc_msgSendId_fixup");
626 }
627
628 llvm::Constant *getMessageSendIdStretFixupFn() {
629 // id objc_msgSendId_stret_fixup(id, struct message_ref_t*, ...)
630 std::vector<const llvm::Type*> Params;
631 Params.push_back(ObjectPtrTy);
632 Params.push_back(MessageRefPtrTy);
633 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
634 Params, true),
635 "objc_msgSendId_stret_fixup");
636 }
637 llvm::Constant *getMessageSendSuper2FixupFn() {
638 // id objc_msgSendSuper2_fixup (struct objc_super *,
639 // struct _super_message_ref_t*, ...)
640 std::vector<const llvm::Type*> Params;
641 Params.push_back(SuperPtrTy);
642 Params.push_back(SuperMessageRefPtrTy);
643 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
644 Params, true),
645 "objc_msgSendSuper2_fixup");
646 }
647
648 llvm::Constant *getMessageSendSuper2StretFixupFn() {
649 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
650 // struct _super_message_ref_t*, ...)
651 std::vector<const llvm::Type*> Params;
652 Params.push_back(SuperPtrTy);
653 Params.push_back(SuperMessageRefPtrTy);
654 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
655 Params, true),
656 "objc_msgSendSuper2_stret_fixup");
657 }
658
659
660
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000661 /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C
662 /// exception personality function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000663 llvm::Value *getEHPersonalityPtr() {
664 llvm::Constant *Personality =
665 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
666 std::vector<const llvm::Type*>(),
667 true),
668 "__objc_personality_v0");
669 return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy);
670 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000671
Chris Lattner8a569112009-04-22 02:15:23 +0000672 llvm::Constant *getUnwindResumeOrRethrowFn() {
673 std::vector<const llvm::Type*> Params;
674 Params.push_back(Int8PtrTy);
675 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
676 Params, false),
677 "_Unwind_Resume_or_Rethrow");
678 }
679
680 llvm::Constant *getObjCEndCatchFn() {
681 std::vector<const llvm::Type*> Params;
682 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
683 Params, false),
684 "objc_end_catch");
685
686 }
687
688 llvm::Constant *getObjCBeginCatchFn() {
689 std::vector<const llvm::Type*> Params;
690 Params.push_back(Int8PtrTy);
691 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
692 Params, false),
693 "objc_begin_catch");
694 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000695
696 const llvm::StructType *EHTypeTy;
697 const llvm::Type *EHTypePtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000698
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000699 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
700 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000701};
702
703class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000704public:
705 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000706 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000707 public:
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000708 unsigned int ivar_bytepos;
709 unsigned int ivar_size;
710 GC_IVAR() : ivar_bytepos(0), ivar_size(0) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000711
712 // Allow sorting based on byte pos.
713 bool operator<(const GC_IVAR &b) const {
714 return ivar_bytepos < b.ivar_bytepos;
715 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000716 };
717
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000718 class SKIP_SCAN {
719 public:
720 unsigned int skip;
721 unsigned int scan;
722 SKIP_SCAN() : skip(0), scan(0) {}
723 };
724
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000725protected:
726 CodeGen::CodeGenModule &CGM;
727 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000728 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000729
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000730 // gc ivar layout bitmap calculation helper caches.
731 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
732 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000733
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000734 /// LazySymbols - Symbols to generate a lazy reference for. See
735 /// DefinedSymbols and FinishModule().
736 std::set<IdentifierInfo*> LazySymbols;
737
738 /// DefinedSymbols - External symbols which are defined by this
739 /// module. The symbols in this list and LazySymbols are used to add
740 /// special linker symbols which ensure that Objective-C modules are
741 /// linked properly.
742 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000743
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000744 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000745 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000746
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000747 /// MethodVarNames - uniqued method variable names.
748 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000749
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000750 /// MethodVarTypes - uniqued method type signatures. We have to use
751 /// a StringMap here because have no other unique reference.
752 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000753
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000754 /// MethodDefinitions - map of methods which have been defined in
755 /// this translation unit.
756 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000757
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000758 /// PropertyNames - uniqued method variable names.
759 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000760
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000761 /// ClassReferences - uniqued class references.
762 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000763
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000764 /// SelectorReferences - uniqued selector references.
765 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000766
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000767 /// Protocols - Protocols for which an objc_protocol structure has
768 /// been emitted. Forward declarations are handled by creating an
769 /// empty structure whose initializer is filled in when/if defined.
770 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000771
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000772 /// DefinedProtocols - Protocols which have actually been
773 /// defined. We should not need this, see FIXME in GenerateProtocol.
774 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000775
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000776 /// DefinedClasses - List of defined classes.
777 std::vector<llvm::GlobalValue*> DefinedClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000778
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000779 /// DefinedCategories - List of defined categories.
780 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000781
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000782 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000783 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000784 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000785
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000786 /// GetNameForMethod - Return a name for the given method.
787 /// \param[out] NameOut - The return value.
788 void GetNameForMethod(const ObjCMethodDecl *OMD,
789 const ObjCContainerDecl *CD,
790 std::string &NameOut);
791
792 /// GetMethodVarName - Return a unique constant for the given
793 /// selector's name. The return value has type char *.
794 llvm::Constant *GetMethodVarName(Selector Sel);
795 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
796 llvm::Constant *GetMethodVarName(const std::string &Name);
797
798 /// GetMethodVarType - Return a unique constant for the given
799 /// selector's name. The return value has type char *.
800
801 // FIXME: This is a horrible name.
802 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000803 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000804
805 /// GetPropertyName - Return a unique constant for the given
806 /// name. The return value has type char *.
807 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
808
809 // FIXME: This can be dropped once string functions are unified.
810 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
811 const Decl *Container);
812
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000813 /// GetClassName - Return a unique constant for the given selector's
814 /// name. The return value has type char *.
815 llvm::Constant *GetClassName(IdentifierInfo *Ident);
816
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000817 /// BuildIvarLayout - Builds ivar layout bitmap for the class
818 /// implementation for the __strong or __weak case.
819 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000820 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
821 bool ForStrongLayout);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000822
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000823 void BuildAggrIvarLayout(const ObjCInterfaceDecl *OI,
824 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000825 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +0000826 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000827 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000828 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000829
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000830 /// GetIvarLayoutName - Returns a unique constant for the given
831 /// ivar layout bitmap.
832 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
833 const ObjCCommonTypesHelper &ObjCTypes);
834
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000835 /// EmitPropertyList - Emit the given property list. The return
836 /// value has type PropertyListPtrTy.
837 llvm::Constant *EmitPropertyList(const std::string &Name,
838 const Decl *Container,
839 const ObjCContainerDecl *OCD,
840 const ObjCCommonTypesHelper &ObjCTypes);
841
Fariborz Jahanianda320092009-01-29 19:24:30 +0000842 /// GetProtocolRef - Return a reference to the internal protocol
843 /// description, creating an empty one if it has not been
844 /// defined. The return value has type ProtocolPtrTy.
845 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000846
Chris Lattnercd0ee142009-03-31 08:33:16 +0000847 /// GetFieldBaseOffset - return's field byte offset.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000848 uint64_t GetFieldBaseOffset(const ObjCInterfaceDecl *OI,
849 const llvm::StructLayout *Layout,
Chris Lattnercd0ee142009-03-31 08:33:16 +0000850 const FieldDecl *Field);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000851
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000852 /// CreateMetadataVar - Create a global variable with internal
853 /// linkage for use by the Objective-C runtime.
854 ///
855 /// This is a convenience wrapper which not only creates the
856 /// variable, but also sets the section and alignment and adds the
857 /// global to the UsedGlobals list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000858 ///
859 /// \param Name - The variable name.
860 /// \param Init - The variable initializer; this is also used to
861 /// define the type of the variable.
862 /// \param Section - The section the variable should go into, or 0.
863 /// \param Align - The alignment for the variable, or 0.
864 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000865 /// "llvm.used".
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000866 llvm::GlobalVariable *CreateMetadataVar(const std::string &Name,
867 llvm::Constant *Init,
868 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000869 unsigned Align,
870 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000871
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000872 /// GetNamedIvarList - Return the list of ivars in the interface
873 /// itself (not including super classes and not including unnamed
874 /// bitfields).
875 ///
876 /// For the non-fragile ABI, this also includes synthesized property
877 /// ivars.
878 void GetNamedIvarList(const ObjCInterfaceDecl *OID,
879 llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const;
880
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000881public:
882 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
883 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000884
Steve Naroff33fdb732009-03-31 16:53:37 +0000885 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000886
887 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
888 const ObjCContainerDecl *CD=0);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000889
890 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
891
892 /// GetOrEmitProtocol - Get the protocol object for the given
893 /// declaration, emitting it if necessary. The return value has type
894 /// ProtocolPtrTy.
895 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
896
897 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
898 /// object for the given declaration, emitting it if needed. These
899 /// forward references will be filled in with empty bodies if no
900 /// definition is seen. The return value has type ProtocolPtrTy.
901 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000902};
903
904class CGObjCMac : public CGObjCCommonMac {
905private:
906 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000907 /// EmitImageInfo - Emit the image info marker used to encode some module
908 /// level information.
909 void EmitImageInfo();
910
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000911 /// EmitModuleInfo - Another marker encoding module level
912 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000913 void EmitModuleInfo();
914
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000915 /// EmitModuleSymols - Emit module symbols, the list of defined
916 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000917 llvm::Constant *EmitModuleSymbols();
918
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000919 /// FinishModule - Write out global data structures at the end of
920 /// processing a translation unit.
921 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000922
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000923 /// EmitClassExtension - Generate the class extension structure used
924 /// to store the weak ivar layout and properties. The return value
925 /// has type ClassExtensionPtrTy.
926 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
927
928 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
929 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000930 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000931 const ObjCInterfaceDecl *ID);
932
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000933 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000934 QualType ResultType,
935 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000936 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000937 QualType Arg0Ty,
938 bool IsSuper,
939 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000940
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000941 /// EmitIvarList - Emit the ivar list for the given
942 /// implementation. If ForClass is true the list of class ivars
943 /// (i.e. metaclass ivars) is emitted, otherwise the list of
944 /// interface ivars will be emitted. The return value has type
945 /// IvarListPtrTy.
946 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000947 bool ForClass);
948
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000949 /// EmitMetaClass - Emit a forward reference to the class structure
950 /// for the metaclass of the given interface. The return value has
951 /// type ClassPtrTy.
952 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
953
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000954 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000955 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000956 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
957 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000958 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000959 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000960
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000961 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000962
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000963 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000964
965 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000966 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000967 llvm::Constant *EmitMethodList(const std::string &Name,
968 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000969 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000970
971 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000972 /// method declarations.
973 /// - TypeName: The name for the type containing the methods.
974 /// - IsProtocol: True iff these methods are for a protocol.
975 /// - ClassMethds: True iff these are class methods.
976 /// - Required: When true, only "required" methods are
977 /// listed. Similarly, when false only "optional" methods are
978 /// listed. For classes this should always be true.
979 /// - begin, end: The method list to output.
980 ///
981 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000982 llvm::Constant *EmitMethodDescList(const std::string &Name,
983 const char *Section,
984 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000985
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000986 /// GetOrEmitProtocol - Get the protocol object for the given
987 /// declaration, emitting it if necessary. The return value has type
988 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000989 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000990
991 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
992 /// object for the given declaration, emitting it if needed. These
993 /// forward references will be filled in with empty bodies if no
994 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000995 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000996
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000997 /// EmitProtocolExtension - Generate the protocol extension
998 /// structure used to store optional instance and class methods, and
999 /// protocol properties. The return value has type
1000 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001001 llvm::Constant *
1002 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1003 const ConstantVector &OptInstanceMethods,
1004 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001005
1006 /// EmitProtocolList - Generate the list of referenced
1007 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc93372008-08-21 21:57:41 +00001008 llvm::Constant *EmitProtocolList(const std::string &Name,
1009 ObjCProtocolDecl::protocol_iterator begin,
1010 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001011
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001012 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1013 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001014 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001015
Fariborz Jahanianda320092009-01-29 19:24:30 +00001016 public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001017 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001018
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001019 virtual llvm::Function *ModuleInitFunction();
1020
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001021 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001022 QualType ResultType,
1023 Selector Sel,
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 Dunbarc17a4d32008-08-11 02:45:11 +00001027
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001028 virtual CodeGen::RValue
1029 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001030 QualType ResultType,
1031 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001032 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001033 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001034 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001035 bool IsClassMessage,
1036 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001037
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001038 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001039 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001040
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001041 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001042
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001043 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001044
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001045 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001046
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001047 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001048 const ObjCProtocolDecl *PD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00001049
Chris Lattner74391b42009-03-22 21:03:39 +00001050 virtual llvm::Constant *GetPropertyGetFunction();
1051 virtual llvm::Constant *GetPropertySetFunction();
1052 virtual llvm::Constant *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001053
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001054 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1055 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001056 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1057 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001058 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001059 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001060 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1061 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001062 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1063 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001064 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1065 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001066 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1067 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001068
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001069 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1070 QualType ObjectTy,
1071 llvm::Value *BaseValue,
1072 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001073 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001074 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001075 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001076 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001077};
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001078
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001079class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001080private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001081 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001082 llvm::GlobalVariable* ObjCEmptyCacheVar;
1083 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001084
Daniel Dunbar11394522009-04-18 08:51:00 +00001085 /// SuperClassReferences - uniqued super class references.
1086 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
1087
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001088 /// MetaClassReferences - uniqued meta class references.
1089 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001090
1091 /// EHTypeReferences - uniqued class ehtype references.
1092 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001093
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001094 /// FinishNonFragileABIModule - Write out global data structures at the end of
1095 /// processing a translation unit.
1096 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001097
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00001098 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1099 unsigned InstanceStart,
1100 unsigned InstanceSize,
1101 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001102 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
1103 llvm::Constant *IsAGV,
1104 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001105 llvm::Constant *ClassRoGV,
1106 bool HiddenVisibility);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001107
1108 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
1109
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001110 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
1111
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001112 /// EmitMethodList - Emit the method list for the given
1113 /// implementation. The return value has type MethodListnfABITy.
1114 llvm::Constant *EmitMethodList(const std::string &Name,
1115 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001116 const ConstantVector &Methods);
1117 /// EmitIvarList - Emit the ivar list for the given
1118 /// implementation. If ForClass is true the list of class ivars
1119 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1120 /// interface ivars will be emitted. The return value has type
1121 /// IvarListnfABIPtrTy.
1122 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00001123
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001124 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001125 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001126 unsigned long int offset);
1127
Fariborz Jahanianda320092009-01-29 19:24:30 +00001128 /// GetOrEmitProtocol - Get the protocol object for the given
1129 /// declaration, emitting it if necessary. The return value has type
1130 /// ProtocolPtrTy.
1131 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
1132
1133 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1134 /// object for the given declaration, emitting it if needed. These
1135 /// forward references will be filled in with empty bodies if no
1136 /// definition is seen. The return value has type ProtocolPtrTy.
1137 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
1138
1139 /// EmitProtocolList - Generate the list of referenced
1140 /// protocols. The return value has type ProtocolListPtrTy.
1141 llvm::Constant *EmitProtocolList(const std::string &Name,
1142 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001143 ObjCProtocolDecl::protocol_iterator end);
1144
1145 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1146 QualType ResultType,
1147 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00001148 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001149 QualType Arg0Ty,
1150 bool IsSuper,
1151 const CallArgList &CallArgs);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001152
1153 /// GetClassGlobal - Return the global variable for the Objective-C
1154 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001155 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
1156
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001157 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001158 /// for the given class reference.
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001159 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001160 const ObjCInterfaceDecl *ID);
1161
1162 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1163 /// for the given super class reference.
1164 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1165 const ObjCInterfaceDecl *ID);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001166
1167 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1168 /// meta-data
1169 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
1170 const ObjCInterfaceDecl *ID);
1171
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001172 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1173 /// the given ivar.
1174 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001175 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00001176 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001177 const ObjCIvarDecl *Ivar);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001178
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001179 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1180 /// for the given selector.
1181 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbare588b992009-03-01 04:46:24 +00001182
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001183 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001184 /// interface. The return value has type EHTypePtrTy.
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001185 llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
1186 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001187
1188 const char *getMetaclassSymbolPrefix() const {
1189 return "OBJC_METACLASS_$_";
1190 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001191
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001192 const char *getClassSymbolPrefix() const {
1193 return "OBJC_CLASS_$_";
1194 }
1195
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001196 void GetClassSizeInfo(const ObjCInterfaceDecl *OID,
1197 uint32_t &InstanceStart,
1198 uint32_t &InstanceSize);
1199
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001200public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001201 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001202 // FIXME. All stubs for now!
1203 virtual llvm::Function *ModuleInitFunction();
1204
1205 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
1206 QualType ResultType,
1207 Selector Sel,
1208 llvm::Value *Receiver,
1209 bool IsClassMessage,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001210 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001211
1212 virtual CodeGen::RValue
1213 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
1214 QualType ResultType,
1215 Selector Sel,
1216 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001217 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001218 llvm::Value *Receiver,
1219 bool IsClassMessage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001220 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001221
1222 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001223 const ObjCInterfaceDecl *ID);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001224
1225 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001226 { return EmitSelector(Builder, Sel); }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001227
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001228 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001229
1230 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001231 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001232 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001233
Chris Lattner74391b42009-03-22 21:03:39 +00001234 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001235 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001236 }
Chris Lattner74391b42009-03-22 21:03:39 +00001237 virtual llvm::Constant *GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001238 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001239 }
Chris Lattner74391b42009-03-22 21:03:39 +00001240 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001241 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001242 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001243
1244 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00001245 const Stmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001246 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001247 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001248 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001249 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001250 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001251 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001252 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001253 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001254 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001255 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001256 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001257 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001258 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1259 QualType ObjectTy,
1260 llvm::Value *BaseValue,
1261 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001262 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001263 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001264 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001265 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001266};
1267
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001268} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001269
1270/* *** Helper Functions *** */
1271
1272/// getConstantGEP() - Help routine to construct simple GEPs.
1273static llvm::Constant *getConstantGEP(llvm::Constant *C,
1274 unsigned idx0,
1275 unsigned idx1) {
1276 llvm::Value *Idxs[] = {
1277 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
1278 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
1279 };
1280 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
1281}
1282
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001283/// hasObjCExceptionAttribute - Return true if this class or any super
1284/// class has the __objc_exception__ attribute.
1285static bool hasObjCExceptionAttribute(const ObjCInterfaceDecl *OID) {
Daniel Dunbarb11fa0d2009-04-13 21:08:27 +00001286 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001287 return true;
1288 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
1289 return hasObjCExceptionAttribute(Super);
1290 return false;
1291}
1292
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001293/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001294
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001295CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
1296 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001297{
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001298 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001299 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001300}
1301
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001302/// GetClass - Return a reference to the class for the given interface
1303/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001304llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001305 const ObjCInterfaceDecl *ID) {
1306 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001307}
1308
1309/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001310llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001311 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001312}
1313
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001314/// Generate a constant CFString object.
1315/*
1316 struct __builtin_CFString {
1317 const int *isa; // point to __CFConstantStringClassReference
1318 int flags;
1319 const char *str;
1320 long length;
1321 };
1322*/
1323
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001324llvm::Constant *CGObjCCommonMac::GenerateConstantString(
Steve Naroff33fdb732009-03-31 16:53:37 +00001325 const ObjCStringLiteral *SL) {
Steve Naroff8d4141f2009-04-01 13:55:36 +00001326 return CGM.GetAddrOfConstantCFString(SL->getString());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001327}
1328
1329/// Generates a message send where the super is the receiver. This is
1330/// a message send to self with special delivery semantics indicating
1331/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001332CodeGen::RValue
1333CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001334 QualType ResultType,
1335 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001336 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001337 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001338 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001339 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001340 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001341 // Create and init a super structure; this is a (receiver, class)
1342 // pair we will pass to objc_msgSendSuper.
1343 llvm::Value *ObjCSuper =
1344 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
1345 llvm::Value *ReceiverAsObject =
1346 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
1347 CGF.Builder.CreateStore(ReceiverAsObject,
1348 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001349
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001350 // If this is a class message the metaclass is passed as the target.
1351 llvm::Value *Target;
1352 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001353 if (isCategoryImpl) {
1354 // Message sent to 'super' in a class method defined in a category
1355 // implementation requires an odd treatment.
1356 // If we are in a class method, we must retrieve the
1357 // _metaclass_ for the current class, pointed at by
1358 // the class's "isa" pointer. The following assumes that
1359 // isa" is the first ivar in a class (which it must be).
1360 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1361 Target = CGF.Builder.CreateStructGEP(Target, 0);
1362 Target = CGF.Builder.CreateLoad(Target);
1363 }
1364 else {
1365 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1366 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1367 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1368 Target = Super;
1369 }
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001370 } else {
1371 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1372 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001373 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
1374 // and ObjCTypes types.
1375 const llvm::Type *ClassTy =
1376 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001377 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001378 CGF.Builder.CreateStore(Target,
1379 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
1380
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001381 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001382 ObjCSuper, ObjCTypes.SuperPtrCTy,
1383 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001384}
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001385
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001386/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001387CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001388 QualType ResultType,
1389 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001390 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001391 bool IsClassMessage,
1392 const CallArgList &CallArgs) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001393 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001394 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001395 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001396}
1397
1398CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001399 QualType ResultType,
1400 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001401 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001402 QualType Arg0Ty,
1403 bool IsSuper,
1404 const CallArgList &CallArgs) {
1405 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001406 if (!IsSuper)
1407 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001408 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
1409 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
1410 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001411 CGF.getContext().getObjCSelType()));
1412 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001413
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001414 CodeGenTypes &Types = CGM.getTypes();
1415 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
Fariborz Jahanian65257ca2009-04-29 22:47:27 +00001416 // FIXME. vararg flag must be true when this API is used for 64bit code gen.
1417 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001418
1419 llvm::Constant *Fn;
Daniel Dunbar88b53962009-02-02 22:03:45 +00001420 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Daniel Dunbar5669e572008-10-17 03:24:53 +00001421 Fn = ObjCTypes.getSendStretFn(IsSuper);
1422 } else if (ResultType->isFloatingType()) {
1423 // FIXME: Sadly, this is wrong. This actually depends on the
1424 // architecture. This happens to be right for x86-32 though.
1425 Fn = ObjCTypes.getSendFpretFn(IsSuper);
1426 } else {
1427 Fn = ObjCTypes.getSendFn(IsSuper);
1428 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +00001429 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +00001430 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001431}
1432
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001433llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001434 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001435 // FIXME: I don't understand why gcc generates this, or where it is
1436 // resolved. Investigate. Its also wasteful to look this up over and
1437 // over.
1438 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1439
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001440 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
1441 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001442}
1443
Fariborz Jahanianda320092009-01-29 19:24:30 +00001444void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001445 // FIXME: We shouldn't need this, the protocol decl should contain
1446 // enough information to tell us whether this was a declaration or a
1447 // definition.
1448 DefinedProtocols.insert(PD->getIdentifier());
1449
1450 // If we have generated a forward reference to this protocol, emit
1451 // it now. Otherwise do nothing, the protocol objects are lazily
1452 // emitted.
1453 if (Protocols.count(PD->getIdentifier()))
1454 GetOrEmitProtocol(PD);
1455}
1456
Fariborz Jahanianda320092009-01-29 19:24:30 +00001457llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001458 if (DefinedProtocols.count(PD->getIdentifier()))
1459 return GetOrEmitProtocol(PD);
1460 return GetOrEmitProtocolRef(PD);
1461}
1462
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001463/*
1464 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1465 struct _objc_protocol {
1466 struct _objc_protocol_extension *isa;
1467 char *protocol_name;
1468 struct _objc_protocol_list *protocol_list;
1469 struct _objc__method_prototype_list *instance_methods;
1470 struct _objc__method_prototype_list *class_methods
1471 };
1472
1473 See EmitProtocolExtension().
1474*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001475llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1476 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1477
1478 // Early exit if a defining object has already been generated.
1479 if (Entry && Entry->hasInitializer())
1480 return Entry;
1481
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001482 // FIXME: I don't understand why gcc generates this, or where it is
1483 // resolved. Investigate. Its also wasteful to look this up over and
1484 // over.
1485 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1486
Chris Lattner8ec03f52008-11-24 03:54:41 +00001487 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001488
1489 // Construct method lists.
1490 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1491 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00001492 for (ObjCProtocolDecl::instmeth_iterator
1493 i = PD->instmeth_begin(CGM.getContext()),
1494 e = PD->instmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001495 ObjCMethodDecl *MD = *i;
1496 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1497 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1498 OptInstanceMethods.push_back(C);
1499 } else {
1500 InstanceMethods.push_back(C);
1501 }
1502 }
1503
Douglas Gregor6ab35242009-04-09 21:40:53 +00001504 for (ObjCProtocolDecl::classmeth_iterator
1505 i = PD->classmeth_begin(CGM.getContext()),
1506 e = PD->classmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001507 ObjCMethodDecl *MD = *i;
1508 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1509 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1510 OptClassMethods.push_back(C);
1511 } else {
1512 ClassMethods.push_back(C);
1513 }
1514 }
1515
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001516 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001517 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001518 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc93372008-08-21 21:57:41 +00001519 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001520 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00001521 PD->protocol_begin(),
1522 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001523 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001524 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
1525 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001526 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1527 InstanceMethods);
1528 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001529 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
1530 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001531 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1532 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001533 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
1534 Values);
1535
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001536 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001537 // Already created, fix the linkage and update the initializer.
1538 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001539 Entry->setInitializer(Init);
1540 } else {
1541 Entry =
1542 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
1543 llvm::GlobalValue::InternalLinkage,
1544 Init,
1545 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
1546 &CGM.getModule());
1547 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001548 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001549 UsedGlobals.push_back(Entry);
1550 // FIXME: Is this necessary? Why only for protocol?
1551 Entry->setAlignment(4);
1552 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001553
1554 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001555}
1556
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001557llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001558 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1559
1560 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001561 // We use the initializer as a marker of whether this is a forward
1562 // reference or not. At module finalization we add the empty
1563 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001564 Entry =
1565 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001566 llvm::GlobalValue::ExternalLinkage,
1567 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001568 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001569 &CGM.getModule());
1570 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001571 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001572 UsedGlobals.push_back(Entry);
1573 // FIXME: Is this necessary? Why only for protocol?
1574 Entry->setAlignment(4);
1575 }
1576
1577 return Entry;
1578}
1579
1580/*
1581 struct _objc_protocol_extension {
1582 uint32_t size;
1583 struct objc_method_description_list *optional_instance_methods;
1584 struct objc_method_description_list *optional_class_methods;
1585 struct objc_property_list *instance_properties;
1586 };
1587*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001588llvm::Constant *
1589CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1590 const ConstantVector &OptInstanceMethods,
1591 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001592 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001593 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001594 std::vector<llvm::Constant*> Values(4);
1595 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001596 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001597 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1598 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001599 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1600 OptInstanceMethods);
1601 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001602 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1603 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001604 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1605 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001606 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1607 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001608 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001609
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001610 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001611 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1612 Values[3]->isNullValue())
1613 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1614
1615 llvm::Constant *Init =
1616 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001617
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001618 // No special section, but goes in llvm.used
1619 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
1620 Init,
1621 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001622}
1623
1624/*
1625 struct objc_protocol_list {
1626 struct objc_protocol_list *next;
1627 long count;
1628 Protocol *list[];
1629 };
1630*/
Daniel Dunbardbc93372008-08-21 21:57:41 +00001631llvm::Constant *
1632CGObjCMac::EmitProtocolList(const std::string &Name,
1633 ObjCProtocolDecl::protocol_iterator begin,
1634 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001635 std::vector<llvm::Constant*> ProtocolRefs;
1636
Daniel Dunbardbc93372008-08-21 21:57:41 +00001637 for (; begin != end; ++begin)
1638 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001639
1640 // Just return null for empty protocol lists
1641 if (ProtocolRefs.empty())
1642 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1643
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001644 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001645 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1646
1647 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001648 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001649 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1650 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1651 Values[2] =
1652 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1653 ProtocolRefs.size()),
1654 ProtocolRefs);
1655
1656 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1657 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001658 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001659 4, false);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001660 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1661}
1662
1663/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001664 struct _objc_property {
1665 const char * const name;
1666 const char * const attributes;
1667 };
1668
1669 struct _objc_property_list {
1670 uint32_t entsize; // sizeof (struct _objc_property)
1671 uint32_t prop_count;
1672 struct _objc_property[prop_count];
1673 };
1674*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001675llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1676 const Decl *Container,
1677 const ObjCContainerDecl *OCD,
1678 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001679 std::vector<llvm::Constant*> Properties, Prop(2);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001680 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(CGM.getContext()),
1681 E = OCD->prop_end(CGM.getContext()); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00001682 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001683 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001684 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001685 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1686 Prop));
1687 }
1688
1689 // Return null for empty list.
1690 if (Properties.empty())
1691 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1692
1693 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001694 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001695 std::vector<llvm::Constant*> Values(3);
1696 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1697 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1698 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1699 Properties.size());
1700 Values[2] = llvm::ConstantArray::get(AT, Properties);
1701 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1702
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001703 llvm::GlobalVariable *GV =
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001704 CreateMetadataVar(Name, Init,
1705 (ObjCABI == 2) ? "__DATA, __objc_const" :
1706 "__OBJC,__property,regular,no_dead_strip",
1707 (ObjCABI == 2) ? 8 : 4,
1708 true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001709 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001710}
1711
1712/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001713 struct objc_method_description_list {
1714 int count;
1715 struct objc_method_description list[];
1716 };
1717*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001718llvm::Constant *
1719CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1720 std::vector<llvm::Constant*> Desc(2);
1721 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1722 ObjCTypes.SelectorPtrTy);
1723 Desc[1] = GetMethodVarType(MD);
1724 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1725 Desc);
1726}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001727
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001728llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1729 const char *Section,
1730 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001731 // Return null for empty list.
1732 if (Methods.empty())
1733 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1734
1735 std::vector<llvm::Constant*> Values(2);
1736 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1737 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1738 Methods.size());
1739 Values[1] = llvm::ConstantArray::get(AT, Methods);
1740 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1741
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001742 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001743 return llvm::ConstantExpr::getBitCast(GV,
1744 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001745}
1746
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001747/*
1748 struct _objc_category {
1749 char *category_name;
1750 char *class_name;
1751 struct _objc_method_list *instance_methods;
1752 struct _objc_method_list *class_methods;
1753 struct _objc_protocol_list *protocols;
1754 uint32_t size; // <rdar://4585769>
1755 struct _objc_property_list *instance_properties;
1756 };
1757 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001758void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001759 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001760
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001761 // FIXME: This is poor design, the OCD should have a pointer to the
1762 // category decl. Additionally, note that Category can be null for
1763 // the @implementation w/o an @interface case. Sema should just
1764 // create one for us as it does for @implementation so everyone else
1765 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001766 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001767 const ObjCCategoryDecl *Category =
1768 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001769 std::string ExtName(Interface->getNameAsString() + "_" +
1770 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001771
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001772 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001773 for (ObjCCategoryImplDecl::instmeth_iterator
1774 i = OCD->instmeth_begin(CGM.getContext()),
1775 e = OCD->instmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001776 // Instance methods should always be defined.
1777 InstanceMethods.push_back(GetMethodConstant(*i));
1778 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001779 for (ObjCCategoryImplDecl::classmeth_iterator
1780 i = OCD->classmeth_begin(CGM.getContext()),
1781 e = OCD->classmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001782 // Class methods should always be defined.
1783 ClassMethods.push_back(GetMethodConstant(*i));
1784 }
1785
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001786 std::vector<llvm::Constant*> Values(7);
1787 Values[0] = GetClassName(OCD->getIdentifier());
1788 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00001789 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001790 Values[2] =
1791 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1792 ExtName,
1793 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001794 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001795 Values[3] =
1796 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001797 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001798 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001799 if (Category) {
1800 Values[4] =
1801 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1802 Category->protocol_begin(),
1803 Category->protocol_end());
1804 } else {
1805 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1806 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001807 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001808
1809 // If there is no category @interface then there can be no properties.
1810 if (Category) {
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001811 Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001812 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001813 } else {
1814 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1815 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001816
1817 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1818 Values);
1819
1820 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001821 CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init,
1822 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001823 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001824 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001825}
1826
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001827// FIXME: Get from somewhere?
1828enum ClassFlags {
1829 eClassFlags_Factory = 0x00001,
1830 eClassFlags_Meta = 0x00002,
1831 // <rdr://5142207>
1832 eClassFlags_HasCXXStructors = 0x02000,
1833 eClassFlags_Hidden = 0x20000,
1834 eClassFlags_ABI2_Hidden = 0x00010,
1835 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1836};
1837
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001838/*
1839 struct _objc_class {
1840 Class isa;
1841 Class super_class;
1842 const char *name;
1843 long version;
1844 long info;
1845 long instance_size;
1846 struct _objc_ivar_list *ivars;
1847 struct _objc_method_list *methods;
1848 struct _objc_cache *cache;
1849 struct _objc_protocol_list *protocols;
1850 // Objective-C 1.0 extensions (<rdr://4585769>)
1851 const char *ivar_layout;
1852 struct _objc_class_ext *ext;
1853 };
1854
1855 See EmitClassExtension();
1856 */
1857void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001858 DefinedSymbols.insert(ID->getIdentifier());
1859
Chris Lattner8ec03f52008-11-24 03:54:41 +00001860 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001861 // FIXME: Gross
1862 ObjCInterfaceDecl *Interface =
1863 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc93372008-08-21 21:57:41 +00001864 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001865 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00001866 Interface->protocol_begin(),
1867 Interface->protocol_end());
Daniel Dunbar84ad77a2009-04-22 09:39:34 +00001868 const llvm::Type *InterfaceTy = GetConcreteClassStruct(CGM, Interface);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001869 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001870 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001871
1872 // FIXME: Set CXX-structors flag.
Daniel Dunbar04d40782009-04-14 06:00:08 +00001873 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001874 Flags |= eClassFlags_Hidden;
1875
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001876 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001877 for (ObjCImplementationDecl::instmeth_iterator
1878 i = ID->instmeth_begin(CGM.getContext()),
1879 e = ID->instmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001880 // Instance methods should always be defined.
1881 InstanceMethods.push_back(GetMethodConstant(*i));
1882 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001883 for (ObjCImplementationDecl::classmeth_iterator
1884 i = ID->classmeth_begin(CGM.getContext()),
1885 e = ID->classmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001886 // Class methods should always be defined.
1887 ClassMethods.push_back(GetMethodConstant(*i));
1888 }
1889
Douglas Gregor653f1b12009-04-23 01:02:12 +00001890 for (ObjCImplementationDecl::propimpl_iterator
1891 i = ID->propimpl_begin(CGM.getContext()),
1892 e = ID->propimpl_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001893 ObjCPropertyImplDecl *PID = *i;
1894
1895 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1896 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1897
1898 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1899 if (llvm::Constant *C = GetMethodConstant(MD))
1900 InstanceMethods.push_back(C);
1901 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1902 if (llvm::Constant *C = GetMethodConstant(MD))
1903 InstanceMethods.push_back(C);
1904 }
1905 }
1906
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001907 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001908 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001909 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001910 // Record a reference to the super class.
1911 LazySymbols.insert(Super->getIdentifier());
1912
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001913 Values[ 1] =
1914 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1915 ObjCTypes.ClassPtrTy);
1916 } else {
1917 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1918 }
1919 Values[ 2] = GetClassName(ID->getIdentifier());
1920 // Version is always 0.
1921 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1922 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1923 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001924 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001925 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001926 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001927 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001928 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001929 // cache is always NULL.
1930 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1931 Values[ 9] = Protocols;
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00001932 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001933 Values[11] = EmitClassExtension(ID);
1934 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1935 Values);
1936
1937 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001938 CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init,
1939 "__OBJC,__class,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001940 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001941 DefinedClasses.push_back(GV);
1942}
1943
1944llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1945 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001946 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001947 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001948 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001949 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001950
Daniel Dunbar04d40782009-04-14 06:00:08 +00001951 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001952 Flags |= eClassFlags_Hidden;
1953
1954 std::vector<llvm::Constant*> Values(12);
1955 // The isa for the metaclass is the root of the hierarchy.
1956 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1957 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1958 Root = Super;
1959 Values[ 0] =
1960 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1961 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001962 // The super class for the metaclass is emitted as the name of the
1963 // super class. The runtime fixes this up to point to the
1964 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001965 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1966 Values[ 1] =
1967 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1968 ObjCTypes.ClassPtrTy);
1969 } else {
1970 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1971 }
1972 Values[ 2] = GetClassName(ID->getIdentifier());
1973 // Version is always 0.
1974 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1975 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1976 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001977 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001978 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001979 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001980 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001981 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001982 // cache is always NULL.
1983 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1984 Values[ 9] = Protocols;
1985 // ivar_layout for metaclass is always NULL.
1986 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1987 // The class extension is always unused for metaclasses.
1988 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1989 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1990 Values);
1991
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001992 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001993 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001994
1995 // Check for a forward reference.
1996 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1997 if (GV) {
1998 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1999 "Forward metaclass reference has incorrect type.");
2000 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2001 GV->setInitializer(Init);
2002 } else {
2003 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
2004 llvm::GlobalValue::InternalLinkage,
2005 Init, Name,
2006 &CGM.getModule());
2007 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002008 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002009 GV->setAlignment(4);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002010 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002011
2012 return GV;
2013}
2014
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002015llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002016 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002017
2018 // FIXME: Should we look these up somewhere other than the
2019 // module. Its a bit silly since we only generate these while
2020 // processing an implementation, so exactly one pointer would work
2021 // if know when we entered/exitted an implementation block.
2022
2023 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002024 // Previously, metaclass with internal linkage may have been defined.
2025 // pass 'true' as 2nd argument so it is returned.
2026 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002027 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2028 "Forward metaclass reference has incorrect type.");
2029 return GV;
2030 } else {
2031 // Generate as an external reference to keep a consistent
2032 // module. This will be patched up when we emit the metaclass.
2033 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
2034 llvm::GlobalValue::ExternalLinkage,
2035 0,
2036 Name,
2037 &CGM.getModule());
2038 }
2039}
2040
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002041/*
2042 struct objc_class_ext {
2043 uint32_t size;
2044 const char *weak_ivar_layout;
2045 struct _objc_property_list *properties;
2046 };
2047*/
2048llvm::Constant *
2049CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
2050 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002051 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002052
2053 std::vector<llvm::Constant*> Values(3);
2054 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002055 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002056 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002057 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002058
2059 // Return null if no extension bits are used.
2060 if (Values[1]->isNullValue() && Values[2]->isNullValue())
2061 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
2062
2063 llvm::Constant *Init =
2064 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002065 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002066 Init, "__OBJC,__class_ext,regular,no_dead_strip",
2067 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002068}
2069
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00002070/// getInterfaceDeclForIvar - Get the interface declaration node where
2071/// this ivar is declared in.
2072/// FIXME. Ideally, this info should be in the ivar node. But currently
2073/// it is not and prevailing wisdom is that ASTs should not have more
2074/// info than is absolutely needed, even though this info reflects the
2075/// source language.
2076///
2077static const ObjCInterfaceDecl *getInterfaceDeclForIvar(
2078 const ObjCInterfaceDecl *OI,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002079 const ObjCIvarDecl *IVD,
2080 ASTContext &Context) {
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00002081 if (!OI)
2082 return 0;
2083 assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface");
2084 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
2085 E = OI->ivar_end(); I != E; ++I)
2086 if ((*I)->getIdentifier() == IVD->getIdentifier())
2087 return OI;
Fariborz Jahanian5a4b4532009-03-31 17:00:52 +00002088 // look into properties.
Douglas Gregor6ab35242009-04-09 21:40:53 +00002089 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(Context),
2090 E = OI->prop_end(Context); I != E; ++I) {
Fariborz Jahanian5a4b4532009-03-31 17:00:52 +00002091 ObjCPropertyDecl *PDecl = (*I);
2092 if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl())
2093 if (IV->getIdentifier() == IVD->getIdentifier())
2094 return OI;
2095 }
Douglas Gregor6ab35242009-04-09 21:40:53 +00002096 return getInterfaceDeclForIvar(OI->getSuperClass(), IVD, Context);
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00002097}
2098
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002099/*
2100 struct objc_ivar {
2101 char *ivar_name;
2102 char *ivar_type;
2103 int ivar_offset;
2104 };
2105
2106 struct objc_ivar_list {
2107 int ivar_count;
2108 struct objc_ivar list[count];
2109 };
2110 */
2111llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002112 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002113 std::vector<llvm::Constant*> Ivars, Ivar(3);
2114
2115 // When emitting the root class GCC emits ivar entries for the
2116 // actual class structure. It is not clear if we need to follow this
2117 // behavior; for now lets try and get away with not doing it. If so,
2118 // the cleanest solution would be to make up an ObjCInterfaceDecl
2119 // for the class.
2120 if (ForClass)
2121 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002122
2123 ObjCInterfaceDecl *OID =
2124 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002125
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002126 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
2127 GetNamedIvarList(OID, OIvars);
2128
2129 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2130 ObjCIvarDecl *IVD = OIvars[i];
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002131 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2132 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00002133 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002134 ComputeIvarBaseOffset(CGM, OID, IVD));
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002135 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002136 }
2137
2138 // Return null for empty list.
2139 if (Ivars.empty())
2140 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
2141
2142 std::vector<llvm::Constant*> Values(2);
2143 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
2144 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
2145 Ivars.size());
2146 Values[1] = llvm::ConstantArray::get(AT, Ivars);
2147 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2148
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002149 llvm::GlobalVariable *GV;
2150 if (ForClass)
2151 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(),
Daniel Dunbar58a29122009-03-09 22:18:41 +00002152 Init, "__OBJC,__class_vars,regular,no_dead_strip",
2153 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002154 else
2155 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_"
2156 + ID->getNameAsString(),
2157 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002158 4, true);
2159 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002160}
2161
2162/*
2163 struct objc_method {
2164 SEL method_name;
2165 char *method_types;
2166 void *method;
2167 };
2168
2169 struct objc_method_list {
2170 struct objc_method_list *obsolete;
2171 int count;
2172 struct objc_method methods_list[count];
2173 };
2174*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002175
2176/// GetMethodConstant - Return a struct objc_method constant for the
2177/// given method if it has been defined. The result is null if the
2178/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002179llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002180 // FIXME: Use DenseMap::lookup
2181 llvm::Function *Fn = MethodDefinitions[MD];
2182 if (!Fn)
2183 return 0;
2184
2185 std::vector<llvm::Constant*> Method(3);
2186 Method[0] =
2187 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2188 ObjCTypes.SelectorPtrTy);
2189 Method[1] = GetMethodVarType(MD);
2190 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
2191 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
2192}
2193
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002194llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
2195 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002196 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002197 // Return null for empty list.
2198 if (Methods.empty())
2199 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
2200
2201 std::vector<llvm::Constant*> Values(3);
2202 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
2203 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
2204 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
2205 Methods.size());
2206 Values[2] = llvm::ConstantArray::get(AT, Methods);
2207 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2208
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002209 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002210 return llvm::ConstantExpr::getBitCast(GV,
2211 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002212}
2213
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002214llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00002215 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002216 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002217 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002218
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002219 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002220 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002221 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002222 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002223 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002224 llvm::GlobalValue::InternalLinkage,
2225 Name,
2226 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002227 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002228
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002229 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002230}
2231
Daniel Dunbar48fa0642009-04-19 02:03:42 +00002232/// GetFieldBaseOffset - return the field's byte offset.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002233uint64_t CGObjCCommonMac::GetFieldBaseOffset(const ObjCInterfaceDecl *OI,
2234 const llvm::StructLayout *Layout,
Chris Lattnercd0ee142009-03-31 08:33:16 +00002235 const FieldDecl *Field) {
Daniel Dunbar97776872009-04-22 07:32:20 +00002236 // Is this a C struct?
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00002237 if (!OI)
2238 return Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
Daniel Dunbar97776872009-04-22 07:32:20 +00002239 return ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002240}
2241
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002242llvm::GlobalVariable *
2243CGObjCCommonMac::CreateMetadataVar(const std::string &Name,
2244 llvm::Constant *Init,
2245 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002246 unsigned Align,
2247 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002248 const llvm::Type *Ty = Init->getType();
2249 llvm::GlobalVariable *GV =
2250 new llvm::GlobalVariable(Ty, false,
2251 llvm::GlobalValue::InternalLinkage,
2252 Init,
2253 Name,
2254 &CGM.getModule());
2255 if (Section)
2256 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002257 if (Align)
2258 GV->setAlignment(Align);
2259 if (AddToUsed)
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002260 UsedGlobals.push_back(GV);
2261 return GV;
2262}
2263
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002264llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002265 // Abuse this interface function as a place to finalize.
2266 FinishModule();
2267
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002268 return NULL;
2269}
2270
Chris Lattner74391b42009-03-22 21:03:39 +00002271llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002272 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002273}
2274
Chris Lattner74391b42009-03-22 21:03:39 +00002275llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002276 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002277}
2278
Chris Lattner74391b42009-03-22 21:03:39 +00002279llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002280 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002281}
2282
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002283/*
2284
2285Objective-C setjmp-longjmp (sjlj) Exception Handling
2286--
2287
2288The basic framework for a @try-catch-finally is as follows:
2289{
2290 objc_exception_data d;
2291 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002292 bool _call_try_exit = true;
2293
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002294 objc_exception_try_enter(&d);
2295 if (!setjmp(d.jmp_buf)) {
2296 ... try body ...
2297 } else {
2298 // exception path
2299 id _caught = objc_exception_extract(&d);
2300
2301 // enter new try scope for handlers
2302 if (!setjmp(d.jmp_buf)) {
2303 ... match exception and execute catch blocks ...
2304
2305 // fell off end, rethrow.
2306 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00002307 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002308 } else {
2309 // exception in catch block
2310 _rethrow = objc_exception_extract(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002311 _call_try_exit = false;
2312 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002313 }
2314 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002315 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002316
2317finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002318 if (_call_try_exit)
2319 objc_exception_try_exit(&d);
2320
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002321 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002322 ... dispatch to finally destination ...
2323
2324finally_rethrow:
2325 objc_exception_throw(_rethrow);
2326
2327finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002328}
2329
2330This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00002331uses _rethrow to determine if objc_exception_try_exit should be called
2332and if the object should be rethrown. This breaks in the face of
2333throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002334
2335We specialize this framework for a few particular circumstances:
2336
2337 - If there are no catch blocks, then we avoid emitting the second
2338 exception handling context.
2339
2340 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2341 e)) we avoid emitting the code to rethrow an uncaught exception.
2342
2343 - FIXME: If there is no @finally block we can do a few more
2344 simplifications.
2345
2346Rethrows and Jumps-Through-Finally
2347--
2348
2349Support for implicit rethrows and jumping through the finally block is
2350handled by storing the current exception-handling context in
2351ObjCEHStack.
2352
Daniel Dunbar898d5082008-09-30 01:06:03 +00002353In order to implement proper @finally semantics, we support one basic
2354mechanism for jumping through the finally block to an arbitrary
2355destination. Constructs which generate exits from a @try or @catch
2356block use this mechanism to implement the proper semantics by chaining
2357jumps, as necessary.
2358
2359This mechanism works like the one used for indirect goto: we
2360arbitrarily assign an ID to each destination and store the ID for the
2361destination in a variable prior to entering the finally block. At the
2362end of the finally block we simply create a switch to the proper
2363destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002364
2365Code gen for @synchronized(expr) stmt;
2366Effectively generating code for:
2367objc_sync_enter(expr);
2368@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002369*/
2370
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002371void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2372 const Stmt &S) {
2373 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002374 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00002375 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002376 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar55e87422008-11-11 02:29:29 +00002377 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
2378 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
2379 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar1c566672009-02-24 01:43:46 +00002380
2381 // For @synchronized, call objc_sync_enter(sync.expr). The
2382 // evaluation of the expression must occur before we enter the
2383 // @synchronized. We can safely avoid a temp here because jumps into
2384 // @synchronized are illegal & this will dominate uses.
2385 llvm::Value *SyncArg = 0;
2386 if (!isTry) {
2387 SyncArg =
2388 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2389 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00002390 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002391 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002392
2393 // Push an EH context entry, used for handling rethrows and jumps
2394 // through finally.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002395 CGF.PushCleanupBlock(FinallyBlock);
2396
Anders Carlsson273558f2009-02-07 21:37:21 +00002397 CGF.ObjCEHValueStack.push_back(0);
2398
Daniel Dunbar898d5082008-09-30 01:06:03 +00002399 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00002400 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2401 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002402 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
2403 "_rethrow");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002404 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
2405 "_call_try_exit");
2406 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr);
2407
Anders Carlsson80f25672008-09-09 17:59:25 +00002408 // Enter a new try block and call setjmp.
Chris Lattner34b02a12009-04-22 02:26:14 +00002409 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Anders Carlsson80f25672008-09-09 17:59:25 +00002410 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
2411 "jmpbufarray");
2412 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
Chris Lattner34b02a12009-04-22 02:26:14 +00002413 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlsson80f25672008-09-09 17:59:25 +00002414 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00002415
Daniel Dunbar55e87422008-11-11 02:29:29 +00002416 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2417 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002418 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002419 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002420
2421 // Emit the @try block.
2422 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002423 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
2424 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002425 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002426
2427 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002428 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002429
2430 // Retrieve the exception object. We may emit multiple blocks but
2431 // nothing can cross this so the value is already in SSA form.
Chris Lattner34b02a12009-04-22 02:26:14 +00002432 llvm::Value *Caught =
2433 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2434 ExceptionData, "caught");
Anders Carlsson273558f2009-02-07 21:37:21 +00002435 CGF.ObjCEHValueStack.back() = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002436 if (!isTry)
2437 {
2438 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002439 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002440 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002441 }
2442 else if (const ObjCAtCatchStmt* CatchStmt =
2443 cast<ObjCAtTryStmt>(S).getCatchStmts())
2444 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00002445 // Enter a new exception try block (in case a @catch block throws
2446 // an exception).
Chris Lattner34b02a12009-04-22 02:26:14 +00002447 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002448
Chris Lattner34b02a12009-04-22 02:26:14 +00002449 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlsson80f25672008-09-09 17:59:25 +00002450 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002451 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00002452
Daniel Dunbar55e87422008-11-11 02:29:29 +00002453 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
2454 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002455 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002456
2457 CGF.EmitBlock(CatchBlock);
2458
Daniel Dunbar55e40722008-09-27 07:03:52 +00002459 // Handle catch list. As a special case we check if everything is
2460 // matched and avoid generating code for falling off the end if
2461 // so.
2462 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00002463 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00002464 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00002465
Steve Naroff7ba138a2009-03-03 19:52:17 +00002466 const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Daniel Dunbar129271a2008-09-27 07:36:24 +00002467 const PointerType *PT = 0;
2468
Anders Carlsson80f25672008-09-09 17:59:25 +00002469 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00002470 if (!CatchParam) {
2471 AllMatched = true;
2472 } else {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002473 PT = CatchParam->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002474
Daniel Dunbar97f61d12008-09-27 22:21:14 +00002475 // catch(id e) always matches.
2476 // FIXME: For the time being we also match id<X>; this should
2477 // be rejected by Sema instead.
Steve Naroff389bf462009-02-12 17:52:19 +00002478 if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) ||
Steve Naroff7ba138a2009-03-03 19:52:17 +00002479 CatchParam->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00002480 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00002481 }
2482
Daniel Dunbar55e40722008-09-27 07:03:52 +00002483 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00002484 if (CatchParam) {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002485 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002486 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002487 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002488 }
Anders Carlsson1452f552008-09-11 08:21:54 +00002489
Anders Carlssondde0a942008-09-11 09:15:33 +00002490 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002491 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002492 break;
2493 }
2494
Daniel Dunbar129271a2008-09-27 07:36:24 +00002495 assert(PT && "Unexpected non-pointer type in @catch");
2496 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00002497 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002498 assert(ObjCType && "Catch parameter must have Objective-C type!");
2499
2500 // Check if the @catch block matches the exception object.
2501 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
2502
Chris Lattner34b02a12009-04-22 02:26:14 +00002503 llvm::Value *Match =
2504 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
2505 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00002506
Daniel Dunbar55e87422008-11-11 02:29:29 +00002507 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00002508
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002509 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002510 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002511
2512 // Emit the @catch block.
2513 CGF.EmitBlock(MatchedBlock);
Steve Naroff7ba138a2009-03-03 19:52:17 +00002514 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002515 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002516
2517 llvm::Value *Tmp =
Steve Naroff7ba138a2009-03-03 19:52:17 +00002518 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002519 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002520 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002521
2522 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002523 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002524
2525 CGF.EmitBlock(NextCatchBlock);
2526 }
2527
Daniel Dunbar55e40722008-09-27 07:03:52 +00002528 if (!AllMatched) {
2529 // None of the handlers caught the exception, so store it to be
2530 // rethrown at the end of the @finally block.
2531 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002532 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002533 }
2534
2535 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002536 CGF.EmitBlock(CatchHandler);
Chris Lattner34b02a12009-04-22 02:26:14 +00002537 CGF.Builder.CreateStore(
2538 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2539 ExceptionData),
Daniel Dunbar55e40722008-09-27 07:03:52 +00002540 RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002541 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002542 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002543 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00002544 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002545 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002546 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson80f25672008-09-09 17:59:25 +00002547 }
2548
Daniel Dunbar898d5082008-09-30 01:06:03 +00002549 // Pop the exception-handling stack entry. It is important to do
2550 // this now, because the code in the @finally block is not in this
2551 // context.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002552 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2553
Anders Carlsson273558f2009-02-07 21:37:21 +00002554 CGF.ObjCEHValueStack.pop_back();
2555
Anders Carlsson80f25672008-09-09 17:59:25 +00002556 // Emit the @finally block.
2557 CGF.EmitBlock(FinallyBlock);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002558 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2559
2560 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2561
2562 CGF.EmitBlock(FinallyExit);
Chris Lattner34b02a12009-04-22 02:26:14 +00002563 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002564
2565 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002566 if (isTry) {
2567 if (const ObjCAtFinallyStmt* FinallyStmt =
2568 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2569 CGF.EmitStmt(FinallyStmt->getFinallyBody());
Daniel Dunbar1c566672009-02-24 01:43:46 +00002570 } else {
2571 // Emit objc_sync_exit(expr); as finally's sole statement for
2572 // @synchronized.
Chris Lattnerbbccd612009-04-22 02:38:11 +00002573 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002574 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002575
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002576 // Emit the switch block
2577 if (Info.SwitchBlock)
2578 CGF.EmitBlock(Info.SwitchBlock);
2579 if (Info.EndBlock)
2580 CGF.EmitBlock(Info.EndBlock);
2581
Daniel Dunbar898d5082008-09-30 01:06:03 +00002582 CGF.EmitBlock(FinallyRethrow);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002583 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbar898d5082008-09-30 01:06:03 +00002584 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002585 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00002586
2587 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002588}
2589
2590void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002591 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002592 llvm::Value *ExceptionAsObject;
2593
2594 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2595 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2596 ExceptionAsObject =
2597 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2598 } else {
Anders Carlsson273558f2009-02-07 21:37:21 +00002599 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002600 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00002601 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002602 }
2603
Chris Lattnerbbccd612009-04-22 02:38:11 +00002604 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002605 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002606
2607 // Clear the insertion point to indicate we are in unreachable code.
2608 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002609}
2610
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002611/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002612/// object: objc_read_weak (id *src)
2613///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002614llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002615 llvm::Value *AddrWeakObj)
2616{
Eli Friedman8339b352009-03-07 03:57:15 +00002617 const llvm::Type* DestTy =
2618 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002619 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00002620 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002621 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00002622 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002623 return read_weak;
2624}
2625
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002626/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2627/// objc_assign_weak (id src, id *dst)
2628///
2629void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2630 llvm::Value *src, llvm::Value *dst)
2631{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002632 const llvm::Type * SrcTy = src->getType();
2633 if (!isa<llvm::PointerType>(SrcTy)) {
2634 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2635 assert(Size <= 8 && "does not support size > 8");
2636 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2637 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002638 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2639 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002640 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2641 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00002642 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002643 src, dst, "weakassign");
2644 return;
2645}
2646
Fariborz Jahanian58626502008-11-19 00:59:10 +00002647/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2648/// objc_assign_global (id src, id *dst)
2649///
2650void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2651 llvm::Value *src, llvm::Value *dst)
2652{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002653 const llvm::Type * SrcTy = src->getType();
2654 if (!isa<llvm::PointerType>(SrcTy)) {
2655 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2656 assert(Size <= 8 && "does not support size > 8");
2657 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2658 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002659 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2660 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002661 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2662 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002663 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00002664 src, dst, "globalassign");
2665 return;
2666}
2667
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002668/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2669/// objc_assign_ivar (id src, id *dst)
2670///
2671void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2672 llvm::Value *src, llvm::Value *dst)
2673{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002674 const llvm::Type * SrcTy = src->getType();
2675 if (!isa<llvm::PointerType>(SrcTy)) {
2676 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2677 assert(Size <= 8 && "does not support size > 8");
2678 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2679 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002680 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2681 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002682 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2683 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002684 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(),
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002685 src, dst, "assignivar");
2686 return;
2687}
2688
Fariborz Jahanian58626502008-11-19 00:59:10 +00002689/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2690/// objc_assign_strongCast (id src, id *dst)
2691///
2692void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2693 llvm::Value *src, llvm::Value *dst)
2694{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002695 const llvm::Type * SrcTy = src->getType();
2696 if (!isa<llvm::PointerType>(SrcTy)) {
2697 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2698 assert(Size <= 8 && "does not support size > 8");
2699 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2700 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002701 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2702 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002703 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2704 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002705 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00002706 src, dst, "weakassign");
2707 return;
2708}
2709
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002710/// EmitObjCValueForIvar - Code Gen for ivar reference.
2711///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002712LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2713 QualType ObjectTy,
2714 llvm::Value *BaseValue,
2715 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002716 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00002717 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00002718 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
2719 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002720}
2721
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002722llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00002723 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002724 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00002725 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002726 return llvm::ConstantInt::get(
2727 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2728 Offset);
2729}
2730
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002731/* *** Private Interface *** */
2732
2733/// EmitImageInfo - Emit the image info marker used to encode some module
2734/// level information.
2735///
2736/// See: <rdr://4810609&4810587&4810587>
2737/// struct IMAGE_INFO {
2738/// unsigned version;
2739/// unsigned flags;
2740/// };
2741enum ImageInfoFlags {
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002742 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what
2743 // this implies.
2744 eImageInfo_GarbageCollected = (1 << 1),
2745 eImageInfo_GCOnly = (1 << 2),
2746 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
2747
2748 // A flag indicating that the module has no instances of an
2749 // @synthesize of a superclass variable. <rdar://problem/6803242>
2750 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002751};
2752
2753void CGObjCMac::EmitImageInfo() {
2754 unsigned version = 0; // Version is unused?
2755 unsigned flags = 0;
2756
2757 // FIXME: Fix and continue?
2758 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2759 flags |= eImageInfo_GarbageCollected;
2760 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2761 flags |= eImageInfo_GCOnly;
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002762
2763 // We never allow @synthesize of a superclass property.
2764 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002765
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002766 // Emitted as int[2];
2767 llvm::Constant *values[2] = {
2768 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2769 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2770 };
2771 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002772
2773 const char *Section;
2774 if (ObjCABI == 1)
2775 Section = "__OBJC, __image_info,regular";
2776 else
2777 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002778 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002779 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
2780 llvm::ConstantArray::get(AT, values, 2),
2781 Section,
2782 0,
2783 true);
2784 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002785}
2786
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002787
2788// struct objc_module {
2789// unsigned long version;
2790// unsigned long size;
2791// const char *name;
2792// Symtab symtab;
2793// };
2794
2795// FIXME: Get from somewhere
2796static const int ModuleVersion = 7;
2797
2798void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002799 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002800
2801 std::vector<llvm::Constant*> Values(4);
2802 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2803 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002804 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002805 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002806 Values[3] = EmitModuleSymbols();
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002807 CreateMetadataVar("\01L_OBJC_MODULES",
2808 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
2809 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002810 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002811}
2812
2813llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002814 unsigned NumClasses = DefinedClasses.size();
2815 unsigned NumCategories = DefinedCategories.size();
2816
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002817 // Return null if no symbols were defined.
2818 if (!NumClasses && !NumCategories)
2819 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2820
2821 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002822 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2823 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2824 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2825 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2826
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002827 // The runtime expects exactly the list of defined classes followed
2828 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002829 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002830 for (unsigned i=0; i<NumClasses; i++)
2831 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2832 ObjCTypes.Int8PtrTy);
2833 for (unsigned i=0; i<NumCategories; i++)
2834 Symbols[NumClasses + i] =
2835 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2836 ObjCTypes.Int8PtrTy);
2837
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002838 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002839 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002840 NumClasses + NumCategories),
2841 Symbols);
2842
2843 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2844
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002845 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002846 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
2847 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002848 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002849 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2850}
2851
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002852llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002853 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002854 LazySymbols.insert(ID->getIdentifier());
2855
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002856 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2857
2858 if (!Entry) {
2859 llvm::Constant *Casted =
2860 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2861 ObjCTypes.ClassPtrTy);
2862 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002863 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
2864 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002865 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002866 }
2867
2868 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002869}
2870
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002871llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002872 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2873
2874 if (!Entry) {
2875 llvm::Constant *Casted =
2876 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2877 ObjCTypes.SelectorPtrTy);
2878 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002879 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
2880 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002881 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002882 }
2883
2884 return Builder.CreateLoad(Entry, false, "tmp");
2885}
2886
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002887llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002888 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002889
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002890 if (!Entry)
2891 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
2892 llvm::ConstantArray::get(Ident->getName()),
2893 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00002894 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002895
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002896 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002897}
2898
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00002899/// GetIvarLayoutName - Returns a unique constant for the given
2900/// ivar layout bitmap.
2901llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
2902 const ObjCCommonTypesHelper &ObjCTypes) {
2903 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
2904}
2905
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002906void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCInterfaceDecl *OI,
2907 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002908 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00002909 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002910 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +00002911 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002912 bool IsUnion = (RD && RD->isUnion());
2913 uint64_t MaxUnionIvarSize = 0;
2914 uint64_t MaxSkippedUnionIvarSize = 0;
2915 FieldDecl *MaxField = 0;
2916 FieldDecl *MaxSkippedField = 0;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002917 FieldDecl *LastFieldBitfield = 0;
2918
Chris Lattnerf1690852009-03-31 08:48:01 +00002919 unsigned base = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002920 if (RecFields.empty())
2921 return;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002922 if (IsUnion)
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002923 base = BytePos + GetFieldBaseOffset(OI, Layout, RecFields[0]);
Chris Lattnerf1690852009-03-31 08:48:01 +00002924 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
2925 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
2926
2927 llvm::SmallVector<FieldDecl*, 16> TmpRecFields;
2928
2929 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002930 FieldDecl *Field = RecFields[i];
2931 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002932 if (!Field->getIdentifier() || Field->isBitField()) {
2933 LastFieldBitfield = Field;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002934 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002935 }
2936 LastFieldBitfield = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002937 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00002938 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002939 if (FQT->isUnionType())
2940 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002941 else
2942 assert(FQT->isRecordType() &&
2943 "only union/record is supported for ivar layout bitmap");
2944
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002945 const RecordType *RT = FQT->getAsRecordType();
2946 const RecordDecl *RD = RT->getDecl();
Daniel Dunbarb02532a2009-04-19 23:41:48 +00002947 // FIXME - Find a more efficient way of passing records down.
Douglas Gregor6ab35242009-04-09 21:40:53 +00002948 TmpRecFields.append(RD->field_begin(CGM.getContext()),
2949 RD->field_end(CGM.getContext()));
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00002950 const llvm::Type *Ty = CGM.getTypes().ConvertType(FQT);
2951 const llvm::StructLayout *RecLayout =
2952 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
2953
2954 BuildAggrIvarLayout(0, RecLayout, RD, TmpRecFields,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002955 BytePos + GetFieldBaseOffset(OI, Layout, Field),
Fariborz Jahanian81adc052009-04-24 16:17:09 +00002956 ForStrongLayout, HasUnion);
Chris Lattnerf1690852009-03-31 08:48:01 +00002957 TmpRecFields.clear();
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002958 continue;
2959 }
Chris Lattnerf1690852009-03-31 08:48:01 +00002960
2961 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002962 const ConstantArrayType *CArray =
2963 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002964 uint64_t ElCount = CArray->getSize().getZExtValue();
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002965 assert(CArray && "only array with know element size is supported");
2966 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00002967 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
2968 const ConstantArrayType *CArray =
2969 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002970 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00002971 FQT = CArray->getElementType();
2972 }
2973
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002974 assert(!FQT->isUnionType() &&
2975 "layout for array of unions not supported");
2976 if (FQT->isRecordType()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00002977 int OldIndex = IvarsInfo.size() - 1;
2978 int OldSkIndex = SkipIvars.size() -1;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002979
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002980 // FIXME - Use a common routine with the above!
2981 const RecordType *RT = FQT->getAsRecordType();
2982 const RecordDecl *RD = RT->getDecl();
2983 // FIXME - Find a more efficiant way of passing records down.
Douglas Gregor6ab35242009-04-09 21:40:53 +00002984 TmpRecFields.append(RD->field_begin(CGM.getContext()),
2985 RD->field_end(CGM.getContext()));
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00002986 const llvm::Type *Ty = CGM.getTypes().ConvertType(FQT);
2987 const llvm::StructLayout *RecLayout =
2988 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Chris Lattnerf1690852009-03-31 08:48:01 +00002989
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00002990 BuildAggrIvarLayout(0, RecLayout, RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00002991 TmpRecFields,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002992 BytePos + GetFieldBaseOffset(OI, Layout, Field),
Fariborz Jahanian81adc052009-04-24 16:17:09 +00002993 ForStrongLayout, HasUnion);
Chris Lattnerf1690852009-03-31 08:48:01 +00002994 TmpRecFields.clear();
2995
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002996 // Replicate layout information for each array element. Note that
2997 // one element is already done.
2998 uint64_t ElIx = 1;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00002999 for (int FirstIndex = IvarsInfo.size() - 1,
3000 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003001 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003002 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3003 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003004 GC_IVAR gcivar;
3005 gcivar.ivar_bytepos = IvarsInfo[i].ivar_bytepos + Size*ElIx;
3006 gcivar.ivar_size = IvarsInfo[i].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003007 IvarsInfo.push_back(gcivar);
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003008 }
3009
Chris Lattnerf1690852009-03-31 08:48:01 +00003010 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003011 GC_IVAR skivar;
3012 skivar.ivar_bytepos = SkipIvars[i].ivar_bytepos + Size*ElIx;
3013 skivar.ivar_size = SkipIvars[i].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003014 SkipIvars.push_back(skivar);
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003015 }
3016 }
3017 continue;
3018 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003019 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003020 // At this point, we are done with Record/Union and array there of.
3021 // For other arrays we are down to its element type.
3022 QualType::GCAttrTypes GCAttr = QualType::GCNone;
3023 do {
3024 if (FQT.isObjCGCStrong() || FQT.isObjCGCWeak()) {
3025 GCAttr = FQT.isObjCGCStrong() ? QualType::Strong : QualType::Weak;
3026 break;
3027 }
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003028 else if (CGM.getContext().isObjCObjectPointerType(FQT)) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003029 GCAttr = QualType::Strong;
3030 break;
3031 }
3032 else if (const PointerType *PT = FQT->getAsPointerType()) {
3033 FQT = PT->getPointeeType();
3034 }
3035 else {
3036 break;
3037 }
3038 } while (true);
Chris Lattnerf1690852009-03-31 08:48:01 +00003039
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003040 if ((ForStrongLayout && GCAttr == QualType::Strong)
3041 || (!ForStrongLayout && GCAttr == QualType::Weak)) {
3042 if (IsUnion)
3043 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003044 uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType())
3045 / WordSizeInBits;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003046 if (UnionIvarSize > MaxUnionIvarSize)
3047 {
3048 MaxUnionIvarSize = UnionIvarSize;
3049 MaxField = Field;
3050 }
3051 }
3052 else
3053 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003054 GC_IVAR gcivar;
3055 gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field);
3056 gcivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) /
3057 WordSizeInBits;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003058 IvarsInfo.push_back(gcivar);
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003059 }
3060 }
3061 else if ((ForStrongLayout &&
3062 (GCAttr == QualType::GCNone || GCAttr == QualType::Weak))
3063 || (!ForStrongLayout && GCAttr != QualType::Weak)) {
3064 if (IsUnion)
3065 {
3066 uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType());
3067 if (UnionIvarSize > MaxSkippedUnionIvarSize)
3068 {
3069 MaxSkippedUnionIvarSize = UnionIvarSize;
3070 MaxSkippedField = Field;
3071 }
3072 }
3073 else
3074 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003075 GC_IVAR skivar;
3076 skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field);
3077 skivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) /
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003078 ByteSizeInBits;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003079 SkipIvars.push_back(skivar);
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003080 }
3081 }
3082 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003083 if (LastFieldBitfield) {
3084 // Last field was a bitfield. Must update skip info.
3085 GC_IVAR skivar;
3086 skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout,
3087 LastFieldBitfield);
3088 Expr *BitWidth = LastFieldBitfield->getBitWidth();
3089 uint64_t BitFieldSize =
Eli Friedman9a901bb2009-04-26 19:19:15 +00003090 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003091 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3092 + ((BitFieldSize % ByteSizeInBits) != 0);
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003093 SkipIvars.push_back(skivar);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003094 }
3095
Chris Lattnerf1690852009-03-31 08:48:01 +00003096 if (MaxField) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003097 GC_IVAR gcivar;
3098 gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, MaxField);
3099 gcivar.ivar_size = MaxUnionIvarSize;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003100 IvarsInfo.push_back(gcivar);
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003101 }
Chris Lattnerf1690852009-03-31 08:48:01 +00003102
3103 if (MaxSkippedField) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003104 GC_IVAR skivar;
3105 skivar.ivar_bytepos = BytePos +
3106 GetFieldBaseOffset(OI, Layout, MaxSkippedField);
3107 skivar.ivar_size = MaxSkippedUnionIvarSize;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003108 SkipIvars.push_back(skivar);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003109 }
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003110}
3111
3112/// BuildIvarLayout - Builds ivar layout bitmap for the class
3113/// implementation for the __strong or __weak case.
3114/// The layout map displays which words in ivar list must be skipped
3115/// and which must be scanned by GC (see below). String is built of bytes.
3116/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3117/// of words to skip and right nibble is count of words to scan. So, each
3118/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3119/// represented by a 0x00 byte which also ends the string.
3120/// 1. when ForStrongLayout is true, following ivars are scanned:
3121/// - id, Class
3122/// - object *
3123/// - __strong anything
3124///
3125/// 2. When ForStrongLayout is false, following ivars are scanned:
3126/// - __weak anything
3127///
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003128llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003129 const ObjCImplementationDecl *OMD,
3130 bool ForStrongLayout) {
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003131 bool hasUnion = false;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003132
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003133 unsigned int WordsToScan, WordsToSkip;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003134 const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
3135 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
3136 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003137
Chris Lattnerf1690852009-03-31 08:48:01 +00003138 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003139 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003140 CGM.getContext().CollectObjCIvars(OI, RecFields);
3141 if (RecFields.empty())
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003142 return llvm::Constant::getNullValue(PtrTy);
Chris Lattnerf1690852009-03-31 08:48:01 +00003143
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003144 SkipIvars.clear();
3145 IvarsInfo.clear();
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00003146
Daniel Dunbar84ad77a2009-04-22 09:39:34 +00003147 const llvm::StructLayout *Layout =
3148 CGM.getTargetData().getStructLayout(GetConcreteClassStruct(CGM, OI));
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003149 BuildAggrIvarLayout(OI, Layout, 0, RecFields, 0, ForStrongLayout, hasUnion);
3150 if (IvarsInfo.empty())
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003151 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003152
3153 // Sort on byte position in case we encounterred a union nested in
3154 // the ivar list.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003155 if (hasUnion && !IvarsInfo.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003156 std::sort(IvarsInfo.begin(), IvarsInfo.end());
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003157 if (hasUnion && !SkipIvars.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003158 std::sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003159
3160 // Build the string of skip/scan nibbles
Fariborz Jahanian8c2f2d12009-04-24 17:15:27 +00003161 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003162 unsigned int WordSize =
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003163 CGM.getTypes().getTargetData().getTypePaddedSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003164 if (IvarsInfo[0].ivar_bytepos == 0) {
3165 WordsToSkip = 0;
3166 WordsToScan = IvarsInfo[0].ivar_size;
3167 }
3168 else {
3169 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3170 WordsToScan = IvarsInfo[0].ivar_size;
3171 }
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003172 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++)
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003173 {
3174 unsigned int TailPrevGCObjC =
3175 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
3176 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC)
3177 {
3178 // consecutive 'scanned' object pointers.
3179 WordsToScan += IvarsInfo[i].ivar_size;
3180 }
3181 else
3182 {
3183 // Skip over 'gc'able object pointer which lay over each other.
3184 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3185 continue;
3186 // Must skip over 1 or more words. We save current skip/scan values
3187 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003188 SKIP_SCAN SkScan;
3189 SkScan.skip = WordsToSkip;
3190 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003191 SkipScanIvars.push_back(SkScan);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003192
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003193 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003194 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3195 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003196 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003197 WordsToSkip = 0;
3198 WordsToScan = IvarsInfo[i].ivar_size;
3199 }
3200 }
3201 if (WordsToScan > 0)
3202 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003203 SKIP_SCAN SkScan;
3204 SkScan.skip = WordsToSkip;
3205 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003206 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003207 }
3208
3209 bool BytesSkipped = false;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003210 if (!SkipIvars.empty())
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003211 {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003212 unsigned int LastIndex = SkipIvars.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003213 int LastByteSkipped =
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003214 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
3215 LastIndex = IvarsInfo.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003216 int LastByteScanned =
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003217 IvarsInfo[LastIndex].ivar_bytepos +
3218 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003219 BytesSkipped = (LastByteSkipped > LastByteScanned);
3220 // Compute number of bytes to skip at the tail end of the last ivar scanned.
3221 if (BytesSkipped)
3222 {
3223 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003224 SKIP_SCAN SkScan;
3225 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3226 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003227 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003228 }
3229 }
3230 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3231 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003232 int SkipScan = SkipScanIvars.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003233 for (int i = 0; i <= SkipScan; i++)
3234 {
3235 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3236 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3237 // 0xM0 followed by 0x0N detected.
3238 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3239 for (int j = i+1; j < SkipScan; j++)
3240 SkipScanIvars[j] = SkipScanIvars[j+1];
3241 --SkipScan;
3242 }
3243 }
3244
3245 // Generate the string.
3246 std::string BitMap;
3247 for (int i = 0; i <= SkipScan; i++)
3248 {
3249 unsigned char byte;
3250 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3251 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3252 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3253 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
3254
3255 if (skip_small > 0 || skip_big > 0)
3256 BytesSkipped = true;
3257 // first skip big.
3258 for (unsigned int ix = 0; ix < skip_big; ix++)
3259 BitMap += (unsigned char)(0xf0);
3260
3261 // next (skip small, scan)
3262 if (skip_small)
3263 {
3264 byte = skip_small << 4;
3265 if (scan_big > 0)
3266 {
3267 byte |= 0xf;
3268 --scan_big;
3269 }
3270 else if (scan_small)
3271 {
3272 byte |= scan_small;
3273 scan_small = 0;
3274 }
3275 BitMap += byte;
3276 }
3277 // next scan big
3278 for (unsigned int ix = 0; ix < scan_big; ix++)
3279 BitMap += (unsigned char)(0x0f);
3280 // last scan small
3281 if (scan_small)
3282 {
3283 byte = scan_small;
3284 BitMap += byte;
3285 }
3286 }
3287 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003288 unsigned char zero = 0;
3289 BitMap += zero;
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003290
3291 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
3292 printf("\n%s ivar layout for class '%s': ",
3293 ForStrongLayout ? "strong" : "weak",
3294 OMD->getClassInterface()->getNameAsCString());
3295 const unsigned char *s = (unsigned char*)BitMap.c_str();
3296 for (unsigned i = 0; i < BitMap.size(); i++)
3297 if (!(s[i] & 0xf0))
3298 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3299 else
3300 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3301 printf("\n");
3302 }
3303
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003304 // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as
3305 // final layout.
3306 if (ForStrongLayout && !BytesSkipped)
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003307 return llvm::Constant::getNullValue(PtrTy);
3308 llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3309 llvm::ConstantArray::get(BitMap.c_str()),
3310 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003311 1, true);
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003312 return getConstantGEP(Entry, 0, 0);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003313}
3314
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003315llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003316 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3317
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003318 // FIXME: Avoid std::string copying.
3319 if (!Entry)
3320 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
3321 llvm::ConstantArray::get(Sel.getAsString()),
3322 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003323 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003324
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003325 return getConstantGEP(Entry, 0, 0);
3326}
3327
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003328// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003329llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003330 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3331}
3332
3333// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003334llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003335 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
3336}
3337
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003338llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003339 std::string TypeStr;
3340 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3341
3342 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003343
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003344 if (!Entry)
3345 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
3346 llvm::ConstantArray::get(TypeStr),
3347 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003348 1, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003349
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003350 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003351}
3352
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003353llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003354 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003355 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3356 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00003357
3358 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3359
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003360 if (!Entry)
3361 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
3362 llvm::ConstantArray::get(TypeStr),
3363 "__TEXT,__cstring,cstring_literals",
3364 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00003365
3366 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003367}
3368
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003369// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003370llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003371 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
3372
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003373 if (!Entry)
3374 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
3375 llvm::ConstantArray::get(Ident->getName()),
3376 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003377 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003378
3379 return getConstantGEP(Entry, 0, 0);
3380}
3381
3382// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003383// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003384llvm::Constant *
3385 CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3386 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003387 std::string TypeStr;
3388 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003389 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3390}
3391
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003392void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
3393 const ObjCContainerDecl *CD,
3394 std::string &NameOut) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00003395 NameOut = '\01';
3396 NameOut += (D->isInstanceMethod() ? '-' : '+');
Chris Lattner077bf5e2008-11-24 03:33:13 +00003397 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003398 assert (CD && "Missing container decl in GetNameForMethod");
3399 NameOut += CD->getNameAsString();
Fariborz Jahanian1e9aef32009-04-16 18:34:20 +00003400 if (const ObjCCategoryImplDecl *CID =
3401 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) {
3402 NameOut += '(';
3403 NameOut += CID->getNameAsString();
3404 NameOut+= ')';
3405 }
Chris Lattner077bf5e2008-11-24 03:33:13 +00003406 NameOut += ' ';
3407 NameOut += D->getSelector().getAsString();
3408 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003409}
3410
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003411void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003412 EmitModuleInfo();
3413
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003414 // Emit the dummy bodies for any protocols which were referenced but
3415 // never defined.
3416 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
3417 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
3418 if (i->second->hasInitializer())
3419 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003420
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003421 std::vector<llvm::Constant*> Values(5);
3422 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
3423 Values[1] = GetClassName(i->first);
3424 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
3425 Values[3] = Values[4] =
3426 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
3427 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
3428 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
3429 Values));
3430 }
3431
3432 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003433 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003434 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003435 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003436 }
3437
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003438 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003439 llvm::GlobalValue *GV =
3440 new llvm::GlobalVariable(AT, false,
3441 llvm::GlobalValue::AppendingLinkage,
3442 llvm::ConstantArray::get(AT, Used),
3443 "llvm.used",
3444 &CGM.getModule());
3445
3446 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003447
3448 // Add assembler directives to add lazy undefined symbol references
3449 // for classes which are referenced but not defined. This is
3450 // important for correct linker interaction.
3451
3452 // FIXME: Uh, this isn't particularly portable.
3453 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00003454
3455 if (!CGM.getModule().getModuleInlineAsm().empty())
3456 s << "\n";
3457
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003458 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
3459 e = LazySymbols.end(); i != e; ++i) {
3460 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
3461 }
3462 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
3463 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003464 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003465 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
3466 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00003467
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003468 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003469}
3470
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003471CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003472 : CGObjCCommonMac(cgm),
3473 ObjCTypes(cgm)
3474{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003475 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003476 ObjCABI = 2;
3477}
3478
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003479/* *** */
3480
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003481ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
3482: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003483{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003484 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3485 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003486
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003487 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003488 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003489 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003490 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003491 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
3492
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003493 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00003494 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003495 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003496
3497 // FIXME: It would be nice to unify this with the opaque type, so
3498 // that the IR comes out a bit cleaner.
3499 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
3500 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003501
3502 // I'm not sure I like this. The implicit coordination is a bit
3503 // gross. We should solve this in a reasonable fashion because this
3504 // is a pretty common task (match some runtime data structure with
3505 // an LLVM data structure).
3506
3507 // FIXME: This is leaked.
3508 // FIXME: Merge with rewriter code?
3509
3510 // struct _objc_super {
3511 // id self;
3512 // Class cls;
3513 // }
3514 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3515 SourceLocation(),
3516 &Ctx.Idents.get("_objc_super"));
Douglas Gregor6ab35242009-04-09 21:40:53 +00003517 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3518 Ctx.getObjCIdType(), 0, false));
3519 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3520 Ctx.getObjCClassType(), 0, false));
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003521 RD->completeDefinition(Ctx);
3522
3523 SuperCTy = Ctx.getTagDeclType(RD);
3524 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
3525
3526 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003527 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
3528
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003529 // struct _prop_t {
3530 // char *name;
3531 // char *attributes;
3532 // }
Chris Lattner1c02f862009-04-22 02:53:24 +00003533 PropertyTy = llvm::StructType::get(Int8PtrTy, Int8PtrTy, NULL);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003534 CGM.getModule().addTypeName("struct._prop_t",
3535 PropertyTy);
3536
3537 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003538 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003539 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003540 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003541 // }
3542 PropertyListTy = llvm::StructType::get(IntTy,
3543 IntTy,
3544 llvm::ArrayType::get(PropertyTy, 0),
3545 NULL);
3546 CGM.getModule().addTypeName("struct._prop_list_t",
3547 PropertyListTy);
3548 // struct _prop_list_t *
3549 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
3550
3551 // struct _objc_method {
3552 // SEL _cmd;
3553 // char *method_type;
3554 // char *_imp;
3555 // }
3556 MethodTy = llvm::StructType::get(SelectorPtrTy,
3557 Int8PtrTy,
3558 Int8PtrTy,
3559 NULL);
3560 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003561
3562 // struct _objc_cache *
3563 CacheTy = llvm::OpaqueType::get();
3564 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
3565 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003566}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003567
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003568ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
3569 : ObjCCommonTypesHelper(cgm)
3570{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003571 // struct _objc_method_description {
3572 // SEL name;
3573 // char *types;
3574 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003575 MethodDescriptionTy =
3576 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003577 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003578 NULL);
3579 CGM.getModule().addTypeName("struct._objc_method_description",
3580 MethodDescriptionTy);
3581
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003582 // struct _objc_method_description_list {
3583 // int count;
3584 // struct _objc_method_description[1];
3585 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003586 MethodDescriptionListTy =
3587 llvm::StructType::get(IntTy,
3588 llvm::ArrayType::get(MethodDescriptionTy, 0),
3589 NULL);
3590 CGM.getModule().addTypeName("struct._objc_method_description_list",
3591 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003592
3593 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003594 MethodDescriptionListPtrTy =
3595 llvm::PointerType::getUnqual(MethodDescriptionListTy);
3596
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003597 // Protocol description structures
3598
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003599 // struct _objc_protocol_extension {
3600 // uint32_t size; // sizeof(struct _objc_protocol_extension)
3601 // struct _objc_method_description_list *optional_instance_methods;
3602 // struct _objc_method_description_list *optional_class_methods;
3603 // struct _objc_property_list *instance_properties;
3604 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003605 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003606 llvm::StructType::get(IntTy,
3607 MethodDescriptionListPtrTy,
3608 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003609 PropertyListPtrTy,
3610 NULL);
3611 CGM.getModule().addTypeName("struct._objc_protocol_extension",
3612 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003613
3614 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003615 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
3616
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003617 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003618
3619 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
3620 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3621
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003622 const llvm::Type *T =
3623 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
3624 LongTy,
3625 llvm::ArrayType::get(ProtocolTyHolder, 0),
3626 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003627 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
3628
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003629 // struct _objc_protocol {
3630 // struct _objc_protocol_extension *isa;
3631 // char *protocol_name;
3632 // struct _objc_protocol **_objc_protocol_list;
3633 // struct _objc_method_description_list *instance_methods;
3634 // struct _objc_method_description_list *class_methods;
3635 // }
3636 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003637 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003638 llvm::PointerType::getUnqual(ProtocolListTyHolder),
3639 MethodDescriptionListPtrTy,
3640 MethodDescriptionListPtrTy,
3641 NULL);
3642 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
3643
3644 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
3645 CGM.getModule().addTypeName("struct._objc_protocol_list",
3646 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003647 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003648 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
3649
3650 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003651 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003652 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003653
3654 // Class description structures
3655
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003656 // struct _objc_ivar {
3657 // char *ivar_name;
3658 // char *ivar_type;
3659 // int ivar_offset;
3660 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003661 IvarTy = llvm::StructType::get(Int8PtrTy,
3662 Int8PtrTy,
3663 IntTy,
3664 NULL);
3665 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
3666
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003667 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003668 IvarListTy = llvm::OpaqueType::get();
3669 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
3670 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
3671
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003672 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003673 MethodListTy = llvm::OpaqueType::get();
3674 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
3675 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
3676
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003677 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003678 ClassExtensionTy =
3679 llvm::StructType::get(IntTy,
3680 Int8PtrTy,
3681 PropertyListPtrTy,
3682 NULL);
3683 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
3684 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
3685
3686 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3687
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003688 // struct _objc_class {
3689 // Class isa;
3690 // Class super_class;
3691 // char *name;
3692 // long version;
3693 // long info;
3694 // long instance_size;
3695 // struct _objc_ivar_list *ivars;
3696 // struct _objc_method_list *methods;
3697 // struct _objc_cache *cache;
3698 // struct _objc_protocol_list *protocols;
3699 // char *ivar_layout;
3700 // struct _objc_class_ext *ext;
3701 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003702 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3703 llvm::PointerType::getUnqual(ClassTyHolder),
3704 Int8PtrTy,
3705 LongTy,
3706 LongTy,
3707 LongTy,
3708 IvarListPtrTy,
3709 MethodListPtrTy,
3710 CachePtrTy,
3711 ProtocolListPtrTy,
3712 Int8PtrTy,
3713 ClassExtensionPtrTy,
3714 NULL);
3715 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
3716
3717 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
3718 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
3719 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
3720
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003721 // struct _objc_category {
3722 // char *category_name;
3723 // char *class_name;
3724 // struct _objc_method_list *instance_method;
3725 // struct _objc_method_list *class_method;
3726 // uint32_t size; // sizeof(struct _objc_category)
3727 // struct _objc_property_list *instance_properties;// category's @property
3728 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003729 CategoryTy = llvm::StructType::get(Int8PtrTy,
3730 Int8PtrTy,
3731 MethodListPtrTy,
3732 MethodListPtrTy,
3733 ProtocolListPtrTy,
3734 IntTy,
3735 PropertyListPtrTy,
3736 NULL);
3737 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
3738
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003739 // Global metadata structures
3740
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003741 // struct _objc_symtab {
3742 // long sel_ref_cnt;
3743 // SEL *refs;
3744 // short cls_def_cnt;
3745 // short cat_def_cnt;
3746 // char *defs[cls_def_cnt + cat_def_cnt];
3747 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003748 SymtabTy = llvm::StructType::get(LongTy,
3749 SelectorPtrTy,
3750 ShortTy,
3751 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003752 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003753 NULL);
3754 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
3755 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
3756
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003757 // struct _objc_module {
3758 // long version;
3759 // long size; // sizeof(struct _objc_module)
3760 // char *name;
3761 // struct _objc_symtab* symtab;
3762 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003763 ModuleTy =
3764 llvm::StructType::get(LongTy,
3765 LongTy,
3766 Int8PtrTy,
3767 SymtabPtrTy,
3768 NULL);
3769 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003770
Anders Carlsson2abd89c2008-08-31 04:05:03 +00003771
Anders Carlsson124526b2008-09-09 10:10:21 +00003772 // FIXME: This is the size of the setjmp buffer and should be
3773 // target specific. 18 is what's used on 32-bit X86.
3774 uint64_t SetJmpBufferSize = 18;
3775
3776 // Exceptions
3777 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00003778 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00003779
3780 ExceptionDataTy =
3781 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
3782 SetJmpBufferSize),
3783 StackPtrTy, NULL);
3784 CGM.getModule().addTypeName("struct._objc_exception_data",
3785 ExceptionDataTy);
3786
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003787}
3788
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003789ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003790: ObjCCommonTypesHelper(cgm)
3791{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003792 // struct _method_list_t {
3793 // uint32_t entsize; // sizeof(struct _objc_method)
3794 // uint32_t method_count;
3795 // struct _objc_method method_list[method_count];
3796 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003797 MethodListnfABITy = llvm::StructType::get(IntTy,
3798 IntTy,
3799 llvm::ArrayType::get(MethodTy, 0),
3800 NULL);
3801 CGM.getModule().addTypeName("struct.__method_list_t",
3802 MethodListnfABITy);
3803 // struct method_list_t *
3804 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003805
3806 // struct _protocol_t {
3807 // id isa; // NULL
3808 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003809 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003810 // const struct method_list_t * const instance_methods;
3811 // const struct method_list_t * const class_methods;
3812 // const struct method_list_t *optionalInstanceMethods;
3813 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003814 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003815 // const uint32_t size; // sizeof(struct _protocol_t)
3816 // const uint32_t flags; // = 0
3817 // }
3818
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003819 // Holder for struct _protocol_list_t *
3820 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3821
3822 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3823 Int8PtrTy,
3824 llvm::PointerType::getUnqual(
3825 ProtocolListTyHolder),
3826 MethodListnfABIPtrTy,
3827 MethodListnfABIPtrTy,
3828 MethodListnfABIPtrTy,
3829 MethodListnfABIPtrTy,
3830 PropertyListPtrTy,
3831 IntTy,
3832 IntTy,
3833 NULL);
3834 CGM.getModule().addTypeName("struct._protocol_t",
3835 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003836
3837 // struct _protocol_t*
3838 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003839
Fariborz Jahanianda320092009-01-29 19:24:30 +00003840 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003841 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00003842 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003843 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003844 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3845 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003846 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003847 NULL);
3848 CGM.getModule().addTypeName("struct._objc_protocol_list",
3849 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003850 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3851 ProtocolListnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003852
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003853 // struct _objc_protocol_list*
3854 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003855
3856 // struct _ivar_t {
3857 // unsigned long int *offset; // pointer to ivar offset location
3858 // char *name;
3859 // char *type;
3860 // uint32_t alignment;
3861 // uint32_t size;
3862 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003863 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3864 Int8PtrTy,
3865 Int8PtrTy,
3866 IntTy,
3867 IntTy,
3868 NULL);
3869 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3870
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003871 // struct _ivar_list_t {
3872 // uint32 entsize; // sizeof(struct _ivar_t)
3873 // uint32 count;
3874 // struct _iver_t list[count];
3875 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003876 IvarListnfABITy = llvm::StructType::get(IntTy,
3877 IntTy,
3878 llvm::ArrayType::get(
3879 IvarnfABITy, 0),
3880 NULL);
3881 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3882
3883 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003884
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003885 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003886 // uint32_t const flags;
3887 // uint32_t const instanceStart;
3888 // uint32_t const instanceSize;
3889 // uint32_t const reserved; // only when building for 64bit targets
3890 // const uint8_t * const ivarLayout;
3891 // const char *const name;
3892 // const struct _method_list_t * const baseMethods;
3893 // const struct _objc_protocol_list *const baseProtocols;
3894 // const struct _ivar_list_t *const ivars;
3895 // const uint8_t * const weakIvarLayout;
3896 // const struct _prop_list_t * const properties;
3897 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003898
3899 // FIXME. Add 'reserved' field in 64bit abi mode!
3900 ClassRonfABITy = llvm::StructType::get(IntTy,
3901 IntTy,
3902 IntTy,
3903 Int8PtrTy,
3904 Int8PtrTy,
3905 MethodListnfABIPtrTy,
3906 ProtocolListnfABIPtrTy,
3907 IvarListnfABIPtrTy,
3908 Int8PtrTy,
3909 PropertyListPtrTy,
3910 NULL);
3911 CGM.getModule().addTypeName("struct._class_ro_t",
3912 ClassRonfABITy);
3913
3914 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3915 std::vector<const llvm::Type*> Params;
3916 Params.push_back(ObjectPtrTy);
3917 Params.push_back(SelectorPtrTy);
3918 ImpnfABITy = llvm::PointerType::getUnqual(
3919 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3920
3921 // struct _class_t {
3922 // struct _class_t *isa;
3923 // struct _class_t * const superclass;
3924 // void *cache;
3925 // IMP *vtable;
3926 // struct class_ro_t *ro;
3927 // }
3928
3929 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3930 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3931 llvm::PointerType::getUnqual(ClassTyHolder),
3932 CachePtrTy,
3933 llvm::PointerType::getUnqual(ImpnfABITy),
3934 llvm::PointerType::getUnqual(
3935 ClassRonfABITy),
3936 NULL);
3937 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3938
3939 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3940 ClassnfABITy);
3941
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003942 // LLVM for struct _class_t *
3943 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3944
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003945 // struct _category_t {
3946 // const char * const name;
3947 // struct _class_t *const cls;
3948 // const struct _method_list_t * const instance_methods;
3949 // const struct _method_list_t * const class_methods;
3950 // const struct _protocol_list_t * const protocols;
3951 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003952 // }
3953 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003954 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003955 MethodListnfABIPtrTy,
3956 MethodListnfABIPtrTy,
3957 ProtocolListnfABIPtrTy,
3958 PropertyListPtrTy,
3959 NULL);
3960 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003961
3962 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003963 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3964 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003965
3966 // MessageRefTy - LLVM for:
3967 // struct _message_ref_t {
3968 // IMP messenger;
3969 // SEL name;
3970 // };
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003971
3972 // First the clang type for struct _message_ref_t
3973 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3974 SourceLocation(),
3975 &Ctx.Idents.get("_message_ref_t"));
Douglas Gregor6ab35242009-04-09 21:40:53 +00003976 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3977 Ctx.VoidPtrTy, 0, false));
3978 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3979 Ctx.getObjCSelType(), 0, false));
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003980 RD->completeDefinition(Ctx);
3981
3982 MessageRefCTy = Ctx.getTagDeclType(RD);
3983 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
3984 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003985
3986 // MessageRefPtrTy - LLVM for struct _message_ref_t*
3987 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
3988
3989 // SuperMessageRefTy - LLVM for:
3990 // struct _super_message_ref_t {
3991 // SUPER_IMP messenger;
3992 // SEL name;
3993 // };
3994 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy,
3995 SelectorPtrTy,
3996 NULL);
3997 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
3998
3999 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
4000 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4001
Daniel Dunbare588b992009-03-01 04:46:24 +00004002
4003 // struct objc_typeinfo {
4004 // const void** vtable; // objc_ehtype_vtable + 2
4005 // const char* name; // c++ typeinfo string
4006 // Class cls;
4007 // };
4008 EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy),
4009 Int8PtrTy,
4010 ClassnfABIPtrTy,
4011 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004012 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Daniel Dunbare588b992009-03-01 04:46:24 +00004013 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004014}
4015
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004016llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
4017 FinishNonFragileABIModule();
4018
4019 return NULL;
4020}
4021
4022void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4023 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004024
4025 // Build list of all implemented classe addresses in array
4026 // L_OBJC_LABEL_CLASS_$.
4027 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
4028 // list of 'nonlazy' implementations (defined as those with a +load{}
4029 // method!!).
4030 unsigned NumClasses = DefinedClasses.size();
4031 if (NumClasses) {
4032 std::vector<llvm::Constant*> Symbols(NumClasses);
4033 for (unsigned i=0; i<NumClasses; i++)
4034 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
4035 ObjCTypes.Int8PtrTy);
4036 llvm::Constant* Init =
4037 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
4038 NumClasses),
4039 Symbols);
4040
4041 llvm::GlobalVariable *GV =
4042 new llvm::GlobalVariable(Init->getType(), false,
4043 llvm::GlobalValue::InternalLinkage,
4044 Init,
4045 "\01L_OBJC_LABEL_CLASS_$",
4046 &CGM.getModule());
Daniel Dunbar58a29122009-03-09 22:18:41 +00004047 GV->setAlignment(8);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004048 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
4049 UsedGlobals.push_back(GV);
4050 }
4051
4052 // Build list of all implemented category addresses in array
4053 // L_OBJC_LABEL_CATEGORY_$.
4054 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
4055 // list of 'nonlazy' category implementations (defined as those with a +load{}
4056 // method!!).
4057 unsigned NumCategory = DefinedCategories.size();
4058 if (NumCategory) {
4059 std::vector<llvm::Constant*> Symbols(NumCategory);
4060 for (unsigned i=0; i<NumCategory; i++)
4061 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
4062 ObjCTypes.Int8PtrTy);
4063 llvm::Constant* Init =
4064 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
4065 NumCategory),
4066 Symbols);
4067
4068 llvm::GlobalVariable *GV =
4069 new llvm::GlobalVariable(Init->getType(), false,
4070 llvm::GlobalValue::InternalLinkage,
4071 Init,
4072 "\01L_OBJC_LABEL_CATEGORY_$",
4073 &CGM.getModule());
Daniel Dunbar58a29122009-03-09 22:18:41 +00004074 GV->setAlignment(8);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004075 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
4076 UsedGlobals.push_back(GV);
4077 }
4078
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004079 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
4080 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
4081 std::vector<llvm::Constant*> Values(2);
4082 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
Fariborz Jahanian067986e2009-02-24 21:08:09 +00004083 unsigned int flags = 0;
Fariborz Jahanian66a5c2c2009-02-24 23:34:44 +00004084 // FIXME: Fix and continue?
4085 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
4086 flags |= eImageInfo_GarbageCollected;
4087 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
4088 flags |= eImageInfo_GCOnly;
Fariborz Jahanian067986e2009-02-24 21:08:09 +00004089 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004090 llvm::Constant* Init = llvm::ConstantArray::get(
4091 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
4092 Values);
4093 llvm::GlobalVariable *IMGV =
4094 new llvm::GlobalVariable(Init->getType(), false,
4095 llvm::GlobalValue::InternalLinkage,
4096 Init,
4097 "\01L_OBJC_IMAGE_INFO",
4098 &CGM.getModule());
4099 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
Daniel Dunbar325f7582009-04-23 08:03:21 +00004100 IMGV->setConstant(true);
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004101 UsedGlobals.push_back(IMGV);
4102
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004103 std::vector<llvm::Constant*> Used;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004104
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004105 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
4106 e = UsedGlobals.end(); i != e; ++i) {
4107 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
4108 }
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004109
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004110 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
4111 llvm::GlobalValue *GV =
4112 new llvm::GlobalVariable(AT, false,
4113 llvm::GlobalValue::AppendingLinkage,
4114 llvm::ConstantArray::get(AT, Used),
4115 "llvm.used",
4116 &CGM.getModule());
4117
4118 GV->setSection("llvm.metadata");
4119
4120}
4121
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004122// Metadata flags
4123enum MetaDataDlags {
4124 CLS = 0x0,
4125 CLS_META = 0x1,
4126 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004127 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004128 CLS_EXCEPTION = 0x20
4129};
4130/// BuildClassRoTInitializer - generate meta-data for:
4131/// struct _class_ro_t {
4132/// uint32_t const flags;
4133/// uint32_t const instanceStart;
4134/// uint32_t const instanceSize;
4135/// uint32_t const reserved; // only when building for 64bit targets
4136/// const uint8_t * const ivarLayout;
4137/// const char *const name;
4138/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004139/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004140/// const struct _ivar_list_t *const ivars;
4141/// const uint8_t * const weakIvarLayout;
4142/// const struct _prop_list_t * const properties;
4143/// }
4144///
4145llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
4146 unsigned flags,
4147 unsigned InstanceStart,
4148 unsigned InstanceSize,
4149 const ObjCImplementationDecl *ID) {
4150 std::string ClassName = ID->getNameAsString();
4151 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
4152 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4153 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4154 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
4155 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004156 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4157 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004158 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004159 // const struct _method_list_t * const baseMethods;
4160 std::vector<llvm::Constant*> Methods;
4161 std::string MethodListName("\01l_OBJC_$_");
4162 if (flags & CLS_META) {
4163 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004164 for (ObjCImplementationDecl::classmeth_iterator
4165 i = ID->classmeth_begin(CGM.getContext()),
4166 e = ID->classmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004167 // Class methods should always be defined.
4168 Methods.push_back(GetMethodConstant(*i));
4169 }
4170 } else {
4171 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004172 for (ObjCImplementationDecl::instmeth_iterator
4173 i = ID->instmeth_begin(CGM.getContext()),
4174 e = ID->instmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004175 // Instance methods should always be defined.
4176 Methods.push_back(GetMethodConstant(*i));
4177 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00004178 for (ObjCImplementationDecl::propimpl_iterator
4179 i = ID->propimpl_begin(CGM.getContext()),
4180 e = ID->propimpl_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004181 ObjCPropertyImplDecl *PID = *i;
4182
4183 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4184 ObjCPropertyDecl *PD = PID->getPropertyDecl();
4185
4186 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4187 if (llvm::Constant *C = GetMethodConstant(MD))
4188 Methods.push_back(C);
4189 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4190 if (llvm::Constant *C = GetMethodConstant(MD))
4191 Methods.push_back(C);
4192 }
4193 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004194 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004195 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004196 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004197
4198 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4199 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
4200 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
4201 + OID->getNameAsString(),
4202 OID->protocol_begin(),
4203 OID->protocol_end());
4204
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004205 if (flags & CLS_META)
4206 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4207 else
4208 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004209 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4210 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004211 if (flags & CLS_META)
4212 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
4213 else
4214 Values[ 9] =
4215 EmitPropertyList(
4216 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
4217 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004218 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
4219 Values);
4220 llvm::GlobalVariable *CLASS_RO_GV =
4221 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
4222 llvm::GlobalValue::InternalLinkage,
4223 Init,
4224 (flags & CLS_META) ?
4225 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4226 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
4227 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004228 CLASS_RO_GV->setAlignment(
4229 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004230 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004231 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004232
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004233}
4234
4235/// BuildClassMetaData - This routine defines that to-level meta-data
4236/// for the given ClassName for:
4237/// struct _class_t {
4238/// struct _class_t *isa;
4239/// struct _class_t * const superclass;
4240/// void *cache;
4241/// IMP *vtable;
4242/// struct class_ro_t *ro;
4243/// }
4244///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004245llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
4246 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004247 llvm::Constant *IsAGV,
4248 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004249 llvm::Constant *ClassRoGV,
4250 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004251 std::vector<llvm::Constant*> Values(5);
4252 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004253 Values[1] = SuperClassGV
4254 ? SuperClassGV
4255 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004256 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4257 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4258 Values[4] = ClassRoGV; // &CLASS_RO_GV
4259 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
4260 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004261 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4262 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004263 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004264 GV->setAlignment(
4265 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004266 if (HiddenVisibility)
4267 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004268 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004269}
4270
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004271void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCInterfaceDecl *OID,
4272 uint32_t &InstanceStart,
4273 uint32_t &InstanceSize) {
Daniel Dunbar97776872009-04-22 07:32:20 +00004274 // Find first and last (non-padding) ivars in this interface.
4275
4276 // FIXME: Use iterator.
4277 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
4278 GetNamedIvarList(OID, OIvars);
4279
4280 if (OIvars.empty()) {
4281 InstanceStart = InstanceSize = 0;
4282 return;
Daniel Dunbard4ae6c02009-04-22 04:39:47 +00004283 }
Daniel Dunbar97776872009-04-22 07:32:20 +00004284
4285 const ObjCIvarDecl *First = OIvars.front();
4286 const ObjCIvarDecl *Last = OIvars.back();
4287
4288 InstanceStart = ComputeIvarBaseOffset(CGM, OID, First);
4289 const llvm::Type *FieldTy =
4290 CGM.getTypes().ConvertTypeForMem(Last->getType());
4291 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004292// FIXME. This breaks compatibility with llvm-gcc-4.2 (but makes it compatible
4293// with gcc-4.2). We postpone this for now.
4294#if 0
4295 if (Last->isBitField()) {
4296 Expr *BitWidth = Last->getBitWidth();
4297 uint64_t BitFieldSize =
Eli Friedman9a901bb2009-04-26 19:19:15 +00004298 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004299 Size = (BitFieldSize / 8) + ((BitFieldSize % 8) != 0);
4300 }
4301#endif
Daniel Dunbar97776872009-04-22 07:32:20 +00004302 InstanceSize = ComputeIvarBaseOffset(CGM, OID, Last) + Size;
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004303}
4304
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004305void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4306 std::string ClassName = ID->getNameAsString();
4307 if (!ObjCEmptyCacheVar) {
4308 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004309 ObjCTypes.CacheTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004310 false,
4311 llvm::GlobalValue::ExternalLinkage,
4312 0,
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004313 "_objc_empty_cache",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004314 &CGM.getModule());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004315
4316 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004317 ObjCTypes.ImpnfABITy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004318 false,
4319 llvm::GlobalValue::ExternalLinkage,
4320 0,
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004321 "_objc_empty_vtable",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004322 &CGM.getModule());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004323 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004324 assert(ID->getClassInterface() &&
4325 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004326 // FIXME: Is this correct (that meta class size is never computed)?
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004327 uint32_t InstanceStart =
4328 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
4329 uint32_t InstanceSize = InstanceStart;
4330 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004331 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4332 std::string ObjCClassName(getClassSymbolPrefix());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004333
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004334 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004335
Daniel Dunbar04d40782009-04-14 06:00:08 +00004336 bool classIsHidden =
4337 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004338 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004339 flags |= OBJC2_CLS_HIDDEN;
4340 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004341 // class is root
4342 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004343 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004344 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004345 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004346 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004347 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4348 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4349 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004350 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004351 // work on super class metadata symbol.
4352 std::string SuperClassName =
4353 ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004354 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004355 }
4356 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4357 InstanceStart,
4358 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004359 std::string TClassName = ObjCMetaClassName + ClassName;
4360 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004361 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4362 classIsHidden);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004363
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004364 // Metadata for the class
4365 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004366 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004367 flags |= OBJC2_CLS_HIDDEN;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004368
4369 if (hasObjCExceptionAttribute(ID->getClassInterface()))
4370 flags |= CLS_EXCEPTION;
4371
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004372 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004373 flags |= CLS_ROOT;
4374 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00004375 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004376 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004377 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004378 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004379 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004380 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004381 GetClassSizeInfo(ID->getClassInterface(), InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004382 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004383 InstanceStart,
4384 InstanceSize,
4385 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004386
4387 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004388 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004389 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4390 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004391 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004392
4393 // Force the definition of the EHType if necessary.
4394 if (flags & CLS_EXCEPTION)
4395 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004396}
4397
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004398/// GenerateProtocolRef - This routine is called to generate code for
4399/// a protocol reference expression; as in:
4400/// @code
4401/// @protocol(Proto1);
4402/// @endcode
4403/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4404/// which will hold address of the protocol meta-data.
4405///
4406llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
4407 const ObjCProtocolDecl *PD) {
4408
Fariborz Jahanian960cd062009-04-10 18:47:34 +00004409 // This routine is called for @protocol only. So, we must build definition
4410 // of protocol's meta-data (not a reference to it!)
4411 //
4412 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004413 ObjCTypes.ExternalProtocolPtrTy);
4414
4415 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
4416 ProtocolName += PD->getNameAsCString();
4417
4418 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
4419 if (PTGV)
4420 return Builder.CreateLoad(PTGV, false, "tmp");
4421 PTGV = new llvm::GlobalVariable(
4422 Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00004423 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004424 Init,
4425 ProtocolName,
4426 &CGM.getModule());
4427 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
4428 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4429 UsedGlobals.push_back(PTGV);
4430 return Builder.CreateLoad(PTGV, false, "tmp");
4431}
4432
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004433/// GenerateCategory - Build metadata for a category implementation.
4434/// struct _category_t {
4435/// const char * const name;
4436/// struct _class_t *const cls;
4437/// const struct _method_list_t * const instance_methods;
4438/// const struct _method_list_t * const class_methods;
4439/// const struct _protocol_list_t * const protocols;
4440/// const struct _prop_list_t * const properties;
4441/// }
4442///
4443void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
4444{
4445 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004446 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
4447 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004448 "_$_" + OCD->getNameAsString());
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004449 std::string ExtClassName(getClassSymbolPrefix() +
4450 Interface->getNameAsString());
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004451
4452 std::vector<llvm::Constant*> Values(6);
4453 Values[0] = GetClassName(OCD->getIdentifier());
4454 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004455 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004456 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004457 std::vector<llvm::Constant*> Methods;
4458 std::string MethodListName(Prefix);
4459 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
4460 "_$_" + OCD->getNameAsString();
4461
Douglas Gregor653f1b12009-04-23 01:02:12 +00004462 for (ObjCCategoryImplDecl::instmeth_iterator
4463 i = OCD->instmeth_begin(CGM.getContext()),
4464 e = OCD->instmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004465 // Instance methods should always be defined.
4466 Methods.push_back(GetMethodConstant(*i));
4467 }
4468
4469 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004470 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004471 Methods);
4472
4473 MethodListName = Prefix;
4474 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
4475 OCD->getNameAsString();
4476 Methods.clear();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004477 for (ObjCCategoryImplDecl::classmeth_iterator
4478 i = OCD->classmeth_begin(CGM.getContext()),
4479 e = OCD->classmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004480 // Class methods should always be defined.
4481 Methods.push_back(GetMethodConstant(*i));
4482 }
4483
4484 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004485 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004486 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004487 const ObjCCategoryDecl *Category =
4488 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004489 if (Category) {
4490 std::string ExtName(Interface->getNameAsString() + "_$_" +
4491 OCD->getNameAsString());
4492 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
4493 + Interface->getNameAsString() + "_$_"
4494 + Category->getNameAsString(),
4495 Category->protocol_begin(),
4496 Category->protocol_end());
4497 Values[5] =
4498 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
4499 OCD, Category, ObjCTypes);
4500 }
4501 else {
4502 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4503 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
4504 }
4505
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004506 llvm::Constant *Init =
4507 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
4508 Values);
4509 llvm::GlobalVariable *GCATV
4510 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
4511 false,
4512 llvm::GlobalValue::InternalLinkage,
4513 Init,
4514 ExtCatName,
4515 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004516 GCATV->setAlignment(
4517 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004518 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004519 UsedGlobals.push_back(GCATV);
4520 DefinedCategories.push_back(GCATV);
4521}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004522
4523/// GetMethodConstant - Return a struct objc_method constant for the
4524/// given method if it has been defined. The result is null if the
4525/// method has not been defined. The return value has type MethodPtrTy.
4526llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
4527 const ObjCMethodDecl *MD) {
4528 // FIXME: Use DenseMap::lookup
4529 llvm::Function *Fn = MethodDefinitions[MD];
4530 if (!Fn)
4531 return 0;
4532
4533 std::vector<llvm::Constant*> Method(3);
4534 Method[0] =
4535 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4536 ObjCTypes.SelectorPtrTy);
4537 Method[1] = GetMethodVarType(MD);
4538 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
4539 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
4540}
4541
4542/// EmitMethodList - Build meta-data for method declarations
4543/// struct _method_list_t {
4544/// uint32_t entsize; // sizeof(struct _objc_method)
4545/// uint32_t method_count;
4546/// struct _objc_method method_list[method_count];
4547/// }
4548///
4549llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
4550 const std::string &Name,
4551 const char *Section,
4552 const ConstantVector &Methods) {
4553 // Return null for empty list.
4554 if (Methods.empty())
4555 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
4556
4557 std::vector<llvm::Constant*> Values(3);
4558 // sizeof(struct _objc_method)
4559 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
4560 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4561 // method_count
4562 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
4563 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
4564 Methods.size());
4565 Values[2] = llvm::ConstantArray::get(AT, Methods);
4566 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4567
4568 llvm::GlobalVariable *GV =
4569 new llvm::GlobalVariable(Init->getType(), false,
4570 llvm::GlobalValue::InternalLinkage,
4571 Init,
4572 Name,
4573 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004574 GV->setAlignment(
4575 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004576 GV->setSection(Section);
4577 UsedGlobals.push_back(GV);
4578 return llvm::ConstantExpr::getBitCast(GV,
4579 ObjCTypes.MethodListnfABIPtrTy);
4580}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004581
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004582/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
4583/// the given ivar.
4584///
4585llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004586 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004587 const ObjCIvarDecl *Ivar) {
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00004588 std::string Name = "OBJC_IVAR_$_" +
Douglas Gregor6ab35242009-04-09 21:40:53 +00004589 getInterfaceDeclForIvar(ID, Ivar, CGM.getContext())->getNameAsString() +
4590 '.' + Ivar->getNameAsString();
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004591 llvm::GlobalVariable *IvarOffsetGV =
4592 CGM.getModule().getGlobalVariable(Name);
4593 if (!IvarOffsetGV)
4594 IvarOffsetGV =
4595 new llvm::GlobalVariable(ObjCTypes.LongTy,
4596 false,
4597 llvm::GlobalValue::ExternalLinkage,
4598 0,
4599 Name,
4600 &CGM.getModule());
4601 return IvarOffsetGV;
4602}
4603
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004604llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004605 const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004606 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004607 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00004608 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
4609 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
4610 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004611 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004612 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00004613
4614 // FIXME: This matches gcc, but shouldn't the visibility be set on
4615 // the use as well (i.e., in ObjCIvarOffsetVariable).
4616 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
4617 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
4618 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004619 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00004620 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00004621 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004622 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004623 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004624}
4625
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004626/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00004627/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004628/// IvarListnfABIPtrTy.
4629/// struct _ivar_t {
4630/// unsigned long int *offset; // pointer to ivar offset location
4631/// char *name;
4632/// char *type;
4633/// uint32_t alignment;
4634/// uint32_t size;
4635/// }
4636/// struct _ivar_list_t {
4637/// uint32 entsize; // sizeof(struct _ivar_t)
4638/// uint32 count;
4639/// struct _iver_t list[count];
4640/// }
4641///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004642
4643void CGObjCCommonMac::GetNamedIvarList(const ObjCInterfaceDecl *OID,
4644 llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const {
4645 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
4646 E = OID->ivar_end(); I != E; ++I) {
4647 // Ignore unnamed bit-fields.
4648 if (!(*I)->getDeclName())
4649 continue;
4650
4651 Res.push_back(*I);
4652 }
4653
4654 for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(CGM.getContext()),
4655 E = OID->prop_end(CGM.getContext()); I != E; ++I)
4656 if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl())
4657 Res.push_back(IV);
4658}
4659
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004660llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
4661 const ObjCImplementationDecl *ID) {
4662
4663 std::vector<llvm::Constant*> Ivars, Ivar(5);
4664
4665 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4666 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
4667
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004668 // FIXME. Consolidate this with similar code in GenerateClass.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00004669
Daniel Dunbar91636d62009-04-20 00:33:43 +00004670 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00004671 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004672 GetNamedIvarList(OID, OIvars);
Fariborz Jahanian99eee362009-04-01 19:37:34 +00004673
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004674 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
4675 ObjCIvarDecl *IVD = OIvars[i];
Daniel Dunbar3eec8aa2009-04-20 05:53:40 +00004676 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar97776872009-04-22 07:32:20 +00004677 ComputeIvarBaseOffset(CGM, OID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004678 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
4679 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004680 const llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004681 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004682 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
4683 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004684 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004685 Align = llvm::Log2_32(Align);
4686 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00004687 // NOTE. Size of a bitfield does not match gcc's, because of the
4688 // way bitfields are treated special in each. But I am told that
4689 // 'size' for bitfield ivars is ignored by the runtime so it does
4690 // not matter. If it matters, there is enough info to get the
4691 // bitfield right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004692 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4693 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
4694 }
4695 // Return null for empty list.
4696 if (Ivars.empty())
4697 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4698 std::vector<llvm::Constant*> Values(3);
4699 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
4700 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4701 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
4702 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
4703 Ivars.size());
4704 Values[2] = llvm::ConstantArray::get(AT, Ivars);
4705 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4706 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4707 llvm::GlobalVariable *GV =
4708 new llvm::GlobalVariable(Init->getType(), false,
4709 llvm::GlobalValue::InternalLinkage,
4710 Init,
4711 Prefix + OID->getNameAsString(),
4712 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004713 GV->setAlignment(
4714 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004715 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004716
4717 UsedGlobals.push_back(GV);
4718 return llvm::ConstantExpr::getBitCast(GV,
4719 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004720}
4721
4722llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4723 const ObjCProtocolDecl *PD) {
4724 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4725
4726 if (!Entry) {
4727 // We use the initializer as a marker of whether this is a forward
4728 // reference or not. At module finalization we add the empty
4729 // contents for protocols which were referenced but never defined.
4730 Entry =
4731 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
4732 llvm::GlobalValue::ExternalLinkage,
4733 0,
4734 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
4735 &CGM.getModule());
4736 Entry->setSection("__DATA,__datacoal_nt,coalesced");
4737 UsedGlobals.push_back(Entry);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004738 }
4739
4740 return Entry;
4741}
4742
4743/// GetOrEmitProtocol - Generate the protocol meta-data:
4744/// @code
4745/// struct _protocol_t {
4746/// id isa; // NULL
4747/// const char * const protocol_name;
4748/// const struct _protocol_list_t * protocol_list; // super protocols
4749/// const struct method_list_t * const instance_methods;
4750/// const struct method_list_t * const class_methods;
4751/// const struct method_list_t *optionalInstanceMethods;
4752/// const struct method_list_t *optionalClassMethods;
4753/// const struct _prop_list_t * properties;
4754/// const uint32_t size; // sizeof(struct _protocol_t)
4755/// const uint32_t flags; // = 0
4756/// }
4757/// @endcode
4758///
4759
4760llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4761 const ObjCProtocolDecl *PD) {
4762 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4763
4764 // Early exit if a defining object has already been generated.
4765 if (Entry && Entry->hasInitializer())
4766 return Entry;
4767
4768 const char *ProtocolName = PD->getNameAsCString();
4769
4770 // Construct method lists.
4771 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4772 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00004773 for (ObjCProtocolDecl::instmeth_iterator
4774 i = PD->instmeth_begin(CGM.getContext()),
4775 e = PD->instmeth_end(CGM.getContext());
4776 i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004777 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004778 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004779 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4780 OptInstanceMethods.push_back(C);
4781 } else {
4782 InstanceMethods.push_back(C);
4783 }
4784 }
4785
Douglas Gregor6ab35242009-04-09 21:40:53 +00004786 for (ObjCProtocolDecl::classmeth_iterator
4787 i = PD->classmeth_begin(CGM.getContext()),
4788 e = PD->classmeth_end(CGM.getContext());
4789 i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004790 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004791 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004792 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4793 OptClassMethods.push_back(C);
4794 } else {
4795 ClassMethods.push_back(C);
4796 }
4797 }
4798
4799 std::vector<llvm::Constant*> Values(10);
4800 // isa is NULL
4801 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
4802 Values[1] = GetClassName(PD->getIdentifier());
4803 Values[2] = EmitProtocolList(
4804 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4805 PD->protocol_begin(),
4806 PD->protocol_end());
4807
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004808 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004809 + PD->getNameAsString(),
4810 "__DATA, __objc_const",
4811 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004812 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004813 + PD->getNameAsString(),
4814 "__DATA, __objc_const",
4815 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004816 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004817 + PD->getNameAsString(),
4818 "__DATA, __objc_const",
4819 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004820 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004821 + PD->getNameAsString(),
4822 "__DATA, __objc_const",
4823 OptClassMethods);
4824 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4825 0, PD, ObjCTypes);
4826 uint32_t Size =
4827 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
4828 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4829 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
4830 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
4831 Values);
4832
4833 if (Entry) {
4834 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00004835 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004836 Entry->setInitializer(Init);
4837 } else {
4838 Entry =
4839 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004840 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004841 Init,
4842 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
4843 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004844 Entry->setAlignment(
4845 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004846 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004847 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004848 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
4849
4850 // Use this protocol meta-data to build protocol list table in section
4851 // __DATA, __objc_protolist
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004852 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004853 ObjCTypes.ProtocolnfABIPtrTy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004854 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004855 Entry,
4856 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
4857 +ProtocolName,
4858 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004859 PTGV->setAlignment(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004860 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00004861 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004862 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4863 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004864 return Entry;
4865}
4866
4867/// EmitProtocolList - Generate protocol list meta-data:
4868/// @code
4869/// struct _protocol_list_t {
4870/// long protocol_count; // Note, this is 32/64 bit
4871/// struct _protocol_t[protocol_count];
4872/// }
4873/// @endcode
4874///
4875llvm::Constant *
4876CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4877 ObjCProtocolDecl::protocol_iterator begin,
4878 ObjCProtocolDecl::protocol_iterator end) {
4879 std::vector<llvm::Constant*> ProtocolRefs;
4880
Fariborz Jahanianda320092009-01-29 19:24:30 +00004881 // Just return null for empty protocol lists
Daniel Dunbar948e2582009-02-15 07:36:20 +00004882 if (begin == end)
Fariborz Jahanianda320092009-01-29 19:24:30 +00004883 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4884
Daniel Dunbar948e2582009-02-15 07:36:20 +00004885 // FIXME: We shouldn't need to do this lookup here, should we?
Fariborz Jahanianda320092009-01-29 19:24:30 +00004886 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4887 if (GV)
Daniel Dunbar948e2582009-02-15 07:36:20 +00004888 return llvm::ConstantExpr::getBitCast(GV,
4889 ObjCTypes.ProtocolListnfABIPtrTy);
4890
4891 for (; begin != end; ++begin)
4892 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4893
Fariborz Jahanianda320092009-01-29 19:24:30 +00004894 // This list is null terminated.
4895 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004896 ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004897
4898 std::vector<llvm::Constant*> Values(2);
4899 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4900 Values[1] =
Daniel Dunbar948e2582009-02-15 07:36:20 +00004901 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004902 ProtocolRefs.size()),
4903 ProtocolRefs);
4904
4905 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4906 GV = new llvm::GlobalVariable(Init->getType(), false,
4907 llvm::GlobalValue::InternalLinkage,
4908 Init,
4909 Name,
4910 &CGM.getModule());
4911 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004912 GV->setAlignment(
4913 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004914 UsedGlobals.push_back(GV);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004915 return llvm::ConstantExpr::getBitCast(GV,
4916 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004917}
4918
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004919/// GetMethodDescriptionConstant - This routine build following meta-data:
4920/// struct _objc_method {
4921/// SEL _cmd;
4922/// char *method_type;
4923/// char *_imp;
4924/// }
4925
4926llvm::Constant *
4927CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4928 std::vector<llvm::Constant*> Desc(3);
4929 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4930 ObjCTypes.SelectorPtrTy);
4931 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004932 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004933 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4934 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4935}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004936
4937/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4938/// This code gen. amounts to generating code for:
4939/// @code
4940/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4941/// @encode
4942///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004943LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004944 CodeGen::CodeGenFunction &CGF,
4945 QualType ObjectTy,
4946 llvm::Value *BaseValue,
4947 const ObjCIvarDecl *Ivar,
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004948 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00004949 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00004950 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
4951 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004952}
4953
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004954llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
4955 CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00004956 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004957 const ObjCIvarDecl *Ivar) {
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00004958 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),
4959 false, "ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004960}
4961
Fariborz Jahanian46551122009-02-04 00:22:57 +00004962CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4963 CodeGen::CodeGenFunction &CGF,
4964 QualType ResultType,
4965 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004966 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00004967 QualType Arg0Ty,
4968 bool IsSuper,
4969 const CallArgList &CallArgs) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004970 // FIXME. Even though IsSuper is passes. This function doese not
4971 // handle calls to 'super' receivers.
4972 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004973 llvm::Value *Arg0 = Receiver;
4974 if (!IsSuper)
4975 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004976
4977 // Find the message function name.
Fariborz Jahanianef163782009-02-05 01:13:09 +00004978 // FIXME. This is too much work to get the ABI-specific result type
4979 // needed to find the message name.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004980 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4981 llvm::SmallVector<QualType, 16>());
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00004982 llvm::Constant *Fn = 0;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004983 std::string Name("\01l_");
4984 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004985#if 0
4986 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004987 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00004988 Fn = ObjCTypes.getMessageSendIdStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004989 // FIXME. Is there a better way of getting these names.
4990 // They are available in RuntimeFunctions vector pair.
4991 Name += "objc_msgSendId_stret_fixup";
4992 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004993 else
4994#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004995 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00004996 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004997 Name += "objc_msgSendSuper2_stret_fixup";
4998 }
4999 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005000 {
Chris Lattner1c02f862009-04-22 02:53:24 +00005001 Fn = ObjCTypes.getMessageSendStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005002 Name += "objc_msgSend_stret_fixup";
5003 }
5004 }
Fariborz Jahanian5b2bad02009-04-30 16:31:11 +00005005 else if (!IsSuper && ResultType->isFloatingType()) {
5006 if (const BuiltinType *BT = ResultType->getAsBuiltinType()) {
5007 BuiltinType::Kind k = BT->getKind();
5008 if (k == BuiltinType::LongDouble) {
5009 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5010 Name += "objc_msgSend_fpret_fixup";
5011 }
5012 else {
5013 Fn = ObjCTypes.getMessageSendFixupFn();
5014 Name += "objc_msgSend_fixup";
5015 }
5016 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005017 }
5018 else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005019#if 0
5020// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005021 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005022 Fn = ObjCTypes.getMessageSendIdFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005023 Name += "objc_msgSendId_fixup";
5024 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005025 else
5026#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005027 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005028 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005029 Name += "objc_msgSendSuper2_fixup";
5030 }
5031 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005032 {
Chris Lattner1c02f862009-04-22 02:53:24 +00005033 Fn = ObjCTypes.getMessageSendFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005034 Name += "objc_msgSend_fixup";
5035 }
5036 }
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005037 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005038 Name += '_';
5039 std::string SelName(Sel.getAsString());
5040 // Replace all ':' in selector name with '_' ouch!
5041 for(unsigned i = 0; i < SelName.size(); i++)
5042 if (SelName[i] == ':')
5043 SelName[i] = '_';
5044 Name += SelName;
5045 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5046 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005047 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005048 std::vector<llvm::Constant*> Values(2);
5049 Values[0] = Fn;
5050 Values[1] = GetMethodVarName(Sel);
5051 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
5052 GV = new llvm::GlobalVariable(Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005053 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005054 Init,
5055 Name,
5056 &CGM.getModule());
5057 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005058 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005059 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005060 }
5061 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005062
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005063 CallArgList ActualArgs;
5064 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
5065 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
5066 ObjCTypes.MessageRefCPtrTy));
5067 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005068 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
5069 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5070 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00005071 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005072 Callee = CGF.Builder.CreateBitCast(Callee,
5073 llvm::PointerType::getUnqual(FTy));
5074 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005075}
5076
5077/// Generate code for a message send expression in the nonfragile abi.
5078CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
5079 CodeGen::CodeGenFunction &CGF,
5080 QualType ResultType,
5081 Selector Sel,
5082 llvm::Value *Receiver,
5083 bool IsClassMessage,
5084 const CallArgList &CallArgs) {
Fariborz Jahanian46551122009-02-04 00:22:57 +00005085 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005086 Receiver, CGF.getContext().getObjCIdType(),
Fariborz Jahanian46551122009-02-04 00:22:57 +00005087 false, CallArgs);
5088}
5089
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005090llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005091CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005092 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5093
Daniel Dunbardfff2302009-03-02 05:18:14 +00005094 if (!GV) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005095 GV = new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
5096 llvm::GlobalValue::ExternalLinkage,
5097 0, Name, &CGM.getModule());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005098 }
5099
5100 return GV;
5101}
5102
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005103llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005104 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005105 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
5106
5107 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005108 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005109 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005110 Entry =
5111 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5112 llvm::GlobalValue::InternalLinkage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005113 ClassGV,
Daniel Dunbar11394522009-04-18 08:51:00 +00005114 "\01L_OBJC_CLASSLIST_REFERENCES_$_",
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005115 &CGM.getModule());
5116 Entry->setAlignment(
5117 CGM.getTargetData().getPrefTypeAlignment(
5118 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005119 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
5120 UsedGlobals.push_back(Entry);
5121 }
5122
5123 return Builder.CreateLoad(Entry, false, "tmp");
5124}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005125
Daniel Dunbar11394522009-04-18 08:51:00 +00005126llvm::Value *
5127CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
5128 const ObjCInterfaceDecl *ID) {
5129 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
5130
5131 if (!Entry) {
5132 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5133 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5134 Entry =
5135 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5136 llvm::GlobalValue::InternalLinkage,
5137 ClassGV,
5138 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
5139 &CGM.getModule());
5140 Entry->setAlignment(
5141 CGM.getTargetData().getPrefTypeAlignment(
5142 ObjCTypes.ClassnfABIPtrTy));
5143 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005144 UsedGlobals.push_back(Entry);
5145 }
5146
5147 return Builder.CreateLoad(Entry, false, "tmp");
5148}
5149
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005150/// EmitMetaClassRef - Return a Value * of the address of _class_t
5151/// meta-data
5152///
5153llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5154 const ObjCInterfaceDecl *ID) {
5155 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5156 if (Entry)
5157 return Builder.CreateLoad(Entry, false, "tmp");
5158
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005159 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005160 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005161 Entry =
5162 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5163 llvm::GlobalValue::InternalLinkage,
5164 MetaClassGV,
5165 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
5166 &CGM.getModule());
5167 Entry->setAlignment(
5168 CGM.getTargetData().getPrefTypeAlignment(
5169 ObjCTypes.ClassnfABIPtrTy));
5170
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005171 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005172 UsedGlobals.push_back(Entry);
5173
5174 return Builder.CreateLoad(Entry, false, "tmp");
5175}
5176
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005177/// GetClass - Return a reference to the class for the given interface
5178/// decl.
5179llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5180 const ObjCInterfaceDecl *ID) {
5181 return EmitClassRef(Builder, ID);
5182}
5183
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005184/// Generates a message send where the super is the receiver. This is
5185/// a message send to self with special delivery semantics indicating
5186/// which class's method should be called.
5187CodeGen::RValue
5188CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
5189 QualType ResultType,
5190 Selector Sel,
5191 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005192 bool isCategoryImpl,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005193 llvm::Value *Receiver,
5194 bool IsClassMessage,
5195 const CodeGen::CallArgList &CallArgs) {
5196 // ...
5197 // Create and init a super structure; this is a (receiver, class)
5198 // pair we will pass to objc_msgSendSuper.
5199 llvm::Value *ObjCSuper =
5200 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
5201
5202 llvm::Value *ReceiverAsObject =
5203 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5204 CGF.Builder.CreateStore(ReceiverAsObject,
5205 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
5206
5207 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005208 llvm::Value *Target;
5209 if (IsClassMessage) {
5210 if (isCategoryImpl) {
5211 // Message sent to "super' in a class method defined in
5212 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005213 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005214 Target = CGF.Builder.CreateStructGEP(Target, 0);
5215 Target = CGF.Builder.CreateLoad(Target);
5216 }
5217 else
5218 Target = EmitMetaClassRef(CGF.Builder, Class);
5219 }
5220 else
Daniel Dunbar11394522009-04-18 08:51:00 +00005221 Target = EmitSuperClassRef(CGF.Builder, Class);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005222
5223 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
5224 // and ObjCTypes types.
5225 const llvm::Type *ClassTy =
5226 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5227 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5228 CGF.Builder.CreateStore(Target,
5229 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
5230
5231 return EmitMessageSend(CGF, ResultType, Sel,
5232 ObjCSuper, ObjCTypes.SuperPtrCTy,
5233 true, CallArgs);
5234}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005235
5236llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
5237 Selector Sel) {
5238 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
5239
5240 if (!Entry) {
5241 llvm::Constant *Casted =
5242 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5243 ObjCTypes.SelectorPtrTy);
5244 Entry =
5245 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
5246 llvm::GlobalValue::InternalLinkage,
5247 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
5248 &CGM.getModule());
5249 Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip");
5250 UsedGlobals.push_back(Entry);
5251 }
5252
5253 return Builder.CreateLoad(Entry, false, "tmp");
5254}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005255/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
5256/// objc_assign_ivar (id src, id *dst)
5257///
5258void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
5259 llvm::Value *src, llvm::Value *dst)
5260{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005261 const llvm::Type * SrcTy = src->getType();
5262 if (!isa<llvm::PointerType>(SrcTy)) {
5263 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5264 assert(Size <= 8 && "does not support size > 8");
5265 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5266 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005267 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5268 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005269 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5270 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005271 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005272 src, dst, "assignivar");
5273 return;
5274}
5275
5276/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5277/// objc_assign_strongCast (id src, id *dst)
5278///
5279void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
5280 CodeGen::CodeGenFunction &CGF,
5281 llvm::Value *src, llvm::Value *dst)
5282{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005283 const llvm::Type * SrcTy = src->getType();
5284 if (!isa<llvm::PointerType>(SrcTy)) {
5285 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5286 assert(Size <= 8 && "does not support size > 8");
5287 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5288 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005289 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5290 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005291 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5292 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005293 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005294 src, dst, "weakassign");
5295 return;
5296}
5297
5298/// EmitObjCWeakRead - Code gen for loading value of a __weak
5299/// object: objc_read_weak (id *src)
5300///
5301llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
5302 CodeGen::CodeGenFunction &CGF,
5303 llvm::Value *AddrWeakObj)
5304{
Eli Friedman8339b352009-03-07 03:57:15 +00005305 const llvm::Type* DestTy =
5306 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005307 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005308 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005309 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005310 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005311 return read_weak;
5312}
5313
5314/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5315/// objc_assign_weak (id src, id *dst)
5316///
5317void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
5318 llvm::Value *src, llvm::Value *dst)
5319{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005320 const llvm::Type * SrcTy = src->getType();
5321 if (!isa<llvm::PointerType>(SrcTy)) {
5322 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5323 assert(Size <= 8 && "does not support size > 8");
5324 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5325 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005326 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5327 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005328 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5329 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005330 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005331 src, dst, "weakassign");
5332 return;
5333}
5334
5335/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5336/// objc_assign_global (id src, id *dst)
5337///
5338void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
5339 llvm::Value *src, llvm::Value *dst)
5340{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005341 const llvm::Type * SrcTy = src->getType();
5342 if (!isa<llvm::PointerType>(SrcTy)) {
5343 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5344 assert(Size <= 8 && "does not support size > 8");
5345 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5346 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005347 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5348 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005349 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5350 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005351 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005352 src, dst, "globalassign");
5353 return;
5354}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005355
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005356void
5357CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5358 const Stmt &S) {
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005359 bool isTry = isa<ObjCAtTryStmt>(S);
5360 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
5361 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005362 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005363 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005364 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005365 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
5366
5367 // For @synchronized, call objc_sync_enter(sync.expr). The
5368 // evaluation of the expression must occur before we enter the
5369 // @synchronized. We can safely avoid a temp here because jumps into
5370 // @synchronized are illegal & this will dominate uses.
5371 llvm::Value *SyncArg = 0;
5372 if (!isTry) {
5373 SyncArg =
5374 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
5375 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005376 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005377 }
5378
5379 // Push an EH context entry, used for handling rethrows and jumps
5380 // through finally.
5381 CGF.PushCleanupBlock(FinallyBlock);
5382
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005383 CGF.setInvokeDest(TryHandler);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005384
5385 CGF.EmitBlock(TryBlock);
5386 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
5387 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
5388 CGF.EmitBranchThroughCleanup(FinallyEnd);
5389
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005390 // Emit the exception handler.
5391
5392 CGF.EmitBlock(TryHandler);
5393
5394 llvm::Value *llvm_eh_exception =
5395 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
5396 llvm::Value *llvm_eh_selector_i64 =
5397 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64);
5398 llvm::Value *llvm_eh_typeid_for_i64 =
5399 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64);
5400 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5401 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow");
5402
5403 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
5404 SelectorArgs.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005405 SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005406
5407 // Construct the lists of (type, catch body) to handle.
Daniel Dunbarede8de92009-03-06 00:01:21 +00005408 llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005409 bool HasCatchAll = false;
5410 if (isTry) {
5411 if (const ObjCAtCatchStmt* CatchStmt =
5412 cast<ObjCAtTryStmt>(S).getCatchStmts()) {
5413 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005414 const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Steve Naroff7ba138a2009-03-03 19:52:17 +00005415 Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody()));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005416
5417 // catch(...) always matches.
Steve Naroff7ba138a2009-03-03 19:52:17 +00005418 if (!CatchDecl) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005419 // Use i8* null here to signal this is a catch all, not a cleanup.
5420 llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
5421 SelectorArgs.push_back(Null);
5422 HasCatchAll = true;
5423 break;
5424 }
5425
Daniel Dunbarede8de92009-03-06 00:01:21 +00005426 if (CGF.getContext().isObjCIdType(CatchDecl->getType()) ||
5427 CatchDecl->getType()->isObjCQualifiedIdType()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005428 llvm::Value *IDEHType =
5429 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5430 if (!IDEHType)
5431 IDEHType =
5432 new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5433 llvm::GlobalValue::ExternalLinkage,
5434 0, "OBJC_EHTYPE_id", &CGM.getModule());
5435 SelectorArgs.push_back(IDEHType);
5436 HasCatchAll = true;
5437 break;
5438 }
5439
5440 // All other types should be Objective-C interface pointer types.
Daniel Dunbarede8de92009-03-06 00:01:21 +00005441 const PointerType *PT = CatchDecl->getType()->getAsPointerType();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005442 assert(PT && "Invalid @catch type.");
5443 const ObjCInterfaceType *IT =
5444 PT->getPointeeType()->getAsObjCInterfaceType();
5445 assert(IT && "Invalid @catch type.");
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005446 llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005447 SelectorArgs.push_back(EHType);
5448 }
5449 }
5450 }
5451
5452 // We use a cleanup unless there was already a catch all.
5453 if (!HasCatchAll) {
5454 SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
Daniel Dunbarede8de92009-03-06 00:01:21 +00005455 Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005456 }
5457
5458 llvm::Value *Selector =
5459 CGF.Builder.CreateCall(llvm_eh_selector_i64,
5460 SelectorArgs.begin(), SelectorArgs.end(),
5461 "selector");
5462 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005463 const ParmVarDecl *CatchParam = Handlers[i].first;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005464 const Stmt *CatchBody = Handlers[i].second;
5465
5466 llvm::BasicBlock *Next = 0;
5467
5468 // The last handler always matches.
5469 if (i + 1 != e) {
5470 assert(CatchParam && "Only last handler can be a catch all.");
5471
5472 llvm::BasicBlock *Match = CGF.createBasicBlock("match");
5473 Next = CGF.createBasicBlock("catch.next");
5474 llvm::Value *Id =
5475 CGF.Builder.CreateCall(llvm_eh_typeid_for_i64,
5476 CGF.Builder.CreateBitCast(SelectorArgs[i+2],
5477 ObjCTypes.Int8PtrTy));
5478 CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id),
5479 Match, Next);
5480
5481 CGF.EmitBlock(Match);
5482 }
5483
5484 if (CatchBody) {
5485 llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end");
5486 llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler");
5487
5488 // Cleanups must call objc_end_catch.
5489 //
5490 // FIXME: It seems incorrect for objc_begin_catch to be inside
5491 // this context, but this matches gcc.
5492 CGF.PushCleanupBlock(MatchEnd);
5493 CGF.setInvokeDest(MatchHandler);
5494
5495 llvm::Value *ExcObject =
Chris Lattner8a569112009-04-22 02:15:23 +00005496 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005497
5498 // Bind the catch parameter if it exists.
5499 if (CatchParam) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005500 ExcObject =
5501 CGF.Builder.CreateBitCast(ExcObject,
5502 CGF.ConvertType(CatchParam->getType()));
5503 // CatchParam is a ParmVarDecl because of the grammar
5504 // construction used to handle this, but for codegen purposes
5505 // we treat this as a local decl.
5506 CGF.EmitLocalBlockVarDecl(*CatchParam);
5507 CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005508 }
5509
5510 CGF.ObjCEHValueStack.push_back(ExcObject);
5511 CGF.EmitStmt(CatchBody);
5512 CGF.ObjCEHValueStack.pop_back();
5513
5514 CGF.EmitBranchThroughCleanup(FinallyEnd);
5515
5516 CGF.EmitBlock(MatchHandler);
5517
5518 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5519 // We are required to emit this call to satisfy LLVM, even
5520 // though we don't use the result.
5521 llvm::SmallVector<llvm::Value*, 8> Args;
5522 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005523 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005524 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
5525 0));
5526 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5527 CGF.Builder.CreateStore(Exc, RethrowPtr);
5528 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5529
5530 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5531
5532 CGF.EmitBlock(MatchEnd);
5533
5534 // Unfortunately, we also have to generate another EH frame here
5535 // in case this throws.
5536 llvm::BasicBlock *MatchEndHandler =
5537 CGF.createBasicBlock("match.end.handler");
5538 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattner8a569112009-04-22 02:15:23 +00005539 CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005540 Cont, MatchEndHandler,
5541 Args.begin(), Args.begin());
5542
5543 CGF.EmitBlock(Cont);
5544 if (Info.SwitchBlock)
5545 CGF.EmitBlock(Info.SwitchBlock);
5546 if (Info.EndBlock)
5547 CGF.EmitBlock(Info.EndBlock);
5548
5549 CGF.EmitBlock(MatchEndHandler);
5550 Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5551 // We are required to emit this call to satisfy LLVM, even
5552 // though we don't use the result.
5553 Args.clear();
5554 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005555 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005556 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
5557 0));
5558 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5559 CGF.Builder.CreateStore(Exc, RethrowPtr);
5560 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5561
5562 if (Next)
5563 CGF.EmitBlock(Next);
5564 } else {
5565 assert(!Next && "catchup should be last handler.");
5566
5567 CGF.Builder.CreateStore(Exc, RethrowPtr);
5568 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5569 }
5570 }
5571
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005572 // Pop the cleanup entry, the @finally is outside this cleanup
5573 // scope.
5574 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5575 CGF.setInvokeDest(PrevLandingPad);
5576
5577 CGF.EmitBlock(FinallyBlock);
5578
5579 if (isTry) {
5580 if (const ObjCAtFinallyStmt* FinallyStmt =
5581 cast<ObjCAtTryStmt>(S).getFinallyStmt())
5582 CGF.EmitStmt(FinallyStmt->getFinallyBody());
5583 } else {
5584 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
5585 // @synchronized.
Chris Lattnerbbccd612009-04-22 02:38:11 +00005586 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005587 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005588
5589 if (Info.SwitchBlock)
5590 CGF.EmitBlock(Info.SwitchBlock);
5591 if (Info.EndBlock)
5592 CGF.EmitBlock(Info.EndBlock);
5593
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005594 // Branch around the rethrow code.
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005595 CGF.EmitBranch(FinallyEnd);
5596
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005597 CGF.EmitBlock(FinallyRethrow);
Chris Lattner8a569112009-04-22 02:15:23 +00005598 CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005599 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005600 CGF.Builder.CreateUnreachable();
5601
5602 CGF.EmitBlock(FinallyEnd);
5603}
5604
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005605/// EmitThrowStmt - Generate code for a throw statement.
5606void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
5607 const ObjCAtThrowStmt &S) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005608 llvm::Value *Exception;
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005609 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005610 Exception = CGF.EmitScalarExpr(ThrowExpr);
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005611 } else {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005612 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
5613 "Unexpected rethrow outside @catch block.");
5614 Exception = CGF.ObjCEHValueStack.back();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005615 }
5616
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005617 llvm::Value *ExceptionAsObject =
5618 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
5619 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
5620 if (InvokeDest) {
5621 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattnerbbccd612009-04-22 02:38:11 +00005622 CGF.Builder.CreateInvoke(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005623 Cont, InvokeDest,
5624 &ExceptionAsObject, &ExceptionAsObject + 1);
5625 CGF.EmitBlock(Cont);
5626 } else
Chris Lattnerbbccd612009-04-22 02:38:11 +00005627 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005628 CGF.Builder.CreateUnreachable();
5629
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005630 // Clear the insertion point to indicate we are in unreachable code.
5631 CGF.Builder.ClearInsertionPoint();
5632}
Daniel Dunbare588b992009-03-01 04:46:24 +00005633
5634llvm::Value *
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005635CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
5636 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00005637 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00005638
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005639 // If we don't need a definition, return the entry if found or check
5640 // if we use an external reference.
5641 if (!ForDefinition) {
5642 if (Entry)
5643 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00005644
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005645 // If this type (or a super class) has the __objc_exception__
5646 // attribute, emit an external reference.
5647 if (hasObjCExceptionAttribute(ID))
5648 return Entry =
5649 new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5650 llvm::GlobalValue::ExternalLinkage,
5651 0,
5652 (std::string("OBJC_EHTYPE_$_") +
5653 ID->getIdentifier()->getName()),
5654 &CGM.getModule());
5655 }
5656
5657 // Otherwise we need to either make a new entry or fill in the
5658 // initializer.
5659 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005660 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00005661 std::string VTableName = "objc_ehtype_vtable";
5662 llvm::GlobalVariable *VTableGV =
5663 CGM.getModule().getGlobalVariable(VTableName);
5664 if (!VTableGV)
5665 VTableGV = new llvm::GlobalVariable(ObjCTypes.Int8PtrTy, false,
5666 llvm::GlobalValue::ExternalLinkage,
5667 0, VTableName, &CGM.getModule());
5668
5669 llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2);
5670
5671 std::vector<llvm::Constant*> Values(3);
5672 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
5673 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005674 Values[2] = GetClassGlobal(ClassName);
Daniel Dunbare588b992009-03-01 04:46:24 +00005675 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
5676
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005677 if (Entry) {
5678 Entry->setInitializer(Init);
5679 } else {
5680 Entry = new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5681 llvm::GlobalValue::WeakAnyLinkage,
5682 Init,
5683 (std::string("OBJC_EHTYPE_$_") +
5684 ID->getIdentifier()->getName()),
5685 &CGM.getModule());
5686 }
5687
Daniel Dunbar04d40782009-04-14 06:00:08 +00005688 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005689 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005690 Entry->setAlignment(8);
5691
5692 if (ForDefinition) {
5693 Entry->setSection("__DATA,__objc_const");
5694 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
5695 } else {
5696 Entry->setSection("__DATA,__datacoal_nt,coalesced");
5697 }
Daniel Dunbare588b992009-03-01 04:46:24 +00005698
5699 return Entry;
5700}
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005701
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005702/* *** */
5703
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005704CodeGen::CGObjCRuntime *
5705CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00005706 return new CGObjCMac(CGM);
5707}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005708
5709CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005710CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005711 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005712}