blob: f759e4a93eab70fdfe7636c0bb9501ecf3704d99 [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"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +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"
Owen Anderson69243822009-07-13 04:10:07 +000026#include "llvm/LLVMContext.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000027#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000028#include "llvm/ADT/DenseSet.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000029#include "llvm/Target/TargetData.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000030#include <sstream>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000031
32using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000033using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000034
Daniel Dunbar97776872009-04-22 07:32:20 +000035// Common CGObjCRuntime functions, these don't belong here, but they
36// don't belong in CGObjCRuntime either so we will live with it for
37// now.
38
Daniel Dunbar532d4da2009-05-03 13:15:50 +000039/// FindIvarInterface - Find the interface containing the ivar.
Daniel Dunbara2435782009-04-22 12:00:04 +000040///
Daniel Dunbar532d4da2009-05-03 13:15:50 +000041/// FIXME: We shouldn't need to do this, the containing context should
42/// be fixed.
43static const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context,
44 const ObjCInterfaceDecl *OID,
45 const ObjCIvarDecl *OIVD,
46 unsigned &Index) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +000047 // FIXME: The index here is closely tied to how
48 // ASTContext::getObjCLayout is implemented. This should be fixed to
49 // get the information from the layout directly.
50 Index = 0;
Fariborz Jahanian98200742009-05-12 18:14:29 +000051 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +000052 Context.ShallowCollectObjCIvars(OID, Ivars);
Fariborz Jahanian98200742009-05-12 18:14:29 +000053 for (unsigned k = 0, e = Ivars.size(); k != e; ++k) {
54 if (OIVD == Ivars[k])
55 return OID;
56 ++Index;
Daniel Dunbara80a0f62009-04-22 17:43:55 +000057 }
Fariborz Jahanian98200742009-05-12 18:14:29 +000058
Daniel Dunbar532d4da2009-05-03 13:15:50 +000059 // Otherwise check in the super class.
Daniel Dunbara81419d2009-05-05 00:36:57 +000060 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Daniel Dunbar532d4da2009-05-03 13:15:50 +000061 return FindIvarInterface(Context, Super, OIVD, Index);
62
63 return 0;
Daniel Dunbara2435782009-04-22 12:00:04 +000064}
65
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000066static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM,
67 const ObjCInterfaceDecl *OID,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000068 const ObjCImplementationDecl *ID,
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000069 const ObjCIvarDecl *Ivar) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +000070 unsigned Index;
71 const ObjCInterfaceDecl *Container =
72 FindIvarInterface(CGM.getContext(), OID, Ivar, Index);
73 assert(Container && "Unable to find ivar container");
74
75 // If we know have an implementation (and the ivar is in it) then
76 // look up in the implementation layout.
77 const ASTRecordLayout *RL;
78 if (ID && ID->getClassInterface() == Container)
79 RL = &CGM.getContext().getASTObjCImplementationLayout(ID);
80 else
81 RL = &CGM.getContext().getASTObjCInterfaceLayout(Container);
82 return RL->getFieldOffset(Index);
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000083}
84
85uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
86 const ObjCInterfaceDecl *OID,
87 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000088 return LookupFieldBitOffset(CGM, OID, 0, Ivar) / 8;
89}
90
91uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
92 const ObjCImplementationDecl *OID,
93 const ObjCIvarDecl *Ivar) {
94 return LookupFieldBitOffset(CGM, OID->getClassInterface(), OID, Ivar) / 8;
Daniel Dunbar97776872009-04-22 07:32:20 +000095}
96
97LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
98 const ObjCInterfaceDecl *OID,
99 llvm::Value *BaseValue,
100 const ObjCIvarDecl *Ivar,
101 unsigned CVRQualifiers,
102 llvm::Value *Offset) {
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000103 // Compute (type*) ( (char *) BaseValue + Offset)
Owen Anderson96e0fc72009-07-29 22:16:19 +0000104 llvm::Type *I8Ptr = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000105 QualType IvarTy = Ivar->getType();
106 const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy);
Daniel Dunbar97776872009-04-22 07:32:20 +0000107 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr);
Daniel Dunbar97776872009-04-22 07:32:20 +0000108 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000109 V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar97776872009-04-22 07:32:20 +0000110
111 if (Ivar->isBitField()) {
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +0000112 // We need to compute the bit offset for the bit-field, the offset
113 // is to the byte. Note, there is a subtle invariant here: we can
114 // only call this routine on non-sythesized ivars but we may be
115 // called for synthesized ivars. However, a synthesized ivar can
116 // never be a bit-field so this is safe.
117 uint64_t BitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar) % 8;
118
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000119 uint64_t BitFieldSize =
120 Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue();
121 return LValue::MakeBitfield(V, BitOffset, BitFieldSize,
Daniel Dunbare38df862009-05-03 07:52:00 +0000122 IvarTy->isSignedIntegerType(),
123 IvarTy.getCVRQualifiers()|CVRQualifiers);
Daniel Dunbar97776872009-04-22 07:32:20 +0000124 }
125
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000126 LValue LV = LValue::MakeAddr(V, IvarTy.getCVRQualifiers()|CVRQualifiers,
127 CGF.CGM.getContext().getObjCGCAttrKind(IvarTy));
Daniel Dunbar97776872009-04-22 07:32:20 +0000128 LValue::SetObjCIvar(LV, true);
129 return LV;
130}
131
132///
133
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000134namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000135
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000136 typedef std::vector<llvm::Constant*> ConstantVector;
137
Mike Stumpf5408fe2009-05-16 07:57:57 +0000138 // FIXME: We should find a nicer way to make the labels for metadata, string
139 // concatenation is lame.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000140
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000141class ObjCCommonTypesHelper {
Owen Andersona1cf15f2009-07-14 23:10:40 +0000142protected:
143 llvm::LLVMContext &VMContext;
144
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000145private:
146 llvm::Constant *getMessageSendFn() const {
147 // id objc_msgSend (id, SEL, ...)
148 std::vector<const llvm::Type*> Params;
149 Params.push_back(ObjectPtrTy);
150 Params.push_back(SelectorPtrTy);
151 return
Owen Anderson96e0fc72009-07-29 22:16:19 +0000152 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000153 Params, true),
154 "objc_msgSend");
155 }
156
157 llvm::Constant *getMessageSendStretFn() const {
158 // id objc_msgSend_stret (id, SEL, ...)
159 std::vector<const llvm::Type*> Params;
160 Params.push_back(ObjectPtrTy);
161 Params.push_back(SelectorPtrTy);
162 return
Owen Anderson96e0fc72009-07-29 22:16:19 +0000163 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000164 Params, true),
165 "objc_msgSend_stret");
166
167 }
168
169 llvm::Constant *getMessageSendFpretFn() const {
170 // FIXME: This should be long double on x86_64?
171 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
172 std::vector<const llvm::Type*> Params;
173 Params.push_back(ObjectPtrTy);
174 Params.push_back(SelectorPtrTy);
175 return
Owen Anderson96e0fc72009-07-29 22:16:19 +0000176 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000177 Params,
178 true),
179 "objc_msgSend_fpret");
180
181 }
182
183 llvm::Constant *getMessageSendSuperFn() const {
184 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
185 const char *SuperName = "objc_msgSendSuper";
186 std::vector<const llvm::Type*> Params;
187 Params.push_back(SuperPtrTy);
188 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000189 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000190 Params, true),
191 SuperName);
192 }
193
194 llvm::Constant *getMessageSendSuperFn2() const {
195 // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
196 const char *SuperName = "objc_msgSendSuper2";
197 std::vector<const llvm::Type*> Params;
198 Params.push_back(SuperPtrTy);
199 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000200 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000201 Params, true),
202 SuperName);
203 }
204
205 llvm::Constant *getMessageSendSuperStretFn() const {
206 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
207 // SEL op, ...)
208 std::vector<const llvm::Type*> Params;
209 Params.push_back(Int8PtrTy);
210 Params.push_back(SuperPtrTy);
211 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000212 return CGM.CreateRuntimeFunction(
Owen Anderson96e0fc72009-07-29 22:16:19 +0000213 llvm::FunctionType::get(llvm::Type::VoidTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000214 Params, true),
215 "objc_msgSendSuper_stret");
216 }
217
218 llvm::Constant *getMessageSendSuperStretFn2() const {
219 // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
220 // SEL op, ...)
221 std::vector<const llvm::Type*> Params;
222 Params.push_back(Int8PtrTy);
223 Params.push_back(SuperPtrTy);
224 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000225 return CGM.CreateRuntimeFunction(
Owen Anderson96e0fc72009-07-29 22:16:19 +0000226 llvm::FunctionType::get(llvm::Type::VoidTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000227 Params, true),
228 "objc_msgSendSuper2_stret");
229 }
230
231 llvm::Constant *getMessageSendSuperFpretFn() const {
232 // There is no objc_msgSendSuper_fpret? How can that work?
233 return getMessageSendSuperFn();
234 }
235
236 llvm::Constant *getMessageSendSuperFpretFn2() const {
237 // There is no objc_msgSendSuper_fpret? How can that work?
238 return getMessageSendSuperFn2();
239 }
240
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000241protected:
242 CodeGen::CodeGenModule &CGM;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000243
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000244public:
Fariborz Jahanian0a855d02009-03-23 19:10:40 +0000245 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000246 const llvm::Type *Int8PtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000247
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000248 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
249 const llvm::Type *ObjectPtrTy;
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000250
251 /// PtrObjectPtrTy - LLVM type for id *
252 const llvm::Type *PtrObjectPtrTy;
253
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000254 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000255 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000256 /// ProtocolPtrTy - LLVM type for external protocol handles
257 /// (typeof(Protocol))
258 const llvm::Type *ExternalProtocolPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000259
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000260 // SuperCTy - clang type for struct objc_super.
261 QualType SuperCTy;
262 // SuperPtrCTy - clang type for struct objc_super *.
263 QualType SuperPtrCTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000264
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000265 /// SuperTy - LLVM type for struct objc_super.
266 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000267 /// SuperPtrTy - LLVM type for struct objc_super *.
268 const llvm::Type *SuperPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000269
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000270 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
271 /// in GCC parlance).
272 const llvm::StructType *PropertyTy;
273
274 /// PropertyListTy - LLVM type for struct objc_property_list
275 /// (_prop_list_t in GCC parlance).
276 const llvm::StructType *PropertyListTy;
277 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
278 const llvm::Type *PropertyListPtrTy;
279
280 // MethodTy - LLVM type for struct objc_method.
281 const llvm::StructType *MethodTy;
282
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000283 /// CacheTy - LLVM type for struct objc_cache.
284 const llvm::Type *CacheTy;
285 /// CachePtrTy - LLVM type for struct objc_cache *.
286 const llvm::Type *CachePtrTy;
287
Chris Lattner72db6c32009-04-22 02:44:54 +0000288 llvm::Constant *getGetPropertyFn() {
289 CodeGen::CodeGenTypes &Types = CGM.getTypes();
290 ASTContext &Ctx = CGM.getContext();
291 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
292 llvm::SmallVector<QualType,16> Params;
293 QualType IdType = Ctx.getObjCIdType();
294 QualType SelType = Ctx.getObjCSelType();
295 Params.push_back(IdType);
296 Params.push_back(SelType);
297 Params.push_back(Ctx.LongTy);
298 Params.push_back(Ctx.BoolTy);
299 const llvm::FunctionType *FTy =
300 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
301 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
302 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000303
Chris Lattner72db6c32009-04-22 02:44:54 +0000304 llvm::Constant *getSetPropertyFn() {
305 CodeGen::CodeGenTypes &Types = CGM.getTypes();
306 ASTContext &Ctx = CGM.getContext();
307 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
308 llvm::SmallVector<QualType,16> Params;
309 QualType IdType = Ctx.getObjCIdType();
310 QualType SelType = Ctx.getObjCSelType();
311 Params.push_back(IdType);
312 Params.push_back(SelType);
313 Params.push_back(Ctx.LongTy);
314 Params.push_back(IdType);
315 Params.push_back(Ctx.BoolTy);
316 Params.push_back(Ctx.BoolTy);
317 const llvm::FunctionType *FTy =
318 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
319 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
320 }
321
322 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000323 CodeGen::CodeGenTypes &Types = CGM.getTypes();
324 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000325 // void objc_enumerationMutation (id)
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000326 llvm::SmallVector<QualType,16> Params;
Daniel Dunbar309a4362009-07-24 07:40:24 +0000327 Params.push_back(Ctx.getObjCIdType());
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000328 const llvm::FunctionType *FTy =
329 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000330 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
331 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000332
333 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000334 llvm::Constant *getGcReadWeakFn() {
335 // id objc_read_weak (id *)
336 std::vector<const llvm::Type*> Args;
337 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000338 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000339 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000340 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
341 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000342
343 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000344 llvm::Constant *getGcAssignWeakFn() {
345 // id objc_assign_weak (id, id *)
346 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
347 Args.push_back(ObjectPtrTy->getPointerTo());
348 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000349 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner96508e12009-04-17 22:12:36 +0000350 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
351 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000352
353 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000354 llvm::Constant *getGcAssignGlobalFn() {
355 // id objc_assign_global(id, id *)
356 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
357 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000358 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000359 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000360 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
361 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000362
363 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000364 llvm::Constant *getGcAssignIvarFn() {
365 // id objc_assign_ivar(id, id *)
366 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
367 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000368 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000369 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000370 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
371 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000372
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000373 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
374 llvm::Constant *GcMemmoveCollectableFn() {
375 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
376 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
377 Args.push_back(Int8PtrTy);
378 Args.push_back(LongTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000379 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000380 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
381 }
382
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000383 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000384 llvm::Constant *getGcAssignStrongCastFn() {
385 // id objc_assign_global(id, id *)
386 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
387 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000388 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000389 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000390 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
391 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000392
393 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000394 llvm::Constant *getExceptionThrowFn() {
395 // void objc_exception_throw(id)
396 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
397 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000398 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000399 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
400 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000401
Daniel Dunbar1c566672009-02-24 01:43:46 +0000402 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000403 llvm::Constant *getSyncEnterFn() {
404 // void objc_sync_enter (id)
405 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
406 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000407 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000408 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
409 }
Daniel Dunbar1c566672009-02-24 01:43:46 +0000410
411 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000412 llvm::Constant *getSyncExitFn() {
413 // void objc_sync_exit (id)
414 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
415 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000416 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000417 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
418 }
Daniel Dunbar1c566672009-02-24 01:43:46 +0000419
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000420 llvm::Constant *getSendFn(bool IsSuper) const {
421 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
422 }
423
424 llvm::Constant *getSendFn2(bool IsSuper) const {
425 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
426 }
427
428 llvm::Constant *getSendStretFn(bool IsSuper) const {
429 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
430 }
431
432 llvm::Constant *getSendStretFn2(bool IsSuper) const {
433 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
434 }
435
436 llvm::Constant *getSendFpretFn(bool IsSuper) const {
437 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
438 }
439
440 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
441 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
442 }
443
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000444 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
445 ~ObjCCommonTypesHelper(){}
446};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000447
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000448/// ObjCTypesHelper - Helper class that encapsulates lazy
449/// construction of varies types used during ObjC generation.
450class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000451public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000452 /// SymtabTy - LLVM type for struct objc_symtab.
453 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000454 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
455 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000456 /// ModuleTy - LLVM type for struct objc_module.
457 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000458
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000459 /// ProtocolTy - LLVM type for struct objc_protocol.
460 const llvm::StructType *ProtocolTy;
461 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
462 const llvm::Type *ProtocolPtrTy;
463 /// ProtocolExtensionTy - LLVM type for struct
464 /// objc_protocol_extension.
465 const llvm::StructType *ProtocolExtensionTy;
466 /// ProtocolExtensionTy - LLVM type for struct
467 /// objc_protocol_extension *.
468 const llvm::Type *ProtocolExtensionPtrTy;
469 /// MethodDescriptionTy - LLVM type for struct
470 /// objc_method_description.
471 const llvm::StructType *MethodDescriptionTy;
472 /// MethodDescriptionListTy - LLVM type for struct
473 /// objc_method_description_list.
474 const llvm::StructType *MethodDescriptionListTy;
475 /// MethodDescriptionListPtrTy - LLVM type for struct
476 /// objc_method_description_list *.
477 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000478 /// ProtocolListTy - LLVM type for struct objc_property_list.
479 const llvm::Type *ProtocolListTy;
480 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
481 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000482 /// CategoryTy - LLVM type for struct objc_category.
483 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000484 /// ClassTy - LLVM type for struct objc_class.
485 const llvm::StructType *ClassTy;
486 /// ClassPtrTy - LLVM type for struct objc_class *.
487 const llvm::Type *ClassPtrTy;
488 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
489 const llvm::StructType *ClassExtensionTy;
490 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
491 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000492 // IvarTy - LLVM type for struct objc_ivar.
493 const llvm::StructType *IvarTy;
494 /// IvarListTy - LLVM type for struct objc_ivar_list.
495 const llvm::Type *IvarListTy;
496 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
497 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000498 /// MethodListTy - LLVM type for struct objc_method_list.
499 const llvm::Type *MethodListTy;
500 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
501 const llvm::Type *MethodListPtrTy;
Anders Carlsson124526b2008-09-09 10:10:21 +0000502
503 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
504 const llvm::Type *ExceptionDataTy;
505
Anders Carlsson124526b2008-09-09 10:10:21 +0000506 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000507 llvm::Constant *getExceptionTryEnterFn() {
508 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000509 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +0000510 return CGM.CreateRuntimeFunction(
Owen Anderson96e0fc72009-07-29 22:16:19 +0000511 llvm::FunctionType::get(llvm::Type::VoidTy,
Chris Lattner34b02a12009-04-22 02:26:14 +0000512 Params, false),
513 "objc_exception_try_enter");
514 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000515
516 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000517 llvm::Constant *getExceptionTryExitFn() {
518 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000519 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +0000520 return CGM.CreateRuntimeFunction(
Owen Anderson96e0fc72009-07-29 22:16:19 +0000521 llvm::FunctionType::get(llvm::Type::VoidTy,
Chris Lattner34b02a12009-04-22 02:26:14 +0000522 Params, false),
523 "objc_exception_try_exit");
524 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000525
526 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000527 llvm::Constant *getExceptionExtractFn() {
528 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000529 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
530 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner34b02a12009-04-22 02:26:14 +0000531 Params, false),
532 "objc_exception_extract");
533
534 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000535
536 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000537 llvm::Constant *getExceptionMatchFn() {
538 std::vector<const llvm::Type*> Params;
539 Params.push_back(ClassPtrTy);
540 Params.push_back(ObjectPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000541 return CGM.CreateRuntimeFunction(
Owen Anderson96e0fc72009-07-29 22:16:19 +0000542 llvm::FunctionType::get(llvm::Type::Int32Ty,
Chris Lattner34b02a12009-04-22 02:26:14 +0000543 Params, false),
544 "objc_exception_match");
545
546 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000547
548 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000549 llvm::Constant *getSetJmpFn() {
550 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000551 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
Chris Lattner34b02a12009-04-22 02:26:14 +0000552 return
Owen Anderson96e0fc72009-07-29 22:16:19 +0000553 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
Chris Lattner34b02a12009-04-22 02:26:14 +0000554 Params, false),
555 "_setjmp");
556
557 }
Chris Lattner10cac6f2008-11-15 21:26:17 +0000558
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000559public:
560 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000561 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000562};
563
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000564/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000565/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000566class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000567public:
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000568
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000569 // MethodListnfABITy - LLVM for struct _method_list_t
570 const llvm::StructType *MethodListnfABITy;
571
572 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
573 const llvm::Type *MethodListnfABIPtrTy;
574
575 // ProtocolnfABITy = LLVM for struct _protocol_t
576 const llvm::StructType *ProtocolnfABITy;
577
Daniel Dunbar948e2582009-02-15 07:36:20 +0000578 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
579 const llvm::Type *ProtocolnfABIPtrTy;
580
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000581 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
582 const llvm::StructType *ProtocolListnfABITy;
583
584 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
585 const llvm::Type *ProtocolListnfABIPtrTy;
586
587 // ClassnfABITy - LLVM for struct _class_t
588 const llvm::StructType *ClassnfABITy;
589
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000590 // ClassnfABIPtrTy - LLVM for struct _class_t*
591 const llvm::Type *ClassnfABIPtrTy;
592
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000593 // IvarnfABITy - LLVM for struct _ivar_t
594 const llvm::StructType *IvarnfABITy;
595
596 // IvarListnfABITy - LLVM for struct _ivar_list_t
597 const llvm::StructType *IvarListnfABITy;
598
599 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
600 const llvm::Type *IvarListnfABIPtrTy;
601
602 // ClassRonfABITy - LLVM for struct _class_ro_t
603 const llvm::StructType *ClassRonfABITy;
604
605 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
606 const llvm::Type *ImpnfABITy;
607
608 // CategorynfABITy - LLVM for struct _category_t
609 const llvm::StructType *CategorynfABITy;
610
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000611 // New types for nonfragile abi messaging.
612
613 // MessageRefTy - LLVM for:
614 // struct _message_ref_t {
615 // IMP messenger;
616 // SEL name;
617 // };
618 const llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000619 // MessageRefCTy - clang type for struct _message_ref_t
620 QualType MessageRefCTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000621
622 // MessageRefPtrTy - LLVM for struct _message_ref_t*
623 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000624 // MessageRefCPtrTy - clang type for struct _message_ref_t*
625 QualType MessageRefCPtrTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000626
Fariborz Jahanianef163782009-02-05 01:13:09 +0000627 // MessengerTy - Type of the messenger (shown as IMP above)
628 const llvm::FunctionType *MessengerTy;
629
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000630 // SuperMessageRefTy - LLVM for:
631 // struct _super_message_ref_t {
632 // SUPER_IMP messenger;
633 // SEL name;
634 // };
635 const llvm::StructType *SuperMessageRefTy;
636
637 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
638 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000639
Chris Lattner1c02f862009-04-22 02:53:24 +0000640 llvm::Constant *getMessageSendFixupFn() {
641 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
642 std::vector<const llvm::Type*> Params;
643 Params.push_back(ObjectPtrTy);
644 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000645 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000646 Params, true),
647 "objc_msgSend_fixup");
648 }
649
650 llvm::Constant *getMessageSendFpretFixupFn() {
651 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
652 std::vector<const llvm::Type*> Params;
653 Params.push_back(ObjectPtrTy);
654 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000655 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000656 Params, true),
657 "objc_msgSend_fpret_fixup");
658 }
659
660 llvm::Constant *getMessageSendStretFixupFn() {
661 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
662 std::vector<const llvm::Type*> Params;
663 Params.push_back(ObjectPtrTy);
664 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000665 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000666 Params, true),
667 "objc_msgSend_stret_fixup");
668 }
669
670 llvm::Constant *getMessageSendIdFixupFn() {
671 // id objc_msgSendId_fixup(id, struct message_ref_t*, ...)
672 std::vector<const llvm::Type*> Params;
673 Params.push_back(ObjectPtrTy);
674 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000675 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000676 Params, true),
677 "objc_msgSendId_fixup");
678 }
679
680 llvm::Constant *getMessageSendIdStretFixupFn() {
681 // id objc_msgSendId_stret_fixup(id, struct message_ref_t*, ...)
682 std::vector<const llvm::Type*> Params;
683 Params.push_back(ObjectPtrTy);
684 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000685 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000686 Params, true),
687 "objc_msgSendId_stret_fixup");
688 }
689 llvm::Constant *getMessageSendSuper2FixupFn() {
690 // id objc_msgSendSuper2_fixup (struct objc_super *,
691 // struct _super_message_ref_t*, ...)
692 std::vector<const llvm::Type*> Params;
693 Params.push_back(SuperPtrTy);
694 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000695 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000696 Params, true),
697 "objc_msgSendSuper2_fixup");
698 }
699
700 llvm::Constant *getMessageSendSuper2StretFixupFn() {
701 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
702 // struct _super_message_ref_t*, ...)
703 std::vector<const llvm::Type*> Params;
704 Params.push_back(SuperPtrTy);
705 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000706 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000707 Params, true),
708 "objc_msgSendSuper2_stret_fixup");
709 }
710
711
712
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000713 /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C
714 /// exception personality function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000715 llvm::Value *getEHPersonalityPtr() {
716 llvm::Constant *Personality =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000717 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000718 true),
719 "__objc_personality_v0");
Owen Anderson3c4972d2009-07-29 18:54:39 +0000720 return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000721 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000722
Chris Lattner8a569112009-04-22 02:15:23 +0000723 llvm::Constant *getUnwindResumeOrRethrowFn() {
724 std::vector<const llvm::Type*> Params;
725 Params.push_back(Int8PtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000726 return CGM.CreateRuntimeFunction(
Owen Anderson96e0fc72009-07-29 22:16:19 +0000727 llvm::FunctionType::get(llvm::Type::VoidTy,
Chris Lattner8a569112009-04-22 02:15:23 +0000728 Params, false),
729 "_Unwind_Resume_or_Rethrow");
730 }
731
732 llvm::Constant *getObjCEndCatchFn() {
Owen Anderson96e0fc72009-07-29 22:16:19 +0000733 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
Chris Lattnerb59761b2009-07-01 04:13:52 +0000734 false),
Chris Lattner8a569112009-04-22 02:15:23 +0000735 "objc_end_catch");
736
737 }
738
739 llvm::Constant *getObjCBeginCatchFn() {
740 std::vector<const llvm::Type*> Params;
741 Params.push_back(Int8PtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000742 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
Chris Lattner8a569112009-04-22 02:15:23 +0000743 Params, false),
744 "objc_begin_catch");
745 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000746
747 const llvm::StructType *EHTypeTy;
748 const llvm::Type *EHTypePtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000749
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000750 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
751 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000752};
753
754class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000755public:
756 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000757 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000758 public:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000759 unsigned ivar_bytepos;
760 unsigned ivar_size;
761 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
762 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000763
764 // Allow sorting based on byte pos.
765 bool operator<(const GC_IVAR &b) const {
766 return ivar_bytepos < b.ivar_bytepos;
767 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000768 };
769
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000770 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000771 public:
772 unsigned skip;
773 unsigned scan;
774 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
775 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000776 };
777
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000778protected:
779 CodeGen::CodeGenModule &CGM;
Owen Anderson69243822009-07-13 04:10:07 +0000780 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000781 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000782 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000783
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000784 // gc ivar layout bitmap calculation helper caches.
785 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
786 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000787
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000788 /// LazySymbols - Symbols to generate a lazy reference for. See
789 /// DefinedSymbols and FinishModule().
790 std::set<IdentifierInfo*> LazySymbols;
791
792 /// DefinedSymbols - External symbols which are defined by this
793 /// module. The symbols in this list and LazySymbols are used to add
794 /// special linker symbols which ensure that Objective-C modules are
795 /// linked properly.
796 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000797
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000798 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000799 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000800
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000801 /// MethodVarNames - uniqued method variable names.
802 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000803
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000804 /// MethodVarTypes - uniqued method type signatures. We have to use
805 /// a StringMap here because have no other unique reference.
806 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000807
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000808 /// MethodDefinitions - map of methods which have been defined in
809 /// this translation unit.
810 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000811
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000812 /// PropertyNames - uniqued method variable names.
813 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000814
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000815 /// ClassReferences - uniqued class references.
816 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000817
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000818 /// SelectorReferences - uniqued selector references.
819 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000820
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000821 /// Protocols - Protocols for which an objc_protocol structure has
822 /// been emitted. Forward declarations are handled by creating an
823 /// empty structure whose initializer is filled in when/if defined.
824 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000825
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000826 /// DefinedProtocols - Protocols which have actually been
827 /// defined. We should not need this, see FIXME in GenerateProtocol.
828 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000829
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000830 /// DefinedClasses - List of defined classes.
831 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000832
833 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
834 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000835
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000836 /// DefinedCategories - List of defined categories.
837 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000838
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000839 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
840 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
841
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000842 /// GetNameForMethod - Return a name for the given method.
843 /// \param[out] NameOut - The return value.
844 void GetNameForMethod(const ObjCMethodDecl *OMD,
845 const ObjCContainerDecl *CD,
846 std::string &NameOut);
847
848 /// GetMethodVarName - Return a unique constant for the given
849 /// selector's name. The return value has type char *.
850 llvm::Constant *GetMethodVarName(Selector Sel);
851 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
852 llvm::Constant *GetMethodVarName(const std::string &Name);
853
854 /// GetMethodVarType - Return a unique constant for the given
855 /// selector's name. The return value has type char *.
856
857 // FIXME: This is a horrible name.
858 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000859 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000860
861 /// GetPropertyName - Return a unique constant for the given
862 /// name. The return value has type char *.
863 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
864
865 // FIXME: This can be dropped once string functions are unified.
866 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
867 const Decl *Container);
868
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000869 /// GetClassName - Return a unique constant for the given selector's
870 /// name. The return value has type char *.
871 llvm::Constant *GetClassName(IdentifierInfo *Ident);
872
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000873 /// BuildIvarLayout - Builds ivar layout bitmap for the class
874 /// implementation for the __strong or __weak case.
875 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000876 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
877 bool ForStrongLayout);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000878
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000879 void BuildAggrIvarRecordLayout(const RecordType *RT,
880 unsigned int BytePos, bool ForStrongLayout,
881 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000882 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000883 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000884 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +0000885 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000886 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000887 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000888
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000889 /// GetIvarLayoutName - Returns a unique constant for the given
890 /// ivar layout bitmap.
891 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
892 const ObjCCommonTypesHelper &ObjCTypes);
893
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000894 /// EmitPropertyList - Emit the given property list. The return
895 /// value has type PropertyListPtrTy.
896 llvm::Constant *EmitPropertyList(const std::string &Name,
897 const Decl *Container,
898 const ObjCContainerDecl *OCD,
899 const ObjCCommonTypesHelper &ObjCTypes);
900
Fariborz Jahanianda320092009-01-29 19:24:30 +0000901 /// GetProtocolRef - Return a reference to the internal protocol
902 /// description, creating an empty one if it has not been
903 /// defined. The return value has type ProtocolPtrTy.
904 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000905
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000906 /// CreateMetadataVar - Create a global variable with internal
907 /// linkage for use by the Objective-C runtime.
908 ///
909 /// This is a convenience wrapper which not only creates the
910 /// variable, but also sets the section and alignment and adds the
Chris Lattnerad64e022009-07-17 23:57:13 +0000911 /// global to the "llvm.used" list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000912 ///
913 /// \param Name - The variable name.
914 /// \param Init - The variable initializer; this is also used to
915 /// define the type of the variable.
916 /// \param Section - The section the variable should go into, or 0.
917 /// \param Align - The alignment for the variable, or 0.
918 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000919 /// "llvm.used".
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000920 llvm::GlobalVariable *CreateMetadataVar(const std::string &Name,
921 llvm::Constant *Init,
922 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000923 unsigned Align,
924 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000925
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000926 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
927 QualType ResultType,
928 llvm::Value *Sel,
929 llvm::Value *Arg0,
930 QualType Arg0Ty,
931 bool IsSuper,
932 const CallArgList &CallArgs,
933 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000934
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000935public:
Owen Anderson69243822009-07-13 04:10:07 +0000936 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
937 CGM(cgm), VMContext(cgm.getLLVMContext())
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000938 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000939
Steve Naroff33fdb732009-03-31 16:53:37 +0000940 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000941
942 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
943 const ObjCContainerDecl *CD=0);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000944
945 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
946
947 /// GetOrEmitProtocol - Get the protocol object for the given
948 /// declaration, emitting it if necessary. The return value has type
949 /// ProtocolPtrTy.
950 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
951
952 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
953 /// object for the given declaration, emitting it if needed. These
954 /// forward references will be filled in with empty bodies if no
955 /// definition is seen. The return value has type ProtocolPtrTy.
956 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000957};
958
959class CGObjCMac : public CGObjCCommonMac {
960private:
961 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000962 /// EmitImageInfo - Emit the image info marker used to encode some module
963 /// level information.
964 void EmitImageInfo();
965
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000966 /// EmitModuleInfo - Another marker encoding module level
967 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000968 void EmitModuleInfo();
969
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000970 /// EmitModuleSymols - Emit module symbols, the list of defined
971 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000972 llvm::Constant *EmitModuleSymbols();
973
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000974 /// FinishModule - Write out global data structures at the end of
975 /// processing a translation unit.
976 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000977
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000978 /// EmitClassExtension - Generate the class extension structure used
979 /// to store the weak ivar layout and properties. The return value
980 /// has type ClassExtensionPtrTy.
981 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
982
983 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
984 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000985 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000986 const ObjCInterfaceDecl *ID);
987
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000988 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000989 QualType ResultType,
990 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000991 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000992 QualType Arg0Ty,
993 bool IsSuper,
994 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000995
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000996 /// EmitIvarList - Emit the ivar list for the given
997 /// implementation. If ForClass is true the list of class ivars
998 /// (i.e. metaclass ivars) is emitted, otherwise the list of
999 /// interface ivars will be emitted. The return value has type
1000 /// IvarListPtrTy.
1001 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001002 bool ForClass);
1003
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001004 /// EmitMetaClass - Emit a forward reference to the class structure
1005 /// for the metaclass of the given interface. The return value has
1006 /// type ClassPtrTy.
1007 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1008
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001009 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001010 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001011 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1012 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001013 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001014
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001015 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001016
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001017 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001018
1019 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001020 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001021 llvm::Constant *EmitMethodList(const std::string &Name,
1022 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001023 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001024
1025 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001026 /// method declarations.
1027 /// - TypeName: The name for the type containing the methods.
1028 /// - IsProtocol: True iff these methods are for a protocol.
1029 /// - ClassMethds: True iff these are class methods.
1030 /// - Required: When true, only "required" methods are
1031 /// listed. Similarly, when false only "optional" methods are
1032 /// listed. For classes this should always be true.
1033 /// - begin, end: The method list to output.
1034 ///
1035 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001036 llvm::Constant *EmitMethodDescList(const std::string &Name,
1037 const char *Section,
1038 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001039
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001040 /// GetOrEmitProtocol - Get the protocol object for the given
1041 /// declaration, emitting it if necessary. The return value has type
1042 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001043 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001044
1045 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1046 /// object for the given declaration, emitting it if needed. These
1047 /// forward references will be filled in with empty bodies if no
1048 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001049 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001050
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001051 /// EmitProtocolExtension - Generate the protocol extension
1052 /// structure used to store optional instance and class methods, and
1053 /// protocol properties. The return value has type
1054 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001055 llvm::Constant *
1056 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1057 const ConstantVector &OptInstanceMethods,
1058 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001059
1060 /// EmitProtocolList - Generate the list of referenced
1061 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +00001062 llvm::Constant *EmitProtocolList(const std::string &Name,
1063 ObjCProtocolDecl::protocol_iterator begin,
1064 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001065
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001066 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1067 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001068 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001069
Fariborz Jahanianda320092009-01-29 19:24:30 +00001070 public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001071 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001072
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001073 virtual llvm::Function *ModuleInitFunction();
1074
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001075 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001076 QualType ResultType,
1077 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001078 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001079 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001080 const CallArgList &CallArgs,
1081 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001082
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001083 virtual CodeGen::RValue
1084 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001085 QualType ResultType,
1086 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001087 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001088 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001089 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001090 bool IsClassMessage,
1091 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001092
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001093 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001094 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001095
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001096 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001097
1098 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1099 /// untyped one.
1100 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1101 const ObjCMethodDecl *Method);
1102
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001103 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001104
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001105 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001106
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001107 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001108 const ObjCProtocolDecl *PD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00001109
Chris Lattner74391b42009-03-22 21:03:39 +00001110 virtual llvm::Constant *GetPropertyGetFunction();
1111 virtual llvm::Constant *GetPropertySetFunction();
1112 virtual llvm::Constant *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001113
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001114 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1115 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001116 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1117 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001118 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001119 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001120 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1121 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001122 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1123 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001124 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1125 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001126 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1127 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001128 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1129 llvm::Value *dest, llvm::Value *src,
1130 unsigned long size);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001131
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001132 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1133 QualType ObjectTy,
1134 llvm::Value *BaseValue,
1135 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001136 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001137 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001138 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001139 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001140};
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001141
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001142class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001143private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001144 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001145 llvm::GlobalVariable* ObjCEmptyCacheVar;
1146 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001147
Daniel Dunbar11394522009-04-18 08:51:00 +00001148 /// SuperClassReferences - uniqued super class references.
1149 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
1150
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001151 /// MetaClassReferences - uniqued meta class references.
1152 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001153
1154 /// EHTypeReferences - uniqued class ehtype references.
1155 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001156
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001157 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001158 /// legacy messaging dispatch.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001159 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001160
1161 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001162 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001163 bool LegacyDispatchedSelector(Selector Sel);
1164
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001165 /// FinishNonFragileABIModule - Write out global data structures at the end of
1166 /// processing a translation unit.
1167 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001168
Daniel Dunbar463b8762009-05-15 21:48:48 +00001169 /// AddModuleClassList - Add the given list of class pointers to the
1170 /// module with the provided symbol and section names.
1171 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1172 const char *SymbolName,
1173 const char *SectionName);
1174
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00001175 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1176 unsigned InstanceStart,
1177 unsigned InstanceSize,
1178 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001179 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
1180 llvm::Constant *IsAGV,
1181 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001182 llvm::Constant *ClassRoGV,
1183 bool HiddenVisibility);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001184
1185 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
1186
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001187 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
1188
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001189 /// EmitMethodList - Emit the method list for the given
1190 /// implementation. The return value has type MethodListnfABITy.
1191 llvm::Constant *EmitMethodList(const std::string &Name,
1192 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001193 const ConstantVector &Methods);
1194 /// EmitIvarList - Emit the ivar list for the given
1195 /// implementation. If ForClass is true the list of class ivars
1196 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1197 /// interface ivars will be emitted. The return value has type
1198 /// IvarListnfABIPtrTy.
1199 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00001200
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001201 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001202 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001203 unsigned long int offset);
1204
Fariborz Jahanianda320092009-01-29 19:24:30 +00001205 /// GetOrEmitProtocol - Get the protocol object for the given
1206 /// declaration, emitting it if necessary. The return value has type
1207 /// ProtocolPtrTy.
1208 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
1209
1210 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1211 /// object for the given declaration, emitting it if needed. These
1212 /// forward references will be filled in with empty bodies if no
1213 /// definition is seen. The return value has type ProtocolPtrTy.
1214 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
1215
1216 /// EmitProtocolList - Generate the list of referenced
1217 /// protocols. The return value has type ProtocolListPtrTy.
1218 llvm::Constant *EmitProtocolList(const std::string &Name,
1219 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001220 ObjCProtocolDecl::protocol_iterator end);
1221
1222 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1223 QualType ResultType,
1224 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00001225 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001226 QualType Arg0Ty,
1227 bool IsSuper,
1228 const CallArgList &CallArgs);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001229
1230 /// GetClassGlobal - Return the global variable for the Objective-C
1231 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001232 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
1233
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001234 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001235 /// for the given class reference.
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001236 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001237 const ObjCInterfaceDecl *ID);
1238
1239 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1240 /// for the given super class reference.
1241 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1242 const ObjCInterfaceDecl *ID);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001243
1244 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1245 /// meta-data
1246 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
1247 const ObjCInterfaceDecl *ID);
1248
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001249 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1250 /// the given ivar.
1251 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001252 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00001253 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001254 const ObjCIvarDecl *Ivar);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001255
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001256 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1257 /// for the given selector.
1258 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbare588b992009-03-01 04:46:24 +00001259
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001260 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001261 /// interface. The return value has type EHTypePtrTy.
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001262 llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
1263 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001264
1265 const char *getMetaclassSymbolPrefix() const {
1266 return "OBJC_METACLASS_$_";
1267 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001268
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001269 const char *getClassSymbolPrefix() const {
1270 return "OBJC_CLASS_$_";
1271 }
1272
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001273 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001274 uint32_t &InstanceStart,
1275 uint32_t &InstanceSize);
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001276
1277 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001278 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001279 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1280 return CGM.getContext().Selectors.getSelector(0, &II);
1281 }
1282
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001283 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001284 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1285 return CGM.getContext().Selectors.getSelector(1, &II);
1286 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001287
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001288 /// ImplementationIsNonLazy - Check whether the given category or
1289 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001290 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001291
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001292public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001293 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001294 // FIXME. All stubs for now!
1295 virtual llvm::Function *ModuleInitFunction();
1296
1297 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
1298 QualType ResultType,
1299 Selector Sel,
1300 llvm::Value *Receiver,
1301 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001302 const CallArgList &CallArgs,
1303 const ObjCMethodDecl *Method);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001304
1305 virtual CodeGen::RValue
1306 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
1307 QualType ResultType,
1308 Selector Sel,
1309 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001310 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001311 llvm::Value *Receiver,
1312 bool IsClassMessage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001313 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001314
1315 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001316 const ObjCInterfaceDecl *ID);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001317
1318 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001319 { return EmitSelector(Builder, Sel); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001320
1321 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1322 /// untyped one.
1323 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1324 const ObjCMethodDecl *Method)
1325 { return EmitSelector(Builder, Method->getSelector()); }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001326
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001327 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001328
1329 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001330 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001331 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001332
Chris Lattner74391b42009-03-22 21:03:39 +00001333 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001334 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001335 }
Chris Lattner74391b42009-03-22 21:03:39 +00001336 virtual llvm::Constant *GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001337 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001338 }
Chris Lattner74391b42009-03-22 21:03:39 +00001339 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001340 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001341 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001342
1343 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00001344 const Stmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001345 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001346 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001347 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001348 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001349 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001350 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001351 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001352 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001353 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001354 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001355 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001356 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001357 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1358 llvm::Value *dest, llvm::Value *src,
1359 unsigned long size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001360 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1361 QualType ObjectTy,
1362 llvm::Value *BaseValue,
1363 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001364 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001365 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001366 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001367 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001368};
1369
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001370} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001371
1372/* *** Helper Functions *** */
1373
1374/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001375static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
1376 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001377 unsigned idx0,
1378 unsigned idx1) {
1379 llvm::Value *Idxs[] = {
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001380 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
1381 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001382 };
Owen Anderson3c4972d2009-07-29 18:54:39 +00001383 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001384}
1385
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001386/// hasObjCExceptionAttribute - Return true if this class or any super
1387/// class has the __objc_exception__ attribute.
Douglas Gregor68584ed2009-06-18 16:11:24 +00001388static bool hasObjCExceptionAttribute(ASTContext &Context,
1389 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001390 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001391 return true;
1392 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001393 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001394 return false;
1395}
1396
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001397/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001398
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001399CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
1400 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001401{
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001402 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001403 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001404}
1405
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001406/// GetClass - Return a reference to the class for the given interface
1407/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001408llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001409 const ObjCInterfaceDecl *ID) {
1410 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001411}
1412
1413/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001414llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001415 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001416}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001417llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
1418 *Method) {
1419 return EmitSelector(Builder, Method->getSelector());
1420}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001421
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001422/// Generate a constant CFString object.
1423/*
1424 struct __builtin_CFString {
1425 const int *isa; // point to __CFConstantStringClassReference
1426 int flags;
1427 const char *str;
1428 long length;
1429 };
1430*/
1431
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001432llvm::Constant *CGObjCCommonMac::GenerateConstantString(
Steve Naroff33fdb732009-03-31 16:53:37 +00001433 const ObjCStringLiteral *SL) {
Steve Naroff8d4141f2009-04-01 13:55:36 +00001434 return CGM.GetAddrOfConstantCFString(SL->getString());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001435}
1436
1437/// Generates a message send where the super is the receiver. This is
1438/// a message send to self with special delivery semantics indicating
1439/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001440CodeGen::RValue
1441CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001442 QualType ResultType,
1443 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001444 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001445 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001446 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001447 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001448 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001449 // Create and init a super structure; this is a (receiver, class)
1450 // pair we will pass to objc_msgSendSuper.
1451 llvm::Value *ObjCSuper =
1452 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
1453 llvm::Value *ReceiverAsObject =
1454 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
1455 CGF.Builder.CreateStore(ReceiverAsObject,
1456 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001457
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001458 // If this is a class message the metaclass is passed as the target.
1459 llvm::Value *Target;
1460 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001461 if (isCategoryImpl) {
1462 // Message sent to 'super' in a class method defined in a category
1463 // implementation requires an odd treatment.
1464 // If we are in a class method, we must retrieve the
1465 // _metaclass_ for the current class, pointed at by
1466 // the class's "isa" pointer. The following assumes that
1467 // isa" is the first ivar in a class (which it must be).
1468 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1469 Target = CGF.Builder.CreateStructGEP(Target, 0);
1470 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00001471 } else {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001472 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1473 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1474 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1475 Target = Super;
1476 }
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001477 } else {
1478 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1479 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001480 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1481 // ObjCTypes types.
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001482 const llvm::Type *ClassTy =
1483 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001484 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001485 CGF.Builder.CreateStore(Target,
1486 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001487 return EmitLegacyMessageSend(CGF, ResultType,
1488 EmitSelector(CGF.Builder, Sel),
1489 ObjCSuper, ObjCTypes.SuperPtrCTy,
1490 true, CallArgs, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001491}
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001492
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001493/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001494CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001495 QualType ResultType,
1496 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001497 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001498 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001499 const CallArgList &CallArgs,
1500 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001501 return EmitLegacyMessageSend(CGF, ResultType,
1502 EmitSelector(CGF.Builder, Sel),
1503 Receiver, CGF.getContext().getObjCIdType(),
1504 false, CallArgs, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001505}
1506
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001507CodeGen::RValue CGObjCCommonMac::EmitLegacyMessageSend(
1508 CodeGen::CodeGenFunction &CGF,
1509 QualType ResultType,
1510 llvm::Value *Sel,
1511 llvm::Value *Arg0,
1512 QualType Arg0Ty,
1513 bool IsSuper,
1514 const CallArgList &CallArgs,
1515 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001516 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001517 if (!IsSuper)
1518 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001519 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001520 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001521 CGF.getContext().getObjCSelType()));
1522 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001523
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001524 CodeGenTypes &Types = CGM.getTypes();
1525 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001526 // In 64bit ABI, type must be assumed VARARG. In 32bit abi,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001527 // it seems not to matter.
1528 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, (ObjCABI == 2));
1529
1530 llvm::Constant *Fn = NULL;
Daniel Dunbar88b53962009-02-02 22:03:45 +00001531 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001532 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
1533 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001534 } else if (ResultType->isFloatingType()) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001535 if (ObjCABI == 2) {
1536 if (const BuiltinType *BT = ResultType->getAsBuiltinType()) {
1537 BuiltinType::Kind k = BT->getKind();
1538 Fn = (k == BuiltinType::LongDouble) ? ObjCTypes.getSendFpretFn2(IsSuper)
1539 : ObjCTypes.getSendFn2(IsSuper);
Daniel Dunbar42f963d2009-06-10 04:38:50 +00001540 } else {
1541 Fn = ObjCTypes.getSendFn2(IsSuper);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001542 }
Mike Stumpb3589f42009-07-30 22:28:39 +00001543 } else
Mike Stumpf5408fe2009-05-16 07:57:57 +00001544 // FIXME. This currently matches gcc's API for x86-32. May need to change
1545 // for others if we have their API.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001546 Fn = ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001547 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001548 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
1549 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001550 }
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001551 assert(Fn && "EmitLegacyMessageSend - unknown API");
Owen Anderson3c4972d2009-07-29 18:54:39 +00001552 Fn = llvm::ConstantExpr::getBitCast(Fn,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001553 llvm::PointerType::getUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +00001554 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001555}
1556
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001557llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001558 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001559 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001560 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001561 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1562
Owen Anderson3c4972d2009-07-29 18:54:39 +00001563 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001564 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001565}
1566
Fariborz Jahanianda320092009-01-29 19:24:30 +00001567void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001568 // FIXME: We shouldn't need this, the protocol decl should contain enough
1569 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001570 DefinedProtocols.insert(PD->getIdentifier());
1571
1572 // If we have generated a forward reference to this protocol, emit
1573 // it now. Otherwise do nothing, the protocol objects are lazily
1574 // emitted.
1575 if (Protocols.count(PD->getIdentifier()))
1576 GetOrEmitProtocol(PD);
1577}
1578
Fariborz Jahanianda320092009-01-29 19:24:30 +00001579llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001580 if (DefinedProtocols.count(PD->getIdentifier()))
1581 return GetOrEmitProtocol(PD);
1582 return GetOrEmitProtocolRef(PD);
1583}
1584
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001585/*
1586 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1587 struct _objc_protocol {
1588 struct _objc_protocol_extension *isa;
1589 char *protocol_name;
1590 struct _objc_protocol_list *protocol_list;
1591 struct _objc__method_prototype_list *instance_methods;
1592 struct _objc__method_prototype_list *class_methods
1593 };
1594
1595 See EmitProtocolExtension().
1596*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001597llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1598 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1599
1600 // Early exit if a defining object has already been generated.
1601 if (Entry && Entry->hasInitializer())
1602 return Entry;
1603
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001604 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001605 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001606 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1607
Chris Lattner8ec03f52008-11-24 03:54:41 +00001608 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001609
1610 // Construct method lists.
1611 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1612 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00001613 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001614 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001615 ObjCMethodDecl *MD = *i;
1616 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1617 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1618 OptInstanceMethods.push_back(C);
1619 } else {
1620 InstanceMethods.push_back(C);
1621 }
1622 }
1623
Douglas Gregor6ab35242009-04-09 21:40:53 +00001624 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001625 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001626 ObjCMethodDecl *MD = *i;
1627 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1628 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1629 OptClassMethods.push_back(C);
1630 } else {
1631 ClassMethods.push_back(C);
1632 }
1633 }
1634
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001635 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001636 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001637 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001638 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001639 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001640 PD->protocol_begin(),
1641 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001642 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001643 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
1644 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001645 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1646 InstanceMethods);
1647 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001648 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
1649 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001650 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1651 ClassMethods);
Owen Anderson08e25242009-07-27 22:29:56 +00001652 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001653 Values);
1654
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001655 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001656 // Already created, fix the linkage and update the initializer.
1657 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001658 Entry->setInitializer(Init);
1659 } else {
1660 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001661 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001662 llvm::GlobalValue::InternalLinkage,
1663 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00001664 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001665 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001666 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001667 // FIXME: Is this necessary? Why only for protocol?
1668 Entry->setAlignment(4);
1669 }
Chris Lattnerad64e022009-07-17 23:57:13 +00001670 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001671
1672 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001673}
1674
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001675llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001676 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1677
1678 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001679 // We use the initializer as a marker of whether this is a forward
1680 // reference or not. At module finalization we add the empty
1681 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001682 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001683 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001684 llvm::GlobalValue::ExternalLinkage,
1685 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00001686 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001687 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001688 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001689 // FIXME: Is this necessary? Why only for protocol?
1690 Entry->setAlignment(4);
1691 }
1692
1693 return Entry;
1694}
1695
1696/*
1697 struct _objc_protocol_extension {
1698 uint32_t size;
1699 struct objc_method_description_list *optional_instance_methods;
1700 struct objc_method_description_list *optional_class_methods;
1701 struct objc_property_list *instance_properties;
1702 };
1703*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001704llvm::Constant *
1705CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1706 const ConstantVector &OptInstanceMethods,
1707 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001708 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00001709 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001710 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001711 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001712 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001713 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1714 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001715 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1716 OptInstanceMethods);
1717 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001718 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1719 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001720 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1721 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001722 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1723 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001724 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001725
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001726 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001727 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1728 Values[3]->isNullValue())
Owen Anderson69243822009-07-13 04:10:07 +00001729 return VMContext.getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001730
1731 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00001732 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001733
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001734 // No special section, but goes in llvm.used
1735 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
1736 Init,
1737 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001738}
1739
1740/*
1741 struct objc_protocol_list {
1742 struct objc_protocol_list *next;
1743 long count;
1744 Protocol *list[];
1745 };
1746*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001747llvm::Constant *
1748CGObjCMac::EmitProtocolList(const std::string &Name,
1749 ObjCProtocolDecl::protocol_iterator begin,
1750 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001751 std::vector<llvm::Constant*> ProtocolRefs;
1752
Daniel Dunbardbc933702008-08-21 21:57:41 +00001753 for (; begin != end; ++begin)
1754 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001755
1756 // Just return null for empty protocol lists
1757 if (ProtocolRefs.empty())
Owen Anderson69243822009-07-13 04:10:07 +00001758 return VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001759
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001760 // This list is null terminated.
Owen Anderson69243822009-07-13 04:10:07 +00001761 ProtocolRefs.push_back(VMContext.getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001762
1763 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001764 // This field is only used by the runtime.
Owen Anderson69243822009-07-13 04:10:07 +00001765 Values[0] = VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001766 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001767 ProtocolRefs.size() - 1);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001768 Values[2] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00001769 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001770 ProtocolRefs.size()),
1771 ProtocolRefs);
1772
Owen Anderson08e25242009-07-27 22:29:56 +00001773 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001774 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001775 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001776 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001777 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001778}
1779
1780/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001781 struct _objc_property {
1782 const char * const name;
1783 const char * const attributes;
1784 };
1785
1786 struct _objc_property_list {
1787 uint32_t entsize; // sizeof (struct _objc_property)
1788 uint32_t prop_count;
1789 struct _objc_property[prop_count];
1790 };
1791*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001792llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1793 const Decl *Container,
1794 const ObjCContainerDecl *OCD,
1795 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001796 std::vector<llvm::Constant*> Properties, Prop(2);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001797 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1798 E = OCD->prop_end(); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00001799 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001800 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001801 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson08e25242009-07-27 22:29:56 +00001802 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001803 Prop));
1804 }
1805
1806 // Return null for empty list.
1807 if (Properties.empty())
Owen Anderson69243822009-07-13 04:10:07 +00001808 return VMContext.getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001809
1810 unsigned PropertySize =
Duncan Sands9408c452009-05-09 07:08:47 +00001811 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001812 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001813 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1814 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00001815 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001816 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00001817 Values[2] = llvm::ConstantArray::get(AT, Properties);
Owen Anderson08e25242009-07-27 22:29:56 +00001818 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001819
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001820 llvm::GlobalVariable *GV =
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001821 CreateMetadataVar(Name, Init,
1822 (ObjCABI == 2) ? "__DATA, __objc_const" :
1823 "__OBJC,__property,regular,no_dead_strip",
1824 (ObjCABI == 2) ? 8 : 4,
1825 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001826 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001827}
1828
1829/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001830 struct objc_method_description_list {
1831 int count;
1832 struct objc_method_description list[];
1833 };
1834*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001835llvm::Constant *
1836CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1837 std::vector<llvm::Constant*> Desc(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001838 Desc[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00001839 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001840 ObjCTypes.SelectorPtrTy);
1841 Desc[1] = GetMethodVarType(MD);
Owen Anderson08e25242009-07-27 22:29:56 +00001842 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001843 Desc);
1844}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001845
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001846llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1847 const char *Section,
1848 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001849 // Return null for empty list.
1850 if (Methods.empty())
Owen Anderson69243822009-07-13 04:10:07 +00001851 return VMContext.getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001852
1853 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001854 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00001855 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001856 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00001857 Values[1] = llvm::ConstantArray::get(AT, Methods);
Owen Anderson08e25242009-07-27 22:29:56 +00001858 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001859
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001860 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001861 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001862 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001863}
1864
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001865/*
1866 struct _objc_category {
1867 char *category_name;
1868 char *class_name;
1869 struct _objc_method_list *instance_methods;
1870 struct _objc_method_list *class_methods;
1871 struct _objc_protocol_list *protocols;
1872 uint32_t size; // <rdar://4585769>
1873 struct _objc_property_list *instance_properties;
1874 };
1875 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001876void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sands9408c452009-05-09 07:08:47 +00001877 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001878
Mike Stumpf5408fe2009-05-16 07:57:57 +00001879 // FIXME: This is poor design, the OCD should have a pointer to the category
1880 // decl. Additionally, note that Category can be null for the @implementation
1881 // w/o an @interface case. Sema should just create one for us as it does for
1882 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001883 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001884 const ObjCCategoryDecl *Category =
1885 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001886 std::string ExtName(Interface->getNameAsString() + "_" +
1887 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001888
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001889 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001890 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001891 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001892 // Instance methods should always be defined.
1893 InstanceMethods.push_back(GetMethodConstant(*i));
1894 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001895 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001896 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001897 // Class methods should always be defined.
1898 ClassMethods.push_back(GetMethodConstant(*i));
1899 }
1900
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001901 std::vector<llvm::Constant*> Values(7);
1902 Values[0] = GetClassName(OCD->getIdentifier());
1903 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00001904 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001905 Values[2] =
1906 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1907 ExtName,
1908 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001909 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001910 Values[3] =
1911 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001912 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001913 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001914 if (Category) {
1915 Values[4] =
1916 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1917 Category->protocol_begin(),
1918 Category->protocol_end());
1919 } else {
Owen Anderson69243822009-07-13 04:10:07 +00001920 Values[4] = VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001921 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001922 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001923
1924 // If there is no category @interface then there can be no properties.
1925 if (Category) {
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001926 Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001927 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001928 } else {
Owen Anderson69243822009-07-13 04:10:07 +00001929 Values[6] = VMContext.getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001930 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001931
Owen Anderson08e25242009-07-27 22:29:56 +00001932 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001933 Values);
1934
1935 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001936 CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init,
1937 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001938 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001939 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001940}
1941
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001942// FIXME: Get from somewhere?
1943enum ClassFlags {
1944 eClassFlags_Factory = 0x00001,
1945 eClassFlags_Meta = 0x00002,
1946 // <rdr://5142207>
1947 eClassFlags_HasCXXStructors = 0x02000,
1948 eClassFlags_Hidden = 0x20000,
1949 eClassFlags_ABI2_Hidden = 0x00010,
1950 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1951};
1952
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001953/*
1954 struct _objc_class {
1955 Class isa;
1956 Class super_class;
1957 const char *name;
1958 long version;
1959 long info;
1960 long instance_size;
1961 struct _objc_ivar_list *ivars;
1962 struct _objc_method_list *methods;
1963 struct _objc_cache *cache;
1964 struct _objc_protocol_list *protocols;
1965 // Objective-C 1.0 extensions (<rdr://4585769>)
1966 const char *ivar_layout;
1967 struct _objc_class_ext *ext;
1968 };
1969
1970 See EmitClassExtension();
1971 */
1972void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001973 DefinedSymbols.insert(ID->getIdentifier());
1974
Chris Lattner8ec03f52008-11-24 03:54:41 +00001975 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001976 // FIXME: Gross
1977 ObjCInterfaceDecl *Interface =
1978 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001979 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001980 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001981 Interface->protocol_begin(),
1982 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001983 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar2bebbf02009-05-03 10:46:44 +00001984 unsigned Size =
1985 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001986
1987 // FIXME: Set CXX-structors flag.
Daniel Dunbar04d40782009-04-14 06:00:08 +00001988 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001989 Flags |= eClassFlags_Hidden;
1990
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001991 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001992 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001993 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001994 // Instance methods should always be defined.
1995 InstanceMethods.push_back(GetMethodConstant(*i));
1996 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001997 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001998 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001999 // Class methods should always be defined.
2000 ClassMethods.push_back(GetMethodConstant(*i));
2001 }
2002
Douglas Gregor653f1b12009-04-23 01:02:12 +00002003 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002004 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002005 ObjCPropertyImplDecl *PID = *i;
2006
2007 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2008 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2009
2010 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2011 if (llvm::Constant *C = GetMethodConstant(MD))
2012 InstanceMethods.push_back(C);
2013 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2014 if (llvm::Constant *C = GetMethodConstant(MD))
2015 InstanceMethods.push_back(C);
2016 }
2017 }
2018
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002019 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar5384b092009-05-03 08:56:52 +00002020 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002021 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002022 // Record a reference to the super class.
2023 LazySymbols.insert(Super->getIdentifier());
2024
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002025 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002026 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002027 ObjCTypes.ClassPtrTy);
2028 } else {
Owen Anderson69243822009-07-13 04:10:07 +00002029 Values[ 1] = VMContext.getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002030 }
2031 Values[ 2] = GetClassName(ID->getIdentifier());
2032 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002033 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2034 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2035 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002036 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002037 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002038 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002039 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002040 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002041 // cache is always NULL.
Owen Anderson69243822009-07-13 04:10:07 +00002042 Values[ 8] = VMContext.getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002043 Values[ 9] = Protocols;
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002044 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002045 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00002046 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002047 Values);
2048
2049 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002050 CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init,
2051 "__OBJC,__class,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002052 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002053 DefinedClasses.push_back(GV);
2054}
2055
2056llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2057 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002058 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002059 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002060 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002061
Daniel Dunbar04d40782009-04-14 06:00:08 +00002062 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002063 Flags |= eClassFlags_Hidden;
2064
2065 std::vector<llvm::Constant*> Values(12);
2066 // The isa for the metaclass is the root of the hierarchy.
2067 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2068 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2069 Root = Super;
2070 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002071 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002072 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002073 // The super class for the metaclass is emitted as the name of the
2074 // super class. The runtime fixes this up to point to the
2075 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002076 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
2077 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002078 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002079 ObjCTypes.ClassPtrTy);
2080 } else {
Owen Anderson69243822009-07-13 04:10:07 +00002081 Values[ 1] = VMContext.getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002082 }
2083 Values[ 2] = GetClassName(ID->getIdentifier());
2084 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002085 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2086 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2087 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002088 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002089 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002090 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002091 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002092 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002093 // cache is always NULL.
Owen Anderson69243822009-07-13 04:10:07 +00002094 Values[ 8] = VMContext.getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002095 Values[ 9] = Protocols;
2096 // ivar_layout for metaclass is always NULL.
Owen Anderson69243822009-07-13 04:10:07 +00002097 Values[10] = VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002098 // The class extension is always unused for metaclasses.
Owen Anderson69243822009-07-13 04:10:07 +00002099 Values[11] = VMContext.getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002100 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002101 Values);
2102
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002103 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002104 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002105
2106 // Check for a forward reference.
2107 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2108 if (GV) {
2109 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2110 "Forward metaclass reference has incorrect type.");
2111 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2112 GV->setInitializer(Init);
2113 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002114 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002115 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002116 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002117 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002118 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002119 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002120 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002121
2122 return GV;
2123}
2124
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002125llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002126 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002127
Mike Stumpf5408fe2009-05-16 07:57:57 +00002128 // FIXME: Should we look these up somewhere other than the module. Its a bit
2129 // silly since we only generate these while processing an implementation, so
2130 // exactly one pointer would work if know when we entered/exitted an
2131 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002132
2133 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002134 // Previously, metaclass with internal linkage may have been defined.
2135 // pass 'true' as 2nd argument so it is returned.
2136 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002137 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2138 "Forward metaclass reference has incorrect type.");
2139 return GV;
2140 } else {
2141 // Generate as an external reference to keep a consistent
2142 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002143 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002144 llvm::GlobalValue::ExternalLinkage,
2145 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002146 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002147 }
2148}
2149
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002150/*
2151 struct objc_class_ext {
2152 uint32_t size;
2153 const char *weak_ivar_layout;
2154 struct _objc_property_list *properties;
2155 };
2156*/
2157llvm::Constant *
2158CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
2159 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002160 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002161
2162 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002163 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002164 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002165 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002166 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002167
2168 // Return null if no extension bits are used.
2169 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson69243822009-07-13 04:10:07 +00002170 return VMContext.getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002171
2172 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002173 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002174 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002175 Init, "__OBJC,__class_ext,regular,no_dead_strip",
2176 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002177}
2178
2179/*
2180 struct objc_ivar {
2181 char *ivar_name;
2182 char *ivar_type;
2183 int ivar_offset;
2184 };
2185
2186 struct objc_ivar_list {
2187 int ivar_count;
2188 struct objc_ivar list[count];
2189 };
2190 */
2191llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002192 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002193 std::vector<llvm::Constant*> Ivars, Ivar(3);
2194
2195 // When emitting the root class GCC emits ivar entries for the
2196 // actual class structure. It is not clear if we need to follow this
2197 // behavior; for now lets try and get away with not doing it. If so,
2198 // the cleanest solution would be to make up an ObjCInterfaceDecl
2199 // for the class.
2200 if (ForClass)
Owen Anderson69243822009-07-13 04:10:07 +00002201 return VMContext.getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002202
2203 ObjCInterfaceDecl *OID =
2204 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002205
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002206 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002207 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002208
2209 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2210 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002211 // Ignore unnamed bit-fields.
2212 if (!IVD->getDeclName())
2213 continue;
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002214 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2215 Ivar[1] = GetMethodVarType(IVD);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002216 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002217 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson08e25242009-07-27 22:29:56 +00002218 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002219 }
2220
2221 // Return null for empty list.
2222 if (Ivars.empty())
Owen Anderson69243822009-07-13 04:10:07 +00002223 return VMContext.getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002224
2225 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002226 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002227 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002228 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002229 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Owen Anderson08e25242009-07-27 22:29:56 +00002230 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002231
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002232 llvm::GlobalVariable *GV;
2233 if (ForClass)
2234 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(),
Daniel Dunbar58a29122009-03-09 22:18:41 +00002235 Init, "__OBJC,__class_vars,regular,no_dead_strip",
2236 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002237 else
2238 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_"
2239 + ID->getNameAsString(),
2240 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002241 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002242 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002243}
2244
2245/*
2246 struct objc_method {
2247 SEL method_name;
2248 char *method_types;
2249 void *method;
2250 };
2251
2252 struct objc_method_list {
2253 struct objc_method_list *obsolete;
2254 int count;
2255 struct objc_method methods_list[count];
2256 };
2257*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002258
2259/// GetMethodConstant - Return a struct objc_method constant for the
2260/// given method if it has been defined. The result is null if the
2261/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002262llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002263 // FIXME: Use DenseMap::lookup
2264 llvm::Function *Fn = MethodDefinitions[MD];
2265 if (!Fn)
2266 return 0;
2267
2268 std::vector<llvm::Constant*> Method(3);
2269 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002270 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002271 ObjCTypes.SelectorPtrTy);
2272 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002273 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002274 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002275}
2276
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002277llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
2278 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002279 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002280 // Return null for empty list.
2281 if (Methods.empty())
Owen Anderson69243822009-07-13 04:10:07 +00002282 return VMContext.getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002283
2284 std::vector<llvm::Constant*> Values(3);
Owen Anderson69243822009-07-13 04:10:07 +00002285 Values[0] = VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002286 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002287 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002288 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002289 Values[2] = llvm::ConstantArray::get(AT, Methods);
Owen Anderson08e25242009-07-27 22:29:56 +00002290 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002291
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002292 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002293 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002294 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002295}
2296
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002297llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00002298 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002299 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002300 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002301
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002302 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002303 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002304 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002305 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002306 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002307 llvm::GlobalValue::InternalLinkage,
2308 Name,
2309 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002310 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002311
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002312 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002313}
2314
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002315llvm::GlobalVariable *
2316CGObjCCommonMac::CreateMetadataVar(const std::string &Name,
2317 llvm::Constant *Init,
2318 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002319 unsigned Align,
2320 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002321 const llvm::Type *Ty = Init->getType();
2322 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002323 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002324 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002325 if (Section)
2326 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002327 if (Align)
2328 GV->setAlignment(Align);
2329 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002330 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002331 return GV;
2332}
2333
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002334llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002335 // Abuse this interface function as a place to finalize.
2336 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002337 return NULL;
2338}
2339
Chris Lattner74391b42009-03-22 21:03:39 +00002340llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002341 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002342}
2343
Chris Lattner74391b42009-03-22 21:03:39 +00002344llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002345 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002346}
2347
Chris Lattner74391b42009-03-22 21:03:39 +00002348llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002349 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002350}
2351
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002352/*
2353
2354Objective-C setjmp-longjmp (sjlj) Exception Handling
2355--
2356
2357The basic framework for a @try-catch-finally is as follows:
2358{
2359 objc_exception_data d;
2360 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002361 bool _call_try_exit = true;
2362
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002363 objc_exception_try_enter(&d);
2364 if (!setjmp(d.jmp_buf)) {
2365 ... try body ...
2366 } else {
2367 // exception path
2368 id _caught = objc_exception_extract(&d);
2369
2370 // enter new try scope for handlers
2371 if (!setjmp(d.jmp_buf)) {
2372 ... match exception and execute catch blocks ...
2373
2374 // fell off end, rethrow.
2375 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00002376 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002377 } else {
2378 // exception in catch block
2379 _rethrow = objc_exception_extract(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002380 _call_try_exit = false;
2381 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002382 }
2383 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002384 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002385
2386finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002387 if (_call_try_exit)
2388 objc_exception_try_exit(&d);
2389
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002390 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002391 ... dispatch to finally destination ...
2392
2393finally_rethrow:
2394 objc_exception_throw(_rethrow);
2395
2396finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002397}
2398
2399This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00002400uses _rethrow to determine if objc_exception_try_exit should be called
2401and if the object should be rethrown. This breaks in the face of
2402throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002403
2404We specialize this framework for a few particular circumstances:
2405
2406 - If there are no catch blocks, then we avoid emitting the second
2407 exception handling context.
2408
2409 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2410 e)) we avoid emitting the code to rethrow an uncaught exception.
2411
2412 - FIXME: If there is no @finally block we can do a few more
2413 simplifications.
2414
2415Rethrows and Jumps-Through-Finally
2416--
2417
2418Support for implicit rethrows and jumping through the finally block is
2419handled by storing the current exception-handling context in
2420ObjCEHStack.
2421
Daniel Dunbar898d5082008-09-30 01:06:03 +00002422In order to implement proper @finally semantics, we support one basic
2423mechanism for jumping through the finally block to an arbitrary
2424destination. Constructs which generate exits from a @try or @catch
2425block use this mechanism to implement the proper semantics by chaining
2426jumps, as necessary.
2427
2428This mechanism works like the one used for indirect goto: we
2429arbitrarily assign an ID to each destination and store the ID for the
2430destination in a variable prior to entering the finally block. At the
2431end of the finally block we simply create a switch to the proper
2432destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002433
2434Code gen for @synchronized(expr) stmt;
2435Effectively generating code for:
2436objc_sync_enter(expr);
2437@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002438*/
2439
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002440void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2441 const Stmt &S) {
2442 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002443 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00002444 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002445 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar55e87422008-11-11 02:29:29 +00002446 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
2447 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
2448 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar1c566672009-02-24 01:43:46 +00002449
2450 // For @synchronized, call objc_sync_enter(sync.expr). The
2451 // evaluation of the expression must occur before we enter the
2452 // @synchronized. We can safely avoid a temp here because jumps into
2453 // @synchronized are illegal & this will dominate uses.
2454 llvm::Value *SyncArg = 0;
2455 if (!isTry) {
2456 SyncArg =
2457 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2458 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00002459 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002460 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002461
2462 // Push an EH context entry, used for handling rethrows and jumps
2463 // through finally.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002464 CGF.PushCleanupBlock(FinallyBlock);
2465
Anders Carlsson273558f2009-02-07 21:37:21 +00002466 CGF.ObjCEHValueStack.push_back(0);
2467
Daniel Dunbar898d5082008-09-30 01:06:03 +00002468 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00002469 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2470 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002471 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
2472 "_rethrow");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002473 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
2474 "_call_try_exit");
Owen Anderson4cd16082009-07-21 18:06:41 +00002475 CGF.Builder.CreateStore(VMContext.getTrue(), CallTryExitPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002476
Anders Carlsson80f25672008-09-09 17:59:25 +00002477 // Enter a new try block and call setjmp.
Chris Lattner34b02a12009-04-22 02:26:14 +00002478 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Anders Carlsson80f25672008-09-09 17:59:25 +00002479 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
2480 "jmpbufarray");
2481 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
Chris Lattner34b02a12009-04-22 02:26:14 +00002482 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlsson80f25672008-09-09 17:59:25 +00002483 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00002484
Daniel Dunbar55e87422008-11-11 02:29:29 +00002485 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2486 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002487 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002488 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002489
2490 // Emit the @try block.
2491 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002492 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
2493 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002494 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002495
2496 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002497 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002498
2499 // Retrieve the exception object. We may emit multiple blocks but
2500 // nothing can cross this so the value is already in SSA form.
Chris Lattner34b02a12009-04-22 02:26:14 +00002501 llvm::Value *Caught =
2502 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2503 ExceptionData, "caught");
Anders Carlsson273558f2009-02-07 21:37:21 +00002504 CGF.ObjCEHValueStack.back() = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002505 if (!isTry)
2506 {
2507 CGF.Builder.CreateStore(Caught, RethrowPtr);
Owen Anderson4cd16082009-07-21 18:06:41 +00002508 CGF.Builder.CreateStore(VMContext.getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002509 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Mike Stumpb3589f42009-07-30 22:28:39 +00002510 } else if (const ObjCAtCatchStmt* CatchStmt =
2511 cast<ObjCAtTryStmt>(S).getCatchStmts()) {
Daniel Dunbar55e40722008-09-27 07:03:52 +00002512 // Enter a new exception try block (in case a @catch block throws
2513 // an exception).
Chris Lattner34b02a12009-04-22 02:26:14 +00002514 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002515
Chris Lattner34b02a12009-04-22 02:26:14 +00002516 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlsson80f25672008-09-09 17:59:25 +00002517 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002518 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00002519
Daniel Dunbar55e87422008-11-11 02:29:29 +00002520 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
2521 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002522 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002523
2524 CGF.EmitBlock(CatchBlock);
2525
Daniel Dunbar55e40722008-09-27 07:03:52 +00002526 // Handle catch list. As a special case we check if everything is
2527 // matched and avoid generating code for falling off the end if
2528 // so.
2529 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00002530 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00002531 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00002532
Steve Naroff7ba138a2009-03-03 19:52:17 +00002533 const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00002534 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00002535
Anders Carlsson80f25672008-09-09 17:59:25 +00002536 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00002537 if (!CatchParam) {
2538 AllMatched = true;
2539 } else {
Steve Naroff14108da2009-07-10 23:34:53 +00002540 OPT = CatchParam->getType()->getAsObjCObjectPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002541
Daniel Dunbar97f61d12008-09-27 22:21:14 +00002542 // catch(id e) always matches.
2543 // FIXME: For the time being we also match id<X>; this should
2544 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00002545 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00002546 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00002547 }
2548
Daniel Dunbar55e40722008-09-27 07:03:52 +00002549 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00002550 if (CatchParam) {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002551 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002552 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002553 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002554 }
Anders Carlsson1452f552008-09-11 08:21:54 +00002555
Anders Carlssondde0a942008-09-11 09:15:33 +00002556 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002557 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002558 break;
2559 }
2560
Steve Naroff14108da2009-07-10 23:34:53 +00002561 assert(OPT && "Unexpected non-object pointer type in @catch");
2562 QualType T = OPT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00002563 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002564 assert(ObjCType && "Catch parameter must have Objective-C type!");
2565
2566 // Check if the @catch block matches the exception object.
2567 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
2568
Chris Lattner34b02a12009-04-22 02:26:14 +00002569 llvm::Value *Match =
2570 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
2571 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00002572
Daniel Dunbar55e87422008-11-11 02:29:29 +00002573 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00002574
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002575 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002576 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002577
2578 // Emit the @catch block.
2579 CGF.EmitBlock(MatchedBlock);
Steve Naroff7ba138a2009-03-03 19:52:17 +00002580 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002581 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002582
2583 llvm::Value *Tmp =
Steve Naroff7ba138a2009-03-03 19:52:17 +00002584 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002585 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002586 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002587
2588 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002589 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002590
2591 CGF.EmitBlock(NextCatchBlock);
2592 }
2593
Daniel Dunbar55e40722008-09-27 07:03:52 +00002594 if (!AllMatched) {
2595 // None of the handlers caught the exception, so store it to be
2596 // rethrown at the end of the @finally block.
2597 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002598 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002599 }
2600
2601 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002602 CGF.EmitBlock(CatchHandler);
Chris Lattner34b02a12009-04-22 02:26:14 +00002603 CGF.Builder.CreateStore(
2604 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2605 ExceptionData),
Daniel Dunbar55e40722008-09-27 07:03:52 +00002606 RethrowPtr);
Owen Anderson4cd16082009-07-21 18:06:41 +00002607 CGF.Builder.CreateStore(VMContext.getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002608 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002609 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00002610 CGF.Builder.CreateStore(Caught, RethrowPtr);
Owen Anderson4cd16082009-07-21 18:06:41 +00002611 CGF.Builder.CreateStore(VMContext.getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002612 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson80f25672008-09-09 17:59:25 +00002613 }
2614
Daniel Dunbar898d5082008-09-30 01:06:03 +00002615 // Pop the exception-handling stack entry. It is important to do
2616 // this now, because the code in the @finally block is not in this
2617 // context.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002618 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2619
Anders Carlsson273558f2009-02-07 21:37:21 +00002620 CGF.ObjCEHValueStack.pop_back();
2621
Anders Carlsson80f25672008-09-09 17:59:25 +00002622 // Emit the @finally block.
2623 CGF.EmitBlock(FinallyBlock);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002624 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2625
2626 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2627
2628 CGF.EmitBlock(FinallyExit);
Chris Lattner34b02a12009-04-22 02:26:14 +00002629 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002630
2631 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002632 if (isTry) {
2633 if (const ObjCAtFinallyStmt* FinallyStmt =
2634 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2635 CGF.EmitStmt(FinallyStmt->getFinallyBody());
Daniel Dunbar1c566672009-02-24 01:43:46 +00002636 } else {
2637 // Emit objc_sync_exit(expr); as finally's sole statement for
2638 // @synchronized.
Chris Lattnerbbccd612009-04-22 02:38:11 +00002639 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002640 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002641
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002642 // Emit the switch block
2643 if (Info.SwitchBlock)
2644 CGF.EmitBlock(Info.SwitchBlock);
2645 if (Info.EndBlock)
2646 CGF.EmitBlock(Info.EndBlock);
2647
Daniel Dunbar898d5082008-09-30 01:06:03 +00002648 CGF.EmitBlock(FinallyRethrow);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002649 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbar898d5082008-09-30 01:06:03 +00002650 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002651 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00002652
2653 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002654}
2655
2656void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002657 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002658 llvm::Value *ExceptionAsObject;
2659
2660 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2661 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2662 ExceptionAsObject =
2663 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2664 } else {
Anders Carlsson273558f2009-02-07 21:37:21 +00002665 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002666 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00002667 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002668 }
2669
Chris Lattnerbbccd612009-04-22 02:38:11 +00002670 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002671 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002672
2673 // Clear the insertion point to indicate we are in unreachable code.
2674 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002675}
2676
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002677/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002678/// object: objc_read_weak (id *src)
2679///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002680llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002681 llvm::Value *AddrWeakObj)
2682{
Eli Friedman8339b352009-03-07 03:57:15 +00002683 const llvm::Type* DestTy =
2684 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002685 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00002686 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002687 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00002688 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002689 return read_weak;
2690}
2691
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002692/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2693/// objc_assign_weak (id src, id *dst)
2694///
2695void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2696 llvm::Value *src, llvm::Value *dst)
2697{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002698 const llvm::Type * SrcTy = src->getType();
2699 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002700 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002701 assert(Size <= 8 && "does not support size > 8");
2702 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2703 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002704 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2705 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002706 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2707 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00002708 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002709 src, dst, "weakassign");
2710 return;
2711}
2712
Fariborz Jahanian58626502008-11-19 00:59:10 +00002713/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2714/// objc_assign_global (id src, id *dst)
2715///
2716void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2717 llvm::Value *src, llvm::Value *dst)
2718{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002719 const llvm::Type * SrcTy = src->getType();
2720 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002721 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002722 assert(Size <= 8 && "does not support size > 8");
2723 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2724 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002725 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2726 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002727 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2728 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002729 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00002730 src, dst, "globalassign");
2731 return;
2732}
2733
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002734/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2735/// objc_assign_ivar (id src, id *dst)
2736///
2737void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2738 llvm::Value *src, llvm::Value *dst)
2739{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002740 const llvm::Type * SrcTy = src->getType();
2741 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002742 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002743 assert(Size <= 8 && "does not support size > 8");
2744 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2745 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002746 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2747 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002748 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2749 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002750 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(),
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002751 src, dst, "assignivar");
2752 return;
2753}
2754
Fariborz Jahanian58626502008-11-19 00:59:10 +00002755/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2756/// objc_assign_strongCast (id src, id *dst)
2757///
2758void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2759 llvm::Value *src, llvm::Value *dst)
2760{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002761 const llvm::Type * SrcTy = src->getType();
2762 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002763 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002764 assert(Size <= 8 && "does not support size > 8");
2765 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2766 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002767 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2768 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002769 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2770 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002771 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00002772 src, dst, "weakassign");
2773 return;
2774}
2775
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002776void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
2777 llvm::Value *DestPtr,
2778 llvm::Value *SrcPtr,
2779 unsigned long size) {
2780 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
2781 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002782 llvm::Value *N = llvm::ConstantInt::get(ObjCTypes.LongTy, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002783 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
2784 DestPtr, SrcPtr, N);
2785 return;
2786}
2787
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002788/// EmitObjCValueForIvar - Code Gen for ivar reference.
2789///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002790LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2791 QualType ObjectTy,
2792 llvm::Value *BaseValue,
2793 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002794 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00002795 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00002796 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
2797 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002798}
2799
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002800llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00002801 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002802 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00002803 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002804 return llvm::ConstantInt::get(
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002805 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2806 Offset);
2807}
2808
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002809/* *** Private Interface *** */
2810
2811/// EmitImageInfo - Emit the image info marker used to encode some module
2812/// level information.
2813///
2814/// See: <rdr://4810609&4810587&4810587>
2815/// struct IMAGE_INFO {
2816/// unsigned version;
2817/// unsigned flags;
2818/// };
2819enum ImageInfoFlags {
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002820 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what
2821 // this implies.
2822 eImageInfo_GarbageCollected = (1 << 1),
2823 eImageInfo_GCOnly = (1 << 2),
2824 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
2825
2826 // A flag indicating that the module has no instances of an
2827 // @synthesize of a superclass variable. <rdar://problem/6803242>
2828 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002829};
2830
2831void CGObjCMac::EmitImageInfo() {
2832 unsigned version = 0; // Version is unused?
2833 unsigned flags = 0;
2834
2835 // FIXME: Fix and continue?
2836 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2837 flags |= eImageInfo_GarbageCollected;
2838 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2839 flags |= eImageInfo_GCOnly;
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002840
2841 // We never allow @synthesize of a superclass property.
2842 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002843
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002844 // Emitted as int[2];
2845 llvm::Constant *values[2] = {
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002846 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2847 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002848 };
Owen Anderson96e0fc72009-07-29 22:16:19 +00002849 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002850
2851 const char *Section;
2852 if (ObjCABI == 1)
2853 Section = "__OBJC, __image_info,regular";
2854 else
2855 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002856 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002857 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson7db6d832009-07-28 18:33:04 +00002858 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002859 Section,
2860 0,
2861 true);
2862 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002863}
2864
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002865
2866// struct objc_module {
2867// unsigned long version;
2868// unsigned long size;
2869// const char *name;
2870// Symtab symtab;
2871// };
2872
2873// FIXME: Get from somewhere
2874static const int ModuleVersion = 7;
2875
2876void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00002877 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002878
2879 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002880 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2881 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002882 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002883 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002884 Values[3] = EmitModuleSymbols();
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002885 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00002886 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002887 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002888 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002889}
2890
2891llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002892 unsigned NumClasses = DefinedClasses.size();
2893 unsigned NumCategories = DefinedCategories.size();
2894
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002895 // Return null if no symbols were defined.
2896 if (!NumClasses && !NumCategories)
Owen Anderson69243822009-07-13 04:10:07 +00002897 return VMContext.getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002898
2899 std::vector<llvm::Constant*> Values(5);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002900 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson69243822009-07-13 04:10:07 +00002901 Values[1] = VMContext.getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002902 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2903 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002904
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002905 // The runtime expects exactly the list of defined classes followed
2906 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002907 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002908 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00002909 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002910 ObjCTypes.Int8PtrTy);
2911 for (unsigned i=0; i<NumCategories; i++)
2912 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002913 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002914 ObjCTypes.Int8PtrTy);
2915
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002916 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00002917 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002918 NumClasses + NumCategories),
2919 Symbols);
2920
Owen Anderson08e25242009-07-27 22:29:56 +00002921 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002922
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002923 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002924 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
2925 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002926 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002927 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002928}
2929
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002930llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002931 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002932 LazySymbols.insert(ID->getIdentifier());
2933
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002934 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2935
2936 if (!Entry) {
2937 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002938 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002939 ObjCTypes.ClassPtrTy);
2940 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002941 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
2942 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002943 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002944 }
2945
2946 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002947}
2948
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002949llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002950 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2951
2952 if (!Entry) {
2953 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002954 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002955 ObjCTypes.SelectorPtrTy);
2956 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002957 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
2958 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002959 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002960 }
2961
2962 return Builder.CreateLoad(Entry, false, "tmp");
2963}
2964
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002965llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002966 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002967
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002968 if (!Entry)
2969 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Owen Anderson7db6d832009-07-28 18:33:04 +00002970 llvm::ConstantArray::get(Ident->getName()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002971 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00002972 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002973
Owen Andersona1cf15f2009-07-14 23:10:40 +00002974 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002975}
2976
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00002977/// GetIvarLayoutName - Returns a unique constant for the given
2978/// ivar layout bitmap.
2979llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
2980 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson69243822009-07-13 04:10:07 +00002981 return VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00002982}
2983
Daniel Dunbard58edcb2009-05-03 14:10:34 +00002984static QualType::GCAttrTypes GetGCAttrTypeForType(ASTContext &Ctx,
2985 QualType FQT) {
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00002986 if (FQT.isObjCGCStrong())
2987 return QualType::Strong;
2988
2989 if (FQT.isObjCGCWeak())
2990 return QualType::Weak;
2991
Steve Narofff4954562009-07-16 15:41:00 +00002992 if (FQT->isObjCObjectPointerType())
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00002993 return QualType::Strong;
2994
Ted Kremenek6217b802009-07-29 21:53:49 +00002995 if (const PointerType *PT = FQT->getAs<PointerType>())
Daniel Dunbard58edcb2009-05-03 14:10:34 +00002996 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00002997
2998 return QualType::GCNone;
2999}
3000
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003001void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
3002 unsigned int BytePos,
3003 bool ForStrongLayout,
3004 bool &HasUnion) {
3005 const RecordDecl *RD = RT->getDecl();
3006 // FIXME - Use iterator.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003007 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003008 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
3009 const llvm::StructLayout *RecLayout =
3010 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
3011
3012 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3013 ForStrongLayout, HasUnion);
3014}
3015
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003016void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003017 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003018 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00003019 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003020 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003021 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003022 bool IsUnion = (RD && RD->isUnion());
3023 uint64_t MaxUnionIvarSize = 0;
3024 uint64_t MaxSkippedUnionIvarSize = 0;
3025 FieldDecl *MaxField = 0;
3026 FieldDecl *MaxSkippedField = 0;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003027 FieldDecl *LastFieldBitfield = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003028 uint64_t MaxFieldOffset = 0;
3029 uint64_t MaxSkippedFieldOffset = 0;
3030 uint64_t LastBitfieldOffset = 0;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003031
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003032 if (RecFields.empty())
3033 return;
Chris Lattnerf1690852009-03-31 08:48:01 +00003034 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3035 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3036
Chris Lattnerf1690852009-03-31 08:48:01 +00003037 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003038 FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003039 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003040 if (RD) {
3041 if (Field->isBitField()) {
3042 CodeGenTypes::BitFieldInfo Info = CGM.getTypes().getBitFieldInfo(Field);
3043 FieldOffset = Layout->getElementOffset(Info.FieldNo);
3044 } else
3045 FieldOffset =
3046 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
3047 } else
Daniel Dunbare05cc982009-05-03 23:35:23 +00003048 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003049
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003050 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003051 if (!Field->getIdentifier() || Field->isBitField()) {
3052 LastFieldBitfield = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003053 LastBitfieldOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003054 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003055 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003056
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003057 LastFieldBitfield = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003058 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003059 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003060 if (FQT->isUnionType())
3061 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003062
Ted Kremenek6217b802009-07-29 21:53:49 +00003063 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003064 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003065 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003066 continue;
3067 }
Chris Lattnerf1690852009-03-31 08:48:01 +00003068
3069 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003070 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003071 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003072 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003073 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003074 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003075 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3076 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003077 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003078 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003079 FQT = CArray->getElementType();
3080 }
3081
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003082 assert(!FQT->isUnionType() &&
3083 "layout for array of unions not supported");
3084 if (FQT->isRecordType()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003085 int OldIndex = IvarsInfo.size() - 1;
3086 int OldSkIndex = SkipIvars.size() -1;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003087
Ted Kremenek6217b802009-07-29 21:53:49 +00003088 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003089 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003090 ForStrongLayout, HasUnion);
3091
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003092 // Replicate layout information for each array element. Note that
3093 // one element is already done.
3094 uint64_t ElIx = 1;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003095 for (int FirstIndex = IvarsInfo.size() - 1,
3096 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003097 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003098 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3099 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3100 IvarsInfo[i].ivar_size));
3101 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3102 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3103 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003104 }
3105 continue;
3106 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003107 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003108 // At this point, we are done with Record/Union and array there of.
3109 // For other arrays we are down to its element type.
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003110 QualType::GCAttrTypes GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003111
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003112 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003113 if ((ForStrongLayout && GCAttr == QualType::Strong)
3114 || (!ForStrongLayout && GCAttr == QualType::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003115 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003116 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003117 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003118 MaxUnionIvarSize = UnionIvarSize;
3119 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003120 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003121 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003122 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003123 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003124 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003125 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003126 } else if ((ForStrongLayout &&
3127 (GCAttr == QualType::GCNone || GCAttr == QualType::Weak))
3128 || (!ForStrongLayout && GCAttr != QualType::Weak)) {
3129 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003130 // FIXME: Why the asymmetry? We divide by word size in bits on other
3131 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003132 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003133 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003134 MaxSkippedUnionIvarSize = UnionIvarSize;
3135 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003136 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003137 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003138 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003139 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003140 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003141 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003142 }
3143 }
3144 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003145
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003146 if (LastFieldBitfield) {
3147 // Last field was a bitfield. Must update skip info.
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003148 Expr *BitWidth = LastFieldBitfield->getBitWidth();
3149 uint64_t BitFieldSize =
Eli Friedman9a901bb2009-04-26 19:19:15 +00003150 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Daniel Dunbar487993b2009-05-03 13:32:01 +00003151 GC_IVAR skivar;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003152 skivar.ivar_bytepos = BytePos + LastBitfieldOffset;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003153 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3154 + ((BitFieldSize % ByteSizeInBits) != 0);
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003155 SkipIvars.push_back(skivar);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003156 }
3157
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003158 if (MaxField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003159 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003160 MaxUnionIvarSize));
3161 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003162 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003163 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003164}
3165
3166/// BuildIvarLayout - Builds ivar layout bitmap for the class
3167/// implementation for the __strong or __weak case.
3168/// The layout map displays which words in ivar list must be skipped
3169/// and which must be scanned by GC (see below). String is built of bytes.
3170/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3171/// of words to skip and right nibble is count of words to scan. So, each
3172/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3173/// represented by a 0x00 byte which also ends the string.
3174/// 1. when ForStrongLayout is true, following ivars are scanned:
3175/// - id, Class
3176/// - object *
3177/// - __strong anything
3178///
3179/// 2. When ForStrongLayout is false, following ivars are scanned:
3180/// - __weak anything
3181///
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003182llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003183 const ObjCImplementationDecl *OMD,
3184 bool ForStrongLayout) {
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003185 bool hasUnion = false;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003186
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003187 unsigned int WordsToScan, WordsToSkip;
Owen Anderson96e0fc72009-07-29 22:16:19 +00003188 const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003189 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
Owen Anderson69243822009-07-13 04:10:07 +00003190 return VMContext.getNullValue(PtrTy);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003191
Chris Lattnerf1690852009-03-31 08:48:01 +00003192 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003193 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003194 CGM.getContext().CollectObjCIvars(OI, RecFields);
Fariborz Jahanian98200742009-05-12 18:14:29 +00003195
Daniel Dunbar37153282009-05-04 04:10:48 +00003196 // Add this implementations synthesized ivars.
Fariborz Jahanian98200742009-05-12 18:14:29 +00003197 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
3198 CGM.getContext().CollectSynthesizedIvars(OI, Ivars);
3199 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3200 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3201
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003202 if (RecFields.empty())
Owen Anderson69243822009-07-13 04:10:07 +00003203 return VMContext.getNullValue(PtrTy);
Chris Lattnerf1690852009-03-31 08:48:01 +00003204
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003205 SkipIvars.clear();
3206 IvarsInfo.clear();
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00003207
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003208 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003209 if (IvarsInfo.empty())
Owen Anderson69243822009-07-13 04:10:07 +00003210 return VMContext.getNullValue(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003211
3212 // Sort on byte position in case we encounterred a union nested in
3213 // the ivar list.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003214 if (hasUnion && !IvarsInfo.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003215 std::sort(IvarsInfo.begin(), IvarsInfo.end());
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003216 if (hasUnion && !SkipIvars.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003217 std::sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003218
3219 // Build the string of skip/scan nibbles
Fariborz Jahanian8c2f2d12009-04-24 17:15:27 +00003220 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003221 unsigned int WordSize =
Duncan Sands9408c452009-05-09 07:08:47 +00003222 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003223 if (IvarsInfo[0].ivar_bytepos == 0) {
3224 WordsToSkip = 0;
3225 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003226 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003227 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3228 WordsToScan = IvarsInfo[0].ivar_size;
3229 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003230 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003231 unsigned int TailPrevGCObjC =
3232 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003233 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003234 // consecutive 'scanned' object pointers.
3235 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003236 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003237 // Skip over 'gc'able object pointer which lay over each other.
3238 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3239 continue;
3240 // Must skip over 1 or more words. We save current skip/scan values
3241 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003242 SKIP_SCAN SkScan;
3243 SkScan.skip = WordsToSkip;
3244 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003245 SkipScanIvars.push_back(SkScan);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003246
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003247 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003248 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3249 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003250 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003251 WordsToSkip = 0;
3252 WordsToScan = IvarsInfo[i].ivar_size;
3253 }
3254 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003255 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003256 SKIP_SCAN SkScan;
3257 SkScan.skip = WordsToSkip;
3258 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003259 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003260 }
3261
3262 bool BytesSkipped = false;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003263 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003264 unsigned int LastIndex = SkipIvars.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003265 int LastByteSkipped =
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003266 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
3267 LastIndex = IvarsInfo.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003268 int LastByteScanned =
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003269 IvarsInfo[LastIndex].ivar_bytepos +
3270 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003271 BytesSkipped = (LastByteSkipped > LastByteScanned);
3272 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003273 if (BytesSkipped) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003274 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003275 SKIP_SCAN SkScan;
3276 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3277 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003278 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003279 }
3280 }
3281 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3282 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003283 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003284 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003285 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3286 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3287 // 0xM0 followed by 0x0N detected.
3288 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3289 for (int j = i+1; j < SkipScan; j++)
3290 SkipScanIvars[j] = SkipScanIvars[j+1];
3291 --SkipScan;
3292 }
3293 }
3294
3295 // Generate the string.
3296 std::string BitMap;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003297 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003298 unsigned char byte;
3299 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3300 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3301 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3302 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
3303
3304 if (skip_small > 0 || skip_big > 0)
3305 BytesSkipped = true;
3306 // first skip big.
3307 for (unsigned int ix = 0; ix < skip_big; ix++)
3308 BitMap += (unsigned char)(0xf0);
3309
3310 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003311 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003312 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003313 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003314 byte |= 0xf;
3315 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003316 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003317 byte |= scan_small;
3318 scan_small = 0;
3319 }
3320 BitMap += byte;
3321 }
3322 // next scan big
3323 for (unsigned int ix = 0; ix < scan_big; ix++)
3324 BitMap += (unsigned char)(0x0f);
3325 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003326 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003327 byte = scan_small;
3328 BitMap += byte;
3329 }
3330 }
3331 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003332 unsigned char zero = 0;
3333 BitMap += zero;
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003334
3335 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
3336 printf("\n%s ivar layout for class '%s': ",
3337 ForStrongLayout ? "strong" : "weak",
3338 OMD->getClassInterface()->getNameAsCString());
3339 const unsigned char *s = (unsigned char*)BitMap.c_str();
3340 for (unsigned i = 0; i < BitMap.size(); i++)
3341 if (!(s[i] & 0xf0))
3342 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3343 else
3344 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3345 printf("\n");
3346 }
3347
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003348 // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as
3349 // final layout.
3350 if (ForStrongLayout && !BytesSkipped)
Owen Anderson69243822009-07-13 04:10:07 +00003351 return VMContext.getNullValue(PtrTy);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003352 llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Owen Anderson7db6d832009-07-28 18:33:04 +00003353 llvm::ConstantArray::get(BitMap.c_str()),
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003354 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003355 1, true);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003356 return getConstantGEP(VMContext, Entry, 0, 0);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003357}
3358
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003359llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003360 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3361
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003362 // FIXME: Avoid std::string copying.
3363 if (!Entry)
3364 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson7db6d832009-07-28 18:33:04 +00003365 llvm::ConstantArray::get(Sel.getAsString()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003366 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003367 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003368
Owen Andersona1cf15f2009-07-14 23:10:40 +00003369 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003370}
3371
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003372// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003373llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003374 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3375}
3376
3377// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003378llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003379 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
3380}
3381
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003382llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003383 std::string TypeStr;
3384 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3385
3386 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003387
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003388 if (!Entry)
3389 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson7db6d832009-07-28 18:33:04 +00003390 llvm::ConstantArray::get(TypeStr),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003391 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003392 1, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003393
Owen Andersona1cf15f2009-07-14 23:10:40 +00003394 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003395}
3396
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003397llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003398 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003399 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3400 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00003401
3402 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3403
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003404 if (!Entry)
3405 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson7db6d832009-07-28 18:33:04 +00003406 llvm::ConstantArray::get(TypeStr),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003407 "__TEXT,__cstring,cstring_literals",
3408 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00003409
Owen Andersona1cf15f2009-07-14 23:10:40 +00003410 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003411}
3412
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003413// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003414llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003415 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
3416
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003417 if (!Entry)
3418 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Owen Anderson7db6d832009-07-28 18:33:04 +00003419 llvm::ConstantArray::get(Ident->getName()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003420 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003421 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003422
Owen Andersona1cf15f2009-07-14 23:10:40 +00003423 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003424}
3425
3426// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003427// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003428llvm::Constant *
3429 CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3430 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003431 std::string TypeStr;
3432 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003433 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3434}
3435
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003436void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
3437 const ObjCContainerDecl *CD,
3438 std::string &NameOut) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00003439 NameOut = '\01';
3440 NameOut += (D->isInstanceMethod() ? '-' : '+');
Chris Lattner077bf5e2008-11-24 03:33:13 +00003441 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003442 assert (CD && "Missing container decl in GetNameForMethod");
3443 NameOut += CD->getNameAsString();
Fariborz Jahanian1e9aef32009-04-16 18:34:20 +00003444 if (const ObjCCategoryImplDecl *CID =
3445 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) {
3446 NameOut += '(';
3447 NameOut += CID->getNameAsString();
3448 NameOut+= ')';
3449 }
Chris Lattner077bf5e2008-11-24 03:33:13 +00003450 NameOut += ' ';
3451 NameOut += D->getSelector().getAsString();
3452 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003453}
3454
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003455void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003456 EmitModuleInfo();
3457
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003458 // Emit the dummy bodies for any protocols which were referenced but
3459 // never defined.
3460 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00003461 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
3462 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003463 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003464
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003465 std::vector<llvm::Constant*> Values(5);
Owen Anderson69243822009-07-13 04:10:07 +00003466 Values[0] = VMContext.getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00003467 Values[1] = GetClassName(I->first);
Owen Anderson69243822009-07-13 04:10:07 +00003468 Values[2] = VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003469 Values[3] = Values[4] =
Owen Anderson69243822009-07-13 04:10:07 +00003470 VMContext.getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00003471 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00003472 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003473 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00003474 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003475 }
3476
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003477 // Add assembler directives to add lazy undefined symbol references
3478 // for classes which are referenced but not defined. This is
3479 // important for correct linker interaction.
3480
3481 // FIXME: Uh, this isn't particularly portable.
3482 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00003483
3484 if (!CGM.getModule().getModuleInlineAsm().empty())
3485 s << "\n";
3486
Chris Lattner41f55d32009-07-28 18:25:06 +00003487 // FIXME: This produces non-determinstic output.
Chris Lattnerad64e022009-07-17 23:57:13 +00003488 for (std::set<IdentifierInfo*>::iterator I = LazySymbols.begin(),
3489 e = LazySymbols.end(); I != e; ++I) {
3490 s << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003491 }
Chris Lattnerad64e022009-07-17 23:57:13 +00003492 for (std::set<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
3493 e = DefinedSymbols.end(); I != e; ++I) {
3494 s << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
3495 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003496 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00003497
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003498 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003499}
3500
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003501CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003502 : CGObjCCommonMac(cgm),
3503 ObjCTypes(cgm)
3504{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003505 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003506 ObjCABI = 2;
3507}
3508
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003509/* *** */
3510
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003511ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Owen Andersona1cf15f2009-07-14 23:10:40 +00003512: VMContext(cgm.getLLVMContext()), CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003513{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003514 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3515 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003516
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003517 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003518 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003519 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003520 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00003521 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003522
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003523 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00003524 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003525 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003526
Mike Stumpf5408fe2009-05-16 07:57:57 +00003527 // FIXME: It would be nice to unify this with the opaque type, so that the IR
3528 // comes out a bit cleaner.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003529 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00003530 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003531
3532 // I'm not sure I like this. The implicit coordination is a bit
3533 // gross. We should solve this in a reasonable fashion because this
3534 // is a pretty common task (match some runtime data structure with
3535 // an LLVM data structure).
3536
3537 // FIXME: This is leaked.
3538 // FIXME: Merge with rewriter code?
3539
3540 // struct _objc_super {
3541 // id self;
3542 // Class cls;
3543 // }
3544 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3545 SourceLocation(),
3546 &Ctx.Idents.get("_objc_super"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003547 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Douglas Gregor6ab35242009-04-09 21:40:53 +00003548 Ctx.getObjCIdType(), 0, false));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003549 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Douglas Gregor6ab35242009-04-09 21:40:53 +00003550 Ctx.getObjCClassType(), 0, false));
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003551 RD->completeDefinition(Ctx);
3552
3553 SuperCTy = Ctx.getTagDeclType(RD);
3554 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
3555
3556 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Owen Anderson96e0fc72009-07-29 22:16:19 +00003557 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003558
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003559 // struct _prop_t {
3560 // char *name;
3561 // char *attributes;
3562 // }
Owen Anderson96e0fc72009-07-29 22:16:19 +00003563 PropertyTy = llvm::StructType::get(Int8PtrTy, Int8PtrTy, NULL);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003564 CGM.getModule().addTypeName("struct._prop_t",
3565 PropertyTy);
3566
3567 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003568 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003569 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003570 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003571 // }
Owen Anderson96e0fc72009-07-29 22:16:19 +00003572 PropertyListTy = llvm::StructType::get(IntTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003573 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00003574 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003575 NULL);
3576 CGM.getModule().addTypeName("struct._prop_list_t",
3577 PropertyListTy);
3578 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00003579 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003580
3581 // struct _objc_method {
3582 // SEL _cmd;
3583 // char *method_type;
3584 // char *_imp;
3585 // }
Owen Anderson96e0fc72009-07-29 22:16:19 +00003586 MethodTy = llvm::StructType::get(SelectorPtrTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003587 Int8PtrTy,
3588 Int8PtrTy,
3589 NULL);
3590 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003591
3592 // struct _objc_cache *
Owen Anderson96e0fc72009-07-29 22:16:19 +00003593 CacheTy = llvm::OpaqueType::get();
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003594 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00003595 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003596}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003597
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003598ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
3599 : ObjCCommonTypesHelper(cgm)
3600{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003601 // struct _objc_method_description {
3602 // SEL name;
3603 // char *types;
3604 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003605 MethodDescriptionTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003606 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003607 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003608 NULL);
3609 CGM.getModule().addTypeName("struct._objc_method_description",
3610 MethodDescriptionTy);
3611
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003612 // struct _objc_method_description_list {
3613 // int count;
3614 // struct _objc_method_description[1];
3615 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003616 MethodDescriptionListTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003617 llvm::StructType::get(IntTy,
3618 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003619 NULL);
3620 CGM.getModule().addTypeName("struct._objc_method_description_list",
3621 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003622
3623 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003624 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003625 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003626
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003627 // Protocol description structures
3628
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003629 // struct _objc_protocol_extension {
3630 // uint32_t size; // sizeof(struct _objc_protocol_extension)
3631 // struct _objc_method_description_list *optional_instance_methods;
3632 // struct _objc_method_description_list *optional_class_methods;
3633 // struct _objc_property_list *instance_properties;
3634 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003635 ProtocolExtensionTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003636 llvm::StructType::get(IntTy,
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003637 MethodDescriptionListPtrTy,
3638 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003639 PropertyListPtrTy,
3640 NULL);
3641 CGM.getModule().addTypeName("struct._objc_protocol_extension",
3642 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003643
3644 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00003645 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003646
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003647 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003648
Owen Anderson96e0fc72009-07-29 22:16:19 +00003649 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
3650 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003651
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003652 const llvm::Type *T =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003653 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003654 LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00003655 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003656 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003657 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
3658
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003659 // struct _objc_protocol {
3660 // struct _objc_protocol_extension *isa;
3661 // char *protocol_name;
3662 // struct _objc_protocol **_objc_protocol_list;
3663 // struct _objc_method_description_list *instance_methods;
3664 // struct _objc_method_description_list *class_methods;
3665 // }
Owen Anderson96e0fc72009-07-29 22:16:19 +00003666 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003667 Int8PtrTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00003668 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003669 MethodDescriptionListPtrTy,
3670 MethodDescriptionListPtrTy,
3671 NULL);
3672 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
3673
3674 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
3675 CGM.getModule().addTypeName("struct._objc_protocol_list",
3676 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003677 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00003678 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003679
3680 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003681 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00003682 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003683
3684 // Class description structures
3685
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003686 // struct _objc_ivar {
3687 // char *ivar_name;
3688 // char *ivar_type;
3689 // int ivar_offset;
3690 // }
Owen Anderson96e0fc72009-07-29 22:16:19 +00003691 IvarTy = llvm::StructType::get(Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003692 Int8PtrTy,
3693 IntTy,
3694 NULL);
3695 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
3696
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003697 // struct _objc_ivar_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00003698 IvarListTy = llvm::OpaqueType::get();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003699 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00003700 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003701
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003702 // struct _objc_method_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00003703 MethodListTy = llvm::OpaqueType::get();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003704 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00003705 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003706
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003707 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003708 ClassExtensionTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003709 llvm::StructType::get(IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003710 Int8PtrTy,
3711 PropertyListPtrTy,
3712 NULL);
3713 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00003714 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003715
Owen Anderson96e0fc72009-07-29 22:16:19 +00003716 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003717
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003718 // struct _objc_class {
3719 // Class isa;
3720 // Class super_class;
3721 // char *name;
3722 // long version;
3723 // long info;
3724 // long instance_size;
3725 // struct _objc_ivar_list *ivars;
3726 // struct _objc_method_list *methods;
3727 // struct _objc_cache *cache;
3728 // struct _objc_protocol_list *protocols;
3729 // char *ivar_layout;
3730 // struct _objc_class_ext *ext;
3731 // };
Owen Anderson96e0fc72009-07-29 22:16:19 +00003732 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3733 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003734 Int8PtrTy,
3735 LongTy,
3736 LongTy,
3737 LongTy,
3738 IvarListPtrTy,
3739 MethodListPtrTy,
3740 CachePtrTy,
3741 ProtocolListPtrTy,
3742 Int8PtrTy,
3743 ClassExtensionPtrTy,
3744 NULL);
3745 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
3746
3747 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
3748 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00003749 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003750
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003751 // struct _objc_category {
3752 // char *category_name;
3753 // char *class_name;
3754 // struct _objc_method_list *instance_method;
3755 // struct _objc_method_list *class_method;
3756 // uint32_t size; // sizeof(struct _objc_category)
3757 // struct _objc_property_list *instance_properties;// category's @property
3758 // }
Owen Anderson96e0fc72009-07-29 22:16:19 +00003759 CategoryTy = llvm::StructType::get(Int8PtrTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003760 Int8PtrTy,
3761 MethodListPtrTy,
3762 MethodListPtrTy,
3763 ProtocolListPtrTy,
3764 IntTy,
3765 PropertyListPtrTy,
3766 NULL);
3767 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
3768
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003769 // Global metadata structures
3770
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003771 // struct _objc_symtab {
3772 // long sel_ref_cnt;
3773 // SEL *refs;
3774 // short cls_def_cnt;
3775 // short cat_def_cnt;
3776 // char *defs[cls_def_cnt + cat_def_cnt];
3777 // }
Owen Anderson96e0fc72009-07-29 22:16:19 +00003778 SymtabTy = llvm::StructType::get(LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003779 SelectorPtrTy,
3780 ShortTy,
3781 ShortTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00003782 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003783 NULL);
3784 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00003785 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003786
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003787 // struct _objc_module {
3788 // long version;
3789 // long size; // sizeof(struct _objc_module)
3790 // char *name;
3791 // struct _objc_symtab* symtab;
3792 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003793 ModuleTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003794 llvm::StructType::get(LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003795 LongTy,
3796 Int8PtrTy,
3797 SymtabPtrTy,
3798 NULL);
3799 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003800
Anders Carlsson2abd89c2008-08-31 04:05:03 +00003801
Mike Stumpf5408fe2009-05-16 07:57:57 +00003802 // FIXME: This is the size of the setjmp buffer and should be target
3803 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00003804 uint64_t SetJmpBufferSize = 18;
3805
3806 // Exceptions
Owen Anderson96e0fc72009-07-29 22:16:19 +00003807 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
3808 llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00003809
3810 ExceptionDataTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003811 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
Anders Carlsson124526b2008-09-09 10:10:21 +00003812 SetJmpBufferSize),
3813 StackPtrTy, NULL);
3814 CGM.getModule().addTypeName("struct._objc_exception_data",
3815 ExceptionDataTy);
3816
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003817}
3818
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003819ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003820: ObjCCommonTypesHelper(cgm)
3821{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003822 // struct _method_list_t {
3823 // uint32_t entsize; // sizeof(struct _objc_method)
3824 // uint32_t method_count;
3825 // struct _objc_method method_list[method_count];
3826 // }
Owen Anderson96e0fc72009-07-29 22:16:19 +00003827 MethodListnfABITy = llvm::StructType::get(IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003828 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00003829 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003830 NULL);
3831 CGM.getModule().addTypeName("struct.__method_list_t",
3832 MethodListnfABITy);
3833 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00003834 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003835
3836 // struct _protocol_t {
3837 // id isa; // NULL
3838 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003839 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003840 // const struct method_list_t * const instance_methods;
3841 // const struct method_list_t * const class_methods;
3842 // const struct method_list_t *optionalInstanceMethods;
3843 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003844 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003845 // const uint32_t size; // sizeof(struct _protocol_t)
3846 // const uint32_t flags; // = 0
3847 // }
3848
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003849 // Holder for struct _protocol_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00003850 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003851
Owen Anderson96e0fc72009-07-29 22:16:19 +00003852 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003853 Int8PtrTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00003854 llvm::PointerType::getUnqual(
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003855 ProtocolListTyHolder),
3856 MethodListnfABIPtrTy,
3857 MethodListnfABIPtrTy,
3858 MethodListnfABIPtrTy,
3859 MethodListnfABIPtrTy,
3860 PropertyListPtrTy,
3861 IntTy,
3862 IntTy,
3863 NULL);
3864 CGM.getModule().addTypeName("struct._protocol_t",
3865 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003866
3867 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00003868 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003869
Fariborz Jahanianda320092009-01-29 19:24:30 +00003870 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003871 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00003872 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003873 // }
Owen Anderson96e0fc72009-07-29 22:16:19 +00003874 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3875 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003876 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003877 NULL);
3878 CGM.getModule().addTypeName("struct._objc_protocol_list",
3879 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003880 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3881 ProtocolListnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003882
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003883 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00003884 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003885
3886 // struct _ivar_t {
3887 // unsigned long int *offset; // pointer to ivar offset location
3888 // char *name;
3889 // char *type;
3890 // uint32_t alignment;
3891 // uint32_t size;
3892 // }
Owen Anderson96e0fc72009-07-29 22:16:19 +00003893 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003894 Int8PtrTy,
3895 Int8PtrTy,
3896 IntTy,
3897 IntTy,
3898 NULL);
3899 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3900
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003901 // struct _ivar_list_t {
3902 // uint32 entsize; // sizeof(struct _ivar_t)
3903 // uint32 count;
3904 // struct _iver_t list[count];
3905 // }
Owen Anderson96e0fc72009-07-29 22:16:19 +00003906 IvarListnfABITy = llvm::StructType::get(IntTy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003907 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00003908 llvm::ArrayType::get(
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003909 IvarnfABITy, 0),
3910 NULL);
3911 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3912
Owen Anderson96e0fc72009-07-29 22:16:19 +00003913 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003914
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003915 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003916 // uint32_t const flags;
3917 // uint32_t const instanceStart;
3918 // uint32_t const instanceSize;
3919 // uint32_t const reserved; // only when building for 64bit targets
3920 // const uint8_t * const ivarLayout;
3921 // const char *const name;
3922 // const struct _method_list_t * const baseMethods;
3923 // const struct _objc_protocol_list *const baseProtocols;
3924 // const struct _ivar_list_t *const ivars;
3925 // const uint8_t * const weakIvarLayout;
3926 // const struct _prop_list_t * const properties;
3927 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003928
3929 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson96e0fc72009-07-29 22:16:19 +00003930 ClassRonfABITy = llvm::StructType::get(IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003931 IntTy,
3932 IntTy,
3933 Int8PtrTy,
3934 Int8PtrTy,
3935 MethodListnfABIPtrTy,
3936 ProtocolListnfABIPtrTy,
3937 IvarListnfABIPtrTy,
3938 Int8PtrTy,
3939 PropertyListPtrTy,
3940 NULL);
3941 CGM.getModule().addTypeName("struct._class_ro_t",
3942 ClassRonfABITy);
3943
3944 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3945 std::vector<const llvm::Type*> Params;
3946 Params.push_back(ObjectPtrTy);
3947 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00003948 ImpnfABITy = llvm::PointerType::getUnqual(
3949 llvm::FunctionType::get(ObjectPtrTy, Params, false));
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003950
3951 // struct _class_t {
3952 // struct _class_t *isa;
3953 // struct _class_t * const superclass;
3954 // void *cache;
3955 // IMP *vtable;
3956 // struct class_ro_t *ro;
3957 // }
3958
Owen Anderson96e0fc72009-07-29 22:16:19 +00003959 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
Owen Andersona1cf15f2009-07-14 23:10:40 +00003960 ClassnfABITy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003961 llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3962 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Andersona1cf15f2009-07-14 23:10:40 +00003963 CachePtrTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00003964 llvm::PointerType::getUnqual(ImpnfABITy),
3965 llvm::PointerType::getUnqual(ClassRonfABITy),
Owen Andersona1cf15f2009-07-14 23:10:40 +00003966 NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003967 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3968
3969 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3970 ClassnfABITy);
3971
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003972 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00003973 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003974
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003975 // struct _category_t {
3976 // const char * const name;
3977 // struct _class_t *const cls;
3978 // const struct _method_list_t * const instance_methods;
3979 // const struct _method_list_t * const class_methods;
3980 // const struct _protocol_list_t * const protocols;
3981 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003982 // }
Owen Anderson96e0fc72009-07-29 22:16:19 +00003983 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003984 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003985 MethodListnfABIPtrTy,
3986 MethodListnfABIPtrTy,
3987 ProtocolListnfABIPtrTy,
3988 PropertyListPtrTy,
3989 NULL);
3990 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003991
3992 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003993 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3994 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003995
3996 // MessageRefTy - LLVM for:
3997 // struct _message_ref_t {
3998 // IMP messenger;
3999 // SEL name;
4000 // };
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004001
4002 // First the clang type for struct _message_ref_t
4003 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
4004 SourceLocation(),
4005 &Ctx.Idents.get("_message_ref_t"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004006 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Douglas Gregor6ab35242009-04-09 21:40:53 +00004007 Ctx.VoidPtrTy, 0, false));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004008 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Douglas Gregor6ab35242009-04-09 21:40:53 +00004009 Ctx.getObjCSelType(), 0, false));
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004010 RD->completeDefinition(Ctx);
4011
4012 MessageRefCTy = Ctx.getTagDeclType(RD);
4013 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4014 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004015
4016 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004017 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004018
4019 // SuperMessageRefTy - LLVM for:
4020 // struct _super_message_ref_t {
4021 // SUPER_IMP messenger;
4022 // SEL name;
4023 // };
Owen Anderson96e0fc72009-07-29 22:16:19 +00004024 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004025 SelectorPtrTy,
4026 NULL);
4027 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
4028
4029 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004030 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004031
Daniel Dunbare588b992009-03-01 04:46:24 +00004032
4033 // struct objc_typeinfo {
4034 // const void** vtable; // objc_ehtype_vtable + 2
4035 // const char* name; // c++ typeinfo string
4036 // Class cls;
4037 // };
Owen Anderson96e0fc72009-07-29 22:16:19 +00004038 EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbare588b992009-03-01 04:46:24 +00004039 Int8PtrTy,
4040 ClassnfABIPtrTy,
4041 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004042 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004043 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004044}
4045
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004046llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
4047 FinishNonFragileABIModule();
4048
4049 return NULL;
4050}
4051
Daniel Dunbar463b8762009-05-15 21:48:48 +00004052void CGObjCNonFragileABIMac::AddModuleClassList(const
4053 std::vector<llvm::GlobalValue*>
4054 &Container,
4055 const char *SymbolName,
4056 const char *SectionName) {
4057 unsigned NumClasses = Container.size();
4058
4059 if (!NumClasses)
4060 return;
4061
4062 std::vector<llvm::Constant*> Symbols(NumClasses);
4063 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004064 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004065 ObjCTypes.Int8PtrTy);
4066 llvm::Constant* Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004067 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004068 NumClasses),
4069 Symbols);
4070
4071 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004072 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004073 llvm::GlobalValue::InternalLinkage,
4074 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004075 SymbolName);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004076 GV->setAlignment(8);
4077 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004078 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004079}
4080
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004081void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4082 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004083
Daniel Dunbar463b8762009-05-15 21:48:48 +00004084 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004085 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar463b8762009-05-15 21:48:48 +00004086 AddModuleClassList(DefinedClasses,
4087 "\01L_OBJC_LABEL_CLASS_$",
4088 "__DATA, __objc_classlist, regular, no_dead_strip");
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004089 AddModuleClassList(DefinedNonLazyClasses,
4090 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4091 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004092
4093 // Build list of all implemented category addresses in array
4094 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar463b8762009-05-15 21:48:48 +00004095 AddModuleClassList(DefinedCategories,
4096 "\01L_OBJC_LABEL_CATEGORY_$",
4097 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004098 AddModuleClassList(DefinedNonLazyCategories,
4099 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4100 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004101
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004102 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
4103 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
4104 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004105 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
Fariborz Jahanian067986e2009-02-24 21:08:09 +00004106 unsigned int flags = 0;
Fariborz Jahanian66a5c2c2009-02-24 23:34:44 +00004107 // FIXME: Fix and continue?
4108 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
4109 flags |= eImageInfo_GarbageCollected;
4110 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
4111 flags |= eImageInfo_GCOnly;
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004112 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
Owen Anderson7db6d832009-07-28 18:33:04 +00004113 llvm::Constant* Init = llvm::ConstantArray::get(
Owen Anderson96e0fc72009-07-29 22:16:19 +00004114 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004115 Values);
4116 llvm::GlobalVariable *IMGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004117 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004118 llvm::GlobalValue::InternalLinkage,
4119 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004120 "\01L_OBJC_IMAGE_INFO");
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004121 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
Daniel Dunbar325f7582009-04-23 08:03:21 +00004122 IMGV->setConstant(true);
Chris Lattnerad64e022009-07-17 23:57:13 +00004123 CGM.AddUsedGlobal(IMGV);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004124}
4125
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004126/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004127/// NonLegacyDispatchMethods; false otherwise. What this means is that
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004128/// except for the 19 selectors in the list, we generate 32bit-style
4129/// message dispatch call for all the rest.
4130///
4131bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004132 if (NonLegacyDispatchMethods.empty()) {
4133 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4134 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4135 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4136 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4137 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4138 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4139 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4140 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4141 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4142 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
4143
4144 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4145 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4146 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4147 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4148 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4149 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4150 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4151 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004152 // "countByEnumeratingWithState:objects:count"
4153 IdentifierInfo *KeyIdents[] = {
4154 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4155 &CGM.getContext().Idents.get("objects"),
4156 &CGM.getContext().Idents.get("count")
4157 };
4158 NonLegacyDispatchMethods.insert(
4159 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004160 }
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004161 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004162}
4163
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004164// Metadata flags
4165enum MetaDataDlags {
4166 CLS = 0x0,
4167 CLS_META = 0x1,
4168 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004169 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004170 CLS_EXCEPTION = 0x20
4171};
4172/// BuildClassRoTInitializer - generate meta-data for:
4173/// struct _class_ro_t {
4174/// uint32_t const flags;
4175/// uint32_t const instanceStart;
4176/// uint32_t const instanceSize;
4177/// uint32_t const reserved; // only when building for 64bit targets
4178/// const uint8_t * const ivarLayout;
4179/// const char *const name;
4180/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004181/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004182/// const struct _ivar_list_t *const ivars;
4183/// const uint8_t * const weakIvarLayout;
4184/// const struct _prop_list_t * const properties;
4185/// }
4186///
4187llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
4188 unsigned flags,
4189 unsigned InstanceStart,
4190 unsigned InstanceSize,
4191 const ObjCImplementationDecl *ID) {
4192 std::string ClassName = ID->getNameAsString();
4193 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004194 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4195 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4196 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004197 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004198 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4199 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004200 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004201 // const struct _method_list_t * const baseMethods;
4202 std::vector<llvm::Constant*> Methods;
4203 std::string MethodListName("\01l_OBJC_$_");
4204 if (flags & CLS_META) {
4205 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004206 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004207 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004208 // Class methods should always be defined.
4209 Methods.push_back(GetMethodConstant(*i));
4210 }
4211 } else {
4212 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004213 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004214 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004215 // Instance methods should always be defined.
4216 Methods.push_back(GetMethodConstant(*i));
4217 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00004218 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004219 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004220 ObjCPropertyImplDecl *PID = *i;
4221
4222 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4223 ObjCPropertyDecl *PD = PID->getPropertyDecl();
4224
4225 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4226 if (llvm::Constant *C = GetMethodConstant(MD))
4227 Methods.push_back(C);
4228 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4229 if (llvm::Constant *C = GetMethodConstant(MD))
4230 Methods.push_back(C);
4231 }
4232 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004233 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004234 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004235 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004236
4237 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4238 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
4239 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
4240 + OID->getNameAsString(),
4241 OID->protocol_begin(),
4242 OID->protocol_end());
4243
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004244 if (flags & CLS_META)
Owen Anderson69243822009-07-13 04:10:07 +00004245 Values[ 7] = VMContext.getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004246 else
4247 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004248 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4249 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004250 if (flags & CLS_META)
Owen Anderson69243822009-07-13 04:10:07 +00004251 Values[ 9] = VMContext.getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004252 else
4253 Values[ 9] =
Chris Lattner41f55d32009-07-28 18:25:06 +00004254 EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004255 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00004256 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004257 Values);
4258 llvm::GlobalVariable *CLASS_RO_GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004259 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004260 llvm::GlobalValue::InternalLinkage,
4261 Init,
4262 (flags & CLS_META) ?
4263 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
Owen Anderson1c431b32009-07-08 19:05:04 +00004264 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004265 CLASS_RO_GV->setAlignment(
4266 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004267 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004268 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004269
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004270}
4271
4272/// BuildClassMetaData - This routine defines that to-level meta-data
4273/// for the given ClassName for:
4274/// struct _class_t {
4275/// struct _class_t *isa;
4276/// struct _class_t * const superclass;
4277/// void *cache;
4278/// IMP *vtable;
4279/// struct class_ro_t *ro;
4280/// }
4281///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004282llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
4283 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004284 llvm::Constant *IsAGV,
4285 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004286 llvm::Constant *ClassRoGV,
4287 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004288 std::vector<llvm::Constant*> Values(5);
4289 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004290 Values[1] = SuperClassGV
4291 ? SuperClassGV
Owen Anderson69243822009-07-13 04:10:07 +00004292 : VMContext.getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004293 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4294 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4295 Values[4] = ClassRoGV; // &CLASS_RO_GV
Owen Anderson08e25242009-07-27 22:29:56 +00004296 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004297 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004298 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4299 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004300 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004301 GV->setAlignment(
4302 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004303 if (HiddenVisibility)
4304 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004305 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004306}
4307
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004308bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00004309CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004310 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004311}
4312
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004313void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004314 uint32_t &InstanceStart,
4315 uint32_t &InstanceSize) {
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004316 const ASTRecordLayout &RL =
4317 CGM.getContext().getASTObjCImplementationLayout(OID);
4318
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004319 // InstanceSize is really instance end.
Anders Carlsson243a6852009-07-18 21:26:44 +00004320 InstanceSize = llvm::RoundUpToAlignment(RL.getDataSize(), 8) / 8;
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004321
4322 // If there are no fields, the start is the same as the end.
4323 if (!RL.getFieldCount())
4324 InstanceStart = InstanceSize;
4325 else
4326 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004327}
4328
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004329void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4330 std::string ClassName = ID->getNameAsString();
4331 if (!ObjCEmptyCacheVar) {
4332 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Owen Anderson1c431b32009-07-08 19:05:04 +00004333 CGM.getModule(),
Daniel Dunbar948e2582009-02-15 07:36:20 +00004334 ObjCTypes.CacheTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004335 false,
4336 llvm::GlobalValue::ExternalLinkage,
4337 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00004338 "_objc_empty_cache");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004339
4340 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Owen Anderson1c431b32009-07-08 19:05:04 +00004341 CGM.getModule(),
Daniel Dunbar948e2582009-02-15 07:36:20 +00004342 ObjCTypes.ImpnfABITy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004343 false,
4344 llvm::GlobalValue::ExternalLinkage,
4345 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00004346 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004347 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004348 assert(ID->getClassInterface() &&
4349 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004350 // FIXME: Is this correct (that meta class size is never computed)?
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004351 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00004352 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004353 uint32_t InstanceSize = InstanceStart;
4354 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004355 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4356 std::string ObjCClassName(getClassSymbolPrefix());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004357
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004358 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004359
Daniel Dunbar04d40782009-04-14 06:00:08 +00004360 bool classIsHidden =
4361 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004362 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004363 flags |= OBJC2_CLS_HIDDEN;
4364 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004365 // class is root
4366 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004367 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004368 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004369 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004370 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004371 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4372 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4373 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004374 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004375 // work on super class metadata symbol.
4376 std::string SuperClassName =
4377 ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004378 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004379 }
4380 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4381 InstanceStart,
4382 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004383 std::string TClassName = ObjCMetaClassName + ClassName;
4384 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004385 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4386 classIsHidden);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004387
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004388 // Metadata for the class
4389 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004390 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004391 flags |= OBJC2_CLS_HIDDEN;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004392
Douglas Gregor68584ed2009-06-18 16:11:24 +00004393 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004394 flags |= CLS_EXCEPTION;
4395
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004396 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004397 flags |= CLS_ROOT;
4398 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00004399 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004400 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004401 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004402 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004403 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004404 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004405 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004406 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004407 InstanceStart,
4408 InstanceSize,
4409 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004410
4411 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004412 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004413 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4414 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004415 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004416
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004417 // Determine if this class is also "non-lazy".
4418 if (ImplementationIsNonLazy(ID))
4419 DefinedNonLazyClasses.push_back(ClassMD);
4420
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004421 // Force the definition of the EHType if necessary.
4422 if (flags & CLS_EXCEPTION)
4423 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004424}
4425
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004426/// GenerateProtocolRef - This routine is called to generate code for
4427/// a protocol reference expression; as in:
4428/// @code
4429/// @protocol(Proto1);
4430/// @endcode
4431/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4432/// which will hold address of the protocol meta-data.
4433///
4434llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
4435 const ObjCProtocolDecl *PD) {
4436
Fariborz Jahanian960cd062009-04-10 18:47:34 +00004437 // This routine is called for @protocol only. So, we must build definition
4438 // of protocol's meta-data (not a reference to it!)
4439 //
Owen Andersona1cf15f2009-07-14 23:10:40 +00004440 llvm::Constant *Init =
Owen Anderson3c4972d2009-07-29 18:54:39 +00004441 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004442 ObjCTypes.ExternalProtocolPtrTy);
4443
4444 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
4445 ProtocolName += PD->getNameAsCString();
4446
4447 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
4448 if (PTGV)
4449 return Builder.CreateLoad(PTGV, false, "tmp");
4450 PTGV = new llvm::GlobalVariable(
Owen Anderson1c431b32009-07-08 19:05:04 +00004451 CGM.getModule(),
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004452 Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00004453 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004454 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004455 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004456 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
4457 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00004458 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004459 return Builder.CreateLoad(PTGV, false, "tmp");
4460}
4461
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004462/// GenerateCategory - Build metadata for a category implementation.
4463/// struct _category_t {
4464/// const char * const name;
4465/// struct _class_t *const cls;
4466/// const struct _method_list_t * const instance_methods;
4467/// const struct _method_list_t * const class_methods;
4468/// const struct _protocol_list_t * const protocols;
4469/// const struct _prop_list_t * const properties;
4470/// }
4471///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004472void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004473 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004474 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
4475 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004476 "_$_" + OCD->getNameAsString());
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004477 std::string ExtClassName(getClassSymbolPrefix() +
4478 Interface->getNameAsString());
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004479
4480 std::vector<llvm::Constant*> Values(6);
4481 Values[0] = GetClassName(OCD->getIdentifier());
4482 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004483 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004484 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004485 std::vector<llvm::Constant*> Methods;
4486 std::string MethodListName(Prefix);
4487 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
4488 "_$_" + OCD->getNameAsString();
4489
Douglas Gregor653f1b12009-04-23 01:02:12 +00004490 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004491 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004492 // Instance methods should always be defined.
4493 Methods.push_back(GetMethodConstant(*i));
4494 }
4495
4496 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004497 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004498 Methods);
4499
4500 MethodListName = Prefix;
4501 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
4502 OCD->getNameAsString();
4503 Methods.clear();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004504 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004505 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004506 // Class methods should always be defined.
4507 Methods.push_back(GetMethodConstant(*i));
4508 }
4509
4510 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004511 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004512 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004513 const ObjCCategoryDecl *Category =
4514 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004515 if (Category) {
4516 std::string ExtName(Interface->getNameAsString() + "_$_" +
4517 OCD->getNameAsString());
4518 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
4519 + Interface->getNameAsString() + "_$_"
4520 + Category->getNameAsString(),
4521 Category->protocol_begin(),
4522 Category->protocol_end());
4523 Values[5] =
4524 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
4525 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00004526 } else {
Owen Anderson69243822009-07-13 04:10:07 +00004527 Values[4] = VMContext.getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4528 Values[5] = VMContext.getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004529 }
4530
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004531 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00004532 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004533 Values);
4534 llvm::GlobalVariable *GCATV
Owen Anderson1c431b32009-07-08 19:05:04 +00004535 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004536 false,
4537 llvm::GlobalValue::InternalLinkage,
4538 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004539 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004540 GCATV->setAlignment(
4541 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004542 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00004543 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004544 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004545
4546 // Determine if this category is also "non-lazy".
4547 if (ImplementationIsNonLazy(OCD))
4548 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004549}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004550
4551/// GetMethodConstant - Return a struct objc_method constant for the
4552/// given method if it has been defined. The result is null if the
4553/// method has not been defined. The return value has type MethodPtrTy.
4554llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
4555 const ObjCMethodDecl *MD) {
4556 // FIXME: Use DenseMap::lookup
4557 llvm::Function *Fn = MethodDefinitions[MD];
4558 if (!Fn)
4559 return 0;
4560
4561 std::vector<llvm::Constant*> Method(3);
4562 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00004563 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Owen Andersona1cf15f2009-07-14 23:10:40 +00004564 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004565 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00004566 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00004567 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004568}
4569
4570/// EmitMethodList - Build meta-data for method declarations
4571/// struct _method_list_t {
4572/// uint32_t entsize; // sizeof(struct _objc_method)
4573/// uint32_t method_count;
4574/// struct _objc_method method_list[method_count];
4575/// }
4576///
4577llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
4578 const std::string &Name,
4579 const char *Section,
4580 const ConstantVector &Methods) {
4581 // Return null for empty list.
4582 if (Methods.empty())
Owen Anderson69243822009-07-13 04:10:07 +00004583 return VMContext.getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004584
4585 std::vector<llvm::Constant*> Values(3);
4586 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00004587 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004588 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004589 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004590 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004591 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004592 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00004593 Values[2] = llvm::ConstantArray::get(AT, Methods);
Owen Anderson08e25242009-07-27 22:29:56 +00004594 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004595
4596 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004597 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004598 llvm::GlobalValue::InternalLinkage,
4599 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004600 Name);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004601 GV->setAlignment(
4602 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004603 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00004604 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00004605 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004606 ObjCTypes.MethodListnfABIPtrTy);
4607}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004608
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004609/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
4610/// the given ivar.
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004611llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004612 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004613 const ObjCIvarDecl *Ivar) {
Daniel Dunbara81419d2009-05-05 00:36:57 +00004614 // FIXME: We shouldn't need to do this lookup.
4615 unsigned Index;
4616 const ObjCInterfaceDecl *Container =
4617 FindIvarInterface(CGM.getContext(), ID, Ivar, Index);
4618 assert(Container && "Unable to find ivar container!");
4619 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00004620 '.' + Ivar->getNameAsString();
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004621 llvm::GlobalVariable *IvarOffsetGV =
4622 CGM.getModule().getGlobalVariable(Name);
4623 if (!IvarOffsetGV)
4624 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004625 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004626 false,
4627 llvm::GlobalValue::ExternalLinkage,
4628 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00004629 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004630 return IvarOffsetGV;
4631}
4632
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004633llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004634 const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004635 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004636 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00004637 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004638 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00004639 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004640 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004641 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00004642
Mike Stumpf5408fe2009-05-16 07:57:57 +00004643 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
4644 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00004645 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
4646 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
4647 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004648 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00004649 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00004650 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004651 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004652 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004653}
4654
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004655/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00004656/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004657/// IvarListnfABIPtrTy.
4658/// struct _ivar_t {
4659/// unsigned long int *offset; // pointer to ivar offset location
4660/// char *name;
4661/// char *type;
4662/// uint32_t alignment;
4663/// uint32_t size;
4664/// }
4665/// struct _ivar_list_t {
4666/// uint32 entsize; // sizeof(struct _ivar_t)
4667/// uint32 count;
4668/// struct _iver_t list[count];
4669/// }
4670///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004671
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004672llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
4673 const ObjCImplementationDecl *ID) {
4674
4675 std::vector<llvm::Constant*> Ivars, Ivar(5);
4676
4677 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4678 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
4679
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004680 // FIXME. Consolidate this with similar code in GenerateClass.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00004681
Daniel Dunbar91636d62009-04-20 00:33:43 +00004682 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00004683 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00004684 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Fariborz Jahanian99eee362009-04-01 19:37:34 +00004685
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004686 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
4687 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00004688 // Ignore unnamed bit-fields.
4689 if (!IVD->getDeclName())
4690 continue;
Daniel Dunbar3eec8aa2009-04-20 05:53:40 +00004691 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004692 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004693 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
4694 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004695 const llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004696 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00004697 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004698 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004699 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004700 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004701 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00004702 // NOTE. Size of a bitfield does not match gcc's, because of the
4703 // way bitfields are treated special in each. But I am told that
4704 // 'size' for bitfield ivars is ignored by the runtime so it does
4705 // not matter. If it matters, there is enough info to get the
4706 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004707 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00004708 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004709 }
4710 // Return null for empty list.
4711 if (Ivars.empty())
Owen Anderson69243822009-07-13 04:10:07 +00004712 return VMContext.getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004713 std::vector<llvm::Constant*> Values(3);
Duncan Sands9408c452009-05-09 07:08:47 +00004714 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004715 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4716 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004717 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004718 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00004719 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Owen Anderson08e25242009-07-27 22:29:56 +00004720 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004721 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4722 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004723 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004724 llvm::GlobalValue::InternalLinkage,
4725 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004726 Prefix + OID->getNameAsString());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004727 GV->setAlignment(
4728 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004729 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004730
Chris Lattnerad64e022009-07-17 23:57:13 +00004731 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00004732 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004733}
4734
4735llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4736 const ObjCProtocolDecl *PD) {
4737 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4738
4739 if (!Entry) {
4740 // We use the initializer as a marker of whether this is a forward
4741 // reference or not. At module finalization we add the empty
4742 // contents for protocols which were referenced but never defined.
4743 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00004744 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004745 llvm::GlobalValue::ExternalLinkage,
4746 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00004747 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString());
Fariborz Jahanianda320092009-01-29 19:24:30 +00004748 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004749 }
4750
4751 return Entry;
4752}
4753
4754/// GetOrEmitProtocol - Generate the protocol meta-data:
4755/// @code
4756/// struct _protocol_t {
4757/// id isa; // NULL
4758/// const char * const protocol_name;
4759/// const struct _protocol_list_t * protocol_list; // super protocols
4760/// const struct method_list_t * const instance_methods;
4761/// const struct method_list_t * const class_methods;
4762/// const struct method_list_t *optionalInstanceMethods;
4763/// const struct method_list_t *optionalClassMethods;
4764/// const struct _prop_list_t * properties;
4765/// const uint32_t size; // sizeof(struct _protocol_t)
4766/// const uint32_t flags; // = 0
4767/// }
4768/// @endcode
4769///
4770
4771llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4772 const ObjCProtocolDecl *PD) {
4773 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4774
4775 // Early exit if a defining object has already been generated.
4776 if (Entry && Entry->hasInitializer())
4777 return Entry;
4778
4779 const char *ProtocolName = PD->getNameAsCString();
4780
4781 // Construct method lists.
4782 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4783 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00004784 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004785 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004786 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004787 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004788 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4789 OptInstanceMethods.push_back(C);
4790 } else {
4791 InstanceMethods.push_back(C);
4792 }
4793 }
4794
Douglas Gregor6ab35242009-04-09 21:40:53 +00004795 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004796 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004797 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004798 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004799 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4800 OptClassMethods.push_back(C);
4801 } else {
4802 ClassMethods.push_back(C);
4803 }
4804 }
4805
4806 std::vector<llvm::Constant*> Values(10);
4807 // isa is NULL
Owen Anderson69243822009-07-13 04:10:07 +00004808 Values[0] = VMContext.getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004809 Values[1] = GetClassName(PD->getIdentifier());
4810 Values[2] = EmitProtocolList(
4811 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4812 PD->protocol_begin(),
4813 PD->protocol_end());
4814
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004815 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004816 + PD->getNameAsString(),
4817 "__DATA, __objc_const",
4818 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004819 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004820 + PD->getNameAsString(),
4821 "__DATA, __objc_const",
4822 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004823 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004824 + PD->getNameAsString(),
4825 "__DATA, __objc_const",
4826 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004827 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004828 + PD->getNameAsString(),
4829 "__DATA, __objc_const",
4830 OptClassMethods);
4831 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4832 0, PD, ObjCTypes);
4833 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00004834 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004835 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson69243822009-07-13 04:10:07 +00004836 Values[9] = VMContext.getNullValue(ObjCTypes.IntTy);
Owen Anderson08e25242009-07-27 22:29:56 +00004837 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004838 Values);
4839
4840 if (Entry) {
4841 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00004842 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004843 Entry->setInitializer(Init);
4844 } else {
4845 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00004846 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004847 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004848 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004849 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004850 Entry->setAlignment(
4851 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004852 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004853 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004854 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00004855 CGM.AddUsedGlobal(Entry);
4856
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004857 // Use this protocol meta-data to build protocol list table in section
4858 // __DATA, __objc_protolist
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004859 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
Owen Anderson1c431b32009-07-08 19:05:04 +00004860 CGM.getModule(),
Daniel Dunbar948e2582009-02-15 07:36:20 +00004861 ObjCTypes.ProtocolnfABIPtrTy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004862 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004863 Entry,
4864 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
Owen Anderson1c431b32009-07-08 19:05:04 +00004865 +ProtocolName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004866 PTGV->setAlignment(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004867 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00004868 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004869 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00004870 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004871 return Entry;
4872}
4873
4874/// EmitProtocolList - Generate protocol list meta-data:
4875/// @code
4876/// struct _protocol_list_t {
4877/// long protocol_count; // Note, this is 32/64 bit
4878/// struct _protocol_t[protocol_count];
4879/// }
4880/// @endcode
4881///
4882llvm::Constant *
4883CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4884 ObjCProtocolDecl::protocol_iterator begin,
4885 ObjCProtocolDecl::protocol_iterator end) {
4886 std::vector<llvm::Constant*> ProtocolRefs;
4887
Fariborz Jahanianda320092009-01-29 19:24:30 +00004888 // Just return null for empty protocol lists
Daniel Dunbar948e2582009-02-15 07:36:20 +00004889 if (begin == end)
Owen Anderson69243822009-07-13 04:10:07 +00004890 return VMContext.getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004891
Daniel Dunbar948e2582009-02-15 07:36:20 +00004892 // FIXME: We shouldn't need to do this lookup here, should we?
Fariborz Jahanianda320092009-01-29 19:24:30 +00004893 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4894 if (GV)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004895 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00004896 ObjCTypes.ProtocolListnfABIPtrTy);
4897
4898 for (; begin != end; ++begin)
4899 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4900
Fariborz Jahanianda320092009-01-29 19:24:30 +00004901 // This list is null terminated.
Owen Anderson69243822009-07-13 04:10:07 +00004902 ProtocolRefs.push_back(VMContext.getNullValue(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004903 ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004904
4905 std::vector<llvm::Constant*> Values(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004906 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004907 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004908 Values[1] =
Owen Anderson7db6d832009-07-28 18:33:04 +00004909 llvm::ConstantArray::get(
Owen Anderson96e0fc72009-07-29 22:16:19 +00004910 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00004911 ProtocolRefs.size()),
4912 ProtocolRefs);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004913
Owen Anderson08e25242009-07-27 22:29:56 +00004914 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Owen Anderson1c431b32009-07-08 19:05:04 +00004915 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004916 llvm::GlobalValue::InternalLinkage,
4917 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004918 Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004919 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004920 GV->setAlignment(
4921 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00004922 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00004923 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00004924 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004925}
4926
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004927/// GetMethodDescriptionConstant - This routine build following meta-data:
4928/// struct _objc_method {
4929/// SEL _cmd;
4930/// char *method_type;
4931/// char *_imp;
4932/// }
4933
4934llvm::Constant *
4935CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4936 std::vector<llvm::Constant*> Desc(3);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004937 Desc[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00004938 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004939 ObjCTypes.SelectorPtrTy);
4940 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004941 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson69243822009-07-13 04:10:07 +00004942 Desc[2] = VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00004943 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004944}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004945
4946/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4947/// This code gen. amounts to generating code for:
4948/// @code
4949/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4950/// @encode
4951///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004952LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004953 CodeGen::CodeGenFunction &CGF,
4954 QualType ObjectTy,
4955 llvm::Value *BaseValue,
4956 const ObjCIvarDecl *Ivar,
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004957 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00004958 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00004959 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
4960 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004961}
4962
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004963llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
4964 CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00004965 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004966 const ObjCIvarDecl *Ivar) {
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00004967 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),
4968 false, "ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004969}
4970
Fariborz Jahanian46551122009-02-04 00:22:57 +00004971CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4972 CodeGen::CodeGenFunction &CGF,
4973 QualType ResultType,
4974 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004975 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00004976 QualType Arg0Ty,
4977 bool IsSuper,
4978 const CallArgList &CallArgs) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00004979 // FIXME. Even though IsSuper is passes. This function doese not handle calls
4980 // to 'super' receivers.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004981 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004982 llvm::Value *Arg0 = Receiver;
4983 if (!IsSuper)
4984 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004985
4986 // Find the message function name.
Mike Stumpf5408fe2009-05-16 07:57:57 +00004987 // FIXME. This is too much work to get the ABI-specific result type needed to
4988 // find the message name.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004989 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4990 llvm::SmallVector<QualType, 16>());
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00004991 llvm::Constant *Fn = 0;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004992 std::string Name("\01l_");
4993 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004994#if 0
4995 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004996 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00004997 Fn = ObjCTypes.getMessageSendIdStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004998 // FIXME. Is there a better way of getting these names.
4999 // They are available in RuntimeFunctions vector pair.
5000 Name += "objc_msgSendId_stret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005001 } else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005002#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005003 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005004 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005005 Name += "objc_msgSendSuper2_stret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005006 } else {
Chris Lattner1c02f862009-04-22 02:53:24 +00005007 Fn = ObjCTypes.getMessageSendStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005008 Name += "objc_msgSend_stret_fixup";
5009 }
Mike Stumpb3589f42009-07-30 22:28:39 +00005010 } else if (!IsSuper && ResultType->isFloatingType()) {
Daniel Dunbarc0183e82009-06-26 18:32:06 +00005011 if (ResultType->isSpecificBuiltinType(BuiltinType::LongDouble)) {
5012 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5013 Name += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005014 } else {
Daniel Dunbarc0183e82009-06-26 18:32:06 +00005015 Fn = ObjCTypes.getMessageSendFixupFn();
5016 Name += "objc_msgSend_fixup";
Fariborz Jahanian5b2bad02009-04-30 16:31:11 +00005017 }
Mike Stumpb3589f42009-07-30 22:28:39 +00005018 } 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";
Mike Stumpb3589f42009-07-30 22:28:39 +00005024 } else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005025#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005026 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005027 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005028 Name += "objc_msgSendSuper2_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005029 } else {
Chris Lattner1c02f862009-04-22 02:53:24 +00005030 Fn = ObjCTypes.getMessageSendFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005031 Name += "objc_msgSend_fixup";
5032 }
5033 }
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005034 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005035 Name += '_';
5036 std::string SelName(Sel.getAsString());
5037 // Replace all ':' in selector name with '_' ouch!
5038 for(unsigned i = 0; i < SelName.size(); i++)
5039 if (SelName[i] == ':')
5040 SelName[i] = '_';
5041 Name += SelName;
5042 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5043 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005044 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005045 std::vector<llvm::Constant*> Values(2);
5046 Values[0] = Fn;
5047 Values[1] = GetMethodVarName(Sel);
Owen Anderson08e25242009-07-27 22:29:56 +00005048 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Owen Anderson1c431b32009-07-08 19:05:04 +00005049 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005050 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005051 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005052 Name);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005053 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005054 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005055 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005056 }
5057 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005058
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005059 CallArgList ActualArgs;
5060 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
5061 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
5062 ObjCTypes.MessageRefCPtrTy));
5063 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005064 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
5065 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5066 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00005067 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005068 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Anderson96e0fc72009-07-29 22:16:19 +00005069 llvm::PointerType::getUnqual(FTy));
Fariborz Jahanianef163782009-02-05 01:13:09 +00005070 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005071}
5072
5073/// Generate code for a message send expression in the nonfragile abi.
5074CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
5075 CodeGen::CodeGenFunction &CGF,
5076 QualType ResultType,
5077 Selector Sel,
5078 llvm::Value *Receiver,
5079 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00005080 const CallArgList &CallArgs,
5081 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005082 return LegacyDispatchedSelector(Sel)
5083 ? EmitLegacyMessageSend(CGF, ResultType, EmitSelector(CGF.Builder, Sel),
5084 Receiver, CGF.getContext().getObjCIdType(),
5085 false, CallArgs, ObjCTypes)
5086 : EmitMessageSend(CGF, ResultType, Sel,
5087 Receiver, CGF.getContext().getObjCIdType(),
5088 false, CallArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005089}
5090
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005091llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005092CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005093 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5094
Daniel Dunbardfff2302009-03-02 05:18:14 +00005095 if (!GV) {
Owen Anderson1c431b32009-07-08 19:05:04 +00005096 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
5097 false, llvm::GlobalValue::ExternalLinkage,
5098 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005099 }
5100
5101 return GV;
5102}
5103
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005104llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005105 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005106 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
5107
5108 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005109 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005110 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005111 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005112 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5113 false, llvm::GlobalValue::InternalLinkage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005114 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005115 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005116 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");
Chris Lattnerad64e022009-07-17 23:57:13 +00005120 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005121 }
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 =
Owen Anderson1c431b32009-07-08 19:05:04 +00005135 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5136 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar11394522009-04-18 08:51:00 +00005137 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005138 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005139 Entry->setAlignment(
5140 CGM.getTargetData().getPrefTypeAlignment(
5141 ObjCTypes.ClassnfABIPtrTy));
5142 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005143 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005144 }
5145
5146 return Builder.CreateLoad(Entry, false, "tmp");
5147}
5148
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005149/// EmitMetaClassRef - Return a Value * of the address of _class_t
5150/// meta-data
5151///
5152llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5153 const ObjCInterfaceDecl *ID) {
5154 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5155 if (Entry)
5156 return Builder.CreateLoad(Entry, false, "tmp");
5157
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005158 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005159 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005160 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005161 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005162 llvm::GlobalValue::InternalLinkage,
5163 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005164 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005165 Entry->setAlignment(
5166 CGM.getTargetData().getPrefTypeAlignment(
5167 ObjCTypes.ClassnfABIPtrTy));
5168
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005169 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005170 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005171
5172 return Builder.CreateLoad(Entry, false, "tmp");
5173}
5174
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005175/// GetClass - Return a reference to the class for the given interface
5176/// decl.
5177llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5178 const ObjCInterfaceDecl *ID) {
5179 return EmitClassRef(Builder, ID);
5180}
5181
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005182/// Generates a message send where the super is the receiver. This is
5183/// a message send to self with special delivery semantics indicating
5184/// which class's method should be called.
5185CodeGen::RValue
5186CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
5187 QualType ResultType,
5188 Selector Sel,
5189 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005190 bool isCategoryImpl,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005191 llvm::Value *Receiver,
5192 bool IsClassMessage,
5193 const CodeGen::CallArgList &CallArgs) {
5194 // ...
5195 // Create and init a super structure; this is a (receiver, class)
5196 // pair we will pass to objc_msgSendSuper.
5197 llvm::Value *ObjCSuper =
5198 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
5199
5200 llvm::Value *ReceiverAsObject =
5201 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5202 CGF.Builder.CreateStore(ReceiverAsObject,
5203 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
5204
5205 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005206 llvm::Value *Target;
5207 if (IsClassMessage) {
5208 if (isCategoryImpl) {
5209 // Message sent to "super' in a class method defined in
5210 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005211 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005212 Target = CGF.Builder.CreateStructGEP(Target, 0);
5213 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00005214 } else
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005215 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stumpb3589f42009-07-30 22:28:39 +00005216 } else
Daniel Dunbar11394522009-04-18 08:51:00 +00005217 Target = EmitSuperClassRef(CGF.Builder, Class);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005218
Mike Stumpf5408fe2009-05-16 07:57:57 +00005219 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5220 // ObjCTypes types.
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005221 const llvm::Type *ClassTy =
5222 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5223 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5224 CGF.Builder.CreateStore(Target,
5225 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
5226
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005227 return (LegacyDispatchedSelector(Sel))
5228 ? EmitLegacyMessageSend(CGF, ResultType,EmitSelector(CGF.Builder, Sel),
5229 ObjCSuper, ObjCTypes.SuperPtrCTy,
5230 true, CallArgs,
5231 ObjCTypes)
5232 : EmitMessageSend(CGF, ResultType, Sel,
5233 ObjCSuper, ObjCTypes.SuperPtrCTy,
5234 true, CallArgs);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005235}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005236
5237llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
5238 Selector Sel) {
5239 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
5240
5241 if (!Entry) {
5242 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00005243 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005244 ObjCTypes.SelectorPtrTy);
5245 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005246 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005247 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00005248 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005249 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005250 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005251 }
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)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005263 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005264 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)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005285 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005286 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
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005298void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
5299 CodeGen::CodeGenFunction &CGF,
5300 llvm::Value *DestPtr,
5301 llvm::Value *SrcPtr,
5302 unsigned long size) {
5303 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5304 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005305 llvm::Value *N = llvm::ConstantInt::get(ObjCTypes.LongTy, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005306 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
5307 DestPtr, SrcPtr, N);
5308 return;
5309}
5310
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005311/// EmitObjCWeakRead - Code gen for loading value of a __weak
5312/// object: objc_read_weak (id *src)
5313///
5314llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
5315 CodeGen::CodeGenFunction &CGF,
5316 llvm::Value *AddrWeakObj)
5317{
Eli Friedman8339b352009-03-07 03:57:15 +00005318 const llvm::Type* DestTy =
5319 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005320 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005321 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005322 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005323 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005324 return read_weak;
5325}
5326
5327/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5328/// objc_assign_weak (id src, id *dst)
5329///
5330void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
5331 llvm::Value *src, llvm::Value *dst)
5332{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005333 const llvm::Type * SrcTy = src->getType();
5334 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005335 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005336 assert(Size <= 8 && "does not support size > 8");
5337 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5338 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005339 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5340 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005341 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5342 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005343 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005344 src, dst, "weakassign");
5345 return;
5346}
5347
5348/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5349/// objc_assign_global (id src, id *dst)
5350///
5351void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
5352 llvm::Value *src, llvm::Value *dst)
5353{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005354 const llvm::Type * SrcTy = src->getType();
5355 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005356 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005357 assert(Size <= 8 && "does not support size > 8");
5358 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5359 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005360 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5361 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005362 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5363 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005364 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005365 src, dst, "globalassign");
5366 return;
5367}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005368
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005369void
5370CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5371 const Stmt &S) {
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005372 bool isTry = isa<ObjCAtTryStmt>(S);
5373 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
5374 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005375 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005376 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005377 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005378 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
5379
5380 // For @synchronized, call objc_sync_enter(sync.expr). The
5381 // evaluation of the expression must occur before we enter the
5382 // @synchronized. We can safely avoid a temp here because jumps into
5383 // @synchronized are illegal & this will dominate uses.
5384 llvm::Value *SyncArg = 0;
5385 if (!isTry) {
5386 SyncArg =
5387 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
5388 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005389 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005390 }
5391
5392 // Push an EH context entry, used for handling rethrows and jumps
5393 // through finally.
5394 CGF.PushCleanupBlock(FinallyBlock);
5395
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005396 CGF.setInvokeDest(TryHandler);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005397
5398 CGF.EmitBlock(TryBlock);
5399 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
5400 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
5401 CGF.EmitBranchThroughCleanup(FinallyEnd);
5402
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005403 // Emit the exception handler.
5404
5405 CGF.EmitBlock(TryHandler);
5406
5407 llvm::Value *llvm_eh_exception =
5408 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
5409 llvm::Value *llvm_eh_selector_i64 =
5410 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64);
5411 llvm::Value *llvm_eh_typeid_for_i64 =
5412 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64);
5413 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5414 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow");
5415
5416 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
5417 SelectorArgs.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005418 SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005419
5420 // Construct the lists of (type, catch body) to handle.
Daniel Dunbarede8de92009-03-06 00:01:21 +00005421 llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005422 bool HasCatchAll = false;
5423 if (isTry) {
5424 if (const ObjCAtCatchStmt* CatchStmt =
5425 cast<ObjCAtTryStmt>(S).getCatchStmts()) {
5426 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005427 const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Steve Naroff7ba138a2009-03-03 19:52:17 +00005428 Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody()));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005429
5430 // catch(...) always matches.
Steve Naroff7ba138a2009-03-03 19:52:17 +00005431 if (!CatchDecl) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005432 // Use i8* null here to signal this is a catch all, not a cleanup.
Owen Anderson69243822009-07-13 04:10:07 +00005433 llvm::Value *Null = VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005434 SelectorArgs.push_back(Null);
5435 HasCatchAll = true;
5436 break;
5437 }
5438
Steve Naroff14108da2009-07-10 23:34:53 +00005439 if (CatchDecl->getType()->isObjCIdType() ||
Daniel Dunbarede8de92009-03-06 00:01:21 +00005440 CatchDecl->getType()->isObjCQualifiedIdType()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005441 llvm::Value *IDEHType =
5442 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5443 if (!IDEHType)
5444 IDEHType =
Owen Anderson1c431b32009-07-08 19:05:04 +00005445 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
5446 false,
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005447 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00005448 0, "OBJC_EHTYPE_id");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005449 SelectorArgs.push_back(IDEHType);
Mike Stumpb3589f42009-07-30 22:28:39 +00005450 } else {
Fariborz Jahanian99438a72009-07-27 23:12:41 +00005451 // All other types should be Objective-C interface pointer types.
5452 const ObjCObjectPointerType *PT =
5453 CatchDecl->getType()->getAsObjCObjectPointerType();
5454 assert(PT && "Invalid @catch type.");
5455 const ObjCInterfaceType *IT = PT->getInterfaceType();
5456 assert(IT && "Invalid @catch type.");
5457 llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false);
5458 SelectorArgs.push_back(EHType);
5459 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005460 }
5461 }
5462 }
5463
5464 // We use a cleanup unless there was already a catch all.
5465 if (!HasCatchAll) {
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005466 SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
Daniel Dunbarede8de92009-03-06 00:01:21 +00005467 Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005468 }
5469
5470 llvm::Value *Selector =
5471 CGF.Builder.CreateCall(llvm_eh_selector_i64,
5472 SelectorArgs.begin(), SelectorArgs.end(),
5473 "selector");
5474 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005475 const ParmVarDecl *CatchParam = Handlers[i].first;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005476 const Stmt *CatchBody = Handlers[i].second;
5477
5478 llvm::BasicBlock *Next = 0;
5479
5480 // The last handler always matches.
5481 if (i + 1 != e) {
5482 assert(CatchParam && "Only last handler can be a catch all.");
5483
5484 llvm::BasicBlock *Match = CGF.createBasicBlock("match");
5485 Next = CGF.createBasicBlock("catch.next");
5486 llvm::Value *Id =
5487 CGF.Builder.CreateCall(llvm_eh_typeid_for_i64,
5488 CGF.Builder.CreateBitCast(SelectorArgs[i+2],
5489 ObjCTypes.Int8PtrTy));
5490 CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id),
5491 Match, Next);
5492
5493 CGF.EmitBlock(Match);
5494 }
5495
5496 if (CatchBody) {
5497 llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end");
5498 llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler");
5499
5500 // Cleanups must call objc_end_catch.
5501 //
Mike Stumpf5408fe2009-05-16 07:57:57 +00005502 // FIXME: It seems incorrect for objc_begin_catch to be inside this
5503 // context, but this matches gcc.
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005504 CGF.PushCleanupBlock(MatchEnd);
5505 CGF.setInvokeDest(MatchHandler);
5506
5507 llvm::Value *ExcObject =
Chris Lattner8a569112009-04-22 02:15:23 +00005508 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005509
5510 // Bind the catch parameter if it exists.
5511 if (CatchParam) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005512 ExcObject =
5513 CGF.Builder.CreateBitCast(ExcObject,
5514 CGF.ConvertType(CatchParam->getType()));
5515 // CatchParam is a ParmVarDecl because of the grammar
5516 // construction used to handle this, but for codegen purposes
5517 // we treat this as a local decl.
5518 CGF.EmitLocalBlockVarDecl(*CatchParam);
5519 CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005520 }
5521
5522 CGF.ObjCEHValueStack.push_back(ExcObject);
5523 CGF.EmitStmt(CatchBody);
5524 CGF.ObjCEHValueStack.pop_back();
5525
5526 CGF.EmitBranchThroughCleanup(FinallyEnd);
5527
5528 CGF.EmitBlock(MatchHandler);
5529
5530 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5531 // We are required to emit this call to satisfy LLVM, even
5532 // though we don't use the result.
5533 llvm::SmallVector<llvm::Value*, 8> Args;
5534 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005535 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005536 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005537 0));
5538 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5539 CGF.Builder.CreateStore(Exc, RethrowPtr);
5540 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5541
5542 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5543
5544 CGF.EmitBlock(MatchEnd);
5545
5546 // Unfortunately, we also have to generate another EH frame here
5547 // in case this throws.
5548 llvm::BasicBlock *MatchEndHandler =
5549 CGF.createBasicBlock("match.end.handler");
5550 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattner8a569112009-04-22 02:15:23 +00005551 CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005552 Cont, MatchEndHandler,
5553 Args.begin(), Args.begin());
5554
5555 CGF.EmitBlock(Cont);
5556 if (Info.SwitchBlock)
5557 CGF.EmitBlock(Info.SwitchBlock);
5558 if (Info.EndBlock)
5559 CGF.EmitBlock(Info.EndBlock);
5560
5561 CGF.EmitBlock(MatchEndHandler);
5562 Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5563 // We are required to emit this call to satisfy LLVM, even
5564 // though we don't use the result.
5565 Args.clear();
5566 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005567 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005568 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005569 0));
5570 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5571 CGF.Builder.CreateStore(Exc, RethrowPtr);
5572 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5573
5574 if (Next)
5575 CGF.EmitBlock(Next);
5576 } else {
5577 assert(!Next && "catchup should be last handler.");
5578
5579 CGF.Builder.CreateStore(Exc, RethrowPtr);
5580 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5581 }
5582 }
5583
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005584 // Pop the cleanup entry, the @finally is outside this cleanup
5585 // scope.
5586 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5587 CGF.setInvokeDest(PrevLandingPad);
5588
5589 CGF.EmitBlock(FinallyBlock);
5590
5591 if (isTry) {
5592 if (const ObjCAtFinallyStmt* FinallyStmt =
5593 cast<ObjCAtTryStmt>(S).getFinallyStmt())
5594 CGF.EmitStmt(FinallyStmt->getFinallyBody());
5595 } else {
5596 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
5597 // @synchronized.
Chris Lattnerbbccd612009-04-22 02:38:11 +00005598 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005599 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005600
5601 if (Info.SwitchBlock)
5602 CGF.EmitBlock(Info.SwitchBlock);
5603 if (Info.EndBlock)
5604 CGF.EmitBlock(Info.EndBlock);
5605
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005606 // Branch around the rethrow code.
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005607 CGF.EmitBranch(FinallyEnd);
5608
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005609 CGF.EmitBlock(FinallyRethrow);
Chris Lattner8a569112009-04-22 02:15:23 +00005610 CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005611 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005612 CGF.Builder.CreateUnreachable();
5613
5614 CGF.EmitBlock(FinallyEnd);
5615}
5616
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005617/// EmitThrowStmt - Generate code for a throw statement.
5618void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
5619 const ObjCAtThrowStmt &S) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005620 llvm::Value *Exception;
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005621 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005622 Exception = CGF.EmitScalarExpr(ThrowExpr);
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005623 } else {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005624 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
5625 "Unexpected rethrow outside @catch block.");
5626 Exception = CGF.ObjCEHValueStack.back();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005627 }
5628
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005629 llvm::Value *ExceptionAsObject =
5630 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
5631 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
5632 if (InvokeDest) {
5633 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattnerbbccd612009-04-22 02:38:11 +00005634 CGF.Builder.CreateInvoke(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005635 Cont, InvokeDest,
5636 &ExceptionAsObject, &ExceptionAsObject + 1);
5637 CGF.EmitBlock(Cont);
5638 } else
Chris Lattnerbbccd612009-04-22 02:38:11 +00005639 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005640 CGF.Builder.CreateUnreachable();
5641
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005642 // Clear the insertion point to indicate we are in unreachable code.
5643 CGF.Builder.ClearInsertionPoint();
5644}
Daniel Dunbare588b992009-03-01 04:46:24 +00005645
5646llvm::Value *
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005647CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
5648 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00005649 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00005650
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005651 // If we don't need a definition, return the entry if found or check
5652 // if we use an external reference.
5653 if (!ForDefinition) {
5654 if (Entry)
5655 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00005656
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005657 // If this type (or a super class) has the __objc_exception__
5658 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00005659 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005660 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005661 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005662 llvm::GlobalValue::ExternalLinkage,
5663 0,
5664 (std::string("OBJC_EHTYPE_$_") +
Owen Anderson1c431b32009-07-08 19:05:04 +00005665 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005666 }
5667
5668 // Otherwise we need to either make a new entry or fill in the
5669 // initializer.
5670 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005671 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00005672 std::string VTableName = "objc_ehtype_vtable";
5673 llvm::GlobalVariable *VTableGV =
5674 CGM.getModule().getGlobalVariable(VTableName);
5675 if (!VTableGV)
Owen Anderson1c431b32009-07-08 19:05:04 +00005676 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
5677 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00005678 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00005679 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00005680
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005681 llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00005682
5683 std::vector<llvm::Constant*> Values(3);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005684 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbare588b992009-03-01 04:46:24 +00005685 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005686 Values[2] = GetClassGlobal(ClassName);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005687 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00005688 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00005689
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005690 if (Entry) {
5691 Entry->setInitializer(Init);
5692 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00005693 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005694 llvm::GlobalValue::WeakAnyLinkage,
5695 Init,
5696 (std::string("OBJC_EHTYPE_$_") +
Owen Anderson1c431b32009-07-08 19:05:04 +00005697 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005698 }
5699
Daniel Dunbar04d40782009-04-14 06:00:08 +00005700 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005701 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005702 Entry->setAlignment(8);
5703
5704 if (ForDefinition) {
5705 Entry->setSection("__DATA,__objc_const");
5706 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
5707 } else {
5708 Entry->setSection("__DATA,__datacoal_nt,coalesced");
5709 }
Daniel Dunbare588b992009-03-01 04:46:24 +00005710
5711 return Entry;
5712}
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005713
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005714/* *** */
5715
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005716CodeGen::CGObjCRuntime *
5717CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00005718 return new CGObjCMac(CGM);
5719}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005720
5721CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005722CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005723 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005724}