blob: 3ad2befd1ab8bdcf7827a42ddd2b645660b89517 [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 Dunbardbc93372008-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);
1471 }
1472 else {
1473 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1474 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1475 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1476 Target = Super;
1477 }
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001478 } else {
1479 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1480 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001481 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1482 // ObjCTypes types.
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001483 const llvm::Type *ClassTy =
1484 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001485 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001486 CGF.Builder.CreateStore(Target,
1487 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001488 return EmitLegacyMessageSend(CGF, ResultType,
1489 EmitSelector(CGF.Builder, Sel),
1490 ObjCSuper, ObjCTypes.SuperPtrCTy,
1491 true, CallArgs, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001492}
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001493
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001494/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001495CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001496 QualType ResultType,
1497 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001498 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001499 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001500 const CallArgList &CallArgs,
1501 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001502 return EmitLegacyMessageSend(CGF, ResultType,
1503 EmitSelector(CGF.Builder, Sel),
1504 Receiver, CGF.getContext().getObjCIdType(),
1505 false, CallArgs, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001506}
1507
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001508CodeGen::RValue CGObjCCommonMac::EmitLegacyMessageSend(
1509 CodeGen::CodeGenFunction &CGF,
1510 QualType ResultType,
1511 llvm::Value *Sel,
1512 llvm::Value *Arg0,
1513 QualType Arg0Ty,
1514 bool IsSuper,
1515 const CallArgList &CallArgs,
1516 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001517 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001518 if (!IsSuper)
1519 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001520 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001521 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001522 CGF.getContext().getObjCSelType()));
1523 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001524
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001525 CodeGenTypes &Types = CGM.getTypes();
1526 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001527 // In 64bit ABI, type must be assumed VARARG. In 32bit abi,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001528 // it seems not to matter.
1529 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, (ObjCABI == 2));
1530
1531 llvm::Constant *Fn = NULL;
Daniel Dunbar88b53962009-02-02 22:03:45 +00001532 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001533 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
1534 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001535 } else if (ResultType->isFloatingType()) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001536 if (ObjCABI == 2) {
1537 if (const BuiltinType *BT = ResultType->getAsBuiltinType()) {
1538 BuiltinType::Kind k = BT->getKind();
1539 Fn = (k == BuiltinType::LongDouble) ? ObjCTypes.getSendFpretFn2(IsSuper)
1540 : ObjCTypes.getSendFn2(IsSuper);
Daniel Dunbar42f963d2009-06-10 04:38:50 +00001541 } else {
1542 Fn = ObjCTypes.getSendFn2(IsSuper);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001543 }
1544 }
1545 else
Mike Stumpf5408fe2009-05-16 07:57:57 +00001546 // FIXME. This currently matches gcc's API for x86-32. May need to change
1547 // for others if we have their API.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001548 Fn = ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001549 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001550 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
1551 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001552 }
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001553 assert(Fn && "EmitLegacyMessageSend - unknown API");
Owen Anderson3c4972d2009-07-29 18:54:39 +00001554 Fn = llvm::ConstantExpr::getBitCast(Fn,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001555 llvm::PointerType::getUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +00001556 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001557}
1558
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001559llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001560 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001561 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001562 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001563 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1564
Owen Anderson3c4972d2009-07-29 18:54:39 +00001565 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001566 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001567}
1568
Fariborz Jahanianda320092009-01-29 19:24:30 +00001569void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001570 // FIXME: We shouldn't need this, the protocol decl should contain enough
1571 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001572 DefinedProtocols.insert(PD->getIdentifier());
1573
1574 // If we have generated a forward reference to this protocol, emit
1575 // it now. Otherwise do nothing, the protocol objects are lazily
1576 // emitted.
1577 if (Protocols.count(PD->getIdentifier()))
1578 GetOrEmitProtocol(PD);
1579}
1580
Fariborz Jahanianda320092009-01-29 19:24:30 +00001581llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001582 if (DefinedProtocols.count(PD->getIdentifier()))
1583 return GetOrEmitProtocol(PD);
1584 return GetOrEmitProtocolRef(PD);
1585}
1586
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001587/*
1588 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1589 struct _objc_protocol {
1590 struct _objc_protocol_extension *isa;
1591 char *protocol_name;
1592 struct _objc_protocol_list *protocol_list;
1593 struct _objc__method_prototype_list *instance_methods;
1594 struct _objc__method_prototype_list *class_methods
1595 };
1596
1597 See EmitProtocolExtension().
1598*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001599llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1600 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1601
1602 // Early exit if a defining object has already been generated.
1603 if (Entry && Entry->hasInitializer())
1604 return Entry;
1605
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001606 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001607 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001608 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1609
Chris Lattner8ec03f52008-11-24 03:54:41 +00001610 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001611
1612 // Construct method lists.
1613 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1614 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00001615 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001616 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001617 ObjCMethodDecl *MD = *i;
1618 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1619 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1620 OptInstanceMethods.push_back(C);
1621 } else {
1622 InstanceMethods.push_back(C);
1623 }
1624 }
1625
Douglas Gregor6ab35242009-04-09 21:40:53 +00001626 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001627 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001628 ObjCMethodDecl *MD = *i;
1629 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1630 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1631 OptClassMethods.push_back(C);
1632 } else {
1633 ClassMethods.push_back(C);
1634 }
1635 }
1636
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001637 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001638 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001639 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc93372008-08-21 21:57:41 +00001640 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001641 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00001642 PD->protocol_begin(),
1643 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001644 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001645 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
1646 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001647 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1648 InstanceMethods);
1649 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001650 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
1651 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001652 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1653 ClassMethods);
Owen Anderson08e25242009-07-27 22:29:56 +00001654 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001655 Values);
1656
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001657 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001658 // Already created, fix the linkage and update the initializer.
1659 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001660 Entry->setInitializer(Init);
1661 } else {
1662 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001663 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001664 llvm::GlobalValue::InternalLinkage,
1665 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00001666 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001667 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001668 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001669 // FIXME: Is this necessary? Why only for protocol?
1670 Entry->setAlignment(4);
1671 }
Chris Lattnerad64e022009-07-17 23:57:13 +00001672 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001673
1674 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001675}
1676
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001677llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001678 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1679
1680 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001681 // We use the initializer as a marker of whether this is a forward
1682 // reference or not. At module finalization we add the empty
1683 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001684 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001685 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001686 llvm::GlobalValue::ExternalLinkage,
1687 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00001688 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001689 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001690 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001691 // FIXME: Is this necessary? Why only for protocol?
1692 Entry->setAlignment(4);
1693 }
1694
1695 return Entry;
1696}
1697
1698/*
1699 struct _objc_protocol_extension {
1700 uint32_t size;
1701 struct objc_method_description_list *optional_instance_methods;
1702 struct objc_method_description_list *optional_class_methods;
1703 struct objc_property_list *instance_properties;
1704 };
1705*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001706llvm::Constant *
1707CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1708 const ConstantVector &OptInstanceMethods,
1709 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001710 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00001711 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001712 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001713 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001714 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001715 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1716 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001717 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1718 OptInstanceMethods);
1719 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001720 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1721 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001722 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1723 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001724 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1725 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001726 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001727
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001728 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001729 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1730 Values[3]->isNullValue())
Owen Anderson69243822009-07-13 04:10:07 +00001731 return VMContext.getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001732
1733 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00001734 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001735
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001736 // No special section, but goes in llvm.used
1737 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
1738 Init,
1739 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001740}
1741
1742/*
1743 struct objc_protocol_list {
1744 struct objc_protocol_list *next;
1745 long count;
1746 Protocol *list[];
1747 };
1748*/
Daniel Dunbardbc93372008-08-21 21:57:41 +00001749llvm::Constant *
1750CGObjCMac::EmitProtocolList(const std::string &Name,
1751 ObjCProtocolDecl::protocol_iterator begin,
1752 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001753 std::vector<llvm::Constant*> ProtocolRefs;
1754
Daniel Dunbardbc93372008-08-21 21:57:41 +00001755 for (; begin != end; ++begin)
1756 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001757
1758 // Just return null for empty protocol lists
1759 if (ProtocolRefs.empty())
Owen Anderson69243822009-07-13 04:10:07 +00001760 return VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001761
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001762 // This list is null terminated.
Owen Anderson69243822009-07-13 04:10:07 +00001763 ProtocolRefs.push_back(VMContext.getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001764
1765 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001766 // This field is only used by the runtime.
Owen Anderson69243822009-07-13 04:10:07 +00001767 Values[0] = VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001768 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001769 ProtocolRefs.size() - 1);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001770 Values[2] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00001771 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001772 ProtocolRefs.size()),
1773 ProtocolRefs);
1774
Owen Anderson08e25242009-07-27 22:29:56 +00001775 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001776 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001777 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001778 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001779 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001780}
1781
1782/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001783 struct _objc_property {
1784 const char * const name;
1785 const char * const attributes;
1786 };
1787
1788 struct _objc_property_list {
1789 uint32_t entsize; // sizeof (struct _objc_property)
1790 uint32_t prop_count;
1791 struct _objc_property[prop_count];
1792 };
1793*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001794llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1795 const Decl *Container,
1796 const ObjCContainerDecl *OCD,
1797 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001798 std::vector<llvm::Constant*> Properties, Prop(2);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001799 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1800 E = OCD->prop_end(); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00001801 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001802 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001803 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson08e25242009-07-27 22:29:56 +00001804 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001805 Prop));
1806 }
1807
1808 // Return null for empty list.
1809 if (Properties.empty())
Owen Anderson69243822009-07-13 04:10:07 +00001810 return VMContext.getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001811
1812 unsigned PropertySize =
Duncan Sands9408c452009-05-09 07:08:47 +00001813 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001814 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001815 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1816 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00001817 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001818 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00001819 Values[2] = llvm::ConstantArray::get(AT, Properties);
Owen Anderson08e25242009-07-27 22:29:56 +00001820 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001821
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001822 llvm::GlobalVariable *GV =
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001823 CreateMetadataVar(Name, Init,
1824 (ObjCABI == 2) ? "__DATA, __objc_const" :
1825 "__OBJC,__property,regular,no_dead_strip",
1826 (ObjCABI == 2) ? 8 : 4,
1827 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001828 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001829}
1830
1831/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001832 struct objc_method_description_list {
1833 int count;
1834 struct objc_method_description list[];
1835 };
1836*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001837llvm::Constant *
1838CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1839 std::vector<llvm::Constant*> Desc(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001840 Desc[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00001841 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001842 ObjCTypes.SelectorPtrTy);
1843 Desc[1] = GetMethodVarType(MD);
Owen Anderson08e25242009-07-27 22:29:56 +00001844 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001845 Desc);
1846}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001847
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001848llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1849 const char *Section,
1850 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001851 // Return null for empty list.
1852 if (Methods.empty())
Owen Anderson69243822009-07-13 04:10:07 +00001853 return VMContext.getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001854
1855 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001856 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00001857 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001858 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00001859 Values[1] = llvm::ConstantArray::get(AT, Methods);
Owen Anderson08e25242009-07-27 22:29:56 +00001860 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001861
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001862 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001863 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001864 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001865}
1866
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001867/*
1868 struct _objc_category {
1869 char *category_name;
1870 char *class_name;
1871 struct _objc_method_list *instance_methods;
1872 struct _objc_method_list *class_methods;
1873 struct _objc_protocol_list *protocols;
1874 uint32_t size; // <rdar://4585769>
1875 struct _objc_property_list *instance_properties;
1876 };
1877 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001878void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sands9408c452009-05-09 07:08:47 +00001879 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001880
Mike Stumpf5408fe2009-05-16 07:57:57 +00001881 // FIXME: This is poor design, the OCD should have a pointer to the category
1882 // decl. Additionally, note that Category can be null for the @implementation
1883 // w/o an @interface case. Sema should just create one for us as it does for
1884 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001885 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001886 const ObjCCategoryDecl *Category =
1887 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001888 std::string ExtName(Interface->getNameAsString() + "_" +
1889 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001890
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001891 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001892 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001893 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001894 // Instance methods should always be defined.
1895 InstanceMethods.push_back(GetMethodConstant(*i));
1896 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001897 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001898 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001899 // Class methods should always be defined.
1900 ClassMethods.push_back(GetMethodConstant(*i));
1901 }
1902
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001903 std::vector<llvm::Constant*> Values(7);
1904 Values[0] = GetClassName(OCD->getIdentifier());
1905 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00001906 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001907 Values[2] =
1908 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1909 ExtName,
1910 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001911 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001912 Values[3] =
1913 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001914 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001915 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001916 if (Category) {
1917 Values[4] =
1918 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1919 Category->protocol_begin(),
1920 Category->protocol_end());
1921 } else {
Owen Anderson69243822009-07-13 04:10:07 +00001922 Values[4] = VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001923 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001924 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001925
1926 // If there is no category @interface then there can be no properties.
1927 if (Category) {
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001928 Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001929 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001930 } else {
Owen Anderson69243822009-07-13 04:10:07 +00001931 Values[6] = VMContext.getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001932 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001933
Owen Anderson08e25242009-07-27 22:29:56 +00001934 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001935 Values);
1936
1937 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001938 CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init,
1939 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001940 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001941 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001942}
1943
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001944// FIXME: Get from somewhere?
1945enum ClassFlags {
1946 eClassFlags_Factory = 0x00001,
1947 eClassFlags_Meta = 0x00002,
1948 // <rdr://5142207>
1949 eClassFlags_HasCXXStructors = 0x02000,
1950 eClassFlags_Hidden = 0x20000,
1951 eClassFlags_ABI2_Hidden = 0x00010,
1952 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1953};
1954
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001955/*
1956 struct _objc_class {
1957 Class isa;
1958 Class super_class;
1959 const char *name;
1960 long version;
1961 long info;
1962 long instance_size;
1963 struct _objc_ivar_list *ivars;
1964 struct _objc_method_list *methods;
1965 struct _objc_cache *cache;
1966 struct _objc_protocol_list *protocols;
1967 // Objective-C 1.0 extensions (<rdr://4585769>)
1968 const char *ivar_layout;
1969 struct _objc_class_ext *ext;
1970 };
1971
1972 See EmitClassExtension();
1973 */
1974void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001975 DefinedSymbols.insert(ID->getIdentifier());
1976
Chris Lattner8ec03f52008-11-24 03:54:41 +00001977 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001978 // FIXME: Gross
1979 ObjCInterfaceDecl *Interface =
1980 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc93372008-08-21 21:57:41 +00001981 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001982 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00001983 Interface->protocol_begin(),
1984 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001985 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar2bebbf02009-05-03 10:46:44 +00001986 unsigned Size =
1987 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001988
1989 // FIXME: Set CXX-structors flag.
Daniel Dunbar04d40782009-04-14 06:00:08 +00001990 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001991 Flags |= eClassFlags_Hidden;
1992
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001993 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001994 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001995 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001996 // Instance methods should always be defined.
1997 InstanceMethods.push_back(GetMethodConstant(*i));
1998 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001999 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002000 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002001 // Class methods should always be defined.
2002 ClassMethods.push_back(GetMethodConstant(*i));
2003 }
2004
Douglas Gregor653f1b12009-04-23 01:02:12 +00002005 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002006 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002007 ObjCPropertyImplDecl *PID = *i;
2008
2009 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2010 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2011
2012 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2013 if (llvm::Constant *C = GetMethodConstant(MD))
2014 InstanceMethods.push_back(C);
2015 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2016 if (llvm::Constant *C = GetMethodConstant(MD))
2017 InstanceMethods.push_back(C);
2018 }
2019 }
2020
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002021 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar5384b092009-05-03 08:56:52 +00002022 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002023 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002024 // Record a reference to the super class.
2025 LazySymbols.insert(Super->getIdentifier());
2026
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002027 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002028 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002029 ObjCTypes.ClassPtrTy);
2030 } else {
Owen Anderson69243822009-07-13 04:10:07 +00002031 Values[ 1] = VMContext.getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002032 }
2033 Values[ 2] = GetClassName(ID->getIdentifier());
2034 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002035 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2036 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2037 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002038 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002039 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002040 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002041 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002042 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002043 // cache is always NULL.
Owen Anderson69243822009-07-13 04:10:07 +00002044 Values[ 8] = VMContext.getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002045 Values[ 9] = Protocols;
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002046 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002047 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00002048 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002049 Values);
2050
2051 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002052 CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init,
2053 "__OBJC,__class,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002054 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002055 DefinedClasses.push_back(GV);
2056}
2057
2058llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2059 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002060 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002061 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002062 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002063
Daniel Dunbar04d40782009-04-14 06:00:08 +00002064 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002065 Flags |= eClassFlags_Hidden;
2066
2067 std::vector<llvm::Constant*> Values(12);
2068 // The isa for the metaclass is the root of the hierarchy.
2069 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2070 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2071 Root = Super;
2072 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002073 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002074 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002075 // The super class for the metaclass is emitted as the name of the
2076 // super class. The runtime fixes this up to point to the
2077 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002078 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
2079 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002080 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002081 ObjCTypes.ClassPtrTy);
2082 } else {
Owen Anderson69243822009-07-13 04:10:07 +00002083 Values[ 1] = VMContext.getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002084 }
2085 Values[ 2] = GetClassName(ID->getIdentifier());
2086 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002087 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2088 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2089 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002090 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002091 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002092 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002093 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002094 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002095 // cache is always NULL.
Owen Anderson69243822009-07-13 04:10:07 +00002096 Values[ 8] = VMContext.getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002097 Values[ 9] = Protocols;
2098 // ivar_layout for metaclass is always NULL.
Owen Anderson69243822009-07-13 04:10:07 +00002099 Values[10] = VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002100 // The class extension is always unused for metaclasses.
Owen Anderson69243822009-07-13 04:10:07 +00002101 Values[11] = VMContext.getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002102 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002103 Values);
2104
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002105 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002106 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002107
2108 // Check for a forward reference.
2109 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2110 if (GV) {
2111 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2112 "Forward metaclass reference has incorrect type.");
2113 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2114 GV->setInitializer(Init);
2115 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002116 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002117 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002118 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002119 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002120 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002121 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002122 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002123
2124 return GV;
2125}
2126
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002127llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002128 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002129
Mike Stumpf5408fe2009-05-16 07:57:57 +00002130 // FIXME: Should we look these up somewhere other than the module. Its a bit
2131 // silly since we only generate these while processing an implementation, so
2132 // exactly one pointer would work if know when we entered/exitted an
2133 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002134
2135 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002136 // Previously, metaclass with internal linkage may have been defined.
2137 // pass 'true' as 2nd argument so it is returned.
2138 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002139 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2140 "Forward metaclass reference has incorrect type.");
2141 return GV;
2142 } else {
2143 // Generate as an external reference to keep a consistent
2144 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002145 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002146 llvm::GlobalValue::ExternalLinkage,
2147 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002148 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002149 }
2150}
2151
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002152/*
2153 struct objc_class_ext {
2154 uint32_t size;
2155 const char *weak_ivar_layout;
2156 struct _objc_property_list *properties;
2157 };
2158*/
2159llvm::Constant *
2160CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
2161 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002162 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002163
2164 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002165 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002166 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002167 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002168 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002169
2170 // Return null if no extension bits are used.
2171 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson69243822009-07-13 04:10:07 +00002172 return VMContext.getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002173
2174 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002175 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002176 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002177 Init, "__OBJC,__class_ext,regular,no_dead_strip",
2178 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002179}
2180
2181/*
2182 struct objc_ivar {
2183 char *ivar_name;
2184 char *ivar_type;
2185 int ivar_offset;
2186 };
2187
2188 struct objc_ivar_list {
2189 int ivar_count;
2190 struct objc_ivar list[count];
2191 };
2192 */
2193llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002194 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002195 std::vector<llvm::Constant*> Ivars, Ivar(3);
2196
2197 // When emitting the root class GCC emits ivar entries for the
2198 // actual class structure. It is not clear if we need to follow this
2199 // behavior; for now lets try and get away with not doing it. If so,
2200 // the cleanest solution would be to make up an ObjCInterfaceDecl
2201 // for the class.
2202 if (ForClass)
Owen Anderson69243822009-07-13 04:10:07 +00002203 return VMContext.getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002204
2205 ObjCInterfaceDecl *OID =
2206 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002207
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002208 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002209 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002210
2211 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2212 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002213 // Ignore unnamed bit-fields.
2214 if (!IVD->getDeclName())
2215 continue;
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002216 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2217 Ivar[1] = GetMethodVarType(IVD);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002218 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002219 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson08e25242009-07-27 22:29:56 +00002220 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002221 }
2222
2223 // Return null for empty list.
2224 if (Ivars.empty())
Owen Anderson69243822009-07-13 04:10:07 +00002225 return VMContext.getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002226
2227 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002228 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002229 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002230 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002231 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Owen Anderson08e25242009-07-27 22:29:56 +00002232 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002233
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002234 llvm::GlobalVariable *GV;
2235 if (ForClass)
2236 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(),
Daniel Dunbar58a29122009-03-09 22:18:41 +00002237 Init, "__OBJC,__class_vars,regular,no_dead_strip",
2238 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002239 else
2240 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_"
2241 + ID->getNameAsString(),
2242 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002243 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002244 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002245}
2246
2247/*
2248 struct objc_method {
2249 SEL method_name;
2250 char *method_types;
2251 void *method;
2252 };
2253
2254 struct objc_method_list {
2255 struct objc_method_list *obsolete;
2256 int count;
2257 struct objc_method methods_list[count];
2258 };
2259*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002260
2261/// GetMethodConstant - Return a struct objc_method constant for the
2262/// given method if it has been defined. The result is null if the
2263/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002264llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002265 // FIXME: Use DenseMap::lookup
2266 llvm::Function *Fn = MethodDefinitions[MD];
2267 if (!Fn)
2268 return 0;
2269
2270 std::vector<llvm::Constant*> Method(3);
2271 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002272 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002273 ObjCTypes.SelectorPtrTy);
2274 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002275 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002276 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002277}
2278
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002279llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
2280 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002281 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002282 // Return null for empty list.
2283 if (Methods.empty())
Owen Anderson69243822009-07-13 04:10:07 +00002284 return VMContext.getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002285
2286 std::vector<llvm::Constant*> Values(3);
Owen Anderson69243822009-07-13 04:10:07 +00002287 Values[0] = VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002288 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002289 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002290 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002291 Values[2] = llvm::ConstantArray::get(AT, Methods);
Owen Anderson08e25242009-07-27 22:29:56 +00002292 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002293
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002294 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002295 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002296 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002297}
2298
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002299llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00002300 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002301 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002302 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002303
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002304 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002305 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002306 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002307 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002308 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002309 llvm::GlobalValue::InternalLinkage,
2310 Name,
2311 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002312 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002313
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002314 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002315}
2316
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002317llvm::GlobalVariable *
2318CGObjCCommonMac::CreateMetadataVar(const std::string &Name,
2319 llvm::Constant *Init,
2320 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002321 unsigned Align,
2322 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002323 const llvm::Type *Ty = Init->getType();
2324 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002325 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002326 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002327 if (Section)
2328 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002329 if (Align)
2330 GV->setAlignment(Align);
2331 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002332 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002333 return GV;
2334}
2335
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002336llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002337 // Abuse this interface function as a place to finalize.
2338 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002339 return NULL;
2340}
2341
Chris Lattner74391b42009-03-22 21:03:39 +00002342llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002343 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002344}
2345
Chris Lattner74391b42009-03-22 21:03:39 +00002346llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002347 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002348}
2349
Chris Lattner74391b42009-03-22 21:03:39 +00002350llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002351 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002352}
2353
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002354/*
2355
2356Objective-C setjmp-longjmp (sjlj) Exception Handling
2357--
2358
2359The basic framework for a @try-catch-finally is as follows:
2360{
2361 objc_exception_data d;
2362 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002363 bool _call_try_exit = true;
2364
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002365 objc_exception_try_enter(&d);
2366 if (!setjmp(d.jmp_buf)) {
2367 ... try body ...
2368 } else {
2369 // exception path
2370 id _caught = objc_exception_extract(&d);
2371
2372 // enter new try scope for handlers
2373 if (!setjmp(d.jmp_buf)) {
2374 ... match exception and execute catch blocks ...
2375
2376 // fell off end, rethrow.
2377 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00002378 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002379 } else {
2380 // exception in catch block
2381 _rethrow = objc_exception_extract(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002382 _call_try_exit = false;
2383 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002384 }
2385 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002386 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002387
2388finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002389 if (_call_try_exit)
2390 objc_exception_try_exit(&d);
2391
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002392 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002393 ... dispatch to finally destination ...
2394
2395finally_rethrow:
2396 objc_exception_throw(_rethrow);
2397
2398finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002399}
2400
2401This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00002402uses _rethrow to determine if objc_exception_try_exit should be called
2403and if the object should be rethrown. This breaks in the face of
2404throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002405
2406We specialize this framework for a few particular circumstances:
2407
2408 - If there are no catch blocks, then we avoid emitting the second
2409 exception handling context.
2410
2411 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2412 e)) we avoid emitting the code to rethrow an uncaught exception.
2413
2414 - FIXME: If there is no @finally block we can do a few more
2415 simplifications.
2416
2417Rethrows and Jumps-Through-Finally
2418--
2419
2420Support for implicit rethrows and jumping through the finally block is
2421handled by storing the current exception-handling context in
2422ObjCEHStack.
2423
Daniel Dunbar898d5082008-09-30 01:06:03 +00002424In order to implement proper @finally semantics, we support one basic
2425mechanism for jumping through the finally block to an arbitrary
2426destination. Constructs which generate exits from a @try or @catch
2427block use this mechanism to implement the proper semantics by chaining
2428jumps, as necessary.
2429
2430This mechanism works like the one used for indirect goto: we
2431arbitrarily assign an ID to each destination and store the ID for the
2432destination in a variable prior to entering the finally block. At the
2433end of the finally block we simply create a switch to the proper
2434destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002435
2436Code gen for @synchronized(expr) stmt;
2437Effectively generating code for:
2438objc_sync_enter(expr);
2439@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002440*/
2441
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002442void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2443 const Stmt &S) {
2444 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002445 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00002446 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002447 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar55e87422008-11-11 02:29:29 +00002448 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
2449 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
2450 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar1c566672009-02-24 01:43:46 +00002451
2452 // For @synchronized, call objc_sync_enter(sync.expr). The
2453 // evaluation of the expression must occur before we enter the
2454 // @synchronized. We can safely avoid a temp here because jumps into
2455 // @synchronized are illegal & this will dominate uses.
2456 llvm::Value *SyncArg = 0;
2457 if (!isTry) {
2458 SyncArg =
2459 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2460 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00002461 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002462 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002463
2464 // Push an EH context entry, used for handling rethrows and jumps
2465 // through finally.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002466 CGF.PushCleanupBlock(FinallyBlock);
2467
Anders Carlsson273558f2009-02-07 21:37:21 +00002468 CGF.ObjCEHValueStack.push_back(0);
2469
Daniel Dunbar898d5082008-09-30 01:06:03 +00002470 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00002471 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2472 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002473 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
2474 "_rethrow");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002475 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
2476 "_call_try_exit");
Owen Anderson4cd16082009-07-21 18:06:41 +00002477 CGF.Builder.CreateStore(VMContext.getTrue(), CallTryExitPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002478
Anders Carlsson80f25672008-09-09 17:59:25 +00002479 // Enter a new try block and call setjmp.
Chris Lattner34b02a12009-04-22 02:26:14 +00002480 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Anders Carlsson80f25672008-09-09 17:59:25 +00002481 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
2482 "jmpbufarray");
2483 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
Chris Lattner34b02a12009-04-22 02:26:14 +00002484 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlsson80f25672008-09-09 17:59:25 +00002485 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00002486
Daniel Dunbar55e87422008-11-11 02:29:29 +00002487 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2488 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002489 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002490 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002491
2492 // Emit the @try block.
2493 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002494 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
2495 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002496 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002497
2498 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002499 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002500
2501 // Retrieve the exception object. We may emit multiple blocks but
2502 // nothing can cross this so the value is already in SSA form.
Chris Lattner34b02a12009-04-22 02:26:14 +00002503 llvm::Value *Caught =
2504 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2505 ExceptionData, "caught");
Anders Carlsson273558f2009-02-07 21:37:21 +00002506 CGF.ObjCEHValueStack.back() = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002507 if (!isTry)
2508 {
2509 CGF.Builder.CreateStore(Caught, RethrowPtr);
Owen Anderson4cd16082009-07-21 18:06:41 +00002510 CGF.Builder.CreateStore(VMContext.getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002511 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002512 }
2513 else if (const ObjCAtCatchStmt* CatchStmt =
2514 cast<ObjCAtTryStmt>(S).getCatchStmts())
2515 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00002516 // Enter a new exception try block (in case a @catch block throws
2517 // an exception).
Chris Lattner34b02a12009-04-22 02:26:14 +00002518 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002519
Chris Lattner34b02a12009-04-22 02:26:14 +00002520 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlsson80f25672008-09-09 17:59:25 +00002521 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002522 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00002523
Daniel Dunbar55e87422008-11-11 02:29:29 +00002524 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
2525 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002526 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002527
2528 CGF.EmitBlock(CatchBlock);
2529
Daniel Dunbar55e40722008-09-27 07:03:52 +00002530 // Handle catch list. As a special case we check if everything is
2531 // matched and avoid generating code for falling off the end if
2532 // so.
2533 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00002534 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00002535 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00002536
Steve Naroff7ba138a2009-03-03 19:52:17 +00002537 const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00002538 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00002539
Anders Carlsson80f25672008-09-09 17:59:25 +00002540 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00002541 if (!CatchParam) {
2542 AllMatched = true;
2543 } else {
Steve Naroff14108da2009-07-10 23:34:53 +00002544 OPT = CatchParam->getType()->getAsObjCObjectPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002545
Daniel Dunbar97f61d12008-09-27 22:21:14 +00002546 // catch(id e) always matches.
2547 // FIXME: For the time being we also match id<X>; this should
2548 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00002549 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00002550 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00002551 }
2552
Daniel Dunbar55e40722008-09-27 07:03:52 +00002553 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00002554 if (CatchParam) {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002555 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002556 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002557 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002558 }
Anders Carlsson1452f552008-09-11 08:21:54 +00002559
Anders Carlssondde0a942008-09-11 09:15:33 +00002560 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002561 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002562 break;
2563 }
2564
Steve Naroff14108da2009-07-10 23:34:53 +00002565 assert(OPT && "Unexpected non-object pointer type in @catch");
2566 QualType T = OPT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00002567 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002568 assert(ObjCType && "Catch parameter must have Objective-C type!");
2569
2570 // Check if the @catch block matches the exception object.
2571 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
2572
Chris Lattner34b02a12009-04-22 02:26:14 +00002573 llvm::Value *Match =
2574 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
2575 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00002576
Daniel Dunbar55e87422008-11-11 02:29:29 +00002577 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00002578
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002579 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002580 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002581
2582 // Emit the @catch block.
2583 CGF.EmitBlock(MatchedBlock);
Steve Naroff7ba138a2009-03-03 19:52:17 +00002584 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002585 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002586
2587 llvm::Value *Tmp =
Steve Naroff7ba138a2009-03-03 19:52:17 +00002588 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002589 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002590 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002591
2592 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002593 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002594
2595 CGF.EmitBlock(NextCatchBlock);
2596 }
2597
Daniel Dunbar55e40722008-09-27 07:03:52 +00002598 if (!AllMatched) {
2599 // None of the handlers caught the exception, so store it to be
2600 // rethrown at the end of the @finally block.
2601 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002602 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002603 }
2604
2605 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002606 CGF.EmitBlock(CatchHandler);
Chris Lattner34b02a12009-04-22 02:26:14 +00002607 CGF.Builder.CreateStore(
2608 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2609 ExceptionData),
Daniel Dunbar55e40722008-09-27 07:03:52 +00002610 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);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002613 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00002614 CGF.Builder.CreateStore(Caught, RethrowPtr);
Owen Anderson4cd16082009-07-21 18:06:41 +00002615 CGF.Builder.CreateStore(VMContext.getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002616 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson80f25672008-09-09 17:59:25 +00002617 }
2618
Daniel Dunbar898d5082008-09-30 01:06:03 +00002619 // Pop the exception-handling stack entry. It is important to do
2620 // this now, because the code in the @finally block is not in this
2621 // context.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002622 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2623
Anders Carlsson273558f2009-02-07 21:37:21 +00002624 CGF.ObjCEHValueStack.pop_back();
2625
Anders Carlsson80f25672008-09-09 17:59:25 +00002626 // Emit the @finally block.
2627 CGF.EmitBlock(FinallyBlock);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002628 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2629
2630 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2631
2632 CGF.EmitBlock(FinallyExit);
Chris Lattner34b02a12009-04-22 02:26:14 +00002633 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002634
2635 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002636 if (isTry) {
2637 if (const ObjCAtFinallyStmt* FinallyStmt =
2638 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2639 CGF.EmitStmt(FinallyStmt->getFinallyBody());
Daniel Dunbar1c566672009-02-24 01:43:46 +00002640 } else {
2641 // Emit objc_sync_exit(expr); as finally's sole statement for
2642 // @synchronized.
Chris Lattnerbbccd612009-04-22 02:38:11 +00002643 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002644 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002645
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002646 // Emit the switch block
2647 if (Info.SwitchBlock)
2648 CGF.EmitBlock(Info.SwitchBlock);
2649 if (Info.EndBlock)
2650 CGF.EmitBlock(Info.EndBlock);
2651
Daniel Dunbar898d5082008-09-30 01:06:03 +00002652 CGF.EmitBlock(FinallyRethrow);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002653 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbar898d5082008-09-30 01:06:03 +00002654 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002655 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00002656
2657 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002658}
2659
2660void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002661 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002662 llvm::Value *ExceptionAsObject;
2663
2664 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2665 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2666 ExceptionAsObject =
2667 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2668 } else {
Anders Carlsson273558f2009-02-07 21:37:21 +00002669 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002670 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00002671 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002672 }
2673
Chris Lattnerbbccd612009-04-22 02:38:11 +00002674 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002675 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002676
2677 // Clear the insertion point to indicate we are in unreachable code.
2678 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002679}
2680
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002681/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002682/// object: objc_read_weak (id *src)
2683///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002684llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002685 llvm::Value *AddrWeakObj)
2686{
Eli Friedman8339b352009-03-07 03:57:15 +00002687 const llvm::Type* DestTy =
2688 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002689 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00002690 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002691 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00002692 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002693 return read_weak;
2694}
2695
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002696/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2697/// objc_assign_weak (id src, id *dst)
2698///
2699void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2700 llvm::Value *src, llvm::Value *dst)
2701{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002702 const llvm::Type * SrcTy = src->getType();
2703 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002704 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002705 assert(Size <= 8 && "does not support size > 8");
2706 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2707 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002708 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2709 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002710 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2711 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00002712 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002713 src, dst, "weakassign");
2714 return;
2715}
2716
Fariborz Jahanian58626502008-11-19 00:59:10 +00002717/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2718/// objc_assign_global (id src, id *dst)
2719///
2720void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2721 llvm::Value *src, llvm::Value *dst)
2722{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002723 const llvm::Type * SrcTy = src->getType();
2724 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002725 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002726 assert(Size <= 8 && "does not support size > 8");
2727 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2728 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002729 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2730 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002731 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2732 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002733 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00002734 src, dst, "globalassign");
2735 return;
2736}
2737
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002738/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2739/// objc_assign_ivar (id src, id *dst)
2740///
2741void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2742 llvm::Value *src, llvm::Value *dst)
2743{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002744 const llvm::Type * SrcTy = src->getType();
2745 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002746 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002747 assert(Size <= 8 && "does not support size > 8");
2748 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2749 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002750 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2751 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002752 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2753 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002754 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(),
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002755 src, dst, "assignivar");
2756 return;
2757}
2758
Fariborz Jahanian58626502008-11-19 00:59:10 +00002759/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2760/// objc_assign_strongCast (id src, id *dst)
2761///
2762void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2763 llvm::Value *src, llvm::Value *dst)
2764{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002765 const llvm::Type * SrcTy = src->getType();
2766 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002767 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002768 assert(Size <= 8 && "does not support size > 8");
2769 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2770 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002771 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2772 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002773 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2774 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002775 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00002776 src, dst, "weakassign");
2777 return;
2778}
2779
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002780void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
2781 llvm::Value *DestPtr,
2782 llvm::Value *SrcPtr,
2783 unsigned long size) {
2784 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
2785 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002786 llvm::Value *N = llvm::ConstantInt::get(ObjCTypes.LongTy, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002787 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
2788 DestPtr, SrcPtr, N);
2789 return;
2790}
2791
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002792/// EmitObjCValueForIvar - Code Gen for ivar reference.
2793///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002794LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2795 QualType ObjectTy,
2796 llvm::Value *BaseValue,
2797 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002798 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00002799 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00002800 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
2801 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002802}
2803
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002804llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00002805 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002806 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00002807 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002808 return llvm::ConstantInt::get(
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002809 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2810 Offset);
2811}
2812
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002813/* *** Private Interface *** */
2814
2815/// EmitImageInfo - Emit the image info marker used to encode some module
2816/// level information.
2817///
2818/// See: <rdr://4810609&4810587&4810587>
2819/// struct IMAGE_INFO {
2820/// unsigned version;
2821/// unsigned flags;
2822/// };
2823enum ImageInfoFlags {
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002824 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what
2825 // this implies.
2826 eImageInfo_GarbageCollected = (1 << 1),
2827 eImageInfo_GCOnly = (1 << 2),
2828 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
2829
2830 // A flag indicating that the module has no instances of an
2831 // @synthesize of a superclass variable. <rdar://problem/6803242>
2832 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002833};
2834
2835void CGObjCMac::EmitImageInfo() {
2836 unsigned version = 0; // Version is unused?
2837 unsigned flags = 0;
2838
2839 // FIXME: Fix and continue?
2840 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2841 flags |= eImageInfo_GarbageCollected;
2842 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2843 flags |= eImageInfo_GCOnly;
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002844
2845 // We never allow @synthesize of a superclass property.
2846 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002847
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002848 // Emitted as int[2];
2849 llvm::Constant *values[2] = {
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002850 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2851 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002852 };
Owen Anderson96e0fc72009-07-29 22:16:19 +00002853 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002854
2855 const char *Section;
2856 if (ObjCABI == 1)
2857 Section = "__OBJC, __image_info,regular";
2858 else
2859 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002860 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002861 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson7db6d832009-07-28 18:33:04 +00002862 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002863 Section,
2864 0,
2865 true);
2866 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002867}
2868
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002869
2870// struct objc_module {
2871// unsigned long version;
2872// unsigned long size;
2873// const char *name;
2874// Symtab symtab;
2875// };
2876
2877// FIXME: Get from somewhere
2878static const int ModuleVersion = 7;
2879
2880void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00002881 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002882
2883 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002884 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2885 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002886 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002887 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002888 Values[3] = EmitModuleSymbols();
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002889 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00002890 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002891 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002892 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002893}
2894
2895llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002896 unsigned NumClasses = DefinedClasses.size();
2897 unsigned NumCategories = DefinedCategories.size();
2898
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002899 // Return null if no symbols were defined.
2900 if (!NumClasses && !NumCategories)
Owen Anderson69243822009-07-13 04:10:07 +00002901 return VMContext.getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002902
2903 std::vector<llvm::Constant*> Values(5);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002904 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson69243822009-07-13 04:10:07 +00002905 Values[1] = VMContext.getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002906 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2907 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002908
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002909 // The runtime expects exactly the list of defined classes followed
2910 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002911 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002912 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00002913 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002914 ObjCTypes.Int8PtrTy);
2915 for (unsigned i=0; i<NumCategories; i++)
2916 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002917 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002918 ObjCTypes.Int8PtrTy);
2919
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002920 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00002921 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002922 NumClasses + NumCategories),
2923 Symbols);
2924
Owen Anderson08e25242009-07-27 22:29:56 +00002925 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002926
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002927 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002928 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
2929 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002930 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002931 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002932}
2933
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002934llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002935 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002936 LazySymbols.insert(ID->getIdentifier());
2937
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002938 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2939
2940 if (!Entry) {
2941 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002942 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002943 ObjCTypes.ClassPtrTy);
2944 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002945 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
2946 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002947 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002948 }
2949
2950 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002951}
2952
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002953llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002954 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2955
2956 if (!Entry) {
2957 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002958 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002959 ObjCTypes.SelectorPtrTy);
2960 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002961 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
2962 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002963 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002964 }
2965
2966 return Builder.CreateLoad(Entry, false, "tmp");
2967}
2968
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002969llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002970 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002971
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002972 if (!Entry)
2973 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Owen Anderson7db6d832009-07-28 18:33:04 +00002974 llvm::ConstantArray::get(Ident->getName()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002975 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00002976 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002977
Owen Andersona1cf15f2009-07-14 23:10:40 +00002978 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002979}
2980
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00002981/// GetIvarLayoutName - Returns a unique constant for the given
2982/// ivar layout bitmap.
2983llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
2984 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson69243822009-07-13 04:10:07 +00002985 return VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00002986}
2987
Daniel Dunbard58edcb2009-05-03 14:10:34 +00002988static QualType::GCAttrTypes GetGCAttrTypeForType(ASTContext &Ctx,
2989 QualType FQT) {
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00002990 if (FQT.isObjCGCStrong())
2991 return QualType::Strong;
2992
2993 if (FQT.isObjCGCWeak())
2994 return QualType::Weak;
2995
Steve Narofff4954562009-07-16 15:41:00 +00002996 if (FQT->isObjCObjectPointerType())
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00002997 return QualType::Strong;
2998
Ted Kremenek6217b802009-07-29 21:53:49 +00002999 if (const PointerType *PT = FQT->getAs<PointerType>())
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003000 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003001
3002 return QualType::GCNone;
3003}
3004
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003005void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
3006 unsigned int BytePos,
3007 bool ForStrongLayout,
3008 bool &HasUnion) {
3009 const RecordDecl *RD = RT->getDecl();
3010 // FIXME - Use iterator.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003011 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003012 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
3013 const llvm::StructLayout *RecLayout =
3014 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
3015
3016 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3017 ForStrongLayout, HasUnion);
3018}
3019
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003020void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003021 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003022 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00003023 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003024 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003025 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003026 bool IsUnion = (RD && RD->isUnion());
3027 uint64_t MaxUnionIvarSize = 0;
3028 uint64_t MaxSkippedUnionIvarSize = 0;
3029 FieldDecl *MaxField = 0;
3030 FieldDecl *MaxSkippedField = 0;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003031 FieldDecl *LastFieldBitfield = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003032 uint64_t MaxFieldOffset = 0;
3033 uint64_t MaxSkippedFieldOffset = 0;
3034 uint64_t LastBitfieldOffset = 0;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003035
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003036 if (RecFields.empty())
3037 return;
Chris Lattnerf1690852009-03-31 08:48:01 +00003038 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3039 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3040
Chris Lattnerf1690852009-03-31 08:48:01 +00003041 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003042 FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003043 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003044 if (RD) {
3045 if (Field->isBitField()) {
3046 CodeGenTypes::BitFieldInfo Info = CGM.getTypes().getBitFieldInfo(Field);
3047 FieldOffset = Layout->getElementOffset(Info.FieldNo);
3048 } else
3049 FieldOffset =
3050 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
3051 } else
Daniel Dunbare05cc982009-05-03 23:35:23 +00003052 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003053
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003054 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003055 if (!Field->getIdentifier() || Field->isBitField()) {
3056 LastFieldBitfield = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003057 LastBitfieldOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003058 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003059 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003060
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003061 LastFieldBitfield = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003062 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003063 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003064 if (FQT->isUnionType())
3065 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003066
Ted Kremenek6217b802009-07-29 21:53:49 +00003067 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003068 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003069 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003070 continue;
3071 }
Chris Lattnerf1690852009-03-31 08:48:01 +00003072
3073 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003074 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003075 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003076 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003077 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003078 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003079 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3080 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003081 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003082 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003083 FQT = CArray->getElementType();
3084 }
3085
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003086 assert(!FQT->isUnionType() &&
3087 "layout for array of unions not supported");
3088 if (FQT->isRecordType()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003089 int OldIndex = IvarsInfo.size() - 1;
3090 int OldSkIndex = SkipIvars.size() -1;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003091
Ted Kremenek6217b802009-07-29 21:53:49 +00003092 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003093 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003094 ForStrongLayout, HasUnion);
3095
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003096 // Replicate layout information for each array element. Note that
3097 // one element is already done.
3098 uint64_t ElIx = 1;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003099 for (int FirstIndex = IvarsInfo.size() - 1,
3100 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003101 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003102 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3103 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3104 IvarsInfo[i].ivar_size));
3105 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3106 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3107 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003108 }
3109 continue;
3110 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003111 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003112 // At this point, we are done with Record/Union and array there of.
3113 // For other arrays we are down to its element type.
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003114 QualType::GCAttrTypes GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003115
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003116 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003117 if ((ForStrongLayout && GCAttr == QualType::Strong)
3118 || (!ForStrongLayout && GCAttr == QualType::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003119 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003120 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003121 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003122 MaxUnionIvarSize = UnionIvarSize;
3123 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003124 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003125 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003126 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003127 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003128 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003129 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003130 } else if ((ForStrongLayout &&
3131 (GCAttr == QualType::GCNone || GCAttr == QualType::Weak))
3132 || (!ForStrongLayout && GCAttr != QualType::Weak)) {
3133 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003134 // FIXME: Why the asymmetry? We divide by word size in bits on other
3135 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003136 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003137 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003138 MaxSkippedUnionIvarSize = UnionIvarSize;
3139 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003140 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003141 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003142 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003143 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003144 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003145 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003146 }
3147 }
3148 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003149
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003150 if (LastFieldBitfield) {
3151 // Last field was a bitfield. Must update skip info.
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003152 Expr *BitWidth = LastFieldBitfield->getBitWidth();
3153 uint64_t BitFieldSize =
Eli Friedman9a901bb2009-04-26 19:19:15 +00003154 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Daniel Dunbar487993b2009-05-03 13:32:01 +00003155 GC_IVAR skivar;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003156 skivar.ivar_bytepos = BytePos + LastBitfieldOffset;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003157 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3158 + ((BitFieldSize % ByteSizeInBits) != 0);
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003159 SkipIvars.push_back(skivar);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003160 }
3161
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003162 if (MaxField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003163 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003164 MaxUnionIvarSize));
3165 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003166 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003167 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003168}
3169
3170/// BuildIvarLayout - Builds ivar layout bitmap for the class
3171/// implementation for the __strong or __weak case.
3172/// The layout map displays which words in ivar list must be skipped
3173/// and which must be scanned by GC (see below). String is built of bytes.
3174/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3175/// of words to skip and right nibble is count of words to scan. So, each
3176/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3177/// represented by a 0x00 byte which also ends the string.
3178/// 1. when ForStrongLayout is true, following ivars are scanned:
3179/// - id, Class
3180/// - object *
3181/// - __strong anything
3182///
3183/// 2. When ForStrongLayout is false, following ivars are scanned:
3184/// - __weak anything
3185///
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003186llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003187 const ObjCImplementationDecl *OMD,
3188 bool ForStrongLayout) {
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003189 bool hasUnion = false;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003190
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003191 unsigned int WordsToScan, WordsToSkip;
Owen Anderson96e0fc72009-07-29 22:16:19 +00003192 const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003193 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
Owen Anderson69243822009-07-13 04:10:07 +00003194 return VMContext.getNullValue(PtrTy);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003195
Chris Lattnerf1690852009-03-31 08:48:01 +00003196 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003197 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003198 CGM.getContext().CollectObjCIvars(OI, RecFields);
Fariborz Jahanian98200742009-05-12 18:14:29 +00003199
Daniel Dunbar37153282009-05-04 04:10:48 +00003200 // Add this implementations synthesized ivars.
Fariborz Jahanian98200742009-05-12 18:14:29 +00003201 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
3202 CGM.getContext().CollectSynthesizedIvars(OI, Ivars);
3203 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3204 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3205
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003206 if (RecFields.empty())
Owen Anderson69243822009-07-13 04:10:07 +00003207 return VMContext.getNullValue(PtrTy);
Chris Lattnerf1690852009-03-31 08:48:01 +00003208
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003209 SkipIvars.clear();
3210 IvarsInfo.clear();
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00003211
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003212 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003213 if (IvarsInfo.empty())
Owen Anderson69243822009-07-13 04:10:07 +00003214 return VMContext.getNullValue(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003215
3216 // Sort on byte position in case we encounterred a union nested in
3217 // the ivar list.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003218 if (hasUnion && !IvarsInfo.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003219 std::sort(IvarsInfo.begin(), IvarsInfo.end());
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003220 if (hasUnion && !SkipIvars.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003221 std::sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003222
3223 // Build the string of skip/scan nibbles
Fariborz Jahanian8c2f2d12009-04-24 17:15:27 +00003224 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003225 unsigned int WordSize =
Duncan Sands9408c452009-05-09 07:08:47 +00003226 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003227 if (IvarsInfo[0].ivar_bytepos == 0) {
3228 WordsToSkip = 0;
3229 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003230 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003231 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3232 WordsToScan = IvarsInfo[0].ivar_size;
3233 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003234 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003235 unsigned int TailPrevGCObjC =
3236 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003237 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003238 // consecutive 'scanned' object pointers.
3239 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003240 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003241 // Skip over 'gc'able object pointer which lay over each other.
3242 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3243 continue;
3244 // Must skip over 1 or more words. We save current skip/scan values
3245 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003246 SKIP_SCAN SkScan;
3247 SkScan.skip = WordsToSkip;
3248 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003249 SkipScanIvars.push_back(SkScan);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003250
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003251 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003252 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3253 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003254 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003255 WordsToSkip = 0;
3256 WordsToScan = IvarsInfo[i].ivar_size;
3257 }
3258 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003259 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003260 SKIP_SCAN SkScan;
3261 SkScan.skip = WordsToSkip;
3262 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003263 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003264 }
3265
3266 bool BytesSkipped = false;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003267 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003268 unsigned int LastIndex = SkipIvars.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003269 int LastByteSkipped =
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003270 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
3271 LastIndex = IvarsInfo.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003272 int LastByteScanned =
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003273 IvarsInfo[LastIndex].ivar_bytepos +
3274 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003275 BytesSkipped = (LastByteSkipped > LastByteScanned);
3276 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003277 if (BytesSkipped) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003278 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003279 SKIP_SCAN SkScan;
3280 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3281 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003282 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003283 }
3284 }
3285 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3286 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003287 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003288 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003289 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3290 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3291 // 0xM0 followed by 0x0N detected.
3292 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3293 for (int j = i+1; j < SkipScan; j++)
3294 SkipScanIvars[j] = SkipScanIvars[j+1];
3295 --SkipScan;
3296 }
3297 }
3298
3299 // Generate the string.
3300 std::string BitMap;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003301 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003302 unsigned char byte;
3303 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3304 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3305 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3306 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
3307
3308 if (skip_small > 0 || skip_big > 0)
3309 BytesSkipped = true;
3310 // first skip big.
3311 for (unsigned int ix = 0; ix < skip_big; ix++)
3312 BitMap += (unsigned char)(0xf0);
3313
3314 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003315 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003316 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003317 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003318 byte |= 0xf;
3319 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003320 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003321 byte |= scan_small;
3322 scan_small = 0;
3323 }
3324 BitMap += byte;
3325 }
3326 // next scan big
3327 for (unsigned int ix = 0; ix < scan_big; ix++)
3328 BitMap += (unsigned char)(0x0f);
3329 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003330 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003331 byte = scan_small;
3332 BitMap += byte;
3333 }
3334 }
3335 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003336 unsigned char zero = 0;
3337 BitMap += zero;
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003338
3339 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
3340 printf("\n%s ivar layout for class '%s': ",
3341 ForStrongLayout ? "strong" : "weak",
3342 OMD->getClassInterface()->getNameAsCString());
3343 const unsigned char *s = (unsigned char*)BitMap.c_str();
3344 for (unsigned i = 0; i < BitMap.size(); i++)
3345 if (!(s[i] & 0xf0))
3346 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3347 else
3348 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3349 printf("\n");
3350 }
3351
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003352 // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as
3353 // final layout.
3354 if (ForStrongLayout && !BytesSkipped)
Owen Anderson69243822009-07-13 04:10:07 +00003355 return VMContext.getNullValue(PtrTy);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003356 llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Owen Anderson7db6d832009-07-28 18:33:04 +00003357 llvm::ConstantArray::get(BitMap.c_str()),
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003358 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003359 1, true);
Owen Andersona1cf15f2009-07-14 23:10:40 +00003360 return getConstantGEP(VMContext, Entry, 0, 0);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003361}
3362
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003363llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003364 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3365
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003366 // FIXME: Avoid std::string copying.
3367 if (!Entry)
3368 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson7db6d832009-07-28 18:33:04 +00003369 llvm::ConstantArray::get(Sel.getAsString()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003370 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003371 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003372
Owen Andersona1cf15f2009-07-14 23:10:40 +00003373 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003374}
3375
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003376// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003377llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003378 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3379}
3380
3381// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003382llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003383 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
3384}
3385
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003386llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003387 std::string TypeStr;
3388 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3389
3390 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003391
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003392 if (!Entry)
3393 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson7db6d832009-07-28 18:33:04 +00003394 llvm::ConstantArray::get(TypeStr),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003395 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003396 1, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003397
Owen Andersona1cf15f2009-07-14 23:10:40 +00003398 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003399}
3400
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003401llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003402 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003403 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3404 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00003405
3406 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3407
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003408 if (!Entry)
3409 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson7db6d832009-07-28 18:33:04 +00003410 llvm::ConstantArray::get(TypeStr),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003411 "__TEXT,__cstring,cstring_literals",
3412 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00003413
Owen Andersona1cf15f2009-07-14 23:10:40 +00003414 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003415}
3416
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003417// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003418llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003419 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
3420
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003421 if (!Entry)
3422 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Owen Anderson7db6d832009-07-28 18:33:04 +00003423 llvm::ConstantArray::get(Ident->getName()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003424 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003425 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003426
Owen Andersona1cf15f2009-07-14 23:10:40 +00003427 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003428}
3429
3430// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003431// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003432llvm::Constant *
3433 CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3434 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003435 std::string TypeStr;
3436 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003437 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3438}
3439
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003440void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
3441 const ObjCContainerDecl *CD,
3442 std::string &NameOut) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00003443 NameOut = '\01';
3444 NameOut += (D->isInstanceMethod() ? '-' : '+');
Chris Lattner077bf5e2008-11-24 03:33:13 +00003445 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003446 assert (CD && "Missing container decl in GetNameForMethod");
3447 NameOut += CD->getNameAsString();
Fariborz Jahanian1e9aef32009-04-16 18:34:20 +00003448 if (const ObjCCategoryImplDecl *CID =
3449 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) {
3450 NameOut += '(';
3451 NameOut += CID->getNameAsString();
3452 NameOut+= ')';
3453 }
Chris Lattner077bf5e2008-11-24 03:33:13 +00003454 NameOut += ' ';
3455 NameOut += D->getSelector().getAsString();
3456 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003457}
3458
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003459void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003460 EmitModuleInfo();
3461
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003462 // Emit the dummy bodies for any protocols which were referenced but
3463 // never defined.
3464 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00003465 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
3466 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003467 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003468
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003469 std::vector<llvm::Constant*> Values(5);
Owen Anderson69243822009-07-13 04:10:07 +00003470 Values[0] = VMContext.getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00003471 Values[1] = GetClassName(I->first);
Owen Anderson69243822009-07-13 04:10:07 +00003472 Values[2] = VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003473 Values[3] = Values[4] =
Owen Anderson69243822009-07-13 04:10:07 +00003474 VMContext.getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00003475 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00003476 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003477 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00003478 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003479 }
3480
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003481 // Add assembler directives to add lazy undefined symbol references
3482 // for classes which are referenced but not defined. This is
3483 // important for correct linker interaction.
3484
3485 // FIXME: Uh, this isn't particularly portable.
3486 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00003487
3488 if (!CGM.getModule().getModuleInlineAsm().empty())
3489 s << "\n";
3490
Chris Lattner41f55d32009-07-28 18:25:06 +00003491 // FIXME: This produces non-determinstic output.
Chris Lattnerad64e022009-07-17 23:57:13 +00003492 for (std::set<IdentifierInfo*>::iterator I = LazySymbols.begin(),
3493 e = LazySymbols.end(); I != e; ++I) {
3494 s << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003495 }
Chris Lattnerad64e022009-07-17 23:57:13 +00003496 for (std::set<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
3497 e = DefinedSymbols.end(); I != e; ++I) {
3498 s << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
3499 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003500 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00003501
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003502 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003503}
3504
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003505CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003506 : CGObjCCommonMac(cgm),
3507 ObjCTypes(cgm)
3508{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003509 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003510 ObjCABI = 2;
3511}
3512
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003513/* *** */
3514
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003515ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Owen Andersona1cf15f2009-07-14 23:10:40 +00003516: VMContext(cgm.getLLVMContext()), CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003517{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003518 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3519 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003520
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003521 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003522 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003523 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003524 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00003525 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003526
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003527 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00003528 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003529 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003530
Mike Stumpf5408fe2009-05-16 07:57:57 +00003531 // FIXME: It would be nice to unify this with the opaque type, so that the IR
3532 // comes out a bit cleaner.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003533 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00003534 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003535
3536 // I'm not sure I like this. The implicit coordination is a bit
3537 // gross. We should solve this in a reasonable fashion because this
3538 // is a pretty common task (match some runtime data structure with
3539 // an LLVM data structure).
3540
3541 // FIXME: This is leaked.
3542 // FIXME: Merge with rewriter code?
3543
3544 // struct _objc_super {
3545 // id self;
3546 // Class cls;
3547 // }
3548 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3549 SourceLocation(),
3550 &Ctx.Idents.get("_objc_super"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003551 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Douglas Gregor6ab35242009-04-09 21:40:53 +00003552 Ctx.getObjCIdType(), 0, false));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003553 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Douglas Gregor6ab35242009-04-09 21:40:53 +00003554 Ctx.getObjCClassType(), 0, false));
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003555 RD->completeDefinition(Ctx);
3556
3557 SuperCTy = Ctx.getTagDeclType(RD);
3558 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
3559
3560 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Owen Anderson96e0fc72009-07-29 22:16:19 +00003561 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003562
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003563 // struct _prop_t {
3564 // char *name;
3565 // char *attributes;
3566 // }
Owen Anderson96e0fc72009-07-29 22:16:19 +00003567 PropertyTy = llvm::StructType::get(Int8PtrTy, Int8PtrTy, NULL);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003568 CGM.getModule().addTypeName("struct._prop_t",
3569 PropertyTy);
3570
3571 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003572 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003573 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003574 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003575 // }
Owen Anderson96e0fc72009-07-29 22:16:19 +00003576 PropertyListTy = llvm::StructType::get(IntTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003577 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00003578 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003579 NULL);
3580 CGM.getModule().addTypeName("struct._prop_list_t",
3581 PropertyListTy);
3582 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00003583 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003584
3585 // struct _objc_method {
3586 // SEL _cmd;
3587 // char *method_type;
3588 // char *_imp;
3589 // }
Owen Anderson96e0fc72009-07-29 22:16:19 +00003590 MethodTy = llvm::StructType::get(SelectorPtrTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003591 Int8PtrTy,
3592 Int8PtrTy,
3593 NULL);
3594 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003595
3596 // struct _objc_cache *
Owen Anderson96e0fc72009-07-29 22:16:19 +00003597 CacheTy = llvm::OpaqueType::get();
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003598 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00003599 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003600}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003601
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003602ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
3603 : ObjCCommonTypesHelper(cgm)
3604{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003605 // struct _objc_method_description {
3606 // SEL name;
3607 // char *types;
3608 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003609 MethodDescriptionTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003610 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003611 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003612 NULL);
3613 CGM.getModule().addTypeName("struct._objc_method_description",
3614 MethodDescriptionTy);
3615
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003616 // struct _objc_method_description_list {
3617 // int count;
3618 // struct _objc_method_description[1];
3619 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003620 MethodDescriptionListTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003621 llvm::StructType::get(IntTy,
3622 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003623 NULL);
3624 CGM.getModule().addTypeName("struct._objc_method_description_list",
3625 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003626
3627 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003628 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003629 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003630
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003631 // Protocol description structures
3632
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003633 // struct _objc_protocol_extension {
3634 // uint32_t size; // sizeof(struct _objc_protocol_extension)
3635 // struct _objc_method_description_list *optional_instance_methods;
3636 // struct _objc_method_description_list *optional_class_methods;
3637 // struct _objc_property_list *instance_properties;
3638 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003639 ProtocolExtensionTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003640 llvm::StructType::get(IntTy,
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003641 MethodDescriptionListPtrTy,
3642 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003643 PropertyListPtrTy,
3644 NULL);
3645 CGM.getModule().addTypeName("struct._objc_protocol_extension",
3646 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003647
3648 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00003649 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003650
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003651 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003652
Owen Anderson96e0fc72009-07-29 22:16:19 +00003653 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
3654 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003655
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003656 const llvm::Type *T =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003657 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003658 LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00003659 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003660 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003661 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
3662
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003663 // struct _objc_protocol {
3664 // struct _objc_protocol_extension *isa;
3665 // char *protocol_name;
3666 // struct _objc_protocol **_objc_protocol_list;
3667 // struct _objc_method_description_list *instance_methods;
3668 // struct _objc_method_description_list *class_methods;
3669 // }
Owen Anderson96e0fc72009-07-29 22:16:19 +00003670 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003671 Int8PtrTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00003672 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003673 MethodDescriptionListPtrTy,
3674 MethodDescriptionListPtrTy,
3675 NULL);
3676 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
3677
3678 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
3679 CGM.getModule().addTypeName("struct._objc_protocol_list",
3680 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003681 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00003682 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003683
3684 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003685 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00003686 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003687
3688 // Class description structures
3689
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003690 // struct _objc_ivar {
3691 // char *ivar_name;
3692 // char *ivar_type;
3693 // int ivar_offset;
3694 // }
Owen Anderson96e0fc72009-07-29 22:16:19 +00003695 IvarTy = llvm::StructType::get(Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003696 Int8PtrTy,
3697 IntTy,
3698 NULL);
3699 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
3700
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003701 // struct _objc_ivar_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00003702 IvarListTy = llvm::OpaqueType::get();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003703 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00003704 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003705
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003706 // struct _objc_method_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00003707 MethodListTy = llvm::OpaqueType::get();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003708 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00003709 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003710
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003711 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003712 ClassExtensionTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003713 llvm::StructType::get(IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003714 Int8PtrTy,
3715 PropertyListPtrTy,
3716 NULL);
3717 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00003718 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003719
Owen Anderson96e0fc72009-07-29 22:16:19 +00003720 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003721
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003722 // struct _objc_class {
3723 // Class isa;
3724 // Class super_class;
3725 // char *name;
3726 // long version;
3727 // long info;
3728 // long instance_size;
3729 // struct _objc_ivar_list *ivars;
3730 // struct _objc_method_list *methods;
3731 // struct _objc_cache *cache;
3732 // struct _objc_protocol_list *protocols;
3733 // char *ivar_layout;
3734 // struct _objc_class_ext *ext;
3735 // };
Owen Anderson96e0fc72009-07-29 22:16:19 +00003736 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3737 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003738 Int8PtrTy,
3739 LongTy,
3740 LongTy,
3741 LongTy,
3742 IvarListPtrTy,
3743 MethodListPtrTy,
3744 CachePtrTy,
3745 ProtocolListPtrTy,
3746 Int8PtrTy,
3747 ClassExtensionPtrTy,
3748 NULL);
3749 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
3750
3751 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
3752 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00003753 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003754
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003755 // struct _objc_category {
3756 // char *category_name;
3757 // char *class_name;
3758 // struct _objc_method_list *instance_method;
3759 // struct _objc_method_list *class_method;
3760 // uint32_t size; // sizeof(struct _objc_category)
3761 // struct _objc_property_list *instance_properties;// category's @property
3762 // }
Owen Anderson96e0fc72009-07-29 22:16:19 +00003763 CategoryTy = llvm::StructType::get(Int8PtrTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003764 Int8PtrTy,
3765 MethodListPtrTy,
3766 MethodListPtrTy,
3767 ProtocolListPtrTy,
3768 IntTy,
3769 PropertyListPtrTy,
3770 NULL);
3771 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
3772
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003773 // Global metadata structures
3774
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003775 // struct _objc_symtab {
3776 // long sel_ref_cnt;
3777 // SEL *refs;
3778 // short cls_def_cnt;
3779 // short cat_def_cnt;
3780 // char *defs[cls_def_cnt + cat_def_cnt];
3781 // }
Owen Anderson96e0fc72009-07-29 22:16:19 +00003782 SymtabTy = llvm::StructType::get(LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003783 SelectorPtrTy,
3784 ShortTy,
3785 ShortTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00003786 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003787 NULL);
3788 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00003789 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003790
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003791 // struct _objc_module {
3792 // long version;
3793 // long size; // sizeof(struct _objc_module)
3794 // char *name;
3795 // struct _objc_symtab* symtab;
3796 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003797 ModuleTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003798 llvm::StructType::get(LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003799 LongTy,
3800 Int8PtrTy,
3801 SymtabPtrTy,
3802 NULL);
3803 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003804
Anders Carlsson2abd89c2008-08-31 04:05:03 +00003805
Mike Stumpf5408fe2009-05-16 07:57:57 +00003806 // FIXME: This is the size of the setjmp buffer and should be target
3807 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00003808 uint64_t SetJmpBufferSize = 18;
3809
3810 // Exceptions
Owen Anderson96e0fc72009-07-29 22:16:19 +00003811 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
3812 llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00003813
3814 ExceptionDataTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003815 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
Anders Carlsson124526b2008-09-09 10:10:21 +00003816 SetJmpBufferSize),
3817 StackPtrTy, NULL);
3818 CGM.getModule().addTypeName("struct._objc_exception_data",
3819 ExceptionDataTy);
3820
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003821}
3822
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003823ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003824: ObjCCommonTypesHelper(cgm)
3825{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003826 // struct _method_list_t {
3827 // uint32_t entsize; // sizeof(struct _objc_method)
3828 // uint32_t method_count;
3829 // struct _objc_method method_list[method_count];
3830 // }
Owen Anderson96e0fc72009-07-29 22:16:19 +00003831 MethodListnfABITy = llvm::StructType::get(IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003832 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00003833 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003834 NULL);
3835 CGM.getModule().addTypeName("struct.__method_list_t",
3836 MethodListnfABITy);
3837 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00003838 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003839
3840 // struct _protocol_t {
3841 // id isa; // NULL
3842 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003843 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003844 // const struct method_list_t * const instance_methods;
3845 // const struct method_list_t * const class_methods;
3846 // const struct method_list_t *optionalInstanceMethods;
3847 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003848 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003849 // const uint32_t size; // sizeof(struct _protocol_t)
3850 // const uint32_t flags; // = 0
3851 // }
3852
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003853 // Holder for struct _protocol_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00003854 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003855
Owen Anderson96e0fc72009-07-29 22:16:19 +00003856 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003857 Int8PtrTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00003858 llvm::PointerType::getUnqual(
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003859 ProtocolListTyHolder),
3860 MethodListnfABIPtrTy,
3861 MethodListnfABIPtrTy,
3862 MethodListnfABIPtrTy,
3863 MethodListnfABIPtrTy,
3864 PropertyListPtrTy,
3865 IntTy,
3866 IntTy,
3867 NULL);
3868 CGM.getModule().addTypeName("struct._protocol_t",
3869 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003870
3871 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00003872 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003873
Fariborz Jahanianda320092009-01-29 19:24:30 +00003874 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003875 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00003876 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003877 // }
Owen Anderson96e0fc72009-07-29 22:16:19 +00003878 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3879 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003880 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003881 NULL);
3882 CGM.getModule().addTypeName("struct._objc_protocol_list",
3883 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003884 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3885 ProtocolListnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003886
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003887 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00003888 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003889
3890 // struct _ivar_t {
3891 // unsigned long int *offset; // pointer to ivar offset location
3892 // char *name;
3893 // char *type;
3894 // uint32_t alignment;
3895 // uint32_t size;
3896 // }
Owen Anderson96e0fc72009-07-29 22:16:19 +00003897 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003898 Int8PtrTy,
3899 Int8PtrTy,
3900 IntTy,
3901 IntTy,
3902 NULL);
3903 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3904
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003905 // struct _ivar_list_t {
3906 // uint32 entsize; // sizeof(struct _ivar_t)
3907 // uint32 count;
3908 // struct _iver_t list[count];
3909 // }
Owen Anderson96e0fc72009-07-29 22:16:19 +00003910 IvarListnfABITy = llvm::StructType::get(IntTy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003911 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00003912 llvm::ArrayType::get(
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003913 IvarnfABITy, 0),
3914 NULL);
3915 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3916
Owen Anderson96e0fc72009-07-29 22:16:19 +00003917 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003918
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003919 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003920 // uint32_t const flags;
3921 // uint32_t const instanceStart;
3922 // uint32_t const instanceSize;
3923 // uint32_t const reserved; // only when building for 64bit targets
3924 // const uint8_t * const ivarLayout;
3925 // const char *const name;
3926 // const struct _method_list_t * const baseMethods;
3927 // const struct _objc_protocol_list *const baseProtocols;
3928 // const struct _ivar_list_t *const ivars;
3929 // const uint8_t * const weakIvarLayout;
3930 // const struct _prop_list_t * const properties;
3931 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003932
3933 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson96e0fc72009-07-29 22:16:19 +00003934 ClassRonfABITy = llvm::StructType::get(IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003935 IntTy,
3936 IntTy,
3937 Int8PtrTy,
3938 Int8PtrTy,
3939 MethodListnfABIPtrTy,
3940 ProtocolListnfABIPtrTy,
3941 IvarListnfABIPtrTy,
3942 Int8PtrTy,
3943 PropertyListPtrTy,
3944 NULL);
3945 CGM.getModule().addTypeName("struct._class_ro_t",
3946 ClassRonfABITy);
3947
3948 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3949 std::vector<const llvm::Type*> Params;
3950 Params.push_back(ObjectPtrTy);
3951 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00003952 ImpnfABITy = llvm::PointerType::getUnqual(
3953 llvm::FunctionType::get(ObjectPtrTy, Params, false));
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003954
3955 // struct _class_t {
3956 // struct _class_t *isa;
3957 // struct _class_t * const superclass;
3958 // void *cache;
3959 // IMP *vtable;
3960 // struct class_ro_t *ro;
3961 // }
3962
Owen Anderson96e0fc72009-07-29 22:16:19 +00003963 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
Owen Andersona1cf15f2009-07-14 23:10:40 +00003964 ClassnfABITy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003965 llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3966 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Andersona1cf15f2009-07-14 23:10:40 +00003967 CachePtrTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00003968 llvm::PointerType::getUnqual(ImpnfABITy),
3969 llvm::PointerType::getUnqual(ClassRonfABITy),
Owen Andersona1cf15f2009-07-14 23:10:40 +00003970 NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003971 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3972
3973 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3974 ClassnfABITy);
3975
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003976 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00003977 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003978
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003979 // struct _category_t {
3980 // const char * const name;
3981 // struct _class_t *const cls;
3982 // const struct _method_list_t * const instance_methods;
3983 // const struct _method_list_t * const class_methods;
3984 // const struct _protocol_list_t * const protocols;
3985 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003986 // }
Owen Anderson96e0fc72009-07-29 22:16:19 +00003987 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003988 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003989 MethodListnfABIPtrTy,
3990 MethodListnfABIPtrTy,
3991 ProtocolListnfABIPtrTy,
3992 PropertyListPtrTy,
3993 NULL);
3994 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003995
3996 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003997 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3998 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003999
4000 // MessageRefTy - LLVM for:
4001 // struct _message_ref_t {
4002 // IMP messenger;
4003 // SEL name;
4004 // };
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004005
4006 // First the clang type for struct _message_ref_t
4007 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
4008 SourceLocation(),
4009 &Ctx.Idents.get("_message_ref_t"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004010 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Douglas Gregor6ab35242009-04-09 21:40:53 +00004011 Ctx.VoidPtrTy, 0, false));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004012 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Douglas Gregor6ab35242009-04-09 21:40:53 +00004013 Ctx.getObjCSelType(), 0, false));
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004014 RD->completeDefinition(Ctx);
4015
4016 MessageRefCTy = Ctx.getTagDeclType(RD);
4017 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4018 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004019
4020 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004021 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004022
4023 // SuperMessageRefTy - LLVM for:
4024 // struct _super_message_ref_t {
4025 // SUPER_IMP messenger;
4026 // SEL name;
4027 // };
Owen Anderson96e0fc72009-07-29 22:16:19 +00004028 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004029 SelectorPtrTy,
4030 NULL);
4031 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
4032
4033 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004034 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004035
Daniel Dunbare588b992009-03-01 04:46:24 +00004036
4037 // struct objc_typeinfo {
4038 // const void** vtable; // objc_ehtype_vtable + 2
4039 // const char* name; // c++ typeinfo string
4040 // Class cls;
4041 // };
Owen Anderson96e0fc72009-07-29 22:16:19 +00004042 EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbare588b992009-03-01 04:46:24 +00004043 Int8PtrTy,
4044 ClassnfABIPtrTy,
4045 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004046 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004047 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004048}
4049
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004050llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
4051 FinishNonFragileABIModule();
4052
4053 return NULL;
4054}
4055
Daniel Dunbar463b8762009-05-15 21:48:48 +00004056void CGObjCNonFragileABIMac::AddModuleClassList(const
4057 std::vector<llvm::GlobalValue*>
4058 &Container,
4059 const char *SymbolName,
4060 const char *SectionName) {
4061 unsigned NumClasses = Container.size();
4062
4063 if (!NumClasses)
4064 return;
4065
4066 std::vector<llvm::Constant*> Symbols(NumClasses);
4067 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004068 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004069 ObjCTypes.Int8PtrTy);
4070 llvm::Constant* Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004071 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004072 NumClasses),
4073 Symbols);
4074
4075 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004076 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004077 llvm::GlobalValue::InternalLinkage,
4078 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004079 SymbolName);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004080 GV->setAlignment(8);
4081 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004082 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004083}
4084
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004085void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4086 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004087
Daniel Dunbar463b8762009-05-15 21:48:48 +00004088 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004089 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar463b8762009-05-15 21:48:48 +00004090 AddModuleClassList(DefinedClasses,
4091 "\01L_OBJC_LABEL_CLASS_$",
4092 "__DATA, __objc_classlist, regular, no_dead_strip");
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004093 AddModuleClassList(DefinedNonLazyClasses,
4094 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4095 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004096
4097 // Build list of all implemented category addresses in array
4098 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar463b8762009-05-15 21:48:48 +00004099 AddModuleClassList(DefinedCategories,
4100 "\01L_OBJC_LABEL_CATEGORY_$",
4101 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004102 AddModuleClassList(DefinedNonLazyCategories,
4103 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4104 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004105
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004106 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
4107 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
4108 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004109 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
Fariborz Jahanian067986e2009-02-24 21:08:09 +00004110 unsigned int flags = 0;
Fariborz Jahanian66a5c2c2009-02-24 23:34:44 +00004111 // FIXME: Fix and continue?
4112 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
4113 flags |= eImageInfo_GarbageCollected;
4114 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
4115 flags |= eImageInfo_GCOnly;
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004116 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
Owen Anderson7db6d832009-07-28 18:33:04 +00004117 llvm::Constant* Init = llvm::ConstantArray::get(
Owen Anderson96e0fc72009-07-29 22:16:19 +00004118 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004119 Values);
4120 llvm::GlobalVariable *IMGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004121 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004122 llvm::GlobalValue::InternalLinkage,
4123 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004124 "\01L_OBJC_IMAGE_INFO");
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004125 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
Daniel Dunbar325f7582009-04-23 08:03:21 +00004126 IMGV->setConstant(true);
Chris Lattnerad64e022009-07-17 23:57:13 +00004127 CGM.AddUsedGlobal(IMGV);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004128}
4129
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004130/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004131/// NonLegacyDispatchMethods; false otherwise. What this means is that
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004132/// except for the 19 selectors in the list, we generate 32bit-style
4133/// message dispatch call for all the rest.
4134///
4135bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004136 if (NonLegacyDispatchMethods.empty()) {
4137 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4138 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4139 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4140 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4141 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4142 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4143 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4144 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4145 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4146 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
4147
4148 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4149 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4150 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4151 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4152 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4153 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4154 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4155 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004156 // "countByEnumeratingWithState:objects:count"
4157 IdentifierInfo *KeyIdents[] = {
4158 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4159 &CGM.getContext().Idents.get("objects"),
4160 &CGM.getContext().Idents.get("count")
4161 };
4162 NonLegacyDispatchMethods.insert(
4163 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004164 }
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004165 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004166}
4167
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004168// Metadata flags
4169enum MetaDataDlags {
4170 CLS = 0x0,
4171 CLS_META = 0x1,
4172 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004173 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004174 CLS_EXCEPTION = 0x20
4175};
4176/// BuildClassRoTInitializer - generate meta-data for:
4177/// struct _class_ro_t {
4178/// uint32_t const flags;
4179/// uint32_t const instanceStart;
4180/// uint32_t const instanceSize;
4181/// uint32_t const reserved; // only when building for 64bit targets
4182/// const uint8_t * const ivarLayout;
4183/// const char *const name;
4184/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004185/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004186/// const struct _ivar_list_t *const ivars;
4187/// const uint8_t * const weakIvarLayout;
4188/// const struct _prop_list_t * const properties;
4189/// }
4190///
4191llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
4192 unsigned flags,
4193 unsigned InstanceStart,
4194 unsigned InstanceSize,
4195 const ObjCImplementationDecl *ID) {
4196 std::string ClassName = ID->getNameAsString();
4197 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004198 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4199 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4200 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004201 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004202 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4203 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004204 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004205 // const struct _method_list_t * const baseMethods;
4206 std::vector<llvm::Constant*> Methods;
4207 std::string MethodListName("\01l_OBJC_$_");
4208 if (flags & CLS_META) {
4209 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004210 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004211 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004212 // Class methods should always be defined.
4213 Methods.push_back(GetMethodConstant(*i));
4214 }
4215 } else {
4216 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004217 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004218 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004219 // Instance methods should always be defined.
4220 Methods.push_back(GetMethodConstant(*i));
4221 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00004222 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004223 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004224 ObjCPropertyImplDecl *PID = *i;
4225
4226 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4227 ObjCPropertyDecl *PD = PID->getPropertyDecl();
4228
4229 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4230 if (llvm::Constant *C = GetMethodConstant(MD))
4231 Methods.push_back(C);
4232 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4233 if (llvm::Constant *C = GetMethodConstant(MD))
4234 Methods.push_back(C);
4235 }
4236 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004237 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004238 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004239 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004240
4241 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4242 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
4243 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
4244 + OID->getNameAsString(),
4245 OID->protocol_begin(),
4246 OID->protocol_end());
4247
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004248 if (flags & CLS_META)
Owen Anderson69243822009-07-13 04:10:07 +00004249 Values[ 7] = VMContext.getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004250 else
4251 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004252 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4253 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004254 if (flags & CLS_META)
Owen Anderson69243822009-07-13 04:10:07 +00004255 Values[ 9] = VMContext.getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004256 else
4257 Values[ 9] =
Chris Lattner41f55d32009-07-28 18:25:06 +00004258 EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004259 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00004260 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004261 Values);
4262 llvm::GlobalVariable *CLASS_RO_GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004263 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004264 llvm::GlobalValue::InternalLinkage,
4265 Init,
4266 (flags & CLS_META) ?
4267 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
Owen Anderson1c431b32009-07-08 19:05:04 +00004268 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004269 CLASS_RO_GV->setAlignment(
4270 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004271 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004272 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004273
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004274}
4275
4276/// BuildClassMetaData - This routine defines that to-level meta-data
4277/// for the given ClassName for:
4278/// struct _class_t {
4279/// struct _class_t *isa;
4280/// struct _class_t * const superclass;
4281/// void *cache;
4282/// IMP *vtable;
4283/// struct class_ro_t *ro;
4284/// }
4285///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004286llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
4287 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004288 llvm::Constant *IsAGV,
4289 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004290 llvm::Constant *ClassRoGV,
4291 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004292 std::vector<llvm::Constant*> Values(5);
4293 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004294 Values[1] = SuperClassGV
4295 ? SuperClassGV
Owen Anderson69243822009-07-13 04:10:07 +00004296 : VMContext.getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004297 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4298 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4299 Values[4] = ClassRoGV; // &CLASS_RO_GV
Owen Anderson08e25242009-07-27 22:29:56 +00004300 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004301 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004302 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4303 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004304 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004305 GV->setAlignment(
4306 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004307 if (HiddenVisibility)
4308 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004309 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004310}
4311
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004312bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00004313CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004314 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004315}
4316
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004317void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004318 uint32_t &InstanceStart,
4319 uint32_t &InstanceSize) {
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004320 const ASTRecordLayout &RL =
4321 CGM.getContext().getASTObjCImplementationLayout(OID);
4322
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004323 // InstanceSize is really instance end.
Anders Carlsson243a6852009-07-18 21:26:44 +00004324 InstanceSize = llvm::RoundUpToAlignment(RL.getDataSize(), 8) / 8;
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004325
4326 // If there are no fields, the start is the same as the end.
4327 if (!RL.getFieldCount())
4328 InstanceStart = InstanceSize;
4329 else
4330 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004331}
4332
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004333void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4334 std::string ClassName = ID->getNameAsString();
4335 if (!ObjCEmptyCacheVar) {
4336 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Owen Anderson1c431b32009-07-08 19:05:04 +00004337 CGM.getModule(),
Daniel Dunbar948e2582009-02-15 07:36:20 +00004338 ObjCTypes.CacheTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004339 false,
4340 llvm::GlobalValue::ExternalLinkage,
4341 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00004342 "_objc_empty_cache");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004343
4344 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Owen Anderson1c431b32009-07-08 19:05:04 +00004345 CGM.getModule(),
Daniel Dunbar948e2582009-02-15 07:36:20 +00004346 ObjCTypes.ImpnfABITy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004347 false,
4348 llvm::GlobalValue::ExternalLinkage,
4349 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00004350 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004351 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004352 assert(ID->getClassInterface() &&
4353 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004354 // FIXME: Is this correct (that meta class size is never computed)?
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004355 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00004356 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004357 uint32_t InstanceSize = InstanceStart;
4358 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004359 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4360 std::string ObjCClassName(getClassSymbolPrefix());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004361
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004362 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004363
Daniel Dunbar04d40782009-04-14 06:00:08 +00004364 bool classIsHidden =
4365 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004366 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004367 flags |= OBJC2_CLS_HIDDEN;
4368 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004369 // class is root
4370 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004371 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004372 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004373 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004374 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004375 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4376 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4377 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004378 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004379 // work on super class metadata symbol.
4380 std::string SuperClassName =
4381 ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004382 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004383 }
4384 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4385 InstanceStart,
4386 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004387 std::string TClassName = ObjCMetaClassName + ClassName;
4388 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004389 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4390 classIsHidden);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004391
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004392 // Metadata for the class
4393 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004394 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004395 flags |= OBJC2_CLS_HIDDEN;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004396
Douglas Gregor68584ed2009-06-18 16:11:24 +00004397 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004398 flags |= CLS_EXCEPTION;
4399
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004400 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004401 flags |= CLS_ROOT;
4402 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00004403 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004404 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004405 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004406 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004407 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004408 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004409 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004410 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004411 InstanceStart,
4412 InstanceSize,
4413 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004414
4415 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004416 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004417 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4418 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004419 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004420
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004421 // Determine if this class is also "non-lazy".
4422 if (ImplementationIsNonLazy(ID))
4423 DefinedNonLazyClasses.push_back(ClassMD);
4424
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004425 // Force the definition of the EHType if necessary.
4426 if (flags & CLS_EXCEPTION)
4427 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004428}
4429
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004430/// GenerateProtocolRef - This routine is called to generate code for
4431/// a protocol reference expression; as in:
4432/// @code
4433/// @protocol(Proto1);
4434/// @endcode
4435/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4436/// which will hold address of the protocol meta-data.
4437///
4438llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
4439 const ObjCProtocolDecl *PD) {
4440
Fariborz Jahanian960cd062009-04-10 18:47:34 +00004441 // This routine is called for @protocol only. So, we must build definition
4442 // of protocol's meta-data (not a reference to it!)
4443 //
Owen Andersona1cf15f2009-07-14 23:10:40 +00004444 llvm::Constant *Init =
Owen Anderson3c4972d2009-07-29 18:54:39 +00004445 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004446 ObjCTypes.ExternalProtocolPtrTy);
4447
4448 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
4449 ProtocolName += PD->getNameAsCString();
4450
4451 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
4452 if (PTGV)
4453 return Builder.CreateLoad(PTGV, false, "tmp");
4454 PTGV = new llvm::GlobalVariable(
Owen Anderson1c431b32009-07-08 19:05:04 +00004455 CGM.getModule(),
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004456 Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00004457 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004458 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004459 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004460 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
4461 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00004462 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004463 return Builder.CreateLoad(PTGV, false, "tmp");
4464}
4465
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004466/// GenerateCategory - Build metadata for a category implementation.
4467/// struct _category_t {
4468/// const char * const name;
4469/// struct _class_t *const cls;
4470/// const struct _method_list_t * const instance_methods;
4471/// const struct _method_list_t * const class_methods;
4472/// const struct _protocol_list_t * const protocols;
4473/// const struct _prop_list_t * const properties;
4474/// }
4475///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004476void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004477 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004478 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
4479 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004480 "_$_" + OCD->getNameAsString());
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004481 std::string ExtClassName(getClassSymbolPrefix() +
4482 Interface->getNameAsString());
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004483
4484 std::vector<llvm::Constant*> Values(6);
4485 Values[0] = GetClassName(OCD->getIdentifier());
4486 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004487 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004488 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004489 std::vector<llvm::Constant*> Methods;
4490 std::string MethodListName(Prefix);
4491 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
4492 "_$_" + OCD->getNameAsString();
4493
Douglas Gregor653f1b12009-04-23 01:02:12 +00004494 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004495 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004496 // Instance methods should always be defined.
4497 Methods.push_back(GetMethodConstant(*i));
4498 }
4499
4500 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004501 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004502 Methods);
4503
4504 MethodListName = Prefix;
4505 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
4506 OCD->getNameAsString();
4507 Methods.clear();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004508 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004509 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004510 // Class methods should always be defined.
4511 Methods.push_back(GetMethodConstant(*i));
4512 }
4513
4514 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004515 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004516 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004517 const ObjCCategoryDecl *Category =
4518 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004519 if (Category) {
4520 std::string ExtName(Interface->getNameAsString() + "_$_" +
4521 OCD->getNameAsString());
4522 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
4523 + Interface->getNameAsString() + "_$_"
4524 + Category->getNameAsString(),
4525 Category->protocol_begin(),
4526 Category->protocol_end());
4527 Values[5] =
4528 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
4529 OCD, Category, ObjCTypes);
4530 }
4531 else {
Owen Anderson69243822009-07-13 04:10:07 +00004532 Values[4] = VMContext.getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4533 Values[5] = VMContext.getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004534 }
4535
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004536 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00004537 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004538 Values);
4539 llvm::GlobalVariable *GCATV
Owen Anderson1c431b32009-07-08 19:05:04 +00004540 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004541 false,
4542 llvm::GlobalValue::InternalLinkage,
4543 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004544 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004545 GCATV->setAlignment(
4546 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004547 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00004548 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004549 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004550
4551 // Determine if this category is also "non-lazy".
4552 if (ImplementationIsNonLazy(OCD))
4553 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004554}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004555
4556/// GetMethodConstant - Return a struct objc_method constant for the
4557/// given method if it has been defined. The result is null if the
4558/// method has not been defined. The return value has type MethodPtrTy.
4559llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
4560 const ObjCMethodDecl *MD) {
4561 // FIXME: Use DenseMap::lookup
4562 llvm::Function *Fn = MethodDefinitions[MD];
4563 if (!Fn)
4564 return 0;
4565
4566 std::vector<llvm::Constant*> Method(3);
4567 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00004568 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Owen Andersona1cf15f2009-07-14 23:10:40 +00004569 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004570 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00004571 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00004572 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004573}
4574
4575/// EmitMethodList - Build meta-data for method declarations
4576/// struct _method_list_t {
4577/// uint32_t entsize; // sizeof(struct _objc_method)
4578/// uint32_t method_count;
4579/// struct _objc_method method_list[method_count];
4580/// }
4581///
4582llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
4583 const std::string &Name,
4584 const char *Section,
4585 const ConstantVector &Methods) {
4586 // Return null for empty list.
4587 if (Methods.empty())
Owen Anderson69243822009-07-13 04:10:07 +00004588 return VMContext.getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004589
4590 std::vector<llvm::Constant*> Values(3);
4591 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00004592 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004593 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004594 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004595 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004596 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004597 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00004598 Values[2] = llvm::ConstantArray::get(AT, Methods);
Owen Anderson08e25242009-07-27 22:29:56 +00004599 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004600
4601 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004602 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004603 llvm::GlobalValue::InternalLinkage,
4604 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004605 Name);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004606 GV->setAlignment(
4607 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004608 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00004609 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00004610 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004611 ObjCTypes.MethodListnfABIPtrTy);
4612}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004613
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004614/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
4615/// the given ivar.
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004616llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004617 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004618 const ObjCIvarDecl *Ivar) {
Daniel Dunbara81419d2009-05-05 00:36:57 +00004619 // FIXME: We shouldn't need to do this lookup.
4620 unsigned Index;
4621 const ObjCInterfaceDecl *Container =
4622 FindIvarInterface(CGM.getContext(), ID, Ivar, Index);
4623 assert(Container && "Unable to find ivar container!");
4624 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00004625 '.' + Ivar->getNameAsString();
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004626 llvm::GlobalVariable *IvarOffsetGV =
4627 CGM.getModule().getGlobalVariable(Name);
4628 if (!IvarOffsetGV)
4629 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004630 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004631 false,
4632 llvm::GlobalValue::ExternalLinkage,
4633 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00004634 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004635 return IvarOffsetGV;
4636}
4637
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004638llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004639 const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004640 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004641 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00004642 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004643 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00004644 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004645 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004646 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00004647
Mike Stumpf5408fe2009-05-16 07:57:57 +00004648 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
4649 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00004650 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
4651 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
4652 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004653 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00004654 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00004655 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004656 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004657 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004658}
4659
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004660/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00004661/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004662/// IvarListnfABIPtrTy.
4663/// struct _ivar_t {
4664/// unsigned long int *offset; // pointer to ivar offset location
4665/// char *name;
4666/// char *type;
4667/// uint32_t alignment;
4668/// uint32_t size;
4669/// }
4670/// struct _ivar_list_t {
4671/// uint32 entsize; // sizeof(struct _ivar_t)
4672/// uint32 count;
4673/// struct _iver_t list[count];
4674/// }
4675///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004676
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004677llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
4678 const ObjCImplementationDecl *ID) {
4679
4680 std::vector<llvm::Constant*> Ivars, Ivar(5);
4681
4682 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4683 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
4684
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004685 // FIXME. Consolidate this with similar code in GenerateClass.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00004686
Daniel Dunbar91636d62009-04-20 00:33:43 +00004687 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00004688 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00004689 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Fariborz Jahanian99eee362009-04-01 19:37:34 +00004690
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004691 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
4692 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00004693 // Ignore unnamed bit-fields.
4694 if (!IVD->getDeclName())
4695 continue;
Daniel Dunbar3eec8aa2009-04-20 05:53:40 +00004696 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004697 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004698 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
4699 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004700 const llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004701 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00004702 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004703 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004704 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004705 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004706 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00004707 // NOTE. Size of a bitfield does not match gcc's, because of the
4708 // way bitfields are treated special in each. But I am told that
4709 // 'size' for bitfield ivars is ignored by the runtime so it does
4710 // not matter. If it matters, there is enough info to get the
4711 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004712 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00004713 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004714 }
4715 // Return null for empty list.
4716 if (Ivars.empty())
Owen Anderson69243822009-07-13 04:10:07 +00004717 return VMContext.getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004718 std::vector<llvm::Constant*> Values(3);
Duncan Sands9408c452009-05-09 07:08:47 +00004719 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004720 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4721 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004722 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004723 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00004724 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Owen Anderson08e25242009-07-27 22:29:56 +00004725 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004726 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4727 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004728 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004729 llvm::GlobalValue::InternalLinkage,
4730 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004731 Prefix + OID->getNameAsString());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004732 GV->setAlignment(
4733 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004734 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004735
Chris Lattnerad64e022009-07-17 23:57:13 +00004736 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00004737 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004738}
4739
4740llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4741 const ObjCProtocolDecl *PD) {
4742 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4743
4744 if (!Entry) {
4745 // We use the initializer as a marker of whether this is a forward
4746 // reference or not. At module finalization we add the empty
4747 // contents for protocols which were referenced but never defined.
4748 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00004749 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004750 llvm::GlobalValue::ExternalLinkage,
4751 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00004752 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString());
Fariborz Jahanianda320092009-01-29 19:24:30 +00004753 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004754 }
4755
4756 return Entry;
4757}
4758
4759/// GetOrEmitProtocol - Generate the protocol meta-data:
4760/// @code
4761/// struct _protocol_t {
4762/// id isa; // NULL
4763/// const char * const protocol_name;
4764/// const struct _protocol_list_t * protocol_list; // super protocols
4765/// const struct method_list_t * const instance_methods;
4766/// const struct method_list_t * const class_methods;
4767/// const struct method_list_t *optionalInstanceMethods;
4768/// const struct method_list_t *optionalClassMethods;
4769/// const struct _prop_list_t * properties;
4770/// const uint32_t size; // sizeof(struct _protocol_t)
4771/// const uint32_t flags; // = 0
4772/// }
4773/// @endcode
4774///
4775
4776llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4777 const ObjCProtocolDecl *PD) {
4778 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4779
4780 // Early exit if a defining object has already been generated.
4781 if (Entry && Entry->hasInitializer())
4782 return Entry;
4783
4784 const char *ProtocolName = PD->getNameAsCString();
4785
4786 // Construct method lists.
4787 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4788 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00004789 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004790 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004791 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004792 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004793 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4794 OptInstanceMethods.push_back(C);
4795 } else {
4796 InstanceMethods.push_back(C);
4797 }
4798 }
4799
Douglas Gregor6ab35242009-04-09 21:40:53 +00004800 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004801 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004802 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004803 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004804 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4805 OptClassMethods.push_back(C);
4806 } else {
4807 ClassMethods.push_back(C);
4808 }
4809 }
4810
4811 std::vector<llvm::Constant*> Values(10);
4812 // isa is NULL
Owen Anderson69243822009-07-13 04:10:07 +00004813 Values[0] = VMContext.getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004814 Values[1] = GetClassName(PD->getIdentifier());
4815 Values[2] = EmitProtocolList(
4816 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4817 PD->protocol_begin(),
4818 PD->protocol_end());
4819
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004820 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004821 + PD->getNameAsString(),
4822 "__DATA, __objc_const",
4823 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004824 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004825 + PD->getNameAsString(),
4826 "__DATA, __objc_const",
4827 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004828 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004829 + PD->getNameAsString(),
4830 "__DATA, __objc_const",
4831 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004832 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004833 + PD->getNameAsString(),
4834 "__DATA, __objc_const",
4835 OptClassMethods);
4836 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4837 0, PD, ObjCTypes);
4838 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00004839 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004840 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson69243822009-07-13 04:10:07 +00004841 Values[9] = VMContext.getNullValue(ObjCTypes.IntTy);
Owen Anderson08e25242009-07-27 22:29:56 +00004842 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004843 Values);
4844
4845 if (Entry) {
4846 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00004847 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004848 Entry->setInitializer(Init);
4849 } else {
4850 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00004851 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004852 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004853 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004854 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004855 Entry->setAlignment(
4856 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004857 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004858 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004859 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00004860 CGM.AddUsedGlobal(Entry);
4861
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004862 // Use this protocol meta-data to build protocol list table in section
4863 // __DATA, __objc_protolist
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004864 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
Owen Anderson1c431b32009-07-08 19:05:04 +00004865 CGM.getModule(),
Daniel Dunbar948e2582009-02-15 07:36:20 +00004866 ObjCTypes.ProtocolnfABIPtrTy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004867 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004868 Entry,
4869 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
Owen Anderson1c431b32009-07-08 19:05:04 +00004870 +ProtocolName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004871 PTGV->setAlignment(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004872 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00004873 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004874 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00004875 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004876 return Entry;
4877}
4878
4879/// EmitProtocolList - Generate protocol list meta-data:
4880/// @code
4881/// struct _protocol_list_t {
4882/// long protocol_count; // Note, this is 32/64 bit
4883/// struct _protocol_t[protocol_count];
4884/// }
4885/// @endcode
4886///
4887llvm::Constant *
4888CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4889 ObjCProtocolDecl::protocol_iterator begin,
4890 ObjCProtocolDecl::protocol_iterator end) {
4891 std::vector<llvm::Constant*> ProtocolRefs;
4892
Fariborz Jahanianda320092009-01-29 19:24:30 +00004893 // Just return null for empty protocol lists
Daniel Dunbar948e2582009-02-15 07:36:20 +00004894 if (begin == end)
Owen Anderson69243822009-07-13 04:10:07 +00004895 return VMContext.getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004896
Daniel Dunbar948e2582009-02-15 07:36:20 +00004897 // FIXME: We shouldn't need to do this lookup here, should we?
Fariborz Jahanianda320092009-01-29 19:24:30 +00004898 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4899 if (GV)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004900 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00004901 ObjCTypes.ProtocolListnfABIPtrTy);
4902
4903 for (; begin != end; ++begin)
4904 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4905
Fariborz Jahanianda320092009-01-29 19:24:30 +00004906 // This list is null terminated.
Owen Anderson69243822009-07-13 04:10:07 +00004907 ProtocolRefs.push_back(VMContext.getNullValue(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004908 ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004909
4910 std::vector<llvm::Constant*> Values(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004911 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004912 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004913 Values[1] =
Owen Anderson7db6d832009-07-28 18:33:04 +00004914 llvm::ConstantArray::get(
Owen Anderson96e0fc72009-07-29 22:16:19 +00004915 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00004916 ProtocolRefs.size()),
4917 ProtocolRefs);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004918
Owen Anderson08e25242009-07-27 22:29:56 +00004919 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Owen Anderson1c431b32009-07-08 19:05:04 +00004920 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004921 llvm::GlobalValue::InternalLinkage,
4922 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004923 Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004924 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004925 GV->setAlignment(
4926 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00004927 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00004928 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00004929 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004930}
4931
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004932/// GetMethodDescriptionConstant - This routine build following meta-data:
4933/// struct _objc_method {
4934/// SEL _cmd;
4935/// char *method_type;
4936/// char *_imp;
4937/// }
4938
4939llvm::Constant *
4940CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4941 std::vector<llvm::Constant*> Desc(3);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004942 Desc[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00004943 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004944 ObjCTypes.SelectorPtrTy);
4945 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004946 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson69243822009-07-13 04:10:07 +00004947 Desc[2] = VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00004948 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004949}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004950
4951/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4952/// This code gen. amounts to generating code for:
4953/// @code
4954/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4955/// @encode
4956///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004957LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004958 CodeGen::CodeGenFunction &CGF,
4959 QualType ObjectTy,
4960 llvm::Value *BaseValue,
4961 const ObjCIvarDecl *Ivar,
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004962 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00004963 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00004964 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
4965 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004966}
4967
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004968llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
4969 CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00004970 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004971 const ObjCIvarDecl *Ivar) {
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00004972 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),
4973 false, "ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004974}
4975
Fariborz Jahanian46551122009-02-04 00:22:57 +00004976CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4977 CodeGen::CodeGenFunction &CGF,
4978 QualType ResultType,
4979 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004980 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00004981 QualType Arg0Ty,
4982 bool IsSuper,
4983 const CallArgList &CallArgs) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00004984 // FIXME. Even though IsSuper is passes. This function doese not handle calls
4985 // to 'super' receivers.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004986 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004987 llvm::Value *Arg0 = Receiver;
4988 if (!IsSuper)
4989 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004990
4991 // Find the message function name.
Mike Stumpf5408fe2009-05-16 07:57:57 +00004992 // FIXME. This is too much work to get the ABI-specific result type needed to
4993 // find the message name.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004994 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4995 llvm::SmallVector<QualType, 16>());
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00004996 llvm::Constant *Fn = 0;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004997 std::string Name("\01l_");
4998 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004999#if 0
5000 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005001 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005002 Fn = ObjCTypes.getMessageSendIdStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005003 // FIXME. Is there a better way of getting these names.
5004 // They are available in RuntimeFunctions vector pair.
5005 Name += "objc_msgSendId_stret_fixup";
5006 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005007 else
5008#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005009 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005010 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005011 Name += "objc_msgSendSuper2_stret_fixup";
5012 }
5013 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005014 {
Chris Lattner1c02f862009-04-22 02:53:24 +00005015 Fn = ObjCTypes.getMessageSendStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005016 Name += "objc_msgSend_stret_fixup";
5017 }
5018 }
Fariborz Jahanian5b2bad02009-04-30 16:31:11 +00005019 else if (!IsSuper && ResultType->isFloatingType()) {
Daniel Dunbarc0183e82009-06-26 18:32:06 +00005020 if (ResultType->isSpecificBuiltinType(BuiltinType::LongDouble)) {
5021 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5022 Name += "objc_msgSend_fpret_fixup";
5023 }
5024 else {
5025 Fn = ObjCTypes.getMessageSendFixupFn();
5026 Name += "objc_msgSend_fixup";
Fariborz Jahanian5b2bad02009-04-30 16:31:11 +00005027 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005028 }
5029 else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005030#if 0
5031// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005032 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005033 Fn = ObjCTypes.getMessageSendIdFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005034 Name += "objc_msgSendId_fixup";
5035 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005036 else
5037#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005038 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005039 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005040 Name += "objc_msgSendSuper2_fixup";
5041 }
5042 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005043 {
Chris Lattner1c02f862009-04-22 02:53:24 +00005044 Fn = ObjCTypes.getMessageSendFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005045 Name += "objc_msgSend_fixup";
5046 }
5047 }
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005048 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005049 Name += '_';
5050 std::string SelName(Sel.getAsString());
5051 // Replace all ':' in selector name with '_' ouch!
5052 for(unsigned i = 0; i < SelName.size(); i++)
5053 if (SelName[i] == ':')
5054 SelName[i] = '_';
5055 Name += SelName;
5056 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5057 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005058 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005059 std::vector<llvm::Constant*> Values(2);
5060 Values[0] = Fn;
5061 Values[1] = GetMethodVarName(Sel);
Owen Anderson08e25242009-07-27 22:29:56 +00005062 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Owen Anderson1c431b32009-07-08 19:05:04 +00005063 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005064 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005065 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005066 Name);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005067 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005068 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005069 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005070 }
5071 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005072
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005073 CallArgList ActualArgs;
5074 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
5075 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
5076 ObjCTypes.MessageRefCPtrTy));
5077 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005078 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
5079 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5080 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00005081 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005082 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Anderson96e0fc72009-07-29 22:16:19 +00005083 llvm::PointerType::getUnqual(FTy));
Fariborz Jahanianef163782009-02-05 01:13:09 +00005084 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005085}
5086
5087/// Generate code for a message send expression in the nonfragile abi.
5088CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
5089 CodeGen::CodeGenFunction &CGF,
5090 QualType ResultType,
5091 Selector Sel,
5092 llvm::Value *Receiver,
5093 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00005094 const CallArgList &CallArgs,
5095 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005096 return LegacyDispatchedSelector(Sel)
5097 ? EmitLegacyMessageSend(CGF, ResultType, EmitSelector(CGF.Builder, Sel),
5098 Receiver, CGF.getContext().getObjCIdType(),
5099 false, CallArgs, ObjCTypes)
5100 : EmitMessageSend(CGF, ResultType, Sel,
5101 Receiver, CGF.getContext().getObjCIdType(),
5102 false, CallArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005103}
5104
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005105llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005106CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005107 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5108
Daniel Dunbardfff2302009-03-02 05:18:14 +00005109 if (!GV) {
Owen Anderson1c431b32009-07-08 19:05:04 +00005110 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
5111 false, llvm::GlobalValue::ExternalLinkage,
5112 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005113 }
5114
5115 return GV;
5116}
5117
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005118llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005119 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005120 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
5121
5122 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005123 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005124 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005125 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005126 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5127 false, llvm::GlobalValue::InternalLinkage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005128 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005129 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005130 Entry->setAlignment(
5131 CGM.getTargetData().getPrefTypeAlignment(
5132 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005133 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005134 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005135 }
5136
5137 return Builder.CreateLoad(Entry, false, "tmp");
5138}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005139
Daniel Dunbar11394522009-04-18 08:51:00 +00005140llvm::Value *
5141CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
5142 const ObjCInterfaceDecl *ID) {
5143 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
5144
5145 if (!Entry) {
5146 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5147 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5148 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005149 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5150 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar11394522009-04-18 08:51:00 +00005151 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005152 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005153 Entry->setAlignment(
5154 CGM.getTargetData().getPrefTypeAlignment(
5155 ObjCTypes.ClassnfABIPtrTy));
5156 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005157 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005158 }
5159
5160 return Builder.CreateLoad(Entry, false, "tmp");
5161}
5162
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005163/// EmitMetaClassRef - Return a Value * of the address of _class_t
5164/// meta-data
5165///
5166llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5167 const ObjCInterfaceDecl *ID) {
5168 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5169 if (Entry)
5170 return Builder.CreateLoad(Entry, false, "tmp");
5171
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005172 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005173 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005174 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005175 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005176 llvm::GlobalValue::InternalLinkage,
5177 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005178 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005179 Entry->setAlignment(
5180 CGM.getTargetData().getPrefTypeAlignment(
5181 ObjCTypes.ClassnfABIPtrTy));
5182
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005183 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005184 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005185
5186 return Builder.CreateLoad(Entry, false, "tmp");
5187}
5188
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005189/// GetClass - Return a reference to the class for the given interface
5190/// decl.
5191llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5192 const ObjCInterfaceDecl *ID) {
5193 return EmitClassRef(Builder, ID);
5194}
5195
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005196/// Generates a message send where the super is the receiver. This is
5197/// a message send to self with special delivery semantics indicating
5198/// which class's method should be called.
5199CodeGen::RValue
5200CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
5201 QualType ResultType,
5202 Selector Sel,
5203 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005204 bool isCategoryImpl,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005205 llvm::Value *Receiver,
5206 bool IsClassMessage,
5207 const CodeGen::CallArgList &CallArgs) {
5208 // ...
5209 // Create and init a super structure; this is a (receiver, class)
5210 // pair we will pass to objc_msgSendSuper.
5211 llvm::Value *ObjCSuper =
5212 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
5213
5214 llvm::Value *ReceiverAsObject =
5215 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5216 CGF.Builder.CreateStore(ReceiverAsObject,
5217 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
5218
5219 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005220 llvm::Value *Target;
5221 if (IsClassMessage) {
5222 if (isCategoryImpl) {
5223 // Message sent to "super' in a class method defined in
5224 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005225 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005226 Target = CGF.Builder.CreateStructGEP(Target, 0);
5227 Target = CGF.Builder.CreateLoad(Target);
5228 }
5229 else
5230 Target = EmitMetaClassRef(CGF.Builder, Class);
5231 }
5232 else
Daniel Dunbar11394522009-04-18 08:51:00 +00005233 Target = EmitSuperClassRef(CGF.Builder, Class);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005234
Mike Stumpf5408fe2009-05-16 07:57:57 +00005235 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5236 // ObjCTypes types.
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005237 const llvm::Type *ClassTy =
5238 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5239 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5240 CGF.Builder.CreateStore(Target,
5241 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
5242
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005243 return (LegacyDispatchedSelector(Sel))
5244 ? EmitLegacyMessageSend(CGF, ResultType,EmitSelector(CGF.Builder, Sel),
5245 ObjCSuper, ObjCTypes.SuperPtrCTy,
5246 true, CallArgs,
5247 ObjCTypes)
5248 : EmitMessageSend(CGF, ResultType, Sel,
5249 ObjCSuper, ObjCTypes.SuperPtrCTy,
5250 true, CallArgs);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005251}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005252
5253llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
5254 Selector Sel) {
5255 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
5256
5257 if (!Entry) {
5258 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00005259 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005260 ObjCTypes.SelectorPtrTy);
5261 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005262 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005263 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00005264 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005265 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005266 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005267 }
5268
5269 return Builder.CreateLoad(Entry, false, "tmp");
5270}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005271/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
5272/// objc_assign_ivar (id src, id *dst)
5273///
5274void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
5275 llvm::Value *src, llvm::Value *dst)
5276{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005277 const llvm::Type * SrcTy = src->getType();
5278 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005279 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005280 assert(Size <= 8 && "does not support size > 8");
5281 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5282 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005283 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5284 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005285 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5286 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005287 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005288 src, dst, "assignivar");
5289 return;
5290}
5291
5292/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5293/// objc_assign_strongCast (id src, id *dst)
5294///
5295void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
5296 CodeGen::CodeGenFunction &CGF,
5297 llvm::Value *src, llvm::Value *dst)
5298{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005299 const llvm::Type * SrcTy = src->getType();
5300 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005301 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005302 assert(Size <= 8 && "does not support size > 8");
5303 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5304 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005305 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5306 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005307 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5308 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005309 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005310 src, dst, "weakassign");
5311 return;
5312}
5313
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005314void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
5315 CodeGen::CodeGenFunction &CGF,
5316 llvm::Value *DestPtr,
5317 llvm::Value *SrcPtr,
5318 unsigned long size) {
5319 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5320 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005321 llvm::Value *N = llvm::ConstantInt::get(ObjCTypes.LongTy, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005322 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
5323 DestPtr, SrcPtr, N);
5324 return;
5325}
5326
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005327/// EmitObjCWeakRead - Code gen for loading value of a __weak
5328/// object: objc_read_weak (id *src)
5329///
5330llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
5331 CodeGen::CodeGenFunction &CGF,
5332 llvm::Value *AddrWeakObj)
5333{
Eli Friedman8339b352009-03-07 03:57:15 +00005334 const llvm::Type* DestTy =
5335 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005336 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005337 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005338 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005339 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005340 return read_weak;
5341}
5342
5343/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5344/// objc_assign_weak (id src, id *dst)
5345///
5346void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
5347 llvm::Value *src, llvm::Value *dst)
5348{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005349 const llvm::Type * SrcTy = src->getType();
5350 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005351 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005352 assert(Size <= 8 && "does not support size > 8");
5353 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5354 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005355 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5356 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005357 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5358 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005359 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005360 src, dst, "weakassign");
5361 return;
5362}
5363
5364/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5365/// objc_assign_global (id src, id *dst)
5366///
5367void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
5368 llvm::Value *src, llvm::Value *dst)
5369{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005370 const llvm::Type * SrcTy = src->getType();
5371 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005372 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005373 assert(Size <= 8 && "does not support size > 8");
5374 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5375 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005376 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5377 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005378 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5379 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005380 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005381 src, dst, "globalassign");
5382 return;
5383}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005384
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005385void
5386CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5387 const Stmt &S) {
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005388 bool isTry = isa<ObjCAtTryStmt>(S);
5389 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
5390 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005391 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005392 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005393 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005394 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
5395
5396 // For @synchronized, call objc_sync_enter(sync.expr). The
5397 // evaluation of the expression must occur before we enter the
5398 // @synchronized. We can safely avoid a temp here because jumps into
5399 // @synchronized are illegal & this will dominate uses.
5400 llvm::Value *SyncArg = 0;
5401 if (!isTry) {
5402 SyncArg =
5403 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
5404 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005405 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005406 }
5407
5408 // Push an EH context entry, used for handling rethrows and jumps
5409 // through finally.
5410 CGF.PushCleanupBlock(FinallyBlock);
5411
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005412 CGF.setInvokeDest(TryHandler);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005413
5414 CGF.EmitBlock(TryBlock);
5415 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
5416 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
5417 CGF.EmitBranchThroughCleanup(FinallyEnd);
5418
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005419 // Emit the exception handler.
5420
5421 CGF.EmitBlock(TryHandler);
5422
5423 llvm::Value *llvm_eh_exception =
5424 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
5425 llvm::Value *llvm_eh_selector_i64 =
5426 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64);
5427 llvm::Value *llvm_eh_typeid_for_i64 =
5428 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64);
5429 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5430 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow");
5431
5432 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
5433 SelectorArgs.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005434 SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005435
5436 // Construct the lists of (type, catch body) to handle.
Daniel Dunbarede8de92009-03-06 00:01:21 +00005437 llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005438 bool HasCatchAll = false;
5439 if (isTry) {
5440 if (const ObjCAtCatchStmt* CatchStmt =
5441 cast<ObjCAtTryStmt>(S).getCatchStmts()) {
5442 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005443 const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Steve Naroff7ba138a2009-03-03 19:52:17 +00005444 Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody()));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005445
5446 // catch(...) always matches.
Steve Naroff7ba138a2009-03-03 19:52:17 +00005447 if (!CatchDecl) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005448 // Use i8* null here to signal this is a catch all, not a cleanup.
Owen Anderson69243822009-07-13 04:10:07 +00005449 llvm::Value *Null = VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005450 SelectorArgs.push_back(Null);
5451 HasCatchAll = true;
5452 break;
5453 }
5454
Steve Naroff14108da2009-07-10 23:34:53 +00005455 if (CatchDecl->getType()->isObjCIdType() ||
Daniel Dunbarede8de92009-03-06 00:01:21 +00005456 CatchDecl->getType()->isObjCQualifiedIdType()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005457 llvm::Value *IDEHType =
5458 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5459 if (!IDEHType)
5460 IDEHType =
Owen Anderson1c431b32009-07-08 19:05:04 +00005461 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
5462 false,
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005463 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00005464 0, "OBJC_EHTYPE_id");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005465 SelectorArgs.push_back(IDEHType);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005466 }
Fariborz Jahanian99438a72009-07-27 23:12:41 +00005467 else {
5468 // All other types should be Objective-C interface pointer types.
5469 const ObjCObjectPointerType *PT =
5470 CatchDecl->getType()->getAsObjCObjectPointerType();
5471 assert(PT && "Invalid @catch type.");
5472 const ObjCInterfaceType *IT = PT->getInterfaceType();
5473 assert(IT && "Invalid @catch type.");
5474 llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false);
5475 SelectorArgs.push_back(EHType);
5476 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005477 }
5478 }
5479 }
5480
5481 // We use a cleanup unless there was already a catch all.
5482 if (!HasCatchAll) {
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005483 SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
Daniel Dunbarede8de92009-03-06 00:01:21 +00005484 Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005485 }
5486
5487 llvm::Value *Selector =
5488 CGF.Builder.CreateCall(llvm_eh_selector_i64,
5489 SelectorArgs.begin(), SelectorArgs.end(),
5490 "selector");
5491 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005492 const ParmVarDecl *CatchParam = Handlers[i].first;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005493 const Stmt *CatchBody = Handlers[i].second;
5494
5495 llvm::BasicBlock *Next = 0;
5496
5497 // The last handler always matches.
5498 if (i + 1 != e) {
5499 assert(CatchParam && "Only last handler can be a catch all.");
5500
5501 llvm::BasicBlock *Match = CGF.createBasicBlock("match");
5502 Next = CGF.createBasicBlock("catch.next");
5503 llvm::Value *Id =
5504 CGF.Builder.CreateCall(llvm_eh_typeid_for_i64,
5505 CGF.Builder.CreateBitCast(SelectorArgs[i+2],
5506 ObjCTypes.Int8PtrTy));
5507 CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id),
5508 Match, Next);
5509
5510 CGF.EmitBlock(Match);
5511 }
5512
5513 if (CatchBody) {
5514 llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end");
5515 llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler");
5516
5517 // Cleanups must call objc_end_catch.
5518 //
Mike Stumpf5408fe2009-05-16 07:57:57 +00005519 // FIXME: It seems incorrect for objc_begin_catch to be inside this
5520 // context, but this matches gcc.
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005521 CGF.PushCleanupBlock(MatchEnd);
5522 CGF.setInvokeDest(MatchHandler);
5523
5524 llvm::Value *ExcObject =
Chris Lattner8a569112009-04-22 02:15:23 +00005525 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005526
5527 // Bind the catch parameter if it exists.
5528 if (CatchParam) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005529 ExcObject =
5530 CGF.Builder.CreateBitCast(ExcObject,
5531 CGF.ConvertType(CatchParam->getType()));
5532 // CatchParam is a ParmVarDecl because of the grammar
5533 // construction used to handle this, but for codegen purposes
5534 // we treat this as a local decl.
5535 CGF.EmitLocalBlockVarDecl(*CatchParam);
5536 CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005537 }
5538
5539 CGF.ObjCEHValueStack.push_back(ExcObject);
5540 CGF.EmitStmt(CatchBody);
5541 CGF.ObjCEHValueStack.pop_back();
5542
5543 CGF.EmitBranchThroughCleanup(FinallyEnd);
5544
5545 CGF.EmitBlock(MatchHandler);
5546
5547 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5548 // We are required to emit this call to satisfy LLVM, even
5549 // though we don't use the result.
5550 llvm::SmallVector<llvm::Value*, 8> Args;
5551 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005552 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005553 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005554 0));
5555 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5556 CGF.Builder.CreateStore(Exc, RethrowPtr);
5557 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5558
5559 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5560
5561 CGF.EmitBlock(MatchEnd);
5562
5563 // Unfortunately, we also have to generate another EH frame here
5564 // in case this throws.
5565 llvm::BasicBlock *MatchEndHandler =
5566 CGF.createBasicBlock("match.end.handler");
5567 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattner8a569112009-04-22 02:15:23 +00005568 CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005569 Cont, MatchEndHandler,
5570 Args.begin(), Args.begin());
5571
5572 CGF.EmitBlock(Cont);
5573 if (Info.SwitchBlock)
5574 CGF.EmitBlock(Info.SwitchBlock);
5575 if (Info.EndBlock)
5576 CGF.EmitBlock(Info.EndBlock);
5577
5578 CGF.EmitBlock(MatchEndHandler);
5579 Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5580 // We are required to emit this call to satisfy LLVM, even
5581 // though we don't use the result.
5582 Args.clear();
5583 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005584 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005585 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005586 0));
5587 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5588 CGF.Builder.CreateStore(Exc, RethrowPtr);
5589 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5590
5591 if (Next)
5592 CGF.EmitBlock(Next);
5593 } else {
5594 assert(!Next && "catchup should be last handler.");
5595
5596 CGF.Builder.CreateStore(Exc, RethrowPtr);
5597 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5598 }
5599 }
5600
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005601 // Pop the cleanup entry, the @finally is outside this cleanup
5602 // scope.
5603 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5604 CGF.setInvokeDest(PrevLandingPad);
5605
5606 CGF.EmitBlock(FinallyBlock);
5607
5608 if (isTry) {
5609 if (const ObjCAtFinallyStmt* FinallyStmt =
5610 cast<ObjCAtTryStmt>(S).getFinallyStmt())
5611 CGF.EmitStmt(FinallyStmt->getFinallyBody());
5612 } else {
5613 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
5614 // @synchronized.
Chris Lattnerbbccd612009-04-22 02:38:11 +00005615 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005616 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005617
5618 if (Info.SwitchBlock)
5619 CGF.EmitBlock(Info.SwitchBlock);
5620 if (Info.EndBlock)
5621 CGF.EmitBlock(Info.EndBlock);
5622
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005623 // Branch around the rethrow code.
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005624 CGF.EmitBranch(FinallyEnd);
5625
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005626 CGF.EmitBlock(FinallyRethrow);
Chris Lattner8a569112009-04-22 02:15:23 +00005627 CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005628 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005629 CGF.Builder.CreateUnreachable();
5630
5631 CGF.EmitBlock(FinallyEnd);
5632}
5633
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005634/// EmitThrowStmt - Generate code for a throw statement.
5635void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
5636 const ObjCAtThrowStmt &S) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005637 llvm::Value *Exception;
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005638 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005639 Exception = CGF.EmitScalarExpr(ThrowExpr);
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005640 } else {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005641 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
5642 "Unexpected rethrow outside @catch block.");
5643 Exception = CGF.ObjCEHValueStack.back();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005644 }
5645
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005646 llvm::Value *ExceptionAsObject =
5647 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
5648 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
5649 if (InvokeDest) {
5650 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattnerbbccd612009-04-22 02:38:11 +00005651 CGF.Builder.CreateInvoke(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005652 Cont, InvokeDest,
5653 &ExceptionAsObject, &ExceptionAsObject + 1);
5654 CGF.EmitBlock(Cont);
5655 } else
Chris Lattnerbbccd612009-04-22 02:38:11 +00005656 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005657 CGF.Builder.CreateUnreachable();
5658
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005659 // Clear the insertion point to indicate we are in unreachable code.
5660 CGF.Builder.ClearInsertionPoint();
5661}
Daniel Dunbare588b992009-03-01 04:46:24 +00005662
5663llvm::Value *
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005664CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
5665 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00005666 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00005667
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005668 // If we don't need a definition, return the entry if found or check
5669 // if we use an external reference.
5670 if (!ForDefinition) {
5671 if (Entry)
5672 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00005673
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005674 // If this type (or a super class) has the __objc_exception__
5675 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00005676 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005677 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005678 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005679 llvm::GlobalValue::ExternalLinkage,
5680 0,
5681 (std::string("OBJC_EHTYPE_$_") +
Owen Anderson1c431b32009-07-08 19:05:04 +00005682 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005683 }
5684
5685 // Otherwise we need to either make a new entry or fill in the
5686 // initializer.
5687 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005688 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00005689 std::string VTableName = "objc_ehtype_vtable";
5690 llvm::GlobalVariable *VTableGV =
5691 CGM.getModule().getGlobalVariable(VTableName);
5692 if (!VTableGV)
Owen Anderson1c431b32009-07-08 19:05:04 +00005693 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
5694 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00005695 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00005696 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00005697
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005698 llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00005699
5700 std::vector<llvm::Constant*> Values(3);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005701 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbare588b992009-03-01 04:46:24 +00005702 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005703 Values[2] = GetClassGlobal(ClassName);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005704 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00005705 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00005706
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005707 if (Entry) {
5708 Entry->setInitializer(Init);
5709 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00005710 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005711 llvm::GlobalValue::WeakAnyLinkage,
5712 Init,
5713 (std::string("OBJC_EHTYPE_$_") +
Owen Anderson1c431b32009-07-08 19:05:04 +00005714 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005715 }
5716
Daniel Dunbar04d40782009-04-14 06:00:08 +00005717 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005718 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005719 Entry->setAlignment(8);
5720
5721 if (ForDefinition) {
5722 Entry->setSection("__DATA,__objc_const");
5723 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
5724 } else {
5725 Entry->setSection("__DATA,__datacoal_nt,coalesced");
5726 }
Daniel Dunbare588b992009-03-01 04:46:24 +00005727
5728 return Entry;
5729}
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005730
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005731/* *** */
5732
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005733CodeGen::CGObjCRuntime *
5734CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00005735 return new CGObjCMac(CGM);
5736}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005737
5738CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005739CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005740 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005741}