blob: 1ab4bf105666a044acc190f7e469f2d133a21979 [file] [log] [blame]
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001//===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides Objective-C code generation targetting the Apple runtime.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000015
16#include "CodeGenModule.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000017#include "CodeGenFunction.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000019#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000020#include "clang/AST/DeclObjC.h"
Daniel Dunbar2bebbf02009-05-03 10:46:44 +000021#include "clang/AST/RecordLayout.h"
Chris Lattner16f00492009-04-26 01:32:48 +000022#include "clang/AST/StmtObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000023#include "clang/Basic/LangOptions.h"
24
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +000025#include "llvm/Intrinsics.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000026#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000027#include "llvm/ADT/DenseSet.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000028#include "llvm/Target/TargetData.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000029#include <sstream>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000030
31using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000032using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000033
Daniel Dunbar97776872009-04-22 07:32:20 +000034// Common CGObjCRuntime functions, these don't belong here, but they
35// don't belong in CGObjCRuntime either so we will live with it for
36// now.
37
Daniel Dunbar2bebbf02009-05-03 10:46:44 +000038static const llvm::StructType *
39GetConcreteClassStruct(CodeGen::CodeGenModule &CGM,
40 const ObjCInterfaceDecl *OID) {
Daniel Dunbar84ad77a2009-04-22 09:39:34 +000041 assert(!OID->isForwardDecl() && "Invalid interface decl!");
Daniel Dunbar412f59b2009-04-22 10:28:39 +000042 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
43 return cast<llvm::StructType>(CGM.getTypes().ConvertTagDeclType(RD));
Daniel Dunbar84ad77a2009-04-22 09:39:34 +000044}
45
Daniel Dunbara2435782009-04-22 12:00:04 +000046
47/// LookupFieldDeclForIvar - looks up a field decl in the laid out
48/// storage which matches this 'ivar'.
49///
50static const FieldDecl *LookupFieldDeclForIvar(ASTContext &Context,
51 const ObjCInterfaceDecl *OID,
Daniel Dunbara80a0f62009-04-22 17:43:55 +000052 const ObjCIvarDecl *OIVD,
53 const ObjCInterfaceDecl *&Found) {
Daniel Dunbara2435782009-04-22 12:00:04 +000054 assert(!OID->isForwardDecl() && "Invalid interface decl!");
55 const RecordDecl *RecordForDecl = Context.addRecordToClass(OID);
56 assert(RecordForDecl && "lookupFieldDeclForIvar no storage for class");
57 DeclContext::lookup_const_result Lookup =
58 RecordForDecl->lookup(Context, OIVD->getDeclName());
Daniel Dunbara80a0f62009-04-22 17:43:55 +000059
60 if (Lookup.first != Lookup.second) {
61 Found = OID;
62 return cast<FieldDecl>(*Lookup.first);
63 }
64
65 // If lookup failed, try the superclass.
66 //
67 // FIXME: This is slow, we shouldn't need to do this.
68 const ObjCInterfaceDecl *Super = OID->getSuperClass();
Daniel Dunbar2bebbf02009-05-03 10:46:44 +000069 assert(Super && "field decl not found!");
Daniel Dunbara80a0f62009-04-22 17:43:55 +000070 return LookupFieldDeclForIvar(Context, Super, OIVD, Found);
Daniel Dunbara2435782009-04-22 12:00:04 +000071}
72
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000073static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM,
74 const ObjCInterfaceDecl *OID,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000075 const ObjCImplementationDecl *ID,
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000076 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +000077 assert(!OID->isForwardDecl() && "Invalid interface decl!");
Daniel Dunbara80a0f62009-04-22 17:43:55 +000078 const ObjCInterfaceDecl *Container;
79 const FieldDecl *Field =
80 LookupFieldDeclForIvar(CGM.getContext(), OID, Ivar, Container);
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000081 const llvm::StructType *STy =
Daniel Dunbar2bebbf02009-05-03 10:46:44 +000082 GetConcreteClassStruct(CGM, Container);
Daniel Dunbar97776872009-04-22 07:32:20 +000083 const llvm::StructLayout *Layout =
Daniel Dunbar84ad77a2009-04-22 09:39:34 +000084 CGM.getTargetData().getStructLayout(STy);
Daniel Dunbar97776872009-04-22 07:32:20 +000085 if (!Field->isBitField())
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000086 return Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field)) * 8;
Daniel Dunbar97776872009-04-22 07:32:20 +000087
88 // FIXME. Must be a better way of getting a bitfield base offset.
89 CodeGenTypes::BitFieldInfo BFI = CGM.getTypes().getBitFieldInfo(Field);
90 // FIXME: The "field no" for bitfields is something completely
91 // different; it is the offset in multiples of the base type size!
92 uint64_t Offset = CGM.getTypes().getLLVMFieldNo(Field);
93 const llvm::Type *Ty =
94 CGM.getTypes().ConvertTypeForMemRecursive(Field->getType());
95 Offset *= CGM.getTypes().getTargetData().getTypePaddedSizeInBits(Ty);
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000096 return Offset + BFI.Begin;
97}
98
99uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
100 const ObjCInterfaceDecl *OID,
101 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +0000102 return LookupFieldBitOffset(CGM, OID, 0, Ivar) / 8;
103}
104
105uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
106 const ObjCImplementationDecl *OID,
107 const ObjCIvarDecl *Ivar) {
108 return LookupFieldBitOffset(CGM, OID->getClassInterface(), OID, Ivar) / 8;
Daniel Dunbar97776872009-04-22 07:32:20 +0000109}
110
111LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
112 const ObjCInterfaceDecl *OID,
113 llvm::Value *BaseValue,
114 const ObjCIvarDecl *Ivar,
115 unsigned CVRQualifiers,
116 llvm::Value *Offset) {
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000117 // Compute (type*) ( (char *) BaseValue + Offset)
Daniel Dunbar97776872009-04-22 07:32:20 +0000118 llvm::Type *I8Ptr = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000119 QualType IvarTy = Ivar->getType();
120 const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy);
Daniel Dunbar97776872009-04-22 07:32:20 +0000121 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr);
Daniel Dunbar97776872009-04-22 07:32:20 +0000122 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000123 V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar97776872009-04-22 07:32:20 +0000124
125 if (Ivar->isBitField()) {
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +0000126 // We need to compute the bit offset for the bit-field, the offset
127 // is to the byte. Note, there is a subtle invariant here: we can
128 // only call this routine on non-sythesized ivars but we may be
129 // called for synthesized ivars. However, a synthesized ivar can
130 // never be a bit-field so this is safe.
131 uint64_t BitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar) % 8;
132
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000133 uint64_t BitFieldSize =
134 Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue();
135 return LValue::MakeBitfield(V, BitOffset, BitFieldSize,
Daniel Dunbare38df862009-05-03 07:52:00 +0000136 IvarTy->isSignedIntegerType(),
137 IvarTy.getCVRQualifiers()|CVRQualifiers);
Daniel Dunbar97776872009-04-22 07:32:20 +0000138 }
139
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000140 LValue LV = LValue::MakeAddr(V, IvarTy.getCVRQualifiers()|CVRQualifiers,
141 CGF.CGM.getContext().getObjCGCAttrKind(IvarTy));
Daniel Dunbar97776872009-04-22 07:32:20 +0000142 LValue::SetObjCIvar(LV, true);
143 return LV;
144}
145
146///
147
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000148namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000149
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000150 typedef std::vector<llvm::Constant*> ConstantVector;
151
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000152 // FIXME: We should find a nicer way to make the labels for
153 // metadata, string concatenation is lame.
154
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000155class ObjCCommonTypesHelper {
156protected:
157 CodeGen::CodeGenModule &CGM;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000158
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000159public:
Fariborz Jahanian0a855d02009-03-23 19:10:40 +0000160 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000161 const llvm::Type *Int8PtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000162
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000163 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
164 const llvm::Type *ObjectPtrTy;
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000165
166 /// PtrObjectPtrTy - LLVM type for id *
167 const llvm::Type *PtrObjectPtrTy;
168
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000169 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000170 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000171 /// ProtocolPtrTy - LLVM type for external protocol handles
172 /// (typeof(Protocol))
173 const llvm::Type *ExternalProtocolPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000174
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000175 // SuperCTy - clang type for struct objc_super.
176 QualType SuperCTy;
177 // SuperPtrCTy - clang type for struct objc_super *.
178 QualType SuperPtrCTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000179
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000180 /// SuperTy - LLVM type for struct objc_super.
181 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000182 /// SuperPtrTy - LLVM type for struct objc_super *.
183 const llvm::Type *SuperPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000184
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000185 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
186 /// in GCC parlance).
187 const llvm::StructType *PropertyTy;
188
189 /// PropertyListTy - LLVM type for struct objc_property_list
190 /// (_prop_list_t in GCC parlance).
191 const llvm::StructType *PropertyListTy;
192 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
193 const llvm::Type *PropertyListPtrTy;
194
195 // MethodTy - LLVM type for struct objc_method.
196 const llvm::StructType *MethodTy;
197
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000198 /// CacheTy - LLVM type for struct objc_cache.
199 const llvm::Type *CacheTy;
200 /// CachePtrTy - LLVM type for struct objc_cache *.
201 const llvm::Type *CachePtrTy;
202
Chris Lattner72db6c32009-04-22 02:44:54 +0000203 llvm::Constant *getGetPropertyFn() {
204 CodeGen::CodeGenTypes &Types = CGM.getTypes();
205 ASTContext &Ctx = CGM.getContext();
206 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
207 llvm::SmallVector<QualType,16> Params;
208 QualType IdType = Ctx.getObjCIdType();
209 QualType SelType = Ctx.getObjCSelType();
210 Params.push_back(IdType);
211 Params.push_back(SelType);
212 Params.push_back(Ctx.LongTy);
213 Params.push_back(Ctx.BoolTy);
214 const llvm::FunctionType *FTy =
215 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
216 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
217 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000218
Chris Lattner72db6c32009-04-22 02:44:54 +0000219 llvm::Constant *getSetPropertyFn() {
220 CodeGen::CodeGenTypes &Types = CGM.getTypes();
221 ASTContext &Ctx = CGM.getContext();
222 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
223 llvm::SmallVector<QualType,16> Params;
224 QualType IdType = Ctx.getObjCIdType();
225 QualType SelType = Ctx.getObjCSelType();
226 Params.push_back(IdType);
227 Params.push_back(SelType);
228 Params.push_back(Ctx.LongTy);
229 Params.push_back(IdType);
230 Params.push_back(Ctx.BoolTy);
231 Params.push_back(Ctx.BoolTy);
232 const llvm::FunctionType *FTy =
233 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
234 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
235 }
236
237 llvm::Constant *getEnumerationMutationFn() {
238 // void objc_enumerationMutation (id)
239 std::vector<const llvm::Type*> Args;
240 Args.push_back(ObjectPtrTy);
241 llvm::FunctionType *FTy =
242 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
243 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
244 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000245
246 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000247 llvm::Constant *getGcReadWeakFn() {
248 // id objc_read_weak (id *)
249 std::vector<const llvm::Type*> Args;
250 Args.push_back(ObjectPtrTy->getPointerTo());
251 llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false);
252 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
253 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000254
255 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000256 llvm::Constant *getGcAssignWeakFn() {
257 // id objc_assign_weak (id, id *)
258 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
259 Args.push_back(ObjectPtrTy->getPointerTo());
260 llvm::FunctionType *FTy =
261 llvm::FunctionType::get(ObjectPtrTy, Args, false);
262 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
263 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000264
265 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000266 llvm::Constant *getGcAssignGlobalFn() {
267 // id objc_assign_global(id, id *)
268 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
269 Args.push_back(ObjectPtrTy->getPointerTo());
270 llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false);
271 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
272 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000273
274 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000275 llvm::Constant *getGcAssignIvarFn() {
276 // id objc_assign_ivar(id, id *)
277 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
278 Args.push_back(ObjectPtrTy->getPointerTo());
279 llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false);
280 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
281 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000282
283 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000284 llvm::Constant *getGcAssignStrongCastFn() {
285 // id objc_assign_global(id, id *)
286 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
287 Args.push_back(ObjectPtrTy->getPointerTo());
288 llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false);
289 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
290 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000291
292 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000293 llvm::Constant *getExceptionThrowFn() {
294 // void objc_exception_throw(id)
295 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
296 llvm::FunctionType *FTy =
297 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
298 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
299 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000300
Daniel Dunbar1c566672009-02-24 01:43:46 +0000301 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000302 llvm::Constant *getSyncEnterFn() {
303 // void objc_sync_enter (id)
304 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
305 llvm::FunctionType *FTy =
306 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
307 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
308 }
Daniel Dunbar1c566672009-02-24 01:43:46 +0000309
310 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000311 llvm::Constant *getSyncExitFn() {
312 // void objc_sync_exit (id)
313 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
314 llvm::FunctionType *FTy =
315 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
316 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
317 }
Daniel Dunbar1c566672009-02-24 01:43:46 +0000318
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000319 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
320 ~ObjCCommonTypesHelper(){}
321};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000322
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000323/// ObjCTypesHelper - Helper class that encapsulates lazy
324/// construction of varies types used during ObjC generation.
325class ObjCTypesHelper : public ObjCCommonTypesHelper {
326private:
327
Chris Lattner4176b0c2009-04-22 02:32:31 +0000328 llvm::Constant *getMessageSendFn() {
329 // id objc_msgSend (id, SEL, ...)
330 std::vector<const llvm::Type*> Params;
331 Params.push_back(ObjectPtrTy);
332 Params.push_back(SelectorPtrTy);
333 return
334 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
335 Params, true),
336 "objc_msgSend");
337 }
338
339 llvm::Constant *getMessageSendStretFn() {
340 // id objc_msgSend_stret (id, SEL, ...)
341 std::vector<const llvm::Type*> Params;
342 Params.push_back(ObjectPtrTy);
343 Params.push_back(SelectorPtrTy);
344 return
345 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
346 Params, true),
347 "objc_msgSend_stret");
348
349 }
350
351 llvm::Constant *getMessageSendFpretFn() {
352 // FIXME: This should be long double on x86_64?
353 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
354 std::vector<const llvm::Type*> Params;
355 Params.push_back(ObjectPtrTy);
356 Params.push_back(SelectorPtrTy);
357 return
358 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
359 Params,
360 true),
361 "objc_msgSend_fpret");
362
363 }
364
365 llvm::Constant *getMessageSendSuperFn() {
366 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
367 std::vector<const llvm::Type*> Params;
368 Params.push_back(SuperPtrTy);
369 Params.push_back(SelectorPtrTy);
370 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
371 Params, true),
372 "objc_msgSendSuper");
373 }
374 llvm::Constant *getMessageSendSuperStretFn() {
375 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
376 // SEL op, ...)
377 std::vector<const llvm::Type*> Params;
378 Params.push_back(Int8PtrTy);
379 Params.push_back(SuperPtrTy);
380 Params.push_back(SelectorPtrTy);
381 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
382 Params, true),
383 "objc_msgSendSuper_stret");
384 }
385
386 llvm::Constant *getMessageSendSuperFpretFn() {
387 // There is no objc_msgSendSuper_fpret? How can that work?
388 return getMessageSendSuperFn();
389 }
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000390
391public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000392 /// SymtabTy - LLVM type for struct objc_symtab.
393 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000394 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
395 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000396 /// ModuleTy - LLVM type for struct objc_module.
397 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000398
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000399 /// ProtocolTy - LLVM type for struct objc_protocol.
400 const llvm::StructType *ProtocolTy;
401 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
402 const llvm::Type *ProtocolPtrTy;
403 /// ProtocolExtensionTy - LLVM type for struct
404 /// objc_protocol_extension.
405 const llvm::StructType *ProtocolExtensionTy;
406 /// ProtocolExtensionTy - LLVM type for struct
407 /// objc_protocol_extension *.
408 const llvm::Type *ProtocolExtensionPtrTy;
409 /// MethodDescriptionTy - LLVM type for struct
410 /// objc_method_description.
411 const llvm::StructType *MethodDescriptionTy;
412 /// MethodDescriptionListTy - LLVM type for struct
413 /// objc_method_description_list.
414 const llvm::StructType *MethodDescriptionListTy;
415 /// MethodDescriptionListPtrTy - LLVM type for struct
416 /// objc_method_description_list *.
417 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000418 /// ProtocolListTy - LLVM type for struct objc_property_list.
419 const llvm::Type *ProtocolListTy;
420 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
421 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000422 /// CategoryTy - LLVM type for struct objc_category.
423 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000424 /// ClassTy - LLVM type for struct objc_class.
425 const llvm::StructType *ClassTy;
426 /// ClassPtrTy - LLVM type for struct objc_class *.
427 const llvm::Type *ClassPtrTy;
428 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
429 const llvm::StructType *ClassExtensionTy;
430 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
431 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000432 // IvarTy - LLVM type for struct objc_ivar.
433 const llvm::StructType *IvarTy;
434 /// IvarListTy - LLVM type for struct objc_ivar_list.
435 const llvm::Type *IvarListTy;
436 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
437 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000438 /// MethodListTy - LLVM type for struct objc_method_list.
439 const llvm::Type *MethodListTy;
440 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
441 const llvm::Type *MethodListPtrTy;
Anders Carlsson124526b2008-09-09 10:10:21 +0000442
443 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
444 const llvm::Type *ExceptionDataTy;
445
Anders Carlsson124526b2008-09-09 10:10:21 +0000446 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000447 llvm::Constant *getExceptionTryEnterFn() {
448 std::vector<const llvm::Type*> Params;
449 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
450 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
451 Params, false),
452 "objc_exception_try_enter");
453 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000454
455 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000456 llvm::Constant *getExceptionTryExitFn() {
457 std::vector<const llvm::Type*> Params;
458 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
459 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
460 Params, false),
461 "objc_exception_try_exit");
462 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000463
464 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000465 llvm::Constant *getExceptionExtractFn() {
466 std::vector<const llvm::Type*> Params;
467 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
468 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
469 Params, false),
470 "objc_exception_extract");
471
472 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000473
474 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000475 llvm::Constant *getExceptionMatchFn() {
476 std::vector<const llvm::Type*> Params;
477 Params.push_back(ClassPtrTy);
478 Params.push_back(ObjectPtrTy);
479 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
480 Params, false),
481 "objc_exception_match");
482
483 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000484
485 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000486 llvm::Constant *getSetJmpFn() {
487 std::vector<const llvm::Type*> Params;
488 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
489 return
490 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
491 Params, false),
492 "_setjmp");
493
494 }
Chris Lattner10cac6f2008-11-15 21:26:17 +0000495
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000496public:
497 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000498 ~ObjCTypesHelper() {}
Daniel Dunbar5669e572008-10-17 03:24:53 +0000499
500
Chris Lattner74391b42009-03-22 21:03:39 +0000501 llvm::Constant *getSendFn(bool IsSuper) {
Chris Lattner4176b0c2009-04-22 02:32:31 +0000502 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
Daniel Dunbar5669e572008-10-17 03:24:53 +0000503 }
504
Chris Lattner74391b42009-03-22 21:03:39 +0000505 llvm::Constant *getSendStretFn(bool IsSuper) {
Chris Lattner4176b0c2009-04-22 02:32:31 +0000506 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
Daniel Dunbar5669e572008-10-17 03:24:53 +0000507 }
508
Chris Lattner74391b42009-03-22 21:03:39 +0000509 llvm::Constant *getSendFpretFn(bool IsSuper) {
Chris Lattner4176b0c2009-04-22 02:32:31 +0000510 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
Daniel Dunbar5669e572008-10-17 03:24:53 +0000511 }
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000512};
513
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000514/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000515/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000516class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000517public:
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000518
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000519 // MethodListnfABITy - LLVM for struct _method_list_t
520 const llvm::StructType *MethodListnfABITy;
521
522 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
523 const llvm::Type *MethodListnfABIPtrTy;
524
525 // ProtocolnfABITy = LLVM for struct _protocol_t
526 const llvm::StructType *ProtocolnfABITy;
527
Daniel Dunbar948e2582009-02-15 07:36:20 +0000528 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
529 const llvm::Type *ProtocolnfABIPtrTy;
530
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000531 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
532 const llvm::StructType *ProtocolListnfABITy;
533
534 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
535 const llvm::Type *ProtocolListnfABIPtrTy;
536
537 // ClassnfABITy - LLVM for struct _class_t
538 const llvm::StructType *ClassnfABITy;
539
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000540 // ClassnfABIPtrTy - LLVM for struct _class_t*
541 const llvm::Type *ClassnfABIPtrTy;
542
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000543 // IvarnfABITy - LLVM for struct _ivar_t
544 const llvm::StructType *IvarnfABITy;
545
546 // IvarListnfABITy - LLVM for struct _ivar_list_t
547 const llvm::StructType *IvarListnfABITy;
548
549 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
550 const llvm::Type *IvarListnfABIPtrTy;
551
552 // ClassRonfABITy - LLVM for struct _class_ro_t
553 const llvm::StructType *ClassRonfABITy;
554
555 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
556 const llvm::Type *ImpnfABITy;
557
558 // CategorynfABITy - LLVM for struct _category_t
559 const llvm::StructType *CategorynfABITy;
560
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000561 // New types for nonfragile abi messaging.
562
563 // MessageRefTy - LLVM for:
564 // struct _message_ref_t {
565 // IMP messenger;
566 // SEL name;
567 // };
568 const llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000569 // MessageRefCTy - clang type for struct _message_ref_t
570 QualType MessageRefCTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000571
572 // MessageRefPtrTy - LLVM for struct _message_ref_t*
573 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000574 // MessageRefCPtrTy - clang type for struct _message_ref_t*
575 QualType MessageRefCPtrTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000576
Fariborz Jahanianef163782009-02-05 01:13:09 +0000577 // MessengerTy - Type of the messenger (shown as IMP above)
578 const llvm::FunctionType *MessengerTy;
579
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000580 // SuperMessageRefTy - LLVM for:
581 // struct _super_message_ref_t {
582 // SUPER_IMP messenger;
583 // SEL name;
584 // };
585 const llvm::StructType *SuperMessageRefTy;
586
587 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
588 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000589
Chris Lattner1c02f862009-04-22 02:53:24 +0000590 llvm::Constant *getMessageSendFixupFn() {
591 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
592 std::vector<const llvm::Type*> Params;
593 Params.push_back(ObjectPtrTy);
594 Params.push_back(MessageRefPtrTy);
595 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
596 Params, true),
597 "objc_msgSend_fixup");
598 }
599
600 llvm::Constant *getMessageSendFpretFixupFn() {
601 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
602 std::vector<const llvm::Type*> Params;
603 Params.push_back(ObjectPtrTy);
604 Params.push_back(MessageRefPtrTy);
605 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
606 Params, true),
607 "objc_msgSend_fpret_fixup");
608 }
609
610 llvm::Constant *getMessageSendStretFixupFn() {
611 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
612 std::vector<const llvm::Type*> Params;
613 Params.push_back(ObjectPtrTy);
614 Params.push_back(MessageRefPtrTy);
615 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
616 Params, true),
617 "objc_msgSend_stret_fixup");
618 }
619
620 llvm::Constant *getMessageSendIdFixupFn() {
621 // id objc_msgSendId_fixup(id, struct message_ref_t*, ...)
622 std::vector<const llvm::Type*> Params;
623 Params.push_back(ObjectPtrTy);
624 Params.push_back(MessageRefPtrTy);
625 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
626 Params, true),
627 "objc_msgSendId_fixup");
628 }
629
630 llvm::Constant *getMessageSendIdStretFixupFn() {
631 // id objc_msgSendId_stret_fixup(id, struct message_ref_t*, ...)
632 std::vector<const llvm::Type*> Params;
633 Params.push_back(ObjectPtrTy);
634 Params.push_back(MessageRefPtrTy);
635 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
636 Params, true),
637 "objc_msgSendId_stret_fixup");
638 }
639 llvm::Constant *getMessageSendSuper2FixupFn() {
640 // id objc_msgSendSuper2_fixup (struct objc_super *,
641 // struct _super_message_ref_t*, ...)
642 std::vector<const llvm::Type*> Params;
643 Params.push_back(SuperPtrTy);
644 Params.push_back(SuperMessageRefPtrTy);
645 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
646 Params, true),
647 "objc_msgSendSuper2_fixup");
648 }
649
650 llvm::Constant *getMessageSendSuper2StretFixupFn() {
651 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
652 // struct _super_message_ref_t*, ...)
653 std::vector<const llvm::Type*> Params;
654 Params.push_back(SuperPtrTy);
655 Params.push_back(SuperMessageRefPtrTy);
656 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
657 Params, true),
658 "objc_msgSendSuper2_stret_fixup");
659 }
660
661
662
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000663 /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C
664 /// exception personality function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000665 llvm::Value *getEHPersonalityPtr() {
666 llvm::Constant *Personality =
667 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
668 std::vector<const llvm::Type*>(),
669 true),
670 "__objc_personality_v0");
671 return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy);
672 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000673
Chris Lattner8a569112009-04-22 02:15:23 +0000674 llvm::Constant *getUnwindResumeOrRethrowFn() {
675 std::vector<const llvm::Type*> Params;
676 Params.push_back(Int8PtrTy);
677 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
678 Params, false),
679 "_Unwind_Resume_or_Rethrow");
680 }
681
682 llvm::Constant *getObjCEndCatchFn() {
683 std::vector<const llvm::Type*> Params;
684 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
685 Params, false),
686 "objc_end_catch");
687
688 }
689
690 llvm::Constant *getObjCBeginCatchFn() {
691 std::vector<const llvm::Type*> Params;
692 Params.push_back(Int8PtrTy);
693 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
694 Params, false),
695 "objc_begin_catch");
696 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000697
698 const llvm::StructType *EHTypeTy;
699 const llvm::Type *EHTypePtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000700
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000701 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
702 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000703};
704
705class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000706public:
707 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000708 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000709 public:
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000710 unsigned int ivar_bytepos;
711 unsigned int ivar_size;
712 GC_IVAR() : ivar_bytepos(0), ivar_size(0) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000713
714 // Allow sorting based on byte pos.
715 bool operator<(const GC_IVAR &b) const {
716 return ivar_bytepos < b.ivar_bytepos;
717 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000718 };
719
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000720 class SKIP_SCAN {
721 public:
722 unsigned int skip;
723 unsigned int scan;
724 SKIP_SCAN() : skip(0), scan(0) {}
725 };
726
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000727protected:
728 CodeGen::CodeGenModule &CGM;
729 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000730 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000731
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000732 // gc ivar layout bitmap calculation helper caches.
733 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
734 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000735
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000736 /// LazySymbols - Symbols to generate a lazy reference for. See
737 /// DefinedSymbols and FinishModule().
738 std::set<IdentifierInfo*> LazySymbols;
739
740 /// DefinedSymbols - External symbols which are defined by this
741 /// module. The symbols in this list and LazySymbols are used to add
742 /// special linker symbols which ensure that Objective-C modules are
743 /// linked properly.
744 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000745
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000746 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000747 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000748
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000749 /// MethodVarNames - uniqued method variable names.
750 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000751
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000752 /// MethodVarTypes - uniqued method type signatures. We have to use
753 /// a StringMap here because have no other unique reference.
754 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000755
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000756 /// MethodDefinitions - map of methods which have been defined in
757 /// this translation unit.
758 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000759
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000760 /// PropertyNames - uniqued method variable names.
761 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000762
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000763 /// ClassReferences - uniqued class references.
764 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000765
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000766 /// SelectorReferences - uniqued selector references.
767 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000768
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000769 /// Protocols - Protocols for which an objc_protocol structure has
770 /// been emitted. Forward declarations are handled by creating an
771 /// empty structure whose initializer is filled in when/if defined.
772 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000773
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000774 /// DefinedProtocols - Protocols which have actually been
775 /// defined. We should not need this, see FIXME in GenerateProtocol.
776 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000777
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000778 /// DefinedClasses - List of defined classes.
779 std::vector<llvm::GlobalValue*> DefinedClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000780
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000781 /// DefinedCategories - List of defined categories.
782 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000783
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000784 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000785 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000786 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000787
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000788 /// GetNameForMethod - Return a name for the given method.
789 /// \param[out] NameOut - The return value.
790 void GetNameForMethod(const ObjCMethodDecl *OMD,
791 const ObjCContainerDecl *CD,
792 std::string &NameOut);
793
794 /// GetMethodVarName - Return a unique constant for the given
795 /// selector's name. The return value has type char *.
796 llvm::Constant *GetMethodVarName(Selector Sel);
797 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
798 llvm::Constant *GetMethodVarName(const std::string &Name);
799
800 /// GetMethodVarType - Return a unique constant for the given
801 /// selector's name. The return value has type char *.
802
803 // FIXME: This is a horrible name.
804 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000805 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000806
807 /// GetPropertyName - Return a unique constant for the given
808 /// name. The return value has type char *.
809 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
810
811 // FIXME: This can be dropped once string functions are unified.
812 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
813 const Decl *Container);
814
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000815 /// GetClassName - Return a unique constant for the given selector's
816 /// name. The return value has type char *.
817 llvm::Constant *GetClassName(IdentifierInfo *Ident);
818
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000819 /// BuildIvarLayout - Builds ivar layout bitmap for the class
820 /// implementation for the __strong or __weak case.
821 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000822 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
823 bool ForStrongLayout);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000824
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000825 void BuildAggrIvarLayout(const ObjCInterfaceDecl *OI,
826 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000827 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +0000828 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000829 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000830 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000831
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000832 /// GetIvarLayoutName - Returns a unique constant for the given
833 /// ivar layout bitmap.
834 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
835 const ObjCCommonTypesHelper &ObjCTypes);
836
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000837 /// EmitPropertyList - Emit the given property list. The return
838 /// value has type PropertyListPtrTy.
839 llvm::Constant *EmitPropertyList(const std::string &Name,
840 const Decl *Container,
841 const ObjCContainerDecl *OCD,
842 const ObjCCommonTypesHelper &ObjCTypes);
843
Fariborz Jahanianda320092009-01-29 19:24:30 +0000844 /// GetProtocolRef - Return a reference to the internal protocol
845 /// description, creating an empty one if it has not been
846 /// defined. The return value has type ProtocolPtrTy.
847 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000848
Chris Lattnercd0ee142009-03-31 08:33:16 +0000849 /// GetFieldBaseOffset - return's field byte offset.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000850 uint64_t GetFieldBaseOffset(const ObjCInterfaceDecl *OI,
851 const llvm::StructLayout *Layout,
Chris Lattnercd0ee142009-03-31 08:33:16 +0000852 const FieldDecl *Field);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000853
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000854 /// CreateMetadataVar - Create a global variable with internal
855 /// linkage for use by the Objective-C runtime.
856 ///
857 /// This is a convenience wrapper which not only creates the
858 /// variable, but also sets the section and alignment and adds the
859 /// global to the UsedGlobals list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000860 ///
861 /// \param Name - The variable name.
862 /// \param Init - The variable initializer; this is also used to
863 /// define the type of the variable.
864 /// \param Section - The section the variable should go into, or 0.
865 /// \param Align - The alignment for the variable, or 0.
866 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000867 /// "llvm.used".
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000868 llvm::GlobalVariable *CreateMetadataVar(const std::string &Name,
869 llvm::Constant *Init,
870 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000871 unsigned Align,
872 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000873
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000874 /// GetNamedIvarList - Return the list of ivars in the interface
875 /// itself (not including super classes and not including unnamed
876 /// bitfields).
877 ///
878 /// For the non-fragile ABI, this also includes synthesized property
879 /// ivars.
880 void GetNamedIvarList(const ObjCInterfaceDecl *OID,
881 llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const;
882
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000883public:
884 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
885 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000886
Steve Naroff33fdb732009-03-31 16:53:37 +0000887 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000888
889 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
890 const ObjCContainerDecl *CD=0);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000891
892 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
893
894 /// GetOrEmitProtocol - Get the protocol object for the given
895 /// declaration, emitting it if necessary. The return value has type
896 /// ProtocolPtrTy.
897 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
898
899 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
900 /// object for the given declaration, emitting it if needed. These
901 /// forward references will be filled in with empty bodies if no
902 /// definition is seen. The return value has type ProtocolPtrTy.
903 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000904};
905
906class CGObjCMac : public CGObjCCommonMac {
907private:
908 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000909 /// EmitImageInfo - Emit the image info marker used to encode some module
910 /// level information.
911 void EmitImageInfo();
912
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000913 /// EmitModuleInfo - Another marker encoding module level
914 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000915 void EmitModuleInfo();
916
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000917 /// EmitModuleSymols - Emit module symbols, the list of defined
918 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000919 llvm::Constant *EmitModuleSymbols();
920
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000921 /// FinishModule - Write out global data structures at the end of
922 /// processing a translation unit.
923 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000924
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000925 /// EmitClassExtension - Generate the class extension structure used
926 /// to store the weak ivar layout and properties. The return value
927 /// has type ClassExtensionPtrTy.
928 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
929
930 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
931 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000932 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000933 const ObjCInterfaceDecl *ID);
934
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000935 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000936 QualType ResultType,
937 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000938 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000939 QualType Arg0Ty,
940 bool IsSuper,
941 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000942
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000943 /// EmitIvarList - Emit the ivar list for the given
944 /// implementation. If ForClass is true the list of class ivars
945 /// (i.e. metaclass ivars) is emitted, otherwise the list of
946 /// interface ivars will be emitted. The return value has type
947 /// IvarListPtrTy.
948 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000949 bool ForClass);
950
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000951 /// EmitMetaClass - Emit a forward reference to the class structure
952 /// for the metaclass of the given interface. The return value has
953 /// type ClassPtrTy.
954 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
955
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000956 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000957 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000958 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
959 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000960 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000961
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000962 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000963
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000964 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000965
966 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000967 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000968 llvm::Constant *EmitMethodList(const std::string &Name,
969 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000970 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000971
972 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000973 /// method declarations.
974 /// - TypeName: The name for the type containing the methods.
975 /// - IsProtocol: True iff these methods are for a protocol.
976 /// - ClassMethds: True iff these are class methods.
977 /// - Required: When true, only "required" methods are
978 /// listed. Similarly, when false only "optional" methods are
979 /// listed. For classes this should always be true.
980 /// - begin, end: The method list to output.
981 ///
982 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000983 llvm::Constant *EmitMethodDescList(const std::string &Name,
984 const char *Section,
985 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000986
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000987 /// GetOrEmitProtocol - Get the protocol object for the given
988 /// declaration, emitting it if necessary. The return value has type
989 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000990 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000991
992 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
993 /// object for the given declaration, emitting it if needed. These
994 /// forward references will be filled in with empty bodies if no
995 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000996 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000997
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000998 /// EmitProtocolExtension - Generate the protocol extension
999 /// structure used to store optional instance and class methods, and
1000 /// protocol properties. The return value has type
1001 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001002 llvm::Constant *
1003 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1004 const ConstantVector &OptInstanceMethods,
1005 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001006
1007 /// EmitProtocolList - Generate the list of referenced
1008 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +00001009 llvm::Constant *EmitProtocolList(const std::string &Name,
1010 ObjCProtocolDecl::protocol_iterator begin,
1011 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001012
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001013 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1014 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001015 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001016
Fariborz Jahanianda320092009-01-29 19:24:30 +00001017 public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001018 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001019
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001020 virtual llvm::Function *ModuleInitFunction();
1021
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001022 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001023 QualType ResultType,
1024 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001025 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001026 bool IsClassMessage,
1027 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001028
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001029 virtual CodeGen::RValue
1030 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001031 QualType ResultType,
1032 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001033 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001034 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001035 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001036 bool IsClassMessage,
1037 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001038
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001039 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001040 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001041
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001042 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001043
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001044 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001045
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001046 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001047
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001048 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001049 const ObjCProtocolDecl *PD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00001050
Chris Lattner74391b42009-03-22 21:03:39 +00001051 virtual llvm::Constant *GetPropertyGetFunction();
1052 virtual llvm::Constant *GetPropertySetFunction();
1053 virtual llvm::Constant *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001054
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001055 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1056 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001057 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1058 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001059 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001060 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001061 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1062 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001063 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1064 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001065 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1066 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001067 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1068 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001069
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001070 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1071 QualType ObjectTy,
1072 llvm::Value *BaseValue,
1073 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001074 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001075 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001076 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001077 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001078};
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001079
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001080class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001081private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001082 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001083 llvm::GlobalVariable* ObjCEmptyCacheVar;
1084 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001085
Daniel Dunbar11394522009-04-18 08:51:00 +00001086 /// SuperClassReferences - uniqued super class references.
1087 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
1088
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001089 /// MetaClassReferences - uniqued meta class references.
1090 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001091
1092 /// EHTypeReferences - uniqued class ehtype references.
1093 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001094
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001095 /// FinishNonFragileABIModule - Write out global data structures at the end of
1096 /// processing a translation unit.
1097 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001098
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00001099 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1100 unsigned InstanceStart,
1101 unsigned InstanceSize,
1102 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001103 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
1104 llvm::Constant *IsAGV,
1105 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001106 llvm::Constant *ClassRoGV,
1107 bool HiddenVisibility);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001108
1109 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
1110
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001111 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
1112
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001113 /// EmitMethodList - Emit the method list for the given
1114 /// implementation. The return value has type MethodListnfABITy.
1115 llvm::Constant *EmitMethodList(const std::string &Name,
1116 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001117 const ConstantVector &Methods);
1118 /// EmitIvarList - Emit the ivar list for the given
1119 /// implementation. If ForClass is true the list of class ivars
1120 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1121 /// interface ivars will be emitted. The return value has type
1122 /// IvarListnfABIPtrTy.
1123 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00001124
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001125 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001126 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001127 unsigned long int offset);
1128
Fariborz Jahanianda320092009-01-29 19:24:30 +00001129 /// GetOrEmitProtocol - Get the protocol object for the given
1130 /// declaration, emitting it if necessary. The return value has type
1131 /// ProtocolPtrTy.
1132 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
1133
1134 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1135 /// object for the given declaration, emitting it if needed. These
1136 /// forward references will be filled in with empty bodies if no
1137 /// definition is seen. The return value has type ProtocolPtrTy.
1138 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
1139
1140 /// EmitProtocolList - Generate the list of referenced
1141 /// protocols. The return value has type ProtocolListPtrTy.
1142 llvm::Constant *EmitProtocolList(const std::string &Name,
1143 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001144 ObjCProtocolDecl::protocol_iterator end);
1145
1146 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1147 QualType ResultType,
1148 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00001149 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001150 QualType Arg0Ty,
1151 bool IsSuper,
1152 const CallArgList &CallArgs);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001153
1154 /// GetClassGlobal - Return the global variable for the Objective-C
1155 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001156 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
1157
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001158 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001159 /// for the given class reference.
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001160 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001161 const ObjCInterfaceDecl *ID);
1162
1163 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1164 /// for the given super class reference.
1165 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1166 const ObjCInterfaceDecl *ID);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001167
1168 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1169 /// meta-data
1170 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
1171 const ObjCInterfaceDecl *ID);
1172
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001173 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1174 /// the given ivar.
1175 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001176 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00001177 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001178 const ObjCIvarDecl *Ivar);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001179
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001180 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1181 /// for the given selector.
1182 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbare588b992009-03-01 04:46:24 +00001183
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001184 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001185 /// interface. The return value has type EHTypePtrTy.
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001186 llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
1187 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001188
1189 const char *getMetaclassSymbolPrefix() const {
1190 return "OBJC_METACLASS_$_";
1191 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001192
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001193 const char *getClassSymbolPrefix() const {
1194 return "OBJC_CLASS_$_";
1195 }
1196
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001197 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001198 uint32_t &InstanceStart,
1199 uint32_t &InstanceSize);
1200
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001201public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001202 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001203 // FIXME. All stubs for now!
1204 virtual llvm::Function *ModuleInitFunction();
1205
1206 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
1207 QualType ResultType,
1208 Selector Sel,
1209 llvm::Value *Receiver,
1210 bool IsClassMessage,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001211 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001212
1213 virtual CodeGen::RValue
1214 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
1215 QualType ResultType,
1216 Selector Sel,
1217 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001218 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001219 llvm::Value *Receiver,
1220 bool IsClassMessage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001221 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001222
1223 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001224 const ObjCInterfaceDecl *ID);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001225
1226 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001227 { return EmitSelector(Builder, Sel); }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001228
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001229 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001230
1231 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001232 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001233 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001234
Chris Lattner74391b42009-03-22 21:03:39 +00001235 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001236 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001237 }
Chris Lattner74391b42009-03-22 21:03:39 +00001238 virtual llvm::Constant *GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001239 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001240 }
Chris Lattner74391b42009-03-22 21:03:39 +00001241 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001242 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001243 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001244
1245 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00001246 const Stmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001247 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001248 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001249 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001250 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001251 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001252 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001253 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001254 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001255 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001256 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001257 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001258 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001259 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1260 QualType ObjectTy,
1261 llvm::Value *BaseValue,
1262 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001263 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001264 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001265 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001266 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001267};
1268
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001269} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001270
1271/* *** Helper Functions *** */
1272
1273/// getConstantGEP() - Help routine to construct simple GEPs.
1274static llvm::Constant *getConstantGEP(llvm::Constant *C,
1275 unsigned idx0,
1276 unsigned idx1) {
1277 llvm::Value *Idxs[] = {
1278 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
1279 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
1280 };
1281 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
1282}
1283
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001284/// hasObjCExceptionAttribute - Return true if this class or any super
1285/// class has the __objc_exception__ attribute.
1286static bool hasObjCExceptionAttribute(const ObjCInterfaceDecl *OID) {
Daniel Dunbarb11fa0d2009-04-13 21:08:27 +00001287 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001288 return true;
1289 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
1290 return hasObjCExceptionAttribute(Super);
1291 return false;
1292}
1293
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001294/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001295
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001296CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
1297 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001298{
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001299 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001300 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001301}
1302
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001303/// GetClass - Return a reference to the class for the given interface
1304/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001305llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001306 const ObjCInterfaceDecl *ID) {
1307 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001308}
1309
1310/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001311llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001312 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001313}
1314
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001315/// Generate a constant CFString object.
1316/*
1317 struct __builtin_CFString {
1318 const int *isa; // point to __CFConstantStringClassReference
1319 int flags;
1320 const char *str;
1321 long length;
1322 };
1323*/
1324
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001325llvm::Constant *CGObjCCommonMac::GenerateConstantString(
Steve Naroff33fdb732009-03-31 16:53:37 +00001326 const ObjCStringLiteral *SL) {
Steve Naroff8d4141f2009-04-01 13:55:36 +00001327 return CGM.GetAddrOfConstantCFString(SL->getString());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001328}
1329
1330/// Generates a message send where the super is the receiver. This is
1331/// a message send to self with special delivery semantics indicating
1332/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001333CodeGen::RValue
1334CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001335 QualType ResultType,
1336 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001337 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001338 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001339 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001340 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001341 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001342 // Create and init a super structure; this is a (receiver, class)
1343 // pair we will pass to objc_msgSendSuper.
1344 llvm::Value *ObjCSuper =
1345 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
1346 llvm::Value *ReceiverAsObject =
1347 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
1348 CGF.Builder.CreateStore(ReceiverAsObject,
1349 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001350
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001351 // If this is a class message the metaclass is passed as the target.
1352 llvm::Value *Target;
1353 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001354 if (isCategoryImpl) {
1355 // Message sent to 'super' in a class method defined in a category
1356 // implementation requires an odd treatment.
1357 // If we are in a class method, we must retrieve the
1358 // _metaclass_ for the current class, pointed at by
1359 // the class's "isa" pointer. The following assumes that
1360 // isa" is the first ivar in a class (which it must be).
1361 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1362 Target = CGF.Builder.CreateStructGEP(Target, 0);
1363 Target = CGF.Builder.CreateLoad(Target);
1364 }
1365 else {
1366 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1367 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1368 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1369 Target = Super;
1370 }
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001371 } else {
1372 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1373 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001374 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
1375 // and ObjCTypes types.
1376 const llvm::Type *ClassTy =
1377 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001378 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001379 CGF.Builder.CreateStore(Target,
1380 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
1381
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001382 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001383 ObjCSuper, ObjCTypes.SuperPtrCTy,
1384 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001385}
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001386
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001387/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001388CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001389 QualType ResultType,
1390 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001391 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001392 bool IsClassMessage,
1393 const CallArgList &CallArgs) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001394 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001395 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001396 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001397}
1398
1399CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001400 QualType ResultType,
1401 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001402 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001403 QualType Arg0Ty,
1404 bool IsSuper,
1405 const CallArgList &CallArgs) {
1406 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001407 if (!IsSuper)
1408 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001409 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
1410 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
1411 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001412 CGF.getContext().getObjCSelType()));
1413 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001414
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001415 CodeGenTypes &Types = CGM.getTypes();
1416 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
Fariborz Jahanian65257ca2009-04-29 22:47:27 +00001417 // FIXME. vararg flag must be true when this API is used for 64bit code gen.
1418 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001419
1420 llvm::Constant *Fn;
Daniel Dunbar88b53962009-02-02 22:03:45 +00001421 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Daniel Dunbar5669e572008-10-17 03:24:53 +00001422 Fn = ObjCTypes.getSendStretFn(IsSuper);
1423 } else if (ResultType->isFloatingType()) {
1424 // FIXME: Sadly, this is wrong. This actually depends on the
1425 // architecture. This happens to be right for x86-32 though.
1426 Fn = ObjCTypes.getSendFpretFn(IsSuper);
1427 } else {
1428 Fn = ObjCTypes.getSendFn(IsSuper);
1429 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +00001430 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +00001431 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001432}
1433
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001434llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001435 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001436 // FIXME: I don't understand why gcc generates this, or where it is
1437 // resolved. Investigate. Its also wasteful to look this up over and
1438 // over.
1439 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1440
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001441 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
1442 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001443}
1444
Fariborz Jahanianda320092009-01-29 19:24:30 +00001445void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001446 // FIXME: We shouldn't need this, the protocol decl should contain
1447 // enough information to tell us whether this was a declaration or a
1448 // definition.
1449 DefinedProtocols.insert(PD->getIdentifier());
1450
1451 // If we have generated a forward reference to this protocol, emit
1452 // it now. Otherwise do nothing, the protocol objects are lazily
1453 // emitted.
1454 if (Protocols.count(PD->getIdentifier()))
1455 GetOrEmitProtocol(PD);
1456}
1457
Fariborz Jahanianda320092009-01-29 19:24:30 +00001458llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001459 if (DefinedProtocols.count(PD->getIdentifier()))
1460 return GetOrEmitProtocol(PD);
1461 return GetOrEmitProtocolRef(PD);
1462}
1463
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001464/*
1465 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1466 struct _objc_protocol {
1467 struct _objc_protocol_extension *isa;
1468 char *protocol_name;
1469 struct _objc_protocol_list *protocol_list;
1470 struct _objc__method_prototype_list *instance_methods;
1471 struct _objc__method_prototype_list *class_methods
1472 };
1473
1474 See EmitProtocolExtension().
1475*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001476llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1477 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1478
1479 // Early exit if a defining object has already been generated.
1480 if (Entry && Entry->hasInitializer())
1481 return Entry;
1482
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001483 // FIXME: I don't understand why gcc generates this, or where it is
1484 // resolved. Investigate. Its also wasteful to look this up over and
1485 // over.
1486 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1487
Chris Lattner8ec03f52008-11-24 03:54:41 +00001488 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001489
1490 // Construct method lists.
1491 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1492 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00001493 for (ObjCProtocolDecl::instmeth_iterator
1494 i = PD->instmeth_begin(CGM.getContext()),
1495 e = PD->instmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001496 ObjCMethodDecl *MD = *i;
1497 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1498 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1499 OptInstanceMethods.push_back(C);
1500 } else {
1501 InstanceMethods.push_back(C);
1502 }
1503 }
1504
Douglas Gregor6ab35242009-04-09 21:40:53 +00001505 for (ObjCProtocolDecl::classmeth_iterator
1506 i = PD->classmeth_begin(CGM.getContext()),
1507 e = PD->classmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001508 ObjCMethodDecl *MD = *i;
1509 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1510 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1511 OptClassMethods.push_back(C);
1512 } else {
1513 ClassMethods.push_back(C);
1514 }
1515 }
1516
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001517 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001518 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001519 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001520 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001521 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001522 PD->protocol_begin(),
1523 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001524 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001525 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
1526 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001527 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1528 InstanceMethods);
1529 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001530 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
1531 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001532 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1533 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001534 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
1535 Values);
1536
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001537 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001538 // Already created, fix the linkage and update the initializer.
1539 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001540 Entry->setInitializer(Init);
1541 } else {
1542 Entry =
1543 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
1544 llvm::GlobalValue::InternalLinkage,
1545 Init,
1546 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
1547 &CGM.getModule());
1548 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001549 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001550 UsedGlobals.push_back(Entry);
1551 // FIXME: Is this necessary? Why only for protocol?
1552 Entry->setAlignment(4);
1553 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001554
1555 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001556}
1557
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001558llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001559 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1560
1561 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001562 // We use the initializer as a marker of whether this is a forward
1563 // reference or not. At module finalization we add the empty
1564 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001565 Entry =
1566 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001567 llvm::GlobalValue::ExternalLinkage,
1568 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001569 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001570 &CGM.getModule());
1571 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001572 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001573 UsedGlobals.push_back(Entry);
1574 // FIXME: Is this necessary? Why only for protocol?
1575 Entry->setAlignment(4);
1576 }
1577
1578 return Entry;
1579}
1580
1581/*
1582 struct _objc_protocol_extension {
1583 uint32_t size;
1584 struct objc_method_description_list *optional_instance_methods;
1585 struct objc_method_description_list *optional_class_methods;
1586 struct objc_property_list *instance_properties;
1587 };
1588*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001589llvm::Constant *
1590CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1591 const ConstantVector &OptInstanceMethods,
1592 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001593 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001594 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001595 std::vector<llvm::Constant*> Values(4);
1596 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001597 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001598 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1599 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001600 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1601 OptInstanceMethods);
1602 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001603 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1604 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001605 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1606 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001607 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1608 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001609 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001610
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001611 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001612 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1613 Values[3]->isNullValue())
1614 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1615
1616 llvm::Constant *Init =
1617 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001618
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001619 // No special section, but goes in llvm.used
1620 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
1621 Init,
1622 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001623}
1624
1625/*
1626 struct objc_protocol_list {
1627 struct objc_protocol_list *next;
1628 long count;
1629 Protocol *list[];
1630 };
1631*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001632llvm::Constant *
1633CGObjCMac::EmitProtocolList(const std::string &Name,
1634 ObjCProtocolDecl::protocol_iterator begin,
1635 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001636 std::vector<llvm::Constant*> ProtocolRefs;
1637
Daniel Dunbardbc933702008-08-21 21:57:41 +00001638 for (; begin != end; ++begin)
1639 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001640
1641 // Just return null for empty protocol lists
1642 if (ProtocolRefs.empty())
1643 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1644
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001645 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001646 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1647
1648 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001649 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001650 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1651 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1652 Values[2] =
1653 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1654 ProtocolRefs.size()),
1655 ProtocolRefs);
1656
1657 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1658 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001659 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001660 4, false);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001661 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1662}
1663
1664/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001665 struct _objc_property {
1666 const char * const name;
1667 const char * const attributes;
1668 };
1669
1670 struct _objc_property_list {
1671 uint32_t entsize; // sizeof (struct _objc_property)
1672 uint32_t prop_count;
1673 struct _objc_property[prop_count];
1674 };
1675*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001676llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1677 const Decl *Container,
1678 const ObjCContainerDecl *OCD,
1679 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001680 std::vector<llvm::Constant*> Properties, Prop(2);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001681 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(CGM.getContext()),
1682 E = OCD->prop_end(CGM.getContext()); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00001683 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001684 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001685 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001686 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1687 Prop));
1688 }
1689
1690 // Return null for empty list.
1691 if (Properties.empty())
1692 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1693
1694 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001695 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001696 std::vector<llvm::Constant*> Values(3);
1697 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1698 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1699 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1700 Properties.size());
1701 Values[2] = llvm::ConstantArray::get(AT, Properties);
1702 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1703
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001704 llvm::GlobalVariable *GV =
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001705 CreateMetadataVar(Name, Init,
1706 (ObjCABI == 2) ? "__DATA, __objc_const" :
1707 "__OBJC,__property,regular,no_dead_strip",
1708 (ObjCABI == 2) ? 8 : 4,
1709 true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001710 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001711}
1712
1713/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001714 struct objc_method_description_list {
1715 int count;
1716 struct objc_method_description list[];
1717 };
1718*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001719llvm::Constant *
1720CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1721 std::vector<llvm::Constant*> Desc(2);
1722 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1723 ObjCTypes.SelectorPtrTy);
1724 Desc[1] = GetMethodVarType(MD);
1725 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1726 Desc);
1727}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001728
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001729llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1730 const char *Section,
1731 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001732 // Return null for empty list.
1733 if (Methods.empty())
1734 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1735
1736 std::vector<llvm::Constant*> Values(2);
1737 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1738 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1739 Methods.size());
1740 Values[1] = llvm::ConstantArray::get(AT, Methods);
1741 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1742
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001743 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001744 return llvm::ConstantExpr::getBitCast(GV,
1745 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001746}
1747
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001748/*
1749 struct _objc_category {
1750 char *category_name;
1751 char *class_name;
1752 struct _objc_method_list *instance_methods;
1753 struct _objc_method_list *class_methods;
1754 struct _objc_protocol_list *protocols;
1755 uint32_t size; // <rdar://4585769>
1756 struct _objc_property_list *instance_properties;
1757 };
1758 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001759void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001760 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001761
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001762 // FIXME: This is poor design, the OCD should have a pointer to the
1763 // category decl. Additionally, note that Category can be null for
1764 // the @implementation w/o an @interface case. Sema should just
1765 // create one for us as it does for @implementation so everyone else
1766 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001767 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001768 const ObjCCategoryDecl *Category =
1769 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001770 std::string ExtName(Interface->getNameAsString() + "_" +
1771 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001772
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001773 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001774 for (ObjCCategoryImplDecl::instmeth_iterator
1775 i = OCD->instmeth_begin(CGM.getContext()),
1776 e = OCD->instmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001777 // Instance methods should always be defined.
1778 InstanceMethods.push_back(GetMethodConstant(*i));
1779 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001780 for (ObjCCategoryImplDecl::classmeth_iterator
1781 i = OCD->classmeth_begin(CGM.getContext()),
1782 e = OCD->classmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001783 // Class methods should always be defined.
1784 ClassMethods.push_back(GetMethodConstant(*i));
1785 }
1786
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001787 std::vector<llvm::Constant*> Values(7);
1788 Values[0] = GetClassName(OCD->getIdentifier());
1789 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00001790 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001791 Values[2] =
1792 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1793 ExtName,
1794 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001795 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001796 Values[3] =
1797 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001798 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001799 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001800 if (Category) {
1801 Values[4] =
1802 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1803 Category->protocol_begin(),
1804 Category->protocol_end());
1805 } else {
1806 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1807 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001808 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001809
1810 // If there is no category @interface then there can be no properties.
1811 if (Category) {
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001812 Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001813 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001814 } else {
1815 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1816 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001817
1818 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1819 Values);
1820
1821 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001822 CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init,
1823 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001824 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001825 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001826}
1827
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001828// FIXME: Get from somewhere?
1829enum ClassFlags {
1830 eClassFlags_Factory = 0x00001,
1831 eClassFlags_Meta = 0x00002,
1832 // <rdr://5142207>
1833 eClassFlags_HasCXXStructors = 0x02000,
1834 eClassFlags_Hidden = 0x20000,
1835 eClassFlags_ABI2_Hidden = 0x00010,
1836 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1837};
1838
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001839/*
1840 struct _objc_class {
1841 Class isa;
1842 Class super_class;
1843 const char *name;
1844 long version;
1845 long info;
1846 long instance_size;
1847 struct _objc_ivar_list *ivars;
1848 struct _objc_method_list *methods;
1849 struct _objc_cache *cache;
1850 struct _objc_protocol_list *protocols;
1851 // Objective-C 1.0 extensions (<rdr://4585769>)
1852 const char *ivar_layout;
1853 struct _objc_class_ext *ext;
1854 };
1855
1856 See EmitClassExtension();
1857 */
1858void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001859 DefinedSymbols.insert(ID->getIdentifier());
1860
Chris Lattner8ec03f52008-11-24 03:54:41 +00001861 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001862 // FIXME: Gross
1863 ObjCInterfaceDecl *Interface =
1864 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001865 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001866 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001867 Interface->protocol_begin(),
1868 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001869 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar2bebbf02009-05-03 10:46:44 +00001870 unsigned Size =
1871 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001872
1873 // FIXME: Set CXX-structors flag.
Daniel Dunbar04d40782009-04-14 06:00:08 +00001874 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001875 Flags |= eClassFlags_Hidden;
1876
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001877 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001878 for (ObjCImplementationDecl::instmeth_iterator
1879 i = ID->instmeth_begin(CGM.getContext()),
1880 e = ID->instmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001881 // Instance methods should always be defined.
1882 InstanceMethods.push_back(GetMethodConstant(*i));
1883 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001884 for (ObjCImplementationDecl::classmeth_iterator
1885 i = ID->classmeth_begin(CGM.getContext()),
1886 e = ID->classmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001887 // Class methods should always be defined.
1888 ClassMethods.push_back(GetMethodConstant(*i));
1889 }
1890
Douglas Gregor653f1b12009-04-23 01:02:12 +00001891 for (ObjCImplementationDecl::propimpl_iterator
1892 i = ID->propimpl_begin(CGM.getContext()),
1893 e = ID->propimpl_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001894 ObjCPropertyImplDecl *PID = *i;
1895
1896 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1897 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1898
1899 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1900 if (llvm::Constant *C = GetMethodConstant(MD))
1901 InstanceMethods.push_back(C);
1902 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1903 if (llvm::Constant *C = GetMethodConstant(MD))
1904 InstanceMethods.push_back(C);
1905 }
1906 }
1907
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001908 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar5384b092009-05-03 08:56:52 +00001909 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001910 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001911 // Record a reference to the super class.
1912 LazySymbols.insert(Super->getIdentifier());
1913
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001914 Values[ 1] =
1915 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1916 ObjCTypes.ClassPtrTy);
1917 } else {
1918 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1919 }
1920 Values[ 2] = GetClassName(ID->getIdentifier());
1921 // Version is always 0.
1922 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1923 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1924 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001925 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001926 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001927 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001928 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001929 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001930 // cache is always NULL.
1931 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1932 Values[ 9] = Protocols;
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00001933 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001934 Values[11] = EmitClassExtension(ID);
1935 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1936 Values);
1937
1938 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001939 CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init,
1940 "__OBJC,__class,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001941 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001942 DefinedClasses.push_back(GV);
1943}
1944
1945llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1946 llvm::Constant *Protocols,
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 Dunbar9f89f2b2009-05-03 12:57:56 +00004271void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004272 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;
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004278 GetNamedIvarList(OID->getClassInterface(), OIvars);
Daniel Dunbar97776872009-04-22 07:32:20 +00004279
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 Dunbar9f89f2b2009-05-03 12:57:56 +00004381 GetClassSizeInfo(ID, 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 Dunbar9f89f2b2009-05-03 12:57:56 +00004677 ComputeIvarBaseOffset(CGM, ID, 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}