blob: d3a0fc6e549ad539d022103312fc005e181c356c [file] [log] [blame]
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001//===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides Objective-C code generation targetting the Apple runtime.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000015
16#include "CodeGenModule.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000017#include "CodeGenFunction.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000019#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000020#include "clang/AST/DeclObjC.h"
Daniel Dunbar2bebbf02009-05-03 10:46:44 +000021#include "clang/AST/RecordLayout.h"
Chris Lattner16f00492009-04-26 01:32:48 +000022#include "clang/AST/StmtObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000023#include "clang/Basic/LangOptions.h"
24
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +000025#include "llvm/Intrinsics.h"
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)
Daniel Dunbar97776872009-04-22 07:32:20 +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");
Daniel Dunbar1d7e5392009-05-03 08:55:17 +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 {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000142private:
143 llvm::Constant *getMessageSendFn() const {
144 // id objc_msgSend (id, SEL, ...)
145 std::vector<const llvm::Type*> Params;
146 Params.push_back(ObjectPtrTy);
147 Params.push_back(SelectorPtrTy);
148 return
149 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
150 Params, true),
151 "objc_msgSend");
152 }
153
154 llvm::Constant *getMessageSendStretFn() const {
155 // id objc_msgSend_stret (id, SEL, ...)
156 std::vector<const llvm::Type*> Params;
157 Params.push_back(ObjectPtrTy);
158 Params.push_back(SelectorPtrTy);
159 return
160 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
161 Params, true),
162 "objc_msgSend_stret");
163
164 }
165
166 llvm::Constant *getMessageSendFpretFn() const {
167 // FIXME: This should be long double on x86_64?
168 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
169 std::vector<const llvm::Type*> Params;
170 Params.push_back(ObjectPtrTy);
171 Params.push_back(SelectorPtrTy);
172 return
173 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
174 Params,
175 true),
176 "objc_msgSend_fpret");
177
178 }
179
180 llvm::Constant *getMessageSendSuperFn() const {
181 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
182 const char *SuperName = "objc_msgSendSuper";
183 std::vector<const llvm::Type*> Params;
184 Params.push_back(SuperPtrTy);
185 Params.push_back(SelectorPtrTy);
186 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
187 Params, true),
188 SuperName);
189 }
190
191 llvm::Constant *getMessageSendSuperFn2() const {
192 // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
193 const char *SuperName = "objc_msgSendSuper2";
194 std::vector<const llvm::Type*> Params;
195 Params.push_back(SuperPtrTy);
196 Params.push_back(SelectorPtrTy);
197 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
198 Params, true),
199 SuperName);
200 }
201
202 llvm::Constant *getMessageSendSuperStretFn() const {
203 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
204 // SEL op, ...)
205 std::vector<const llvm::Type*> Params;
206 Params.push_back(Int8PtrTy);
207 Params.push_back(SuperPtrTy);
208 Params.push_back(SelectorPtrTy);
209 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
210 Params, true),
211 "objc_msgSendSuper_stret");
212 }
213
214 llvm::Constant *getMessageSendSuperStretFn2() const {
215 // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
216 // SEL op, ...)
217 std::vector<const llvm::Type*> Params;
218 Params.push_back(Int8PtrTy);
219 Params.push_back(SuperPtrTy);
220 Params.push_back(SelectorPtrTy);
221 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
222 Params, true),
223 "objc_msgSendSuper2_stret");
224 }
225
226 llvm::Constant *getMessageSendSuperFpretFn() const {
227 // There is no objc_msgSendSuper_fpret? How can that work?
228 return getMessageSendSuperFn();
229 }
230
231 llvm::Constant *getMessageSendSuperFpretFn2() const {
232 // There is no objc_msgSendSuper_fpret? How can that work?
233 return getMessageSendSuperFn2();
234 }
235
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000236protected:
237 CodeGen::CodeGenModule &CGM;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000238
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000239public:
Fariborz Jahanian0a855d02009-03-23 19:10:40 +0000240 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000241 const llvm::Type *Int8PtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000242
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000243 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
244 const llvm::Type *ObjectPtrTy;
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000245
246 /// PtrObjectPtrTy - LLVM type for id *
247 const llvm::Type *PtrObjectPtrTy;
248
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000249 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000250 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000251 /// ProtocolPtrTy - LLVM type for external protocol handles
252 /// (typeof(Protocol))
253 const llvm::Type *ExternalProtocolPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000254
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000255 // SuperCTy - clang type for struct objc_super.
256 QualType SuperCTy;
257 // SuperPtrCTy - clang type for struct objc_super *.
258 QualType SuperPtrCTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000259
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000260 /// SuperTy - LLVM type for struct objc_super.
261 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000262 /// SuperPtrTy - LLVM type for struct objc_super *.
263 const llvm::Type *SuperPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000264
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000265 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
266 /// in GCC parlance).
267 const llvm::StructType *PropertyTy;
268
269 /// PropertyListTy - LLVM type for struct objc_property_list
270 /// (_prop_list_t in GCC parlance).
271 const llvm::StructType *PropertyListTy;
272 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
273 const llvm::Type *PropertyListPtrTy;
274
275 // MethodTy - LLVM type for struct objc_method.
276 const llvm::StructType *MethodTy;
277
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000278 /// CacheTy - LLVM type for struct objc_cache.
279 const llvm::Type *CacheTy;
280 /// CachePtrTy - LLVM type for struct objc_cache *.
281 const llvm::Type *CachePtrTy;
282
Chris Lattner72db6c32009-04-22 02:44:54 +0000283 llvm::Constant *getGetPropertyFn() {
284 CodeGen::CodeGenTypes &Types = CGM.getTypes();
285 ASTContext &Ctx = CGM.getContext();
286 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
287 llvm::SmallVector<QualType,16> Params;
288 QualType IdType = Ctx.getObjCIdType();
289 QualType SelType = Ctx.getObjCSelType();
290 Params.push_back(IdType);
291 Params.push_back(SelType);
292 Params.push_back(Ctx.LongTy);
293 Params.push_back(Ctx.BoolTy);
294 const llvm::FunctionType *FTy =
295 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
296 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
297 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000298
Chris Lattner72db6c32009-04-22 02:44:54 +0000299 llvm::Constant *getSetPropertyFn() {
300 CodeGen::CodeGenTypes &Types = CGM.getTypes();
301 ASTContext &Ctx = CGM.getContext();
302 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
303 llvm::SmallVector<QualType,16> Params;
304 QualType IdType = Ctx.getObjCIdType();
305 QualType SelType = Ctx.getObjCSelType();
306 Params.push_back(IdType);
307 Params.push_back(SelType);
308 Params.push_back(Ctx.LongTy);
309 Params.push_back(IdType);
310 Params.push_back(Ctx.BoolTy);
311 Params.push_back(Ctx.BoolTy);
312 const llvm::FunctionType *FTy =
313 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
314 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
315 }
316
317 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000318 CodeGen::CodeGenTypes &Types = CGM.getTypes();
319 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000320 // void objc_enumerationMutation (id)
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000321 llvm::SmallVector<QualType,16> Params;
322 QualType IdType = Ctx.getObjCIdType();
323 Params.push_back(IdType);
324 const llvm::FunctionType *FTy =
325 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000326 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
327 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000328
329 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000330 llvm::Constant *getGcReadWeakFn() {
331 // id objc_read_weak (id *)
332 std::vector<const llvm::Type*> Args;
333 Args.push_back(ObjectPtrTy->getPointerTo());
334 llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false);
335 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
336 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000337
338 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000339 llvm::Constant *getGcAssignWeakFn() {
340 // id objc_assign_weak (id, id *)
341 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
342 Args.push_back(ObjectPtrTy->getPointerTo());
343 llvm::FunctionType *FTy =
344 llvm::FunctionType::get(ObjectPtrTy, Args, false);
345 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
346 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000347
348 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000349 llvm::Constant *getGcAssignGlobalFn() {
350 // id objc_assign_global(id, id *)
351 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
352 Args.push_back(ObjectPtrTy->getPointerTo());
353 llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false);
354 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
355 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000356
357 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000358 llvm::Constant *getGcAssignIvarFn() {
359 // id objc_assign_ivar(id, id *)
360 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
361 Args.push_back(ObjectPtrTy->getPointerTo());
362 llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false);
363 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
364 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000365
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000366 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
367 llvm::Constant *GcMemmoveCollectableFn() {
368 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
369 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
370 Args.push_back(Int8PtrTy);
371 Args.push_back(LongTy);
372 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
373 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
374 }
375
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000376 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000377 llvm::Constant *getGcAssignStrongCastFn() {
378 // id objc_assign_global(id, id *)
379 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
380 Args.push_back(ObjectPtrTy->getPointerTo());
381 llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false);
382 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
383 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000384
385 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000386 llvm::Constant *getExceptionThrowFn() {
387 // void objc_exception_throw(id)
388 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
389 llvm::FunctionType *FTy =
390 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
391 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
392 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000393
Daniel Dunbar1c566672009-02-24 01:43:46 +0000394 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000395 llvm::Constant *getSyncEnterFn() {
396 // void objc_sync_enter (id)
397 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
398 llvm::FunctionType *FTy =
399 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
400 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
401 }
Daniel Dunbar1c566672009-02-24 01:43:46 +0000402
403 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000404 llvm::Constant *getSyncExitFn() {
405 // void objc_sync_exit (id)
406 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
407 llvm::FunctionType *FTy =
408 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
409 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
410 }
Daniel Dunbar1c566672009-02-24 01:43:46 +0000411
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000412 llvm::Constant *getSendFn(bool IsSuper) const {
413 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
414 }
415
416 llvm::Constant *getSendFn2(bool IsSuper) const {
417 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
418 }
419
420 llvm::Constant *getSendStretFn(bool IsSuper) const {
421 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
422 }
423
424 llvm::Constant *getSendStretFn2(bool IsSuper) const {
425 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
426 }
427
428 llvm::Constant *getSendFpretFn(bool IsSuper) const {
429 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
430 }
431
432 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
433 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
434 }
435
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000436 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
437 ~ObjCCommonTypesHelper(){}
438};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000439
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000440/// ObjCTypesHelper - Helper class that encapsulates lazy
441/// construction of varies types used during ObjC generation.
442class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000443public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000444 /// SymtabTy - LLVM type for struct objc_symtab.
445 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000446 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
447 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000448 /// ModuleTy - LLVM type for struct objc_module.
449 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000450
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000451 /// ProtocolTy - LLVM type for struct objc_protocol.
452 const llvm::StructType *ProtocolTy;
453 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
454 const llvm::Type *ProtocolPtrTy;
455 /// ProtocolExtensionTy - LLVM type for struct
456 /// objc_protocol_extension.
457 const llvm::StructType *ProtocolExtensionTy;
458 /// ProtocolExtensionTy - LLVM type for struct
459 /// objc_protocol_extension *.
460 const llvm::Type *ProtocolExtensionPtrTy;
461 /// MethodDescriptionTy - LLVM type for struct
462 /// objc_method_description.
463 const llvm::StructType *MethodDescriptionTy;
464 /// MethodDescriptionListTy - LLVM type for struct
465 /// objc_method_description_list.
466 const llvm::StructType *MethodDescriptionListTy;
467 /// MethodDescriptionListPtrTy - LLVM type for struct
468 /// objc_method_description_list *.
469 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000470 /// ProtocolListTy - LLVM type for struct objc_property_list.
471 const llvm::Type *ProtocolListTy;
472 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
473 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000474 /// CategoryTy - LLVM type for struct objc_category.
475 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000476 /// ClassTy - LLVM type for struct objc_class.
477 const llvm::StructType *ClassTy;
478 /// ClassPtrTy - LLVM type for struct objc_class *.
479 const llvm::Type *ClassPtrTy;
480 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
481 const llvm::StructType *ClassExtensionTy;
482 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
483 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000484 // IvarTy - LLVM type for struct objc_ivar.
485 const llvm::StructType *IvarTy;
486 /// IvarListTy - LLVM type for struct objc_ivar_list.
487 const llvm::Type *IvarListTy;
488 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
489 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000490 /// MethodListTy - LLVM type for struct objc_method_list.
491 const llvm::Type *MethodListTy;
492 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
493 const llvm::Type *MethodListPtrTy;
Anders Carlsson124526b2008-09-09 10:10:21 +0000494
495 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
496 const llvm::Type *ExceptionDataTy;
497
Anders Carlsson124526b2008-09-09 10:10:21 +0000498 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000499 llvm::Constant *getExceptionTryEnterFn() {
500 std::vector<const llvm::Type*> Params;
501 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
502 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
503 Params, false),
504 "objc_exception_try_enter");
505 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000506
507 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000508 llvm::Constant *getExceptionTryExitFn() {
509 std::vector<const llvm::Type*> Params;
510 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
511 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
512 Params, false),
513 "objc_exception_try_exit");
514 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000515
516 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000517 llvm::Constant *getExceptionExtractFn() {
518 std::vector<const llvm::Type*> Params;
519 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
520 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
521 Params, false),
522 "objc_exception_extract");
523
524 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000525
526 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000527 llvm::Constant *getExceptionMatchFn() {
528 std::vector<const llvm::Type*> Params;
529 Params.push_back(ClassPtrTy);
530 Params.push_back(ObjectPtrTy);
531 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
532 Params, false),
533 "objc_exception_match");
534
535 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000536
537 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000538 llvm::Constant *getSetJmpFn() {
539 std::vector<const llvm::Type*> Params;
540 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
541 return
542 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
543 Params, false),
544 "_setjmp");
545
546 }
Chris Lattner10cac6f2008-11-15 21:26:17 +0000547
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000548public:
549 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000550 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000551};
552
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000553/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000554/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000555class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000556public:
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000557
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000558 // MethodListnfABITy - LLVM for struct _method_list_t
559 const llvm::StructType *MethodListnfABITy;
560
561 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
562 const llvm::Type *MethodListnfABIPtrTy;
563
564 // ProtocolnfABITy = LLVM for struct _protocol_t
565 const llvm::StructType *ProtocolnfABITy;
566
Daniel Dunbar948e2582009-02-15 07:36:20 +0000567 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
568 const llvm::Type *ProtocolnfABIPtrTy;
569
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000570 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
571 const llvm::StructType *ProtocolListnfABITy;
572
573 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
574 const llvm::Type *ProtocolListnfABIPtrTy;
575
576 // ClassnfABITy - LLVM for struct _class_t
577 const llvm::StructType *ClassnfABITy;
578
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000579 // ClassnfABIPtrTy - LLVM for struct _class_t*
580 const llvm::Type *ClassnfABIPtrTy;
581
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000582 // IvarnfABITy - LLVM for struct _ivar_t
583 const llvm::StructType *IvarnfABITy;
584
585 // IvarListnfABITy - LLVM for struct _ivar_list_t
586 const llvm::StructType *IvarListnfABITy;
587
588 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
589 const llvm::Type *IvarListnfABIPtrTy;
590
591 // ClassRonfABITy - LLVM for struct _class_ro_t
592 const llvm::StructType *ClassRonfABITy;
593
594 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
595 const llvm::Type *ImpnfABITy;
596
597 // CategorynfABITy - LLVM for struct _category_t
598 const llvm::StructType *CategorynfABITy;
599
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000600 // New types for nonfragile abi messaging.
601
602 // MessageRefTy - LLVM for:
603 // struct _message_ref_t {
604 // IMP messenger;
605 // SEL name;
606 // };
607 const llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000608 // MessageRefCTy - clang type for struct _message_ref_t
609 QualType MessageRefCTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000610
611 // MessageRefPtrTy - LLVM for struct _message_ref_t*
612 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000613 // MessageRefCPtrTy - clang type for struct _message_ref_t*
614 QualType MessageRefCPtrTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000615
Fariborz Jahanianef163782009-02-05 01:13:09 +0000616 // MessengerTy - Type of the messenger (shown as IMP above)
617 const llvm::FunctionType *MessengerTy;
618
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000619 // SuperMessageRefTy - LLVM for:
620 // struct _super_message_ref_t {
621 // SUPER_IMP messenger;
622 // SEL name;
623 // };
624 const llvm::StructType *SuperMessageRefTy;
625
626 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
627 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000628
Chris Lattner1c02f862009-04-22 02:53:24 +0000629 llvm::Constant *getMessageSendFixupFn() {
630 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
631 std::vector<const llvm::Type*> Params;
632 Params.push_back(ObjectPtrTy);
633 Params.push_back(MessageRefPtrTy);
634 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
635 Params, true),
636 "objc_msgSend_fixup");
637 }
638
639 llvm::Constant *getMessageSendFpretFixupFn() {
640 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
641 std::vector<const llvm::Type*> Params;
642 Params.push_back(ObjectPtrTy);
643 Params.push_back(MessageRefPtrTy);
644 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
645 Params, true),
646 "objc_msgSend_fpret_fixup");
647 }
648
649 llvm::Constant *getMessageSendStretFixupFn() {
650 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
651 std::vector<const llvm::Type*> Params;
652 Params.push_back(ObjectPtrTy);
653 Params.push_back(MessageRefPtrTy);
654 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
655 Params, true),
656 "objc_msgSend_stret_fixup");
657 }
658
659 llvm::Constant *getMessageSendIdFixupFn() {
660 // id objc_msgSendId_fixup(id, struct message_ref_t*, ...)
661 std::vector<const llvm::Type*> Params;
662 Params.push_back(ObjectPtrTy);
663 Params.push_back(MessageRefPtrTy);
664 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
665 Params, true),
666 "objc_msgSendId_fixup");
667 }
668
669 llvm::Constant *getMessageSendIdStretFixupFn() {
670 // id objc_msgSendId_stret_fixup(id, struct message_ref_t*, ...)
671 std::vector<const llvm::Type*> Params;
672 Params.push_back(ObjectPtrTy);
673 Params.push_back(MessageRefPtrTy);
674 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
675 Params, true),
676 "objc_msgSendId_stret_fixup");
677 }
678 llvm::Constant *getMessageSendSuper2FixupFn() {
679 // id objc_msgSendSuper2_fixup (struct objc_super *,
680 // struct _super_message_ref_t*, ...)
681 std::vector<const llvm::Type*> Params;
682 Params.push_back(SuperPtrTy);
683 Params.push_back(SuperMessageRefPtrTy);
684 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
685 Params, true),
686 "objc_msgSendSuper2_fixup");
687 }
688
689 llvm::Constant *getMessageSendSuper2StretFixupFn() {
690 // id objc_msgSendSuper2_stret_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);
695 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
696 Params, true),
697 "objc_msgSendSuper2_stret_fixup");
698 }
699
700
701
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000702 /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C
703 /// exception personality function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000704 llvm::Value *getEHPersonalityPtr() {
705 llvm::Constant *Personality =
706 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000707 true),
708 "__objc_personality_v0");
709 return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy);
710 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000711
Chris Lattner8a569112009-04-22 02:15:23 +0000712 llvm::Constant *getUnwindResumeOrRethrowFn() {
713 std::vector<const llvm::Type*> Params;
714 Params.push_back(Int8PtrTy);
715 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
716 Params, false),
717 "_Unwind_Resume_or_Rethrow");
718 }
719
720 llvm::Constant *getObjCEndCatchFn() {
Chris Lattner8a569112009-04-22 02:15:23 +0000721 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
Chris Lattnerb59761b2009-07-01 04:13:52 +0000722 false),
Chris Lattner8a569112009-04-22 02:15:23 +0000723 "objc_end_catch");
724
725 }
726
727 llvm::Constant *getObjCBeginCatchFn() {
728 std::vector<const llvm::Type*> Params;
729 Params.push_back(Int8PtrTy);
730 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
731 Params, false),
732 "objc_begin_catch");
733 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000734
735 const llvm::StructType *EHTypeTy;
736 const llvm::Type *EHTypePtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000737
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000738 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
739 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000740};
741
742class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000743public:
744 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000745 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000746 public:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000747 unsigned ivar_bytepos;
748 unsigned ivar_size;
749 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
750 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000751
752 // Allow sorting based on byte pos.
753 bool operator<(const GC_IVAR &b) const {
754 return ivar_bytepos < b.ivar_bytepos;
755 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000756 };
757
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000758 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000759 public:
760 unsigned skip;
761 unsigned scan;
762 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
763 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000764 };
765
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000766protected:
767 CodeGen::CodeGenModule &CGM;
Owen Anderson69243822009-07-13 04:10:07 +0000768 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000769 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000770 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000771
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000772 // gc ivar layout bitmap calculation helper caches.
773 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
774 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000775
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000776 /// LazySymbols - Symbols to generate a lazy reference for. See
777 /// DefinedSymbols and FinishModule().
778 std::set<IdentifierInfo*> LazySymbols;
779
780 /// DefinedSymbols - External symbols which are defined by this
781 /// module. The symbols in this list and LazySymbols are used to add
782 /// special linker symbols which ensure that Objective-C modules are
783 /// linked properly.
784 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000785
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000786 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000787 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000788
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000789 /// MethodVarNames - uniqued method variable names.
790 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000791
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000792 /// MethodVarTypes - uniqued method type signatures. We have to use
793 /// a StringMap here because have no other unique reference.
794 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000795
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000796 /// MethodDefinitions - map of methods which have been defined in
797 /// this translation unit.
798 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000799
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000800 /// PropertyNames - uniqued method variable names.
801 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000802
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000803 /// ClassReferences - uniqued class references.
804 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000805
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000806 /// SelectorReferences - uniqued selector references.
807 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000808
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000809 /// Protocols - Protocols for which an objc_protocol structure has
810 /// been emitted. Forward declarations are handled by creating an
811 /// empty structure whose initializer is filled in when/if defined.
812 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000813
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000814 /// DefinedProtocols - Protocols which have actually been
815 /// defined. We should not need this, see FIXME in GenerateProtocol.
816 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000817
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000818 /// DefinedClasses - List of defined classes.
819 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000820
821 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
822 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000823
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000824 /// DefinedCategories - List of defined categories.
825 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000826
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000827 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
828 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
829
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000830 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000831 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000832 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000833
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000834 /// GetNameForMethod - Return a name for the given method.
835 /// \param[out] NameOut - The return value.
836 void GetNameForMethod(const ObjCMethodDecl *OMD,
837 const ObjCContainerDecl *CD,
838 std::string &NameOut);
839
840 /// GetMethodVarName - Return a unique constant for the given
841 /// selector's name. The return value has type char *.
842 llvm::Constant *GetMethodVarName(Selector Sel);
843 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
844 llvm::Constant *GetMethodVarName(const std::string &Name);
845
846 /// GetMethodVarType - Return a unique constant for the given
847 /// selector's name. The return value has type char *.
848
849 // FIXME: This is a horrible name.
850 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000851 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000852
853 /// GetPropertyName - Return a unique constant for the given
854 /// name. The return value has type char *.
855 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
856
857 // FIXME: This can be dropped once string functions are unified.
858 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
859 const Decl *Container);
860
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000861 /// GetClassName - Return a unique constant for the given selector's
862 /// name. The return value has type char *.
863 llvm::Constant *GetClassName(IdentifierInfo *Ident);
864
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000865 /// BuildIvarLayout - Builds ivar layout bitmap for the class
866 /// implementation for the __strong or __weak case.
867 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000868 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
869 bool ForStrongLayout);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000870
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000871 void BuildAggrIvarRecordLayout(const RecordType *RT,
872 unsigned int BytePos, bool ForStrongLayout,
873 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000874 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000875 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000876 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +0000877 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000878 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000879 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000880
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000881 /// GetIvarLayoutName - Returns a unique constant for the given
882 /// ivar layout bitmap.
883 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
884 const ObjCCommonTypesHelper &ObjCTypes);
885
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000886 /// EmitPropertyList - Emit the given property list. The return
887 /// value has type PropertyListPtrTy.
888 llvm::Constant *EmitPropertyList(const std::string &Name,
889 const Decl *Container,
890 const ObjCContainerDecl *OCD,
891 const ObjCCommonTypesHelper &ObjCTypes);
892
Fariborz Jahanianda320092009-01-29 19:24:30 +0000893 /// GetProtocolRef - Return a reference to the internal protocol
894 /// description, creating an empty one if it has not been
895 /// defined. The return value has type ProtocolPtrTy.
896 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000897
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000898 /// CreateMetadataVar - Create a global variable with internal
899 /// linkage for use by the Objective-C runtime.
900 ///
901 /// This is a convenience wrapper which not only creates the
902 /// variable, but also sets the section and alignment and adds the
903 /// global to the UsedGlobals list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000904 ///
905 /// \param Name - The variable name.
906 /// \param Init - The variable initializer; this is also used to
907 /// define the type of the variable.
908 /// \param Section - The section the variable should go into, or 0.
909 /// \param Align - The alignment for the variable, or 0.
910 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000911 /// "llvm.used".
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000912 llvm::GlobalVariable *CreateMetadataVar(const std::string &Name,
913 llvm::Constant *Init,
914 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000915 unsigned Align,
916 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000917
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000918 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
919 QualType ResultType,
920 llvm::Value *Sel,
921 llvm::Value *Arg0,
922 QualType Arg0Ty,
923 bool IsSuper,
924 const CallArgList &CallArgs,
925 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000926
Fariborz Jahanianc38e9af2009-06-23 21:47:46 +0000927 virtual void MergeMetadataGlobals(std::vector<llvm::Constant*> &UsedArray);
928
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000929public:
Owen Anderson69243822009-07-13 04:10:07 +0000930 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
931 CGM(cgm), VMContext(cgm.getLLVMContext())
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000932 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000933
Steve Naroff33fdb732009-03-31 16:53:37 +0000934 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000935
936 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
937 const ObjCContainerDecl *CD=0);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000938
939 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
940
941 /// GetOrEmitProtocol - Get the protocol object for the given
942 /// declaration, emitting it if necessary. The return value has type
943 /// ProtocolPtrTy.
944 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
945
946 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
947 /// object for the given declaration, emitting it if needed. These
948 /// forward references will be filled in with empty bodies if no
949 /// definition is seen. The return value has type ProtocolPtrTy.
950 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000951};
952
953class CGObjCMac : public CGObjCCommonMac {
954private:
955 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000956 /// EmitImageInfo - Emit the image info marker used to encode some module
957 /// level information.
958 void EmitImageInfo();
959
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000960 /// EmitModuleInfo - Another marker encoding module level
961 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000962 void EmitModuleInfo();
963
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000964 /// EmitModuleSymols - Emit module symbols, the list of defined
965 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000966 llvm::Constant *EmitModuleSymbols();
967
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000968 /// FinishModule - Write out global data structures at the end of
969 /// processing a translation unit.
970 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000971
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000972 /// EmitClassExtension - Generate the class extension structure used
973 /// to store the weak ivar layout and properties. The return value
974 /// has type ClassExtensionPtrTy.
975 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
976
977 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
978 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000979 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000980 const ObjCInterfaceDecl *ID);
981
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000982 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000983 QualType ResultType,
984 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000985 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000986 QualType Arg0Ty,
987 bool IsSuper,
988 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000989
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000990 /// EmitIvarList - Emit the ivar list for the given
991 /// implementation. If ForClass is true the list of class ivars
992 /// (i.e. metaclass ivars) is emitted, otherwise the list of
993 /// interface ivars will be emitted. The return value has type
994 /// IvarListPtrTy.
995 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000996 bool ForClass);
997
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000998 /// EmitMetaClass - Emit a forward reference to the class structure
999 /// for the metaclass of the given interface. The return value has
1000 /// type ClassPtrTy.
1001 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1002
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001003 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001004 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001005 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1006 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001007 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001008
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001009 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001010
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001011 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001012
1013 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001014 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001015 llvm::Constant *EmitMethodList(const std::string &Name,
1016 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001017 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001018
1019 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001020 /// method declarations.
1021 /// - TypeName: The name for the type containing the methods.
1022 /// - IsProtocol: True iff these methods are for a protocol.
1023 /// - ClassMethds: True iff these are class methods.
1024 /// - Required: When true, only "required" methods are
1025 /// listed. Similarly, when false only "optional" methods are
1026 /// listed. For classes this should always be true.
1027 /// - begin, end: The method list to output.
1028 ///
1029 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001030 llvm::Constant *EmitMethodDescList(const std::string &Name,
1031 const char *Section,
1032 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001033
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001034 /// GetOrEmitProtocol - Get the protocol object for the given
1035 /// declaration, emitting it if necessary. The return value has type
1036 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001037 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001038
1039 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1040 /// object for the given declaration, emitting it if needed. These
1041 /// forward references will be filled in with empty bodies if no
1042 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001043 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001044
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001045 /// EmitProtocolExtension - Generate the protocol extension
1046 /// structure used to store optional instance and class methods, and
1047 /// protocol properties. The return value has type
1048 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001049 llvm::Constant *
1050 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1051 const ConstantVector &OptInstanceMethods,
1052 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001053
1054 /// EmitProtocolList - Generate the list of referenced
1055 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +00001056 llvm::Constant *EmitProtocolList(const std::string &Name,
1057 ObjCProtocolDecl::protocol_iterator begin,
1058 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001059
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001060 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1061 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001062 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001063
Fariborz Jahanianda320092009-01-29 19:24:30 +00001064 public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001065 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001066
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001067 virtual llvm::Function *ModuleInitFunction();
1068
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001069 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001070 QualType ResultType,
1071 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001072 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001073 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001074 const CallArgList &CallArgs,
1075 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001076
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001077 virtual CodeGen::RValue
1078 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001079 QualType ResultType,
1080 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001081 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001082 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001083 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001084 bool IsClassMessage,
1085 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001086
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001087 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001088 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001089
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001090 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001091
1092 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1093 /// untyped one.
1094 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1095 const ObjCMethodDecl *Method);
1096
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001097 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001098
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001099 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001100
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001101 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001102 const ObjCProtocolDecl *PD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00001103
Chris Lattner74391b42009-03-22 21:03:39 +00001104 virtual llvm::Constant *GetPropertyGetFunction();
1105 virtual llvm::Constant *GetPropertySetFunction();
1106 virtual llvm::Constant *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001107
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001108 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1109 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001110 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1111 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001112 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001113 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001114 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1115 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001116 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1117 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001118 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1119 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001120 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1121 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001122 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1123 llvm::Value *dest, llvm::Value *src,
1124 unsigned long size);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001125
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001126 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1127 QualType ObjectTy,
1128 llvm::Value *BaseValue,
1129 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001130 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001131 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001132 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001133 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001134};
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001135
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001136class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001137private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001138 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001139 llvm::GlobalVariable* ObjCEmptyCacheVar;
1140 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001141
Daniel Dunbar11394522009-04-18 08:51:00 +00001142 /// SuperClassReferences - uniqued super class references.
1143 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
1144
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001145 /// MetaClassReferences - uniqued meta class references.
1146 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001147
1148 /// EHTypeReferences - uniqued class ehtype references.
1149 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001150
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001151 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001152 /// legacy messaging dispatch.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001153 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001154
1155 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001156 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001157 bool LegacyDispatchedSelector(Selector Sel);
1158
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001159 /// FinishNonFragileABIModule - Write out global data structures at the end of
1160 /// processing a translation unit.
1161 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001162
Daniel Dunbar463b8762009-05-15 21:48:48 +00001163 /// AddModuleClassList - Add the given list of class pointers to the
1164 /// module with the provided symbol and section names.
1165 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1166 const char *SymbolName,
1167 const char *SectionName);
1168
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00001169 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1170 unsigned InstanceStart,
1171 unsigned InstanceSize,
1172 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001173 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
1174 llvm::Constant *IsAGV,
1175 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001176 llvm::Constant *ClassRoGV,
1177 bool HiddenVisibility);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001178
1179 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
1180
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001181 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
1182
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001183 /// EmitMethodList - Emit the method list for the given
1184 /// implementation. The return value has type MethodListnfABITy.
1185 llvm::Constant *EmitMethodList(const std::string &Name,
1186 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001187 const ConstantVector &Methods);
1188 /// EmitIvarList - Emit the ivar list for the given
1189 /// implementation. If ForClass is true the list of class ivars
1190 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1191 /// interface ivars will be emitted. The return value has type
1192 /// IvarListnfABIPtrTy.
1193 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00001194
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001195 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001196 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001197 unsigned long int offset);
1198
Fariborz Jahanianda320092009-01-29 19:24:30 +00001199 /// GetOrEmitProtocol - Get the protocol object for the given
1200 /// declaration, emitting it if necessary. The return value has type
1201 /// ProtocolPtrTy.
1202 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
1203
1204 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1205 /// object for the given declaration, emitting it if needed. These
1206 /// forward references will be filled in with empty bodies if no
1207 /// definition is seen. The return value has type ProtocolPtrTy.
1208 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
1209
1210 /// EmitProtocolList - Generate the list of referenced
1211 /// protocols. The return value has type ProtocolListPtrTy.
1212 llvm::Constant *EmitProtocolList(const std::string &Name,
1213 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001214 ObjCProtocolDecl::protocol_iterator end);
1215
1216 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1217 QualType ResultType,
1218 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00001219 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001220 QualType Arg0Ty,
1221 bool IsSuper,
1222 const CallArgList &CallArgs);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001223
1224 /// GetClassGlobal - Return the global variable for the Objective-C
1225 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001226 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
1227
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001228 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001229 /// for the given class reference.
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001230 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001231 const ObjCInterfaceDecl *ID);
1232
1233 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1234 /// for the given super class reference.
1235 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1236 const ObjCInterfaceDecl *ID);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001237
1238 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1239 /// meta-data
1240 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
1241 const ObjCInterfaceDecl *ID);
1242
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001243 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1244 /// the given ivar.
1245 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001246 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00001247 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001248 const ObjCIvarDecl *Ivar);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001249
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001250 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1251 /// for the given selector.
1252 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbare588b992009-03-01 04:46:24 +00001253
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001254 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001255 /// interface. The return value has type EHTypePtrTy.
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001256 llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
1257 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001258
1259 const char *getMetaclassSymbolPrefix() const {
1260 return "OBJC_METACLASS_$_";
1261 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001262
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001263 const char *getClassSymbolPrefix() const {
1264 return "OBJC_CLASS_$_";
1265 }
1266
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001267 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001268 uint32_t &InstanceStart,
1269 uint32_t &InstanceSize);
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001270
1271 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001272 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001273 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1274 return CGM.getContext().Selectors.getSelector(0, &II);
1275 }
1276
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001277 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001278 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1279 return CGM.getContext().Selectors.getSelector(1, &II);
1280 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001281
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001282 /// ImplementationIsNonLazy - Check whether the given category or
1283 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001284 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001285
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001286public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001287 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001288 // FIXME. All stubs for now!
1289 virtual llvm::Function *ModuleInitFunction();
1290
1291 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
1292 QualType ResultType,
1293 Selector Sel,
1294 llvm::Value *Receiver,
1295 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001296 const CallArgList &CallArgs,
1297 const ObjCMethodDecl *Method);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001298
1299 virtual CodeGen::RValue
1300 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
1301 QualType ResultType,
1302 Selector Sel,
1303 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001304 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001305 llvm::Value *Receiver,
1306 bool IsClassMessage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001307 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001308
1309 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001310 const ObjCInterfaceDecl *ID);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001311
1312 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001313 { return EmitSelector(Builder, Sel); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001314
1315 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1316 /// untyped one.
1317 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1318 const ObjCMethodDecl *Method)
1319 { return EmitSelector(Builder, Method->getSelector()); }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001320
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001321 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001322
1323 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001324 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001325 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001326
Chris Lattner74391b42009-03-22 21:03:39 +00001327 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001328 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001329 }
Chris Lattner74391b42009-03-22 21:03:39 +00001330 virtual llvm::Constant *GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001331 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001332 }
Chris Lattner74391b42009-03-22 21:03:39 +00001333 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001334 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001335 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001336
1337 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00001338 const Stmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001339 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001340 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001341 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001342 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001343 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001344 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001345 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001346 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001347 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001348 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001349 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001350 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001351 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1352 llvm::Value *dest, llvm::Value *src,
1353 unsigned long size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001354 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1355 QualType ObjectTy,
1356 llvm::Value *BaseValue,
1357 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001358 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001359 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001360 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001361 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001362};
1363
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001364} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001365
1366/* *** Helper Functions *** */
1367
1368/// getConstantGEP() - Help routine to construct simple GEPs.
1369static llvm::Constant *getConstantGEP(llvm::Constant *C,
1370 unsigned idx0,
1371 unsigned idx1) {
1372 llvm::Value *Idxs[] = {
1373 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
1374 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
1375 };
1376 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
1377}
1378
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001379/// hasObjCExceptionAttribute - Return true if this class or any super
1380/// class has the __objc_exception__ attribute.
Douglas Gregor68584ed2009-06-18 16:11:24 +00001381static bool hasObjCExceptionAttribute(ASTContext &Context,
1382 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001383 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001384 return true;
1385 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001386 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001387 return false;
1388}
1389
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001390/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001391
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001392CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
1393 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001394{
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001395 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001396 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001397}
1398
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001399/// GetClass - Return a reference to the class for the given interface
1400/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001401llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001402 const ObjCInterfaceDecl *ID) {
1403 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001404}
1405
1406/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001407llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001408 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001409}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001410llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
1411 *Method) {
1412 return EmitSelector(Builder, Method->getSelector());
1413}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001414
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001415/// Generate a constant CFString object.
1416/*
1417 struct __builtin_CFString {
1418 const int *isa; // point to __CFConstantStringClassReference
1419 int flags;
1420 const char *str;
1421 long length;
1422 };
1423*/
1424
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001425llvm::Constant *CGObjCCommonMac::GenerateConstantString(
Steve Naroff33fdb732009-03-31 16:53:37 +00001426 const ObjCStringLiteral *SL) {
Steve Naroff8d4141f2009-04-01 13:55:36 +00001427 return CGM.GetAddrOfConstantCFString(SL->getString());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001428}
1429
1430/// Generates a message send where the super is the receiver. This is
1431/// a message send to self with special delivery semantics indicating
1432/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001433CodeGen::RValue
1434CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001435 QualType ResultType,
1436 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001437 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001438 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001439 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001440 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001441 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001442 // Create and init a super structure; this is a (receiver, class)
1443 // pair we will pass to objc_msgSendSuper.
1444 llvm::Value *ObjCSuper =
1445 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
1446 llvm::Value *ReceiverAsObject =
1447 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
1448 CGF.Builder.CreateStore(ReceiverAsObject,
1449 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001450
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001451 // If this is a class message the metaclass is passed as the target.
1452 llvm::Value *Target;
1453 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001454 if (isCategoryImpl) {
1455 // Message sent to 'super' in a class method defined in a category
1456 // implementation requires an odd treatment.
1457 // If we are in a class method, we must retrieve the
1458 // _metaclass_ for the current class, pointed at by
1459 // the class's "isa" pointer. The following assumes that
1460 // isa" is the first ivar in a class (which it must be).
1461 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1462 Target = CGF.Builder.CreateStructGEP(Target, 0);
1463 Target = CGF.Builder.CreateLoad(Target);
1464 }
1465 else {
1466 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1467 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1468 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1469 Target = Super;
1470 }
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001471 } else {
1472 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1473 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001474 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1475 // ObjCTypes types.
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001476 const llvm::Type *ClassTy =
1477 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001478 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001479 CGF.Builder.CreateStore(Target,
1480 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001481 return EmitLegacyMessageSend(CGF, ResultType,
1482 EmitSelector(CGF.Builder, Sel),
1483 ObjCSuper, ObjCTypes.SuperPtrCTy,
1484 true, CallArgs, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001485}
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001486
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001487/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001488CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001489 QualType ResultType,
1490 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001491 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001492 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001493 const CallArgList &CallArgs,
1494 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001495 return EmitLegacyMessageSend(CGF, ResultType,
1496 EmitSelector(CGF.Builder, Sel),
1497 Receiver, CGF.getContext().getObjCIdType(),
1498 false, CallArgs, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001499}
1500
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001501CodeGen::RValue CGObjCCommonMac::EmitLegacyMessageSend(
1502 CodeGen::CodeGenFunction &CGF,
1503 QualType ResultType,
1504 llvm::Value *Sel,
1505 llvm::Value *Arg0,
1506 QualType Arg0Ty,
1507 bool IsSuper,
1508 const CallArgList &CallArgs,
1509 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001510 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001511 if (!IsSuper)
1512 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001513 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001514 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001515 CGF.getContext().getObjCSelType()));
1516 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001517
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001518 CodeGenTypes &Types = CGM.getTypes();
1519 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001520 // In 64bit ABI, type must be assumed VARARG. In 32bit abi,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001521 // it seems not to matter.
1522 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, (ObjCABI == 2));
1523
1524 llvm::Constant *Fn = NULL;
Daniel Dunbar88b53962009-02-02 22:03:45 +00001525 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001526 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
1527 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001528 } else if (ResultType->isFloatingType()) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001529 if (ObjCABI == 2) {
1530 if (const BuiltinType *BT = ResultType->getAsBuiltinType()) {
1531 BuiltinType::Kind k = BT->getKind();
1532 Fn = (k == BuiltinType::LongDouble) ? ObjCTypes.getSendFpretFn2(IsSuper)
1533 : ObjCTypes.getSendFn2(IsSuper);
Daniel Dunbar42f963d2009-06-10 04:38:50 +00001534 } else {
1535 Fn = ObjCTypes.getSendFn2(IsSuper);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001536 }
1537 }
1538 else
Mike Stumpf5408fe2009-05-16 07:57:57 +00001539 // FIXME. This currently matches gcc's API for x86-32. May need to change
1540 // for others if we have their API.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001541 Fn = ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001542 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001543 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
1544 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001545 }
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001546 assert(Fn && "EmitLegacyMessageSend - unknown API");
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +00001547 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +00001548 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001549}
1550
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001551llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001552 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001553 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001554 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001555 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1556
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001557 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
1558 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001559}
1560
Fariborz Jahanianda320092009-01-29 19:24:30 +00001561void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001562 // FIXME: We shouldn't need this, the protocol decl should contain enough
1563 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001564 DefinedProtocols.insert(PD->getIdentifier());
1565
1566 // If we have generated a forward reference to this protocol, emit
1567 // it now. Otherwise do nothing, the protocol objects are lazily
1568 // emitted.
1569 if (Protocols.count(PD->getIdentifier()))
1570 GetOrEmitProtocol(PD);
1571}
1572
Fariborz Jahanianda320092009-01-29 19:24:30 +00001573llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001574 if (DefinedProtocols.count(PD->getIdentifier()))
1575 return GetOrEmitProtocol(PD);
1576 return GetOrEmitProtocolRef(PD);
1577}
1578
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001579/*
1580 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1581 struct _objc_protocol {
1582 struct _objc_protocol_extension *isa;
1583 char *protocol_name;
1584 struct _objc_protocol_list *protocol_list;
1585 struct _objc__method_prototype_list *instance_methods;
1586 struct _objc__method_prototype_list *class_methods
1587 };
1588
1589 See EmitProtocolExtension().
1590*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001591llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1592 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1593
1594 // Early exit if a defining object has already been generated.
1595 if (Entry && Entry->hasInitializer())
1596 return Entry;
1597
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001598 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001599 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001600 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1601
Chris Lattner8ec03f52008-11-24 03:54:41 +00001602 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001603
1604 // Construct method lists.
1605 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1606 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00001607 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001608 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001609 ObjCMethodDecl *MD = *i;
1610 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1611 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1612 OptInstanceMethods.push_back(C);
1613 } else {
1614 InstanceMethods.push_back(C);
1615 }
1616 }
1617
Douglas Gregor6ab35242009-04-09 21:40:53 +00001618 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001619 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001620 ObjCMethodDecl *MD = *i;
1621 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1622 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1623 OptClassMethods.push_back(C);
1624 } else {
1625 ClassMethods.push_back(C);
1626 }
1627 }
1628
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001629 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001630 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001631 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001632 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001633 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001634 PD->protocol_begin(),
1635 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001636 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001637 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
1638 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001639 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1640 InstanceMethods);
1641 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001642 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
1643 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001644 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1645 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001646 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
1647 Values);
1648
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001649 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001650 // Already created, fix the linkage and update the initializer.
1651 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001652 Entry->setInitializer(Init);
1653 } else {
1654 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001655 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001656 llvm::GlobalValue::InternalLinkage,
1657 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00001658 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001659 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001660 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001661 UsedGlobals.push_back(Entry);
1662 // FIXME: Is this necessary? Why only for protocol?
1663 Entry->setAlignment(4);
1664 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001665
1666 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001667}
1668
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001669llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001670 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1671
1672 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001673 // We use the initializer as a marker of whether this is a forward
1674 // reference or not. At module finalization we add the empty
1675 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001676 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001677 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001678 llvm::GlobalValue::ExternalLinkage,
1679 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00001680 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001681 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001682 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001683 UsedGlobals.push_back(Entry);
1684 // FIXME: Is this necessary? Why only for protocol?
1685 Entry->setAlignment(4);
1686 }
1687
1688 return Entry;
1689}
1690
1691/*
1692 struct _objc_protocol_extension {
1693 uint32_t size;
1694 struct objc_method_description_list *optional_instance_methods;
1695 struct objc_method_description_list *optional_class_methods;
1696 struct objc_property_list *instance_properties;
1697 };
1698*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001699llvm::Constant *
1700CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1701 const ConstantVector &OptInstanceMethods,
1702 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001703 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00001704 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001705 std::vector<llvm::Constant*> Values(4);
1706 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001707 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001708 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1709 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001710 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1711 OptInstanceMethods);
1712 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001713 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1714 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001715 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1716 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001717 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1718 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001719 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001720
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001721 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001722 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1723 Values[3]->isNullValue())
Owen Anderson69243822009-07-13 04:10:07 +00001724 return VMContext.getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001725
1726 llvm::Constant *Init =
1727 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001728
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001729 // No special section, but goes in llvm.used
1730 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
1731 Init,
1732 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001733}
1734
1735/*
1736 struct objc_protocol_list {
1737 struct objc_protocol_list *next;
1738 long count;
1739 Protocol *list[];
1740 };
1741*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001742llvm::Constant *
1743CGObjCMac::EmitProtocolList(const std::string &Name,
1744 ObjCProtocolDecl::protocol_iterator begin,
1745 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001746 std::vector<llvm::Constant*> ProtocolRefs;
1747
Daniel Dunbardbc933702008-08-21 21:57:41 +00001748 for (; begin != end; ++begin)
1749 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001750
1751 // Just return null for empty protocol lists
1752 if (ProtocolRefs.empty())
Owen Anderson69243822009-07-13 04:10:07 +00001753 return VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001754
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001755 // This list is null terminated.
Owen Anderson69243822009-07-13 04:10:07 +00001756 ProtocolRefs.push_back(VMContext.getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001757
1758 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001759 // This field is only used by the runtime.
Owen Anderson69243822009-07-13 04:10:07 +00001760 Values[0] = VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001761 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1762 Values[2] =
1763 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1764 ProtocolRefs.size()),
1765 ProtocolRefs);
1766
1767 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1768 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001769 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001770 4, false);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001771 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1772}
1773
1774/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001775 struct _objc_property {
1776 const char * const name;
1777 const char * const attributes;
1778 };
1779
1780 struct _objc_property_list {
1781 uint32_t entsize; // sizeof (struct _objc_property)
1782 uint32_t prop_count;
1783 struct _objc_property[prop_count];
1784 };
1785*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001786llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1787 const Decl *Container,
1788 const ObjCContainerDecl *OCD,
1789 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001790 std::vector<llvm::Constant*> Properties, Prop(2);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001791 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1792 E = OCD->prop_end(); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00001793 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001794 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001795 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001796 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1797 Prop));
1798 }
1799
1800 // Return null for empty list.
1801 if (Properties.empty())
Owen Anderson69243822009-07-13 04:10:07 +00001802 return VMContext.getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001803
1804 unsigned PropertySize =
Duncan Sands9408c452009-05-09 07:08:47 +00001805 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001806 std::vector<llvm::Constant*> Values(3);
1807 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1808 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1809 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1810 Properties.size());
1811 Values[2] = llvm::ConstantArray::get(AT, Properties);
1812 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1813
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001814 llvm::GlobalVariable *GV =
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001815 CreateMetadataVar(Name, Init,
1816 (ObjCABI == 2) ? "__DATA, __objc_const" :
1817 "__OBJC,__property,regular,no_dead_strip",
1818 (ObjCABI == 2) ? 8 : 4,
1819 true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001820 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001821}
1822
1823/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001824 struct objc_method_description_list {
1825 int count;
1826 struct objc_method_description list[];
1827 };
1828*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001829llvm::Constant *
1830CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1831 std::vector<llvm::Constant*> Desc(2);
1832 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1833 ObjCTypes.SelectorPtrTy);
1834 Desc[1] = GetMethodVarType(MD);
1835 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1836 Desc);
1837}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001838
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001839llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1840 const char *Section,
1841 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001842 // Return null for empty list.
1843 if (Methods.empty())
Owen Anderson69243822009-07-13 04:10:07 +00001844 return VMContext.getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001845
1846 std::vector<llvm::Constant*> Values(2);
1847 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1848 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1849 Methods.size());
1850 Values[1] = llvm::ConstantArray::get(AT, Methods);
1851 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1852
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001853 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001854 return llvm::ConstantExpr::getBitCast(GV,
1855 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001856}
1857
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001858/*
1859 struct _objc_category {
1860 char *category_name;
1861 char *class_name;
1862 struct _objc_method_list *instance_methods;
1863 struct _objc_method_list *class_methods;
1864 struct _objc_protocol_list *protocols;
1865 uint32_t size; // <rdar://4585769>
1866 struct _objc_property_list *instance_properties;
1867 };
1868 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001869void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sands9408c452009-05-09 07:08:47 +00001870 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001871
Mike Stumpf5408fe2009-05-16 07:57:57 +00001872 // FIXME: This is poor design, the OCD should have a pointer to the category
1873 // decl. Additionally, note that Category can be null for the @implementation
1874 // w/o an @interface case. Sema should just create one for us as it does for
1875 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001876 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001877 const ObjCCategoryDecl *Category =
1878 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001879 std::string ExtName(Interface->getNameAsString() + "_" +
1880 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001881
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001882 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001883 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001884 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001885 // Instance methods should always be defined.
1886 InstanceMethods.push_back(GetMethodConstant(*i));
1887 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001888 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001889 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001890 // Class methods should always be defined.
1891 ClassMethods.push_back(GetMethodConstant(*i));
1892 }
1893
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001894 std::vector<llvm::Constant*> Values(7);
1895 Values[0] = GetClassName(OCD->getIdentifier());
1896 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00001897 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001898 Values[2] =
1899 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1900 ExtName,
1901 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001902 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001903 Values[3] =
1904 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001905 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001906 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001907 if (Category) {
1908 Values[4] =
1909 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1910 Category->protocol_begin(),
1911 Category->protocol_end());
1912 } else {
Owen Anderson69243822009-07-13 04:10:07 +00001913 Values[4] = VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001914 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001915 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001916
1917 // If there is no category @interface then there can be no properties.
1918 if (Category) {
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001919 Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001920 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001921 } else {
Owen Anderson69243822009-07-13 04:10:07 +00001922 Values[6] = VMContext.getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001923 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001924
1925 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1926 Values);
1927
1928 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001929 CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init,
1930 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001931 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001932 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001933}
1934
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001935// FIXME: Get from somewhere?
1936enum ClassFlags {
1937 eClassFlags_Factory = 0x00001,
1938 eClassFlags_Meta = 0x00002,
1939 // <rdr://5142207>
1940 eClassFlags_HasCXXStructors = 0x02000,
1941 eClassFlags_Hidden = 0x20000,
1942 eClassFlags_ABI2_Hidden = 0x00010,
1943 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1944};
1945
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001946/*
1947 struct _objc_class {
1948 Class isa;
1949 Class super_class;
1950 const char *name;
1951 long version;
1952 long info;
1953 long instance_size;
1954 struct _objc_ivar_list *ivars;
1955 struct _objc_method_list *methods;
1956 struct _objc_cache *cache;
1957 struct _objc_protocol_list *protocols;
1958 // Objective-C 1.0 extensions (<rdr://4585769>)
1959 const char *ivar_layout;
1960 struct _objc_class_ext *ext;
1961 };
1962
1963 See EmitClassExtension();
1964 */
1965void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001966 DefinedSymbols.insert(ID->getIdentifier());
1967
Chris Lattner8ec03f52008-11-24 03:54:41 +00001968 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001969 // FIXME: Gross
1970 ObjCInterfaceDecl *Interface =
1971 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001972 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001973 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001974 Interface->protocol_begin(),
1975 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001976 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar2bebbf02009-05-03 10:46:44 +00001977 unsigned Size =
1978 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001979
1980 // FIXME: Set CXX-structors flag.
Daniel Dunbar04d40782009-04-14 06:00:08 +00001981 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001982 Flags |= eClassFlags_Hidden;
1983
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001984 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001985 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001986 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001987 // Instance methods should always be defined.
1988 InstanceMethods.push_back(GetMethodConstant(*i));
1989 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001990 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001991 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001992 // Class methods should always be defined.
1993 ClassMethods.push_back(GetMethodConstant(*i));
1994 }
1995
Douglas Gregor653f1b12009-04-23 01:02:12 +00001996 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001997 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001998 ObjCPropertyImplDecl *PID = *i;
1999
2000 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2001 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2002
2003 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2004 if (llvm::Constant *C = GetMethodConstant(MD))
2005 InstanceMethods.push_back(C);
2006 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2007 if (llvm::Constant *C = GetMethodConstant(MD))
2008 InstanceMethods.push_back(C);
2009 }
2010 }
2011
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002012 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar5384b092009-05-03 08:56:52 +00002013 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002014 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002015 // Record a reference to the super class.
2016 LazySymbols.insert(Super->getIdentifier());
2017
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002018 Values[ 1] =
2019 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
2020 ObjCTypes.ClassPtrTy);
2021 } else {
Owen Anderson69243822009-07-13 04:10:07 +00002022 Values[ 1] = VMContext.getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002023 }
2024 Values[ 2] = GetClassName(ID->getIdentifier());
2025 // Version is always 0.
2026 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2027 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2028 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002029 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002030 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002031 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002032 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002033 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002034 // cache is always NULL.
Owen Anderson69243822009-07-13 04:10:07 +00002035 Values[ 8] = VMContext.getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002036 Values[ 9] = Protocols;
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002037 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002038 Values[11] = EmitClassExtension(ID);
2039 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
2040 Values);
2041
2042 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002043 CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init,
2044 "__OBJC,__class,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002045 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002046 DefinedClasses.push_back(GV);
2047}
2048
2049llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2050 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002051 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002052 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002053 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002054
Daniel Dunbar04d40782009-04-14 06:00:08 +00002055 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002056 Flags |= eClassFlags_Hidden;
2057
2058 std::vector<llvm::Constant*> Values(12);
2059 // The isa for the metaclass is the root of the hierarchy.
2060 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2061 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2062 Root = Super;
2063 Values[ 0] =
2064 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
2065 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002066 // The super class for the metaclass is emitted as the name of the
2067 // super class. The runtime fixes this up to point to the
2068 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002069 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
2070 Values[ 1] =
2071 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
2072 ObjCTypes.ClassPtrTy);
2073 } else {
Owen Anderson69243822009-07-13 04:10:07 +00002074 Values[ 1] = VMContext.getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002075 }
2076 Values[ 2] = GetClassName(ID->getIdentifier());
2077 // Version is always 0.
2078 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2079 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2080 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002081 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002082 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002083 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002084 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002085 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002086 // cache is always NULL.
Owen Anderson69243822009-07-13 04:10:07 +00002087 Values[ 8] = VMContext.getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002088 Values[ 9] = Protocols;
2089 // ivar_layout for metaclass is always NULL.
Owen Anderson69243822009-07-13 04:10:07 +00002090 Values[10] = VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002091 // The class extension is always unused for metaclasses.
Owen Anderson69243822009-07-13 04:10:07 +00002092 Values[11] = VMContext.getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002093 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
2094 Values);
2095
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002096 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002097 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002098
2099 // Check for a forward reference.
2100 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2101 if (GV) {
2102 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2103 "Forward metaclass reference has incorrect type.");
2104 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2105 GV->setInitializer(Init);
2106 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002107 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002108 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002109 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002110 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002111 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002112 GV->setAlignment(4);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002113 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002114
2115 return GV;
2116}
2117
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002118llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002119 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002120
Mike Stumpf5408fe2009-05-16 07:57:57 +00002121 // FIXME: Should we look these up somewhere other than the module. Its a bit
2122 // silly since we only generate these while processing an implementation, so
2123 // exactly one pointer would work if know when we entered/exitted an
2124 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002125
2126 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002127 // Previously, metaclass with internal linkage may have been defined.
2128 // pass 'true' as 2nd argument so it is returned.
2129 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002130 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2131 "Forward metaclass reference has incorrect type.");
2132 return GV;
2133 } else {
2134 // Generate as an external reference to keep a consistent
2135 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002136 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002137 llvm::GlobalValue::ExternalLinkage,
2138 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002139 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002140 }
2141}
2142
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002143/*
2144 struct objc_class_ext {
2145 uint32_t size;
2146 const char *weak_ivar_layout;
2147 struct _objc_property_list *properties;
2148 };
2149*/
2150llvm::Constant *
2151CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
2152 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002153 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002154
2155 std::vector<llvm::Constant*> Values(3);
2156 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002157 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002158 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002159 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002160
2161 // Return null if no extension bits are used.
2162 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson69243822009-07-13 04:10:07 +00002163 return VMContext.getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002164
2165 llvm::Constant *Init =
2166 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002167 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002168 Init, "__OBJC,__class_ext,regular,no_dead_strip",
2169 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002170}
2171
2172/*
2173 struct objc_ivar {
2174 char *ivar_name;
2175 char *ivar_type;
2176 int ivar_offset;
2177 };
2178
2179 struct objc_ivar_list {
2180 int ivar_count;
2181 struct objc_ivar list[count];
2182 };
2183 */
2184llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002185 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002186 std::vector<llvm::Constant*> Ivars, Ivar(3);
2187
2188 // When emitting the root class GCC emits ivar entries for the
2189 // actual class structure. It is not clear if we need to follow this
2190 // behavior; for now lets try and get away with not doing it. If so,
2191 // the cleanest solution would be to make up an ObjCInterfaceDecl
2192 // for the class.
2193 if (ForClass)
Owen Anderson69243822009-07-13 04:10:07 +00002194 return VMContext.getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002195
2196 ObjCInterfaceDecl *OID =
2197 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002198
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002199 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002200 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002201
2202 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2203 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002204 // Ignore unnamed bit-fields.
2205 if (!IVD->getDeclName())
2206 continue;
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002207 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2208 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00002209 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002210 ComputeIvarBaseOffset(CGM, OID, IVD));
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002211 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002212 }
2213
2214 // Return null for empty list.
2215 if (Ivars.empty())
Owen Anderson69243822009-07-13 04:10:07 +00002216 return VMContext.getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002217
2218 std::vector<llvm::Constant*> Values(2);
2219 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
2220 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
2221 Ivars.size());
2222 Values[1] = llvm::ConstantArray::get(AT, Ivars);
2223 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2224
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002225 llvm::GlobalVariable *GV;
2226 if (ForClass)
2227 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(),
Daniel Dunbar58a29122009-03-09 22:18:41 +00002228 Init, "__OBJC,__class_vars,regular,no_dead_strip",
2229 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002230 else
2231 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_"
2232 + ID->getNameAsString(),
2233 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002234 4, true);
2235 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002236}
2237
2238/*
2239 struct objc_method {
2240 SEL method_name;
2241 char *method_types;
2242 void *method;
2243 };
2244
2245 struct objc_method_list {
2246 struct objc_method_list *obsolete;
2247 int count;
2248 struct objc_method methods_list[count];
2249 };
2250*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002251
2252/// GetMethodConstant - Return a struct objc_method constant for the
2253/// given method if it has been defined. The result is null if the
2254/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002255llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002256 // FIXME: Use DenseMap::lookup
2257 llvm::Function *Fn = MethodDefinitions[MD];
2258 if (!Fn)
2259 return 0;
2260
2261 std::vector<llvm::Constant*> Method(3);
2262 Method[0] =
2263 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2264 ObjCTypes.SelectorPtrTy);
2265 Method[1] = GetMethodVarType(MD);
2266 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
2267 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
2268}
2269
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002270llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
2271 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002272 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002273 // Return null for empty list.
2274 if (Methods.empty())
Owen Anderson69243822009-07-13 04:10:07 +00002275 return VMContext.getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002276
2277 std::vector<llvm::Constant*> Values(3);
Owen Anderson69243822009-07-13 04:10:07 +00002278 Values[0] = VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002279 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
2280 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
2281 Methods.size());
2282 Values[2] = llvm::ConstantArray::get(AT, Methods);
2283 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2284
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002285 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002286 return llvm::ConstantExpr::getBitCast(GV,
2287 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002288}
2289
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002290llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00002291 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002292 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002293 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002294
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002295 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002296 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002297 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002298 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002299 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002300 llvm::GlobalValue::InternalLinkage,
2301 Name,
2302 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002303 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002304
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002305 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002306}
2307
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002308llvm::GlobalVariable *
2309CGObjCCommonMac::CreateMetadataVar(const std::string &Name,
2310 llvm::Constant *Init,
2311 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002312 unsigned Align,
2313 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002314 const llvm::Type *Ty = Init->getType();
2315 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002316 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002317 llvm::GlobalValue::InternalLinkage,
2318 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00002319 Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002320 if (Section)
2321 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002322 if (Align)
2323 GV->setAlignment(Align);
2324 if (AddToUsed)
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002325 UsedGlobals.push_back(GV);
2326 return GV;
2327}
2328
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002329llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002330 // Abuse this interface function as a place to finalize.
2331 FinishModule();
2332
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002333 return NULL;
2334}
2335
Chris Lattner74391b42009-03-22 21:03:39 +00002336llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002337 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002338}
2339
Chris Lattner74391b42009-03-22 21:03:39 +00002340llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002341 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002342}
2343
Chris Lattner74391b42009-03-22 21:03:39 +00002344llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002345 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002346}
2347
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002348/*
2349
2350Objective-C setjmp-longjmp (sjlj) Exception Handling
2351--
2352
2353The basic framework for a @try-catch-finally is as follows:
2354{
2355 objc_exception_data d;
2356 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002357 bool _call_try_exit = true;
2358
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002359 objc_exception_try_enter(&d);
2360 if (!setjmp(d.jmp_buf)) {
2361 ... try body ...
2362 } else {
2363 // exception path
2364 id _caught = objc_exception_extract(&d);
2365
2366 // enter new try scope for handlers
2367 if (!setjmp(d.jmp_buf)) {
2368 ... match exception and execute catch blocks ...
2369
2370 // fell off end, rethrow.
2371 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00002372 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002373 } else {
2374 // exception in catch block
2375 _rethrow = objc_exception_extract(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002376 _call_try_exit = false;
2377 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002378 }
2379 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002380 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002381
2382finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002383 if (_call_try_exit)
2384 objc_exception_try_exit(&d);
2385
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002386 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002387 ... dispatch to finally destination ...
2388
2389finally_rethrow:
2390 objc_exception_throw(_rethrow);
2391
2392finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002393}
2394
2395This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00002396uses _rethrow to determine if objc_exception_try_exit should be called
2397and if the object should be rethrown. This breaks in the face of
2398throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002399
2400We specialize this framework for a few particular circumstances:
2401
2402 - If there are no catch blocks, then we avoid emitting the second
2403 exception handling context.
2404
2405 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2406 e)) we avoid emitting the code to rethrow an uncaught exception.
2407
2408 - FIXME: If there is no @finally block we can do a few more
2409 simplifications.
2410
2411Rethrows and Jumps-Through-Finally
2412--
2413
2414Support for implicit rethrows and jumping through the finally block is
2415handled by storing the current exception-handling context in
2416ObjCEHStack.
2417
Daniel Dunbar898d5082008-09-30 01:06:03 +00002418In order to implement proper @finally semantics, we support one basic
2419mechanism for jumping through the finally block to an arbitrary
2420destination. Constructs which generate exits from a @try or @catch
2421block use this mechanism to implement the proper semantics by chaining
2422jumps, as necessary.
2423
2424This mechanism works like the one used for indirect goto: we
2425arbitrarily assign an ID to each destination and store the ID for the
2426destination in a variable prior to entering the finally block. At the
2427end of the finally block we simply create a switch to the proper
2428destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002429
2430Code gen for @synchronized(expr) stmt;
2431Effectively generating code for:
2432objc_sync_enter(expr);
2433@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002434*/
2435
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002436void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2437 const Stmt &S) {
2438 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002439 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00002440 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002441 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar55e87422008-11-11 02:29:29 +00002442 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
2443 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
2444 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar1c566672009-02-24 01:43:46 +00002445
2446 // For @synchronized, call objc_sync_enter(sync.expr). The
2447 // evaluation of the expression must occur before we enter the
2448 // @synchronized. We can safely avoid a temp here because jumps into
2449 // @synchronized are illegal & this will dominate uses.
2450 llvm::Value *SyncArg = 0;
2451 if (!isTry) {
2452 SyncArg =
2453 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2454 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00002455 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002456 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002457
2458 // Push an EH context entry, used for handling rethrows and jumps
2459 // through finally.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002460 CGF.PushCleanupBlock(FinallyBlock);
2461
Anders Carlsson273558f2009-02-07 21:37:21 +00002462 CGF.ObjCEHValueStack.push_back(0);
2463
Daniel Dunbar898d5082008-09-30 01:06:03 +00002464 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00002465 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2466 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002467 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
2468 "_rethrow");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002469 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
2470 "_call_try_exit");
2471 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr);
2472
Anders Carlsson80f25672008-09-09 17:59:25 +00002473 // Enter a new try block and call setjmp.
Chris Lattner34b02a12009-04-22 02:26:14 +00002474 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Anders Carlsson80f25672008-09-09 17:59:25 +00002475 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
2476 "jmpbufarray");
2477 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
Chris Lattner34b02a12009-04-22 02:26:14 +00002478 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlsson80f25672008-09-09 17:59:25 +00002479 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00002480
Daniel Dunbar55e87422008-11-11 02:29:29 +00002481 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2482 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002483 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002484 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002485
2486 // Emit the @try block.
2487 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002488 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
2489 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002490 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002491
2492 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002493 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002494
2495 // Retrieve the exception object. We may emit multiple blocks but
2496 // nothing can cross this so the value is already in SSA form.
Chris Lattner34b02a12009-04-22 02:26:14 +00002497 llvm::Value *Caught =
2498 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2499 ExceptionData, "caught");
Anders Carlsson273558f2009-02-07 21:37:21 +00002500 CGF.ObjCEHValueStack.back() = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002501 if (!isTry)
2502 {
2503 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002504 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002505 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002506 }
2507 else if (const ObjCAtCatchStmt* CatchStmt =
2508 cast<ObjCAtTryStmt>(S).getCatchStmts())
2509 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00002510 // Enter a new exception try block (in case a @catch block throws
2511 // an exception).
Chris Lattner34b02a12009-04-22 02:26:14 +00002512 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002513
Chris Lattner34b02a12009-04-22 02:26:14 +00002514 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlsson80f25672008-09-09 17:59:25 +00002515 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002516 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00002517
Daniel Dunbar55e87422008-11-11 02:29:29 +00002518 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
2519 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002520 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002521
2522 CGF.EmitBlock(CatchBlock);
2523
Daniel Dunbar55e40722008-09-27 07:03:52 +00002524 // Handle catch list. As a special case we check if everything is
2525 // matched and avoid generating code for falling off the end if
2526 // so.
2527 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00002528 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00002529 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00002530
Steve Naroff7ba138a2009-03-03 19:52:17 +00002531 const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00002532 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00002533
Anders Carlsson80f25672008-09-09 17:59:25 +00002534 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00002535 if (!CatchParam) {
2536 AllMatched = true;
2537 } else {
Steve Naroff14108da2009-07-10 23:34:53 +00002538 OPT = CatchParam->getType()->getAsObjCObjectPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002539
Daniel Dunbar97f61d12008-09-27 22:21:14 +00002540 // catch(id e) always matches.
2541 // FIXME: For the time being we also match id<X>; this should
2542 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00002543 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00002544 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00002545 }
2546
Daniel Dunbar55e40722008-09-27 07:03:52 +00002547 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00002548 if (CatchParam) {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002549 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002550 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002551 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002552 }
Anders Carlsson1452f552008-09-11 08:21:54 +00002553
Anders Carlssondde0a942008-09-11 09:15:33 +00002554 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002555 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002556 break;
2557 }
2558
Steve Naroff14108da2009-07-10 23:34:53 +00002559 assert(OPT && "Unexpected non-object pointer type in @catch");
2560 QualType T = OPT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00002561 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002562 assert(ObjCType && "Catch parameter must have Objective-C type!");
2563
2564 // Check if the @catch block matches the exception object.
2565 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
2566
Chris Lattner34b02a12009-04-22 02:26:14 +00002567 llvm::Value *Match =
2568 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
2569 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00002570
Daniel Dunbar55e87422008-11-11 02:29:29 +00002571 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00002572
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002573 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002574 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002575
2576 // Emit the @catch block.
2577 CGF.EmitBlock(MatchedBlock);
Steve Naroff7ba138a2009-03-03 19:52:17 +00002578 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002579 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002580
2581 llvm::Value *Tmp =
Steve Naroff7ba138a2009-03-03 19:52:17 +00002582 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002583 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002584 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002585
2586 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002587 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002588
2589 CGF.EmitBlock(NextCatchBlock);
2590 }
2591
Daniel Dunbar55e40722008-09-27 07:03:52 +00002592 if (!AllMatched) {
2593 // None of the handlers caught the exception, so store it to be
2594 // rethrown at the end of the @finally block.
2595 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002596 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002597 }
2598
2599 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002600 CGF.EmitBlock(CatchHandler);
Chris Lattner34b02a12009-04-22 02:26:14 +00002601 CGF.Builder.CreateStore(
2602 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2603 ExceptionData),
Daniel Dunbar55e40722008-09-27 07:03:52 +00002604 RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002605 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002606 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002607 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00002608 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002609 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002610 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson80f25672008-09-09 17:59:25 +00002611 }
2612
Daniel Dunbar898d5082008-09-30 01:06:03 +00002613 // Pop the exception-handling stack entry. It is important to do
2614 // this now, because the code in the @finally block is not in this
2615 // context.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002616 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2617
Anders Carlsson273558f2009-02-07 21:37:21 +00002618 CGF.ObjCEHValueStack.pop_back();
2619
Anders Carlsson80f25672008-09-09 17:59:25 +00002620 // Emit the @finally block.
2621 CGF.EmitBlock(FinallyBlock);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002622 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2623
2624 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2625
2626 CGF.EmitBlock(FinallyExit);
Chris Lattner34b02a12009-04-22 02:26:14 +00002627 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002628
2629 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002630 if (isTry) {
2631 if (const ObjCAtFinallyStmt* FinallyStmt =
2632 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2633 CGF.EmitStmt(FinallyStmt->getFinallyBody());
Daniel Dunbar1c566672009-02-24 01:43:46 +00002634 } else {
2635 // Emit objc_sync_exit(expr); as finally's sole statement for
2636 // @synchronized.
Chris Lattnerbbccd612009-04-22 02:38:11 +00002637 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002638 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002639
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002640 // Emit the switch block
2641 if (Info.SwitchBlock)
2642 CGF.EmitBlock(Info.SwitchBlock);
2643 if (Info.EndBlock)
2644 CGF.EmitBlock(Info.EndBlock);
2645
Daniel Dunbar898d5082008-09-30 01:06:03 +00002646 CGF.EmitBlock(FinallyRethrow);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002647 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbar898d5082008-09-30 01:06:03 +00002648 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002649 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00002650
2651 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002652}
2653
2654void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002655 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002656 llvm::Value *ExceptionAsObject;
2657
2658 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2659 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2660 ExceptionAsObject =
2661 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2662 } else {
Anders Carlsson273558f2009-02-07 21:37:21 +00002663 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002664 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00002665 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002666 }
2667
Chris Lattnerbbccd612009-04-22 02:38:11 +00002668 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002669 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002670
2671 // Clear the insertion point to indicate we are in unreachable code.
2672 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002673}
2674
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002675/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002676/// object: objc_read_weak (id *src)
2677///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002678llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002679 llvm::Value *AddrWeakObj)
2680{
Eli Friedman8339b352009-03-07 03:57:15 +00002681 const llvm::Type* DestTy =
2682 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002683 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00002684 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002685 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00002686 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002687 return read_weak;
2688}
2689
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002690/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2691/// objc_assign_weak (id src, id *dst)
2692///
2693void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2694 llvm::Value *src, llvm::Value *dst)
2695{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002696 const llvm::Type * SrcTy = src->getType();
2697 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002698 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002699 assert(Size <= 8 && "does not support size > 8");
2700 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2701 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002702 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2703 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002704 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2705 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00002706 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002707 src, dst, "weakassign");
2708 return;
2709}
2710
Fariborz Jahanian58626502008-11-19 00:59:10 +00002711/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2712/// objc_assign_global (id src, id *dst)
2713///
2714void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2715 llvm::Value *src, llvm::Value *dst)
2716{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002717 const llvm::Type * SrcTy = src->getType();
2718 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002719 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002720 assert(Size <= 8 && "does not support size > 8");
2721 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2722 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002723 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2724 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002725 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2726 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002727 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00002728 src, dst, "globalassign");
2729 return;
2730}
2731
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002732/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2733/// objc_assign_ivar (id src, id *dst)
2734///
2735void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2736 llvm::Value *src, llvm::Value *dst)
2737{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002738 const llvm::Type * SrcTy = src->getType();
2739 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002740 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002741 assert(Size <= 8 && "does not support size > 8");
2742 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2743 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002744 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2745 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002746 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2747 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002748 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(),
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002749 src, dst, "assignivar");
2750 return;
2751}
2752
Fariborz Jahanian58626502008-11-19 00:59:10 +00002753/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2754/// objc_assign_strongCast (id src, id *dst)
2755///
2756void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2757 llvm::Value *src, llvm::Value *dst)
2758{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002759 const llvm::Type * SrcTy = src->getType();
2760 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002761 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002762 assert(Size <= 8 && "does not support size > 8");
2763 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2764 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002765 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2766 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002767 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2768 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002769 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00002770 src, dst, "weakassign");
2771 return;
2772}
2773
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002774void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
2775 llvm::Value *DestPtr,
2776 llvm::Value *SrcPtr,
2777 unsigned long size) {
2778 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
2779 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
2780 llvm::Value *N = llvm::ConstantInt::get(ObjCTypes.LongTy, size);
2781 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
2782 DestPtr, SrcPtr, N);
2783 return;
2784}
2785
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002786/// EmitObjCValueForIvar - Code Gen for ivar reference.
2787///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002788LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2789 QualType ObjectTy,
2790 llvm::Value *BaseValue,
2791 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002792 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00002793 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00002794 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
2795 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002796}
2797
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002798llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00002799 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002800 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00002801 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002802 return llvm::ConstantInt::get(
2803 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2804 Offset);
2805}
2806
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002807/* *** Private Interface *** */
2808
2809/// EmitImageInfo - Emit the image info marker used to encode some module
2810/// level information.
2811///
2812/// See: <rdr://4810609&4810587&4810587>
2813/// struct IMAGE_INFO {
2814/// unsigned version;
2815/// unsigned flags;
2816/// };
2817enum ImageInfoFlags {
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002818 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what
2819 // this implies.
2820 eImageInfo_GarbageCollected = (1 << 1),
2821 eImageInfo_GCOnly = (1 << 2),
2822 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
2823
2824 // A flag indicating that the module has no instances of an
2825 // @synthesize of a superclass variable. <rdar://problem/6803242>
2826 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002827};
2828
2829void CGObjCMac::EmitImageInfo() {
2830 unsigned version = 0; // Version is unused?
2831 unsigned flags = 0;
2832
2833 // FIXME: Fix and continue?
2834 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2835 flags |= eImageInfo_GarbageCollected;
2836 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2837 flags |= eImageInfo_GCOnly;
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002838
2839 // We never allow @synthesize of a superclass property.
2840 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002841
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002842 // Emitted as int[2];
2843 llvm::Constant *values[2] = {
2844 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2845 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2846 };
2847 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002848
2849 const char *Section;
2850 if (ObjCABI == 1)
2851 Section = "__OBJC, __image_info,regular";
2852 else
2853 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002854 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002855 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
2856 llvm::ConstantArray::get(AT, values, 2),
2857 Section,
2858 0,
2859 true);
2860 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002861}
2862
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002863
2864// struct objc_module {
2865// unsigned long version;
2866// unsigned long size;
2867// const char *name;
2868// Symtab symtab;
2869// };
2870
2871// FIXME: Get from somewhere
2872static const int ModuleVersion = 7;
2873
2874void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00002875 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002876
2877 std::vector<llvm::Constant*> Values(4);
2878 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2879 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002880 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002881 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002882 Values[3] = EmitModuleSymbols();
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002883 CreateMetadataVar("\01L_OBJC_MODULES",
2884 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
2885 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002886 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002887}
2888
2889llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002890 unsigned NumClasses = DefinedClasses.size();
2891 unsigned NumCategories = DefinedCategories.size();
2892
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002893 // Return null if no symbols were defined.
2894 if (!NumClasses && !NumCategories)
Owen Anderson69243822009-07-13 04:10:07 +00002895 return VMContext.getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002896
2897 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002898 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson69243822009-07-13 04:10:07 +00002899 Values[1] = VMContext.getNullValue(ObjCTypes.SelectorPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002900 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2901 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2902
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002903 // The runtime expects exactly the list of defined classes followed
2904 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002905 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002906 for (unsigned i=0; i<NumClasses; i++)
2907 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2908 ObjCTypes.Int8PtrTy);
2909 for (unsigned i=0; i<NumCategories; i++)
2910 Symbols[NumClasses + i] =
2911 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2912 ObjCTypes.Int8PtrTy);
2913
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002914 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002915 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002916 NumClasses + NumCategories),
2917 Symbols);
2918
2919 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2920
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002921 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002922 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
2923 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002924 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002925 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2926}
2927
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002928llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002929 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002930 LazySymbols.insert(ID->getIdentifier());
2931
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002932 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2933
2934 if (!Entry) {
2935 llvm::Constant *Casted =
2936 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2937 ObjCTypes.ClassPtrTy);
2938 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002939 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
2940 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002941 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002942 }
2943
2944 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002945}
2946
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002947llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002948 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2949
2950 if (!Entry) {
2951 llvm::Constant *Casted =
2952 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2953 ObjCTypes.SelectorPtrTy);
2954 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002955 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
2956 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002957 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002958 }
2959
2960 return Builder.CreateLoad(Entry, false, "tmp");
2961}
2962
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002963llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002964 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002965
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002966 if (!Entry)
2967 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
2968 llvm::ConstantArray::get(Ident->getName()),
2969 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00002970 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002971
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002972 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002973}
2974
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00002975/// GetIvarLayoutName - Returns a unique constant for the given
2976/// ivar layout bitmap.
2977llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
2978 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson69243822009-07-13 04:10:07 +00002979 return VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00002980}
2981
Daniel Dunbard58edcb2009-05-03 14:10:34 +00002982static QualType::GCAttrTypes GetGCAttrTypeForType(ASTContext &Ctx,
2983 QualType FQT) {
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00002984 if (FQT.isObjCGCStrong())
2985 return QualType::Strong;
2986
2987 if (FQT.isObjCGCWeak())
2988 return QualType::Weak;
2989
Daniel Dunbard58edcb2009-05-03 14:10:34 +00002990 if (Ctx.isObjCObjectPointerType(FQT))
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00002991 return QualType::Strong;
2992
2993 if (const PointerType *PT = FQT->getAsPointerType())
Daniel Dunbard58edcb2009-05-03 14:10:34 +00002994 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00002995
2996 return QualType::GCNone;
2997}
2998
Daniel Dunbard58edcb2009-05-03 14:10:34 +00002999void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
3000 unsigned int BytePos,
3001 bool ForStrongLayout,
3002 bool &HasUnion) {
3003 const RecordDecl *RD = RT->getDecl();
3004 // FIXME - Use iterator.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003005 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003006 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
3007 const llvm::StructLayout *RecLayout =
3008 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
3009
3010 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3011 ForStrongLayout, HasUnion);
3012}
3013
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003014void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003015 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003016 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00003017 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003018 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003019 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003020 bool IsUnion = (RD && RD->isUnion());
3021 uint64_t MaxUnionIvarSize = 0;
3022 uint64_t MaxSkippedUnionIvarSize = 0;
3023 FieldDecl *MaxField = 0;
3024 FieldDecl *MaxSkippedField = 0;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003025 FieldDecl *LastFieldBitfield = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003026 uint64_t MaxFieldOffset = 0;
3027 uint64_t MaxSkippedFieldOffset = 0;
3028 uint64_t LastBitfieldOffset = 0;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003029
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003030 if (RecFields.empty())
3031 return;
Chris Lattnerf1690852009-03-31 08:48:01 +00003032 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3033 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3034
Chris Lattnerf1690852009-03-31 08:48:01 +00003035 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003036 FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003037 uint64_t FieldOffset;
3038 if (RD)
3039 FieldOffset =
3040 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
3041 else
3042 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003043
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003044 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003045 if (!Field->getIdentifier() || Field->isBitField()) {
3046 LastFieldBitfield = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003047 LastBitfieldOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003048 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003049 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003050
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003051 LastFieldBitfield = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003052 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003053 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003054 if (FQT->isUnionType())
3055 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003056
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003057 BuildAggrIvarRecordLayout(FQT->getAsRecordType(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003058 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003059 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003060 continue;
3061 }
Chris Lattnerf1690852009-03-31 08:48:01 +00003062
3063 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003064 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003065 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003066 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003067 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003068 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003069 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3070 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003071 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003072 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003073 FQT = CArray->getElementType();
3074 }
3075
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003076 assert(!FQT->isUnionType() &&
3077 "layout for array of unions not supported");
3078 if (FQT->isRecordType()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003079 int OldIndex = IvarsInfo.size() - 1;
3080 int OldSkIndex = SkipIvars.size() -1;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003081
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003082 const RecordType *RT = FQT->getAsRecordType();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003083 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003084 ForStrongLayout, HasUnion);
3085
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003086 // Replicate layout information for each array element. Note that
3087 // one element is already done.
3088 uint64_t ElIx = 1;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003089 for (int FirstIndex = IvarsInfo.size() - 1,
3090 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003091 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003092 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3093 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3094 IvarsInfo[i].ivar_size));
3095 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3096 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3097 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003098 }
3099 continue;
3100 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003101 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003102 // At this point, we are done with Record/Union and array there of.
3103 // For other arrays we are down to its element type.
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003104 QualType::GCAttrTypes GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003105
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003106 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003107 if ((ForStrongLayout && GCAttr == QualType::Strong)
3108 || (!ForStrongLayout && GCAttr == QualType::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003109 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003110 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003111 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003112 MaxUnionIvarSize = UnionIvarSize;
3113 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003114 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003115 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003116 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003117 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003118 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003119 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003120 } else if ((ForStrongLayout &&
3121 (GCAttr == QualType::GCNone || GCAttr == QualType::Weak))
3122 || (!ForStrongLayout && GCAttr != QualType::Weak)) {
3123 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003124 // FIXME: Why the asymmetry? We divide by word size in bits on other
3125 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003126 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003127 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003128 MaxSkippedUnionIvarSize = UnionIvarSize;
3129 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003130 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003131 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003132 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003133 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003134 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003135 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003136 }
3137 }
3138 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003139
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003140 if (LastFieldBitfield) {
3141 // Last field was a bitfield. Must update skip info.
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003142 Expr *BitWidth = LastFieldBitfield->getBitWidth();
3143 uint64_t BitFieldSize =
Eli Friedman9a901bb2009-04-26 19:19:15 +00003144 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Daniel Dunbar487993b2009-05-03 13:32:01 +00003145 GC_IVAR skivar;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003146 skivar.ivar_bytepos = BytePos + LastBitfieldOffset;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003147 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3148 + ((BitFieldSize % ByteSizeInBits) != 0);
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003149 SkipIvars.push_back(skivar);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003150 }
3151
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003152 if (MaxField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003153 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003154 MaxUnionIvarSize));
3155 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003156 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003157 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003158}
3159
3160/// BuildIvarLayout - Builds ivar layout bitmap for the class
3161/// implementation for the __strong or __weak case.
3162/// The layout map displays which words in ivar list must be skipped
3163/// and which must be scanned by GC (see below). String is built of bytes.
3164/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3165/// of words to skip and right nibble is count of words to scan. So, each
3166/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3167/// represented by a 0x00 byte which also ends the string.
3168/// 1. when ForStrongLayout is true, following ivars are scanned:
3169/// - id, Class
3170/// - object *
3171/// - __strong anything
3172///
3173/// 2. When ForStrongLayout is false, following ivars are scanned:
3174/// - __weak anything
3175///
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003176llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003177 const ObjCImplementationDecl *OMD,
3178 bool ForStrongLayout) {
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003179 bool hasUnion = false;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003180
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003181 unsigned int WordsToScan, WordsToSkip;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003182 const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
3183 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
Owen Anderson69243822009-07-13 04:10:07 +00003184 return VMContext.getNullValue(PtrTy);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003185
Chris Lattnerf1690852009-03-31 08:48:01 +00003186 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003187 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003188 CGM.getContext().CollectObjCIvars(OI, RecFields);
Fariborz Jahanian98200742009-05-12 18:14:29 +00003189
Daniel Dunbar37153282009-05-04 04:10:48 +00003190 // Add this implementations synthesized ivars.
Fariborz Jahanian98200742009-05-12 18:14:29 +00003191 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
3192 CGM.getContext().CollectSynthesizedIvars(OI, Ivars);
3193 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3194 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3195
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003196 if (RecFields.empty())
Owen Anderson69243822009-07-13 04:10:07 +00003197 return VMContext.getNullValue(PtrTy);
Chris Lattnerf1690852009-03-31 08:48:01 +00003198
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003199 SkipIvars.clear();
3200 IvarsInfo.clear();
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00003201
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003202 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003203 if (IvarsInfo.empty())
Owen Anderson69243822009-07-13 04:10:07 +00003204 return VMContext.getNullValue(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003205
3206 // Sort on byte position in case we encounterred a union nested in
3207 // the ivar list.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003208 if (hasUnion && !IvarsInfo.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003209 std::sort(IvarsInfo.begin(), IvarsInfo.end());
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003210 if (hasUnion && !SkipIvars.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003211 std::sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003212
3213 // Build the string of skip/scan nibbles
Fariborz Jahanian8c2f2d12009-04-24 17:15:27 +00003214 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003215 unsigned int WordSize =
Duncan Sands9408c452009-05-09 07:08:47 +00003216 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003217 if (IvarsInfo[0].ivar_bytepos == 0) {
3218 WordsToSkip = 0;
3219 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003220 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003221 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3222 WordsToScan = IvarsInfo[0].ivar_size;
3223 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003224 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003225 unsigned int TailPrevGCObjC =
3226 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003227 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003228 // consecutive 'scanned' object pointers.
3229 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003230 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003231 // Skip over 'gc'able object pointer which lay over each other.
3232 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3233 continue;
3234 // Must skip over 1 or more words. We save current skip/scan values
3235 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003236 SKIP_SCAN SkScan;
3237 SkScan.skip = WordsToSkip;
3238 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003239 SkipScanIvars.push_back(SkScan);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003240
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003241 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003242 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3243 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003244 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003245 WordsToSkip = 0;
3246 WordsToScan = IvarsInfo[i].ivar_size;
3247 }
3248 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003249 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003250 SKIP_SCAN SkScan;
3251 SkScan.skip = WordsToSkip;
3252 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003253 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003254 }
3255
3256 bool BytesSkipped = false;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003257 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003258 unsigned int LastIndex = SkipIvars.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003259 int LastByteSkipped =
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003260 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
3261 LastIndex = IvarsInfo.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003262 int LastByteScanned =
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003263 IvarsInfo[LastIndex].ivar_bytepos +
3264 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003265 BytesSkipped = (LastByteSkipped > LastByteScanned);
3266 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003267 if (BytesSkipped) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003268 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003269 SKIP_SCAN SkScan;
3270 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3271 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003272 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003273 }
3274 }
3275 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3276 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003277 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003278 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003279 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3280 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3281 // 0xM0 followed by 0x0N detected.
3282 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3283 for (int j = i+1; j < SkipScan; j++)
3284 SkipScanIvars[j] = SkipScanIvars[j+1];
3285 --SkipScan;
3286 }
3287 }
3288
3289 // Generate the string.
3290 std::string BitMap;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003291 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003292 unsigned char byte;
3293 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3294 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3295 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3296 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
3297
3298 if (skip_small > 0 || skip_big > 0)
3299 BytesSkipped = true;
3300 // first skip big.
3301 for (unsigned int ix = 0; ix < skip_big; ix++)
3302 BitMap += (unsigned char)(0xf0);
3303
3304 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003305 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003306 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003307 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003308 byte |= 0xf;
3309 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003310 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003311 byte |= scan_small;
3312 scan_small = 0;
3313 }
3314 BitMap += byte;
3315 }
3316 // next scan big
3317 for (unsigned int ix = 0; ix < scan_big; ix++)
3318 BitMap += (unsigned char)(0x0f);
3319 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003320 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003321 byte = scan_small;
3322 BitMap += byte;
3323 }
3324 }
3325 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003326 unsigned char zero = 0;
3327 BitMap += zero;
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003328
3329 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
3330 printf("\n%s ivar layout for class '%s': ",
3331 ForStrongLayout ? "strong" : "weak",
3332 OMD->getClassInterface()->getNameAsCString());
3333 const unsigned char *s = (unsigned char*)BitMap.c_str();
3334 for (unsigned i = 0; i < BitMap.size(); i++)
3335 if (!(s[i] & 0xf0))
3336 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3337 else
3338 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3339 printf("\n");
3340 }
3341
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003342 // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as
3343 // final layout.
3344 if (ForStrongLayout && !BytesSkipped)
Owen Anderson69243822009-07-13 04:10:07 +00003345 return VMContext.getNullValue(PtrTy);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003346 llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3347 llvm::ConstantArray::get(BitMap.c_str()),
3348 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003349 1, true);
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003350 return getConstantGEP(Entry, 0, 0);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003351}
3352
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003353llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003354 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3355
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003356 // FIXME: Avoid std::string copying.
3357 if (!Entry)
3358 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
3359 llvm::ConstantArray::get(Sel.getAsString()),
3360 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003361 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003362
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003363 return getConstantGEP(Entry, 0, 0);
3364}
3365
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003366// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003367llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003368 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3369}
3370
3371// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003372llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003373 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
3374}
3375
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003376llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003377 std::string TypeStr;
3378 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3379
3380 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003381
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003382 if (!Entry)
3383 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
3384 llvm::ConstantArray::get(TypeStr),
3385 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003386 1, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003387
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003388 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003389}
3390
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003391llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003392 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003393 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3394 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00003395
3396 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3397
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003398 if (!Entry)
3399 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
3400 llvm::ConstantArray::get(TypeStr),
3401 "__TEXT,__cstring,cstring_literals",
3402 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00003403
3404 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003405}
3406
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003407// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003408llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003409 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
3410
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003411 if (!Entry)
3412 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
3413 llvm::ConstantArray::get(Ident->getName()),
3414 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003415 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003416
3417 return getConstantGEP(Entry, 0, 0);
3418}
3419
3420// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003421// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003422llvm::Constant *
3423 CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3424 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003425 std::string TypeStr;
3426 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003427 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3428}
3429
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003430void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
3431 const ObjCContainerDecl *CD,
3432 std::string &NameOut) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00003433 NameOut = '\01';
3434 NameOut += (D->isInstanceMethod() ? '-' : '+');
Chris Lattner077bf5e2008-11-24 03:33:13 +00003435 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003436 assert (CD && "Missing container decl in GetNameForMethod");
3437 NameOut += CD->getNameAsString();
Fariborz Jahanian1e9aef32009-04-16 18:34:20 +00003438 if (const ObjCCategoryImplDecl *CID =
3439 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) {
3440 NameOut += '(';
3441 NameOut += CID->getNameAsString();
3442 NameOut+= ')';
3443 }
Chris Lattner077bf5e2008-11-24 03:33:13 +00003444 NameOut += ' ';
3445 NameOut += D->getSelector().getAsString();
3446 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003447}
3448
Fariborz Jahanianc38e9af2009-06-23 21:47:46 +00003449void CGObjCCommonMac::MergeMetadataGlobals(
3450 std::vector<llvm::Constant*> &UsedArray) {
3451 llvm::Type *i8PTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
3452 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3453 e = UsedGlobals.end(); i != e; ++i) {
3454 UsedArray.push_back(llvm::ConstantExpr::getBitCast(cast<llvm::Constant>(*i),
3455 i8PTy));
3456 }
3457}
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
3465 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
3466 if (i->second->hasInitializer())
3467 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);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +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);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003475 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
3476 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
3477 Values));
3478 }
3479
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003480 // Add assembler directives to add lazy undefined symbol references
3481 // for classes which are referenced but not defined. This is
3482 // important for correct linker interaction.
3483
3484 // FIXME: Uh, this isn't particularly portable.
3485 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00003486
3487 if (!CGM.getModule().getModuleInlineAsm().empty())
3488 s << "\n";
3489
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003490 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
3491 e = LazySymbols.end(); i != e; ++i) {
3492 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
3493 }
3494 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
3495 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003496 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003497 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
3498 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00003499
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003500 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003501}
3502
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003503CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003504 : CGObjCCommonMac(cgm),
3505 ObjCTypes(cgm)
3506{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003507 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003508 ObjCABI = 2;
3509}
3510
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003511/* *** */
3512
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003513ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
3514: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003515{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003516 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3517 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003518
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003519 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003520 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003521 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003522 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003523 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
3524
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003525 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00003526 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003527 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003528
Mike Stumpf5408fe2009-05-16 07:57:57 +00003529 // FIXME: It would be nice to unify this with the opaque type, so that the IR
3530 // comes out a bit cleaner.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003531 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
3532 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003533
3534 // I'm not sure I like this. The implicit coordination is a bit
3535 // gross. We should solve this in a reasonable fashion because this
3536 // is a pretty common task (match some runtime data structure with
3537 // an LLVM data structure).
3538
3539 // FIXME: This is leaked.
3540 // FIXME: Merge with rewriter code?
3541
3542 // struct _objc_super {
3543 // id self;
3544 // Class cls;
3545 // }
3546 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3547 SourceLocation(),
3548 &Ctx.Idents.get("_objc_super"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003549 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Douglas Gregor6ab35242009-04-09 21:40:53 +00003550 Ctx.getObjCIdType(), 0, false));
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.getObjCClassType(), 0, false));
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003553 RD->completeDefinition(Ctx);
3554
3555 SuperCTy = Ctx.getTagDeclType(RD);
3556 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
3557
3558 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003559 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
3560
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003561 // struct _prop_t {
3562 // char *name;
3563 // char *attributes;
3564 // }
Chris Lattner1c02f862009-04-22 02:53:24 +00003565 PropertyTy = llvm::StructType::get(Int8PtrTy, Int8PtrTy, NULL);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003566 CGM.getModule().addTypeName("struct._prop_t",
3567 PropertyTy);
3568
3569 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003570 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003571 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003572 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003573 // }
3574 PropertyListTy = llvm::StructType::get(IntTy,
3575 IntTy,
3576 llvm::ArrayType::get(PropertyTy, 0),
3577 NULL);
3578 CGM.getModule().addTypeName("struct._prop_list_t",
3579 PropertyListTy);
3580 // struct _prop_list_t *
3581 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
3582
3583 // struct _objc_method {
3584 // SEL _cmd;
3585 // char *method_type;
3586 // char *_imp;
3587 // }
3588 MethodTy = llvm::StructType::get(SelectorPtrTy,
3589 Int8PtrTy,
3590 Int8PtrTy,
3591 NULL);
3592 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003593
3594 // struct _objc_cache *
3595 CacheTy = llvm::OpaqueType::get();
3596 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
3597 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003598}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003599
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003600ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
3601 : ObjCCommonTypesHelper(cgm)
3602{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003603 // struct _objc_method_description {
3604 // SEL name;
3605 // char *types;
3606 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003607 MethodDescriptionTy =
3608 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003609 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003610 NULL);
3611 CGM.getModule().addTypeName("struct._objc_method_description",
3612 MethodDescriptionTy);
3613
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003614 // struct _objc_method_description_list {
3615 // int count;
3616 // struct _objc_method_description[1];
3617 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003618 MethodDescriptionListTy =
3619 llvm::StructType::get(IntTy,
3620 llvm::ArrayType::get(MethodDescriptionTy, 0),
3621 NULL);
3622 CGM.getModule().addTypeName("struct._objc_method_description_list",
3623 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003624
3625 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003626 MethodDescriptionListPtrTy =
3627 llvm::PointerType::getUnqual(MethodDescriptionListTy);
3628
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003629 // Protocol description structures
3630
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003631 // struct _objc_protocol_extension {
3632 // uint32_t size; // sizeof(struct _objc_protocol_extension)
3633 // struct _objc_method_description_list *optional_instance_methods;
3634 // struct _objc_method_description_list *optional_class_methods;
3635 // struct _objc_property_list *instance_properties;
3636 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003637 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003638 llvm::StructType::get(IntTy,
3639 MethodDescriptionListPtrTy,
3640 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003641 PropertyListPtrTy,
3642 NULL);
3643 CGM.getModule().addTypeName("struct._objc_protocol_extension",
3644 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003645
3646 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003647 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
3648
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003649 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003650
3651 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
3652 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3653
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003654 const llvm::Type *T =
3655 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
3656 LongTy,
3657 llvm::ArrayType::get(ProtocolTyHolder, 0),
3658 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003659 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
3660
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003661 // struct _objc_protocol {
3662 // struct _objc_protocol_extension *isa;
3663 // char *protocol_name;
3664 // struct _objc_protocol **_objc_protocol_list;
3665 // struct _objc_method_description_list *instance_methods;
3666 // struct _objc_method_description_list *class_methods;
3667 // }
3668 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003669 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003670 llvm::PointerType::getUnqual(ProtocolListTyHolder),
3671 MethodDescriptionListPtrTy,
3672 MethodDescriptionListPtrTy,
3673 NULL);
3674 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
3675
3676 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
3677 CGM.getModule().addTypeName("struct._objc_protocol_list",
3678 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003679 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003680 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
3681
3682 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003683 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003684 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003685
3686 // Class description structures
3687
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003688 // struct _objc_ivar {
3689 // char *ivar_name;
3690 // char *ivar_type;
3691 // int ivar_offset;
3692 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003693 IvarTy = llvm::StructType::get(Int8PtrTy,
3694 Int8PtrTy,
3695 IntTy,
3696 NULL);
3697 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
3698
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003699 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003700 IvarListTy = llvm::OpaqueType::get();
3701 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
3702 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
3703
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003704 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003705 MethodListTy = llvm::OpaqueType::get();
3706 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
3707 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
3708
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003709 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003710 ClassExtensionTy =
3711 llvm::StructType::get(IntTy,
3712 Int8PtrTy,
3713 PropertyListPtrTy,
3714 NULL);
3715 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
3716 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
3717
3718 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3719
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003720 // struct _objc_class {
3721 // Class isa;
3722 // Class super_class;
3723 // char *name;
3724 // long version;
3725 // long info;
3726 // long instance_size;
3727 // struct _objc_ivar_list *ivars;
3728 // struct _objc_method_list *methods;
3729 // struct _objc_cache *cache;
3730 // struct _objc_protocol_list *protocols;
3731 // char *ivar_layout;
3732 // struct _objc_class_ext *ext;
3733 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003734 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3735 llvm::PointerType::getUnqual(ClassTyHolder),
3736 Int8PtrTy,
3737 LongTy,
3738 LongTy,
3739 LongTy,
3740 IvarListPtrTy,
3741 MethodListPtrTy,
3742 CachePtrTy,
3743 ProtocolListPtrTy,
3744 Int8PtrTy,
3745 ClassExtensionPtrTy,
3746 NULL);
3747 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
3748
3749 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
3750 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
3751 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
3752
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003753 // struct _objc_category {
3754 // char *category_name;
3755 // char *class_name;
3756 // struct _objc_method_list *instance_method;
3757 // struct _objc_method_list *class_method;
3758 // uint32_t size; // sizeof(struct _objc_category)
3759 // struct _objc_property_list *instance_properties;// category's @property
3760 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003761 CategoryTy = llvm::StructType::get(Int8PtrTy,
3762 Int8PtrTy,
3763 MethodListPtrTy,
3764 MethodListPtrTy,
3765 ProtocolListPtrTy,
3766 IntTy,
3767 PropertyListPtrTy,
3768 NULL);
3769 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
3770
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003771 // Global metadata structures
3772
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003773 // struct _objc_symtab {
3774 // long sel_ref_cnt;
3775 // SEL *refs;
3776 // short cls_def_cnt;
3777 // short cat_def_cnt;
3778 // char *defs[cls_def_cnt + cat_def_cnt];
3779 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003780 SymtabTy = llvm::StructType::get(LongTy,
3781 SelectorPtrTy,
3782 ShortTy,
3783 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003784 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003785 NULL);
3786 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
3787 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
3788
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003789 // struct _objc_module {
3790 // long version;
3791 // long size; // sizeof(struct _objc_module)
3792 // char *name;
3793 // struct _objc_symtab* symtab;
3794 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003795 ModuleTy =
3796 llvm::StructType::get(LongTy,
3797 LongTy,
3798 Int8PtrTy,
3799 SymtabPtrTy,
3800 NULL);
3801 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003802
Anders Carlsson2abd89c2008-08-31 04:05:03 +00003803
Mike Stumpf5408fe2009-05-16 07:57:57 +00003804 // FIXME: This is the size of the setjmp buffer and should be target
3805 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00003806 uint64_t SetJmpBufferSize = 18;
3807
3808 // Exceptions
3809 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00003810 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00003811
3812 ExceptionDataTy =
3813 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
3814 SetJmpBufferSize),
3815 StackPtrTy, NULL);
3816 CGM.getModule().addTypeName("struct._objc_exception_data",
3817 ExceptionDataTy);
3818
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003819}
3820
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003821ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003822: ObjCCommonTypesHelper(cgm)
3823{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003824 // struct _method_list_t {
3825 // uint32_t entsize; // sizeof(struct _objc_method)
3826 // uint32_t method_count;
3827 // struct _objc_method method_list[method_count];
3828 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003829 MethodListnfABITy = llvm::StructType::get(IntTy,
3830 IntTy,
3831 llvm::ArrayType::get(MethodTy, 0),
3832 NULL);
3833 CGM.getModule().addTypeName("struct.__method_list_t",
3834 MethodListnfABITy);
3835 // struct method_list_t *
3836 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003837
3838 // struct _protocol_t {
3839 // id isa; // NULL
3840 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003841 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003842 // const struct method_list_t * const instance_methods;
3843 // const struct method_list_t * const class_methods;
3844 // const struct method_list_t *optionalInstanceMethods;
3845 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003846 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003847 // const uint32_t size; // sizeof(struct _protocol_t)
3848 // const uint32_t flags; // = 0
3849 // }
3850
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003851 // Holder for struct _protocol_list_t *
3852 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3853
3854 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3855 Int8PtrTy,
3856 llvm::PointerType::getUnqual(
3857 ProtocolListTyHolder),
3858 MethodListnfABIPtrTy,
3859 MethodListnfABIPtrTy,
3860 MethodListnfABIPtrTy,
3861 MethodListnfABIPtrTy,
3862 PropertyListPtrTy,
3863 IntTy,
3864 IntTy,
3865 NULL);
3866 CGM.getModule().addTypeName("struct._protocol_t",
3867 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003868
3869 // struct _protocol_t*
3870 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003871
Fariborz Jahanianda320092009-01-29 19:24:30 +00003872 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003873 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00003874 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003875 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003876 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3877 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003878 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003879 NULL);
3880 CGM.getModule().addTypeName("struct._objc_protocol_list",
3881 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003882 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3883 ProtocolListnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003884
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003885 // struct _objc_protocol_list*
3886 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003887
3888 // struct _ivar_t {
3889 // unsigned long int *offset; // pointer to ivar offset location
3890 // char *name;
3891 // char *type;
3892 // uint32_t alignment;
3893 // uint32_t size;
3894 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003895 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3896 Int8PtrTy,
3897 Int8PtrTy,
3898 IntTy,
3899 IntTy,
3900 NULL);
3901 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3902
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003903 // struct _ivar_list_t {
3904 // uint32 entsize; // sizeof(struct _ivar_t)
3905 // uint32 count;
3906 // struct _iver_t list[count];
3907 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003908 IvarListnfABITy = llvm::StructType::get(IntTy,
3909 IntTy,
3910 llvm::ArrayType::get(
3911 IvarnfABITy, 0),
3912 NULL);
3913 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3914
3915 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003916
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003917 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003918 // uint32_t const flags;
3919 // uint32_t const instanceStart;
3920 // uint32_t const instanceSize;
3921 // uint32_t const reserved; // only when building for 64bit targets
3922 // const uint8_t * const ivarLayout;
3923 // const char *const name;
3924 // const struct _method_list_t * const baseMethods;
3925 // const struct _objc_protocol_list *const baseProtocols;
3926 // const struct _ivar_list_t *const ivars;
3927 // const uint8_t * const weakIvarLayout;
3928 // const struct _prop_list_t * const properties;
3929 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003930
3931 // FIXME. Add 'reserved' field in 64bit abi mode!
3932 ClassRonfABITy = llvm::StructType::get(IntTy,
3933 IntTy,
3934 IntTy,
3935 Int8PtrTy,
3936 Int8PtrTy,
3937 MethodListnfABIPtrTy,
3938 ProtocolListnfABIPtrTy,
3939 IvarListnfABIPtrTy,
3940 Int8PtrTy,
3941 PropertyListPtrTy,
3942 NULL);
3943 CGM.getModule().addTypeName("struct._class_ro_t",
3944 ClassRonfABITy);
3945
3946 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3947 std::vector<const llvm::Type*> Params;
3948 Params.push_back(ObjectPtrTy);
3949 Params.push_back(SelectorPtrTy);
3950 ImpnfABITy = llvm::PointerType::getUnqual(
3951 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3952
3953 // struct _class_t {
3954 // struct _class_t *isa;
3955 // struct _class_t * const superclass;
3956 // void *cache;
3957 // IMP *vtable;
3958 // struct class_ro_t *ro;
3959 // }
3960
3961 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3962 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3963 llvm::PointerType::getUnqual(ClassTyHolder),
3964 CachePtrTy,
3965 llvm::PointerType::getUnqual(ImpnfABITy),
3966 llvm::PointerType::getUnqual(
3967 ClassRonfABITy),
3968 NULL);
3969 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3970
3971 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3972 ClassnfABITy);
3973
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003974 // LLVM for struct _class_t *
3975 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3976
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003977 // struct _category_t {
3978 // const char * const name;
3979 // struct _class_t *const cls;
3980 // const struct _method_list_t * const instance_methods;
3981 // const struct _method_list_t * const class_methods;
3982 // const struct _protocol_list_t * const protocols;
3983 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003984 // }
3985 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003986 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003987 MethodListnfABIPtrTy,
3988 MethodListnfABIPtrTy,
3989 ProtocolListnfABIPtrTy,
3990 PropertyListPtrTy,
3991 NULL);
3992 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003993
3994 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003995 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3996 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003997
3998 // MessageRefTy - LLVM for:
3999 // struct _message_ref_t {
4000 // IMP messenger;
4001 // SEL name;
4002 // };
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004003
4004 // First the clang type for struct _message_ref_t
4005 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
4006 SourceLocation(),
4007 &Ctx.Idents.get("_message_ref_t"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004008 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Douglas Gregor6ab35242009-04-09 21:40:53 +00004009 Ctx.VoidPtrTy, 0, false));
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.getObjCSelType(), 0, false));
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004012 RD->completeDefinition(Ctx);
4013
4014 MessageRefCTy = Ctx.getTagDeclType(RD);
4015 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4016 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004017
4018 // MessageRefPtrTy - LLVM for struct _message_ref_t*
4019 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
4020
4021 // SuperMessageRefTy - LLVM for:
4022 // struct _super_message_ref_t {
4023 // SUPER_IMP messenger;
4024 // SEL name;
4025 // };
4026 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy,
4027 SelectorPtrTy,
4028 NULL);
4029 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
4030
4031 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
4032 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4033
Daniel Dunbare588b992009-03-01 04:46:24 +00004034
4035 // struct objc_typeinfo {
4036 // const void** vtable; // objc_ehtype_vtable + 2
4037 // const char* name; // c++ typeinfo string
4038 // Class cls;
4039 // };
4040 EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy),
4041 Int8PtrTy,
4042 ClassnfABIPtrTy,
4043 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004044 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Daniel Dunbare588b992009-03-01 04:46:24 +00004045 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004046}
4047
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004048llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
4049 FinishNonFragileABIModule();
4050
4051 return NULL;
4052}
4053
Daniel Dunbar463b8762009-05-15 21:48:48 +00004054void CGObjCNonFragileABIMac::AddModuleClassList(const
4055 std::vector<llvm::GlobalValue*>
4056 &Container,
4057 const char *SymbolName,
4058 const char *SectionName) {
4059 unsigned NumClasses = Container.size();
4060
4061 if (!NumClasses)
4062 return;
4063
4064 std::vector<llvm::Constant*> Symbols(NumClasses);
4065 for (unsigned i=0; i<NumClasses; i++)
4066 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
4067 ObjCTypes.Int8PtrTy);
4068 llvm::Constant* Init =
4069 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
4070 NumClasses),
4071 Symbols);
4072
4073 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004074 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004075 llvm::GlobalValue::InternalLinkage,
4076 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004077 SymbolName);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004078 GV->setAlignment(8);
4079 GV->setSection(SectionName);
4080 UsedGlobals.push_back(GV);
4081}
4082
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004083void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4084 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004085
Daniel Dunbar463b8762009-05-15 21:48:48 +00004086 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004087 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar463b8762009-05-15 21:48:48 +00004088 AddModuleClassList(DefinedClasses,
4089 "\01L_OBJC_LABEL_CLASS_$",
4090 "__DATA, __objc_classlist, regular, no_dead_strip");
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004091 AddModuleClassList(DefinedNonLazyClasses,
4092 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4093 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004094
4095 // Build list of all implemented category addresses in array
4096 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar463b8762009-05-15 21:48:48 +00004097 AddModuleClassList(DefinedCategories,
4098 "\01L_OBJC_LABEL_CATEGORY_$",
4099 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004100 AddModuleClassList(DefinedNonLazyCategories,
4101 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4102 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004103
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004104 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
4105 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
4106 std::vector<llvm::Constant*> Values(2);
4107 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
Fariborz Jahanian067986e2009-02-24 21:08:09 +00004108 unsigned int flags = 0;
Fariborz Jahanian66a5c2c2009-02-24 23:34:44 +00004109 // FIXME: Fix and continue?
4110 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
4111 flags |= eImageInfo_GarbageCollected;
4112 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
4113 flags |= eImageInfo_GCOnly;
Fariborz Jahanian067986e2009-02-24 21:08:09 +00004114 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004115 llvm::Constant* Init = llvm::ConstantArray::get(
4116 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
4117 Values);
4118 llvm::GlobalVariable *IMGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004119 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004120 llvm::GlobalValue::InternalLinkage,
4121 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004122 "\01L_OBJC_IMAGE_INFO");
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004123 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
Daniel Dunbar325f7582009-04-23 08:03:21 +00004124 IMGV->setConstant(true);
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004125 UsedGlobals.push_back(IMGV);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004126}
4127
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004128/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004129/// NonLegacyDispatchMethods; false otherwise. What this means is that
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004130/// except for the 19 selectors in the list, we generate 32bit-style
4131/// message dispatch call for all the rest.
4132///
4133bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004134 if (NonLegacyDispatchMethods.empty()) {
4135 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4136 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4137 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4138 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4139 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4140 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4141 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4142 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4143 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4144 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
4145
4146 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4147 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4148 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4149 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4150 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4151 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4152 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4153 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004154 // "countByEnumeratingWithState:objects:count"
4155 IdentifierInfo *KeyIdents[] = {
4156 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4157 &CGM.getContext().Idents.get("objects"),
4158 &CGM.getContext().Idents.get("count")
4159 };
4160 NonLegacyDispatchMethods.insert(
4161 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004162 }
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004163 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004164}
4165
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004166// Metadata flags
4167enum MetaDataDlags {
4168 CLS = 0x0,
4169 CLS_META = 0x1,
4170 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004171 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004172 CLS_EXCEPTION = 0x20
4173};
4174/// BuildClassRoTInitializer - generate meta-data for:
4175/// struct _class_ro_t {
4176/// uint32_t const flags;
4177/// uint32_t const instanceStart;
4178/// uint32_t const instanceSize;
4179/// uint32_t const reserved; // only when building for 64bit targets
4180/// const uint8_t * const ivarLayout;
4181/// const char *const name;
4182/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004183/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004184/// const struct _ivar_list_t *const ivars;
4185/// const uint8_t * const weakIvarLayout;
4186/// const struct _prop_list_t * const properties;
4187/// }
4188///
4189llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
4190 unsigned flags,
4191 unsigned InstanceStart,
4192 unsigned InstanceSize,
4193 const ObjCImplementationDecl *ID) {
4194 std::string ClassName = ID->getNameAsString();
4195 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
4196 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4197 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4198 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
4199 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004200 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4201 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004202 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004203 // const struct _method_list_t * const baseMethods;
4204 std::vector<llvm::Constant*> Methods;
4205 std::string MethodListName("\01l_OBJC_$_");
4206 if (flags & CLS_META) {
4207 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004208 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004209 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004210 // Class methods should always be defined.
4211 Methods.push_back(GetMethodConstant(*i));
4212 }
4213 } else {
4214 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004215 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004216 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004217 // Instance methods should always be defined.
4218 Methods.push_back(GetMethodConstant(*i));
4219 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00004220 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004221 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004222 ObjCPropertyImplDecl *PID = *i;
4223
4224 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4225 ObjCPropertyDecl *PD = PID->getPropertyDecl();
4226
4227 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4228 if (llvm::Constant *C = GetMethodConstant(MD))
4229 Methods.push_back(C);
4230 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4231 if (llvm::Constant *C = GetMethodConstant(MD))
4232 Methods.push_back(C);
4233 }
4234 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004235 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004236 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004237 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004238
4239 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4240 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
4241 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
4242 + OID->getNameAsString(),
4243 OID->protocol_begin(),
4244 OID->protocol_end());
4245
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004246 if (flags & CLS_META)
Owen Anderson69243822009-07-13 04:10:07 +00004247 Values[ 7] = VMContext.getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004248 else
4249 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004250 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4251 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004252 if (flags & CLS_META)
Owen Anderson69243822009-07-13 04:10:07 +00004253 Values[ 9] = VMContext.getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004254 else
4255 Values[ 9] =
4256 EmitPropertyList(
4257 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
4258 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004259 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
4260 Values);
4261 llvm::GlobalVariable *CLASS_RO_GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004262 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004263 llvm::GlobalValue::InternalLinkage,
4264 Init,
4265 (flags & CLS_META) ?
4266 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
Owen Anderson1c431b32009-07-08 19:05:04 +00004267 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004268 CLASS_RO_GV->setAlignment(
4269 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004270 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004271 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004272
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004273}
4274
4275/// BuildClassMetaData - This routine defines that to-level meta-data
4276/// for the given ClassName for:
4277/// struct _class_t {
4278/// struct _class_t *isa;
4279/// struct _class_t * const superclass;
4280/// void *cache;
4281/// IMP *vtable;
4282/// struct class_ro_t *ro;
4283/// }
4284///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004285llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
4286 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004287 llvm::Constant *IsAGV,
4288 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004289 llvm::Constant *ClassRoGV,
4290 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004291 std::vector<llvm::Constant*> Values(5);
4292 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004293 Values[1] = SuperClassGV
4294 ? SuperClassGV
Owen Anderson69243822009-07-13 04:10:07 +00004295 : VMContext.getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004296 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4297 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4298 Values[4] = ClassRoGV; // &CLASS_RO_GV
4299 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
4300 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004301 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4302 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004303 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004304 GV->setAlignment(
4305 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004306 if (HiddenVisibility)
4307 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004308 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004309}
4310
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004311bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00004312CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004313 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004314}
4315
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004316void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004317 uint32_t &InstanceStart,
4318 uint32_t &InstanceSize) {
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004319 const ASTRecordLayout &RL =
4320 CGM.getContext().getASTObjCImplementationLayout(OID);
4321
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004322 // InstanceSize is really instance end.
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004323 InstanceSize = llvm::RoundUpToAlignment(RL.getNextOffset(), 8) / 8;
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004324
4325 // If there are no fields, the start is the same as the end.
4326 if (!RL.getFieldCount())
4327 InstanceStart = InstanceSize;
4328 else
4329 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004330}
4331
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004332void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4333 std::string ClassName = ID->getNameAsString();
4334 if (!ObjCEmptyCacheVar) {
4335 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Owen Anderson1c431b32009-07-08 19:05:04 +00004336 CGM.getModule(),
Daniel Dunbar948e2582009-02-15 07:36:20 +00004337 ObjCTypes.CacheTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004338 false,
4339 llvm::GlobalValue::ExternalLinkage,
4340 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00004341 "_objc_empty_cache");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004342
4343 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Owen Anderson1c431b32009-07-08 19:05:04 +00004344 CGM.getModule(),
Daniel Dunbar948e2582009-02-15 07:36:20 +00004345 ObjCTypes.ImpnfABITy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004346 false,
4347 llvm::GlobalValue::ExternalLinkage,
4348 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00004349 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004350 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004351 assert(ID->getClassInterface() &&
4352 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004353 // FIXME: Is this correct (that meta class size is never computed)?
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004354 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00004355 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004356 uint32_t InstanceSize = InstanceStart;
4357 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004358 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4359 std::string ObjCClassName(getClassSymbolPrefix());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004360
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004361 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004362
Daniel Dunbar04d40782009-04-14 06:00:08 +00004363 bool classIsHidden =
4364 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004365 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004366 flags |= OBJC2_CLS_HIDDEN;
4367 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004368 // class is root
4369 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004370 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004371 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004372 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004373 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004374 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4375 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4376 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004377 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004378 // work on super class metadata symbol.
4379 std::string SuperClassName =
4380 ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004381 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004382 }
4383 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4384 InstanceStart,
4385 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004386 std::string TClassName = ObjCMetaClassName + ClassName;
4387 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004388 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4389 classIsHidden);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004390
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004391 // Metadata for the class
4392 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004393 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004394 flags |= OBJC2_CLS_HIDDEN;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004395
Douglas Gregor68584ed2009-06-18 16:11:24 +00004396 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004397 flags |= CLS_EXCEPTION;
4398
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004399 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004400 flags |= CLS_ROOT;
4401 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00004402 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004403 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004404 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004405 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004406 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004407 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004408 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004409 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004410 InstanceStart,
4411 InstanceSize,
4412 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004413
4414 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004415 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004416 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4417 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004418 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004419
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004420 // Determine if this class is also "non-lazy".
4421 if (ImplementationIsNonLazy(ID))
4422 DefinedNonLazyClasses.push_back(ClassMD);
4423
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004424 // Force the definition of the EHType if necessary.
4425 if (flags & CLS_EXCEPTION)
4426 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004427}
4428
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004429/// GenerateProtocolRef - This routine is called to generate code for
4430/// a protocol reference expression; as in:
4431/// @code
4432/// @protocol(Proto1);
4433/// @endcode
4434/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4435/// which will hold address of the protocol meta-data.
4436///
4437llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
4438 const ObjCProtocolDecl *PD) {
4439
Fariborz Jahanian960cd062009-04-10 18:47:34 +00004440 // This routine is called for @protocol only. So, we must build definition
4441 // of protocol's meta-data (not a reference to it!)
4442 //
4443 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004444 ObjCTypes.ExternalProtocolPtrTy);
4445
4446 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
4447 ProtocolName += PD->getNameAsCString();
4448
4449 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
4450 if (PTGV)
4451 return Builder.CreateLoad(PTGV, false, "tmp");
4452 PTGV = new llvm::GlobalVariable(
Owen Anderson1c431b32009-07-08 19:05:04 +00004453 CGM.getModule(),
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004454 Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00004455 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004456 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004457 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004458 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
4459 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4460 UsedGlobals.push_back(PTGV);
4461 return Builder.CreateLoad(PTGV, false, "tmp");
4462}
4463
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004464/// GenerateCategory - Build metadata for a category implementation.
4465/// struct _category_t {
4466/// const char * const name;
4467/// struct _class_t *const cls;
4468/// const struct _method_list_t * const instance_methods;
4469/// const struct _method_list_t * const class_methods;
4470/// const struct _protocol_list_t * const protocols;
4471/// const struct _prop_list_t * const properties;
4472/// }
4473///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004474void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004475 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004476 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
4477 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004478 "_$_" + OCD->getNameAsString());
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004479 std::string ExtClassName(getClassSymbolPrefix() +
4480 Interface->getNameAsString());
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004481
4482 std::vector<llvm::Constant*> Values(6);
4483 Values[0] = GetClassName(OCD->getIdentifier());
4484 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004485 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004486 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004487 std::vector<llvm::Constant*> Methods;
4488 std::string MethodListName(Prefix);
4489 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
4490 "_$_" + OCD->getNameAsString();
4491
Douglas Gregor653f1b12009-04-23 01:02:12 +00004492 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004493 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004494 // Instance methods should always be defined.
4495 Methods.push_back(GetMethodConstant(*i));
4496 }
4497
4498 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004499 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004500 Methods);
4501
4502 MethodListName = Prefix;
4503 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
4504 OCD->getNameAsString();
4505 Methods.clear();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004506 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004507 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004508 // Class methods should always be defined.
4509 Methods.push_back(GetMethodConstant(*i));
4510 }
4511
4512 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004513 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004514 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004515 const ObjCCategoryDecl *Category =
4516 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004517 if (Category) {
4518 std::string ExtName(Interface->getNameAsString() + "_$_" +
4519 OCD->getNameAsString());
4520 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
4521 + Interface->getNameAsString() + "_$_"
4522 + Category->getNameAsString(),
4523 Category->protocol_begin(),
4524 Category->protocol_end());
4525 Values[5] =
4526 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
4527 OCD, Category, ObjCTypes);
4528 }
4529 else {
Owen Anderson69243822009-07-13 04:10:07 +00004530 Values[4] = VMContext.getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4531 Values[5] = VMContext.getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004532 }
4533
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004534 llvm::Constant *Init =
4535 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
4536 Values);
4537 llvm::GlobalVariable *GCATV
Owen Anderson1c431b32009-07-08 19:05:04 +00004538 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004539 false,
4540 llvm::GlobalValue::InternalLinkage,
4541 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004542 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004543 GCATV->setAlignment(
4544 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004545 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004546 UsedGlobals.push_back(GCATV);
4547 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004548
4549 // Determine if this category is also "non-lazy".
4550 if (ImplementationIsNonLazy(OCD))
4551 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004552}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004553
4554/// GetMethodConstant - Return a struct objc_method constant for the
4555/// given method if it has been defined. The result is null if the
4556/// method has not been defined. The return value has type MethodPtrTy.
4557llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
4558 const ObjCMethodDecl *MD) {
4559 // FIXME: Use DenseMap::lookup
4560 llvm::Function *Fn = MethodDefinitions[MD];
4561 if (!Fn)
4562 return 0;
4563
4564 std::vector<llvm::Constant*> Method(3);
4565 Method[0] =
4566 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4567 ObjCTypes.SelectorPtrTy);
4568 Method[1] = GetMethodVarType(MD);
4569 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
4570 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
4571}
4572
4573/// EmitMethodList - Build meta-data for method declarations
4574/// struct _method_list_t {
4575/// uint32_t entsize; // sizeof(struct _objc_method)
4576/// uint32_t method_count;
4577/// struct _objc_method method_list[method_count];
4578/// }
4579///
4580llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
4581 const std::string &Name,
4582 const char *Section,
4583 const ConstantVector &Methods) {
4584 // Return null for empty list.
4585 if (Methods.empty())
Owen Anderson69243822009-07-13 04:10:07 +00004586 return VMContext.getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004587
4588 std::vector<llvm::Constant*> Values(3);
4589 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00004590 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004591 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4592 // method_count
4593 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
4594 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
4595 Methods.size());
4596 Values[2] = llvm::ConstantArray::get(AT, Methods);
4597 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4598
4599 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004600 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004601 llvm::GlobalValue::InternalLinkage,
4602 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004603 Name);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004604 GV->setAlignment(
4605 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004606 GV->setSection(Section);
4607 UsedGlobals.push_back(GV);
4608 return llvm::ConstantExpr::getBitCast(GV,
4609 ObjCTypes.MethodListnfABIPtrTy);
4610}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004611
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004612/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
4613/// the given ivar.
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004614llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004615 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004616 const ObjCIvarDecl *Ivar) {
Daniel Dunbara81419d2009-05-05 00:36:57 +00004617 // FIXME: We shouldn't need to do this lookup.
4618 unsigned Index;
4619 const ObjCInterfaceDecl *Container =
4620 FindIvarInterface(CGM.getContext(), ID, Ivar, Index);
4621 assert(Container && "Unable to find ivar container!");
4622 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00004623 '.' + Ivar->getNameAsString();
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004624 llvm::GlobalVariable *IvarOffsetGV =
4625 CGM.getModule().getGlobalVariable(Name);
4626 if (!IvarOffsetGV)
4627 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004628 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004629 false,
4630 llvm::GlobalValue::ExternalLinkage,
4631 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00004632 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004633 return IvarOffsetGV;
4634}
4635
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004636llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004637 const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004638 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004639 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00004640 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
4641 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
4642 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004643 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004644 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00004645
Mike Stumpf5408fe2009-05-16 07:57:57 +00004646 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
4647 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00004648 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
4649 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
4650 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004651 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00004652 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00004653 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004654 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004655 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004656}
4657
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004658/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00004659/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004660/// IvarListnfABIPtrTy.
4661/// struct _ivar_t {
4662/// unsigned long int *offset; // pointer to ivar offset location
4663/// char *name;
4664/// char *type;
4665/// uint32_t alignment;
4666/// uint32_t size;
4667/// }
4668/// struct _ivar_list_t {
4669/// uint32 entsize; // sizeof(struct _ivar_t)
4670/// uint32 count;
4671/// struct _iver_t list[count];
4672/// }
4673///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004674
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004675llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
4676 const ObjCImplementationDecl *ID) {
4677
4678 std::vector<llvm::Constant*> Ivars, Ivar(5);
4679
4680 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4681 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
4682
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004683 // FIXME. Consolidate this with similar code in GenerateClass.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00004684
Daniel Dunbar91636d62009-04-20 00:33:43 +00004685 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00004686 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00004687 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Fariborz Jahanian99eee362009-04-01 19:37:34 +00004688
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004689 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
4690 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00004691 // Ignore unnamed bit-fields.
4692 if (!IVD->getDeclName())
4693 continue;
Daniel Dunbar3eec8aa2009-04-20 05:53:40 +00004694 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004695 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004696 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
4697 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004698 const llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004699 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00004700 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004701 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004702 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004703 Align = llvm::Log2_32(Align);
4704 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00004705 // NOTE. Size of a bitfield does not match gcc's, because of the
4706 // way bitfields are treated special in each. But I am told that
4707 // 'size' for bitfield ivars is ignored by the runtime so it does
4708 // not matter. If it matters, there is enough info to get the
4709 // bitfield right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004710 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4711 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
4712 }
4713 // Return null for empty list.
4714 if (Ivars.empty())
Owen Anderson69243822009-07-13 04:10:07 +00004715 return VMContext.getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004716 std::vector<llvm::Constant*> Values(3);
Duncan Sands9408c452009-05-09 07:08:47 +00004717 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004718 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4719 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
4720 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
4721 Ivars.size());
4722 Values[2] = llvm::ConstantArray::get(AT, Ivars);
4723 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4724 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4725 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004726 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004727 llvm::GlobalValue::InternalLinkage,
4728 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004729 Prefix + OID->getNameAsString());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004730 GV->setAlignment(
4731 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004732 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004733
4734 UsedGlobals.push_back(GV);
4735 return llvm::ConstantExpr::getBitCast(GV,
4736 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004737}
4738
4739llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4740 const ObjCProtocolDecl *PD) {
4741 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4742
4743 if (!Entry) {
4744 // We use the initializer as a marker of whether this is a forward
4745 // reference or not. At module finalization we add the empty
4746 // contents for protocols which were referenced but never defined.
4747 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00004748 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004749 llvm::GlobalValue::ExternalLinkage,
4750 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00004751 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString());
Fariborz Jahanianda320092009-01-29 19:24:30 +00004752 Entry->setSection("__DATA,__datacoal_nt,coalesced");
4753 UsedGlobals.push_back(Entry);
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);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004840 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson69243822009-07-13 04:10:07 +00004841 Values[9] = VMContext.getNullValue(ObjCTypes.IntTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004842 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
4843 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);
4860
4861 // Use this protocol meta-data to build protocol list table in section
4862 // __DATA, __objc_protolist
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004863 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
Owen Anderson1c431b32009-07-08 19:05:04 +00004864 CGM.getModule(),
Daniel Dunbar948e2582009-02-15 07:36:20 +00004865 ObjCTypes.ProtocolnfABIPtrTy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004866 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004867 Entry,
4868 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
Owen Anderson1c431b32009-07-08 19:05:04 +00004869 +ProtocolName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004870 PTGV->setAlignment(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004871 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00004872 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004873 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4874 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004875 return Entry;
4876}
4877
4878/// EmitProtocolList - Generate protocol list meta-data:
4879/// @code
4880/// struct _protocol_list_t {
4881/// long protocol_count; // Note, this is 32/64 bit
4882/// struct _protocol_t[protocol_count];
4883/// }
4884/// @endcode
4885///
4886llvm::Constant *
4887CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4888 ObjCProtocolDecl::protocol_iterator begin,
4889 ObjCProtocolDecl::protocol_iterator end) {
4890 std::vector<llvm::Constant*> ProtocolRefs;
4891
Fariborz Jahanianda320092009-01-29 19:24:30 +00004892 // Just return null for empty protocol lists
Daniel Dunbar948e2582009-02-15 07:36:20 +00004893 if (begin == end)
Owen Anderson69243822009-07-13 04:10:07 +00004894 return VMContext.getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004895
Daniel Dunbar948e2582009-02-15 07:36:20 +00004896 // FIXME: We shouldn't need to do this lookup here, should we?
Fariborz Jahanianda320092009-01-29 19:24:30 +00004897 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4898 if (GV)
Daniel Dunbar948e2582009-02-15 07:36:20 +00004899 return llvm::ConstantExpr::getBitCast(GV,
4900 ObjCTypes.ProtocolListnfABIPtrTy);
4901
4902 for (; begin != end; ++begin)
4903 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4904
Fariborz Jahanianda320092009-01-29 19:24:30 +00004905 // This list is null terminated.
Owen Anderson69243822009-07-13 04:10:07 +00004906 ProtocolRefs.push_back(VMContext.getNullValue(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004907 ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004908
4909 std::vector<llvm::Constant*> Values(2);
4910 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4911 Values[1] =
Daniel Dunbar948e2582009-02-15 07:36:20 +00004912 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004913 ProtocolRefs.size()),
4914 ProtocolRefs);
4915
4916 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Owen Anderson1c431b32009-07-08 19:05:04 +00004917 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004918 llvm::GlobalValue::InternalLinkage,
4919 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004920 Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004921 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004922 GV->setAlignment(
4923 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004924 UsedGlobals.push_back(GV);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004925 return llvm::ConstantExpr::getBitCast(GV,
4926 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004927}
4928
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004929/// GetMethodDescriptionConstant - This routine build following meta-data:
4930/// struct _objc_method {
4931/// SEL _cmd;
4932/// char *method_type;
4933/// char *_imp;
4934/// }
4935
4936llvm::Constant *
4937CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4938 std::vector<llvm::Constant*> Desc(3);
4939 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4940 ObjCTypes.SelectorPtrTy);
4941 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004942 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson69243822009-07-13 04:10:07 +00004943 Desc[2] = VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004944 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4945}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004946
4947/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4948/// This code gen. amounts to generating code for:
4949/// @code
4950/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4951/// @encode
4952///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004953LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004954 CodeGen::CodeGenFunction &CGF,
4955 QualType ObjectTy,
4956 llvm::Value *BaseValue,
4957 const ObjCIvarDecl *Ivar,
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004958 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00004959 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00004960 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
4961 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004962}
4963
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004964llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
4965 CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00004966 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004967 const ObjCIvarDecl *Ivar) {
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00004968 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),
4969 false, "ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004970}
4971
Fariborz Jahanian46551122009-02-04 00:22:57 +00004972CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4973 CodeGen::CodeGenFunction &CGF,
4974 QualType ResultType,
4975 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004976 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00004977 QualType Arg0Ty,
4978 bool IsSuper,
4979 const CallArgList &CallArgs) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00004980 // FIXME. Even though IsSuper is passes. This function doese not handle calls
4981 // to 'super' receivers.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004982 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004983 llvm::Value *Arg0 = Receiver;
4984 if (!IsSuper)
4985 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004986
4987 // Find the message function name.
Mike Stumpf5408fe2009-05-16 07:57:57 +00004988 // FIXME. This is too much work to get the ABI-specific result type needed to
4989 // find the message name.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004990 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4991 llvm::SmallVector<QualType, 16>());
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00004992 llvm::Constant *Fn = 0;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004993 std::string Name("\01l_");
4994 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004995#if 0
4996 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004997 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00004998 Fn = ObjCTypes.getMessageSendIdStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004999 // FIXME. Is there a better way of getting these names.
5000 // They are available in RuntimeFunctions vector pair.
5001 Name += "objc_msgSendId_stret_fixup";
5002 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005003 else
5004#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005005 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005006 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005007 Name += "objc_msgSendSuper2_stret_fixup";
5008 }
5009 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005010 {
Chris Lattner1c02f862009-04-22 02:53:24 +00005011 Fn = ObjCTypes.getMessageSendStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005012 Name += "objc_msgSend_stret_fixup";
5013 }
5014 }
Fariborz Jahanian5b2bad02009-04-30 16:31:11 +00005015 else if (!IsSuper && ResultType->isFloatingType()) {
Daniel Dunbarc0183e82009-06-26 18:32:06 +00005016 if (ResultType->isSpecificBuiltinType(BuiltinType::LongDouble)) {
5017 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5018 Name += "objc_msgSend_fpret_fixup";
5019 }
5020 else {
5021 Fn = ObjCTypes.getMessageSendFixupFn();
5022 Name += "objc_msgSend_fixup";
Fariborz Jahanian5b2bad02009-04-30 16:31:11 +00005023 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005024 }
5025 else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005026#if 0
5027// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005028 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005029 Fn = ObjCTypes.getMessageSendIdFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005030 Name += "objc_msgSendId_fixup";
5031 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005032 else
5033#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005034 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005035 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005036 Name += "objc_msgSendSuper2_fixup";
5037 }
5038 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005039 {
Chris Lattner1c02f862009-04-22 02:53:24 +00005040 Fn = ObjCTypes.getMessageSendFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005041 Name += "objc_msgSend_fixup";
5042 }
5043 }
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005044 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005045 Name += '_';
5046 std::string SelName(Sel.getAsString());
5047 // Replace all ':' in selector name with '_' ouch!
5048 for(unsigned i = 0; i < SelName.size(); i++)
5049 if (SelName[i] == ':')
5050 SelName[i] = '_';
5051 Name += SelName;
5052 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5053 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005054 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005055 std::vector<llvm::Constant*> Values(2);
5056 Values[0] = Fn;
5057 Values[1] = GetMethodVarName(Sel);
5058 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Owen Anderson1c431b32009-07-08 19:05:04 +00005059 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005060 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005061 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005062 Name);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005063 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005064 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005065 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005066 }
5067 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005068
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005069 CallArgList ActualArgs;
5070 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
5071 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
5072 ObjCTypes.MessageRefCPtrTy));
5073 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005074 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
5075 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5076 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00005077 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005078 Callee = CGF.Builder.CreateBitCast(Callee,
5079 llvm::PointerType::getUnqual(FTy));
5080 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005081}
5082
5083/// Generate code for a message send expression in the nonfragile abi.
5084CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
5085 CodeGen::CodeGenFunction &CGF,
5086 QualType ResultType,
5087 Selector Sel,
5088 llvm::Value *Receiver,
5089 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00005090 const CallArgList &CallArgs,
5091 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005092 return LegacyDispatchedSelector(Sel)
5093 ? EmitLegacyMessageSend(CGF, ResultType, EmitSelector(CGF.Builder, Sel),
5094 Receiver, CGF.getContext().getObjCIdType(),
5095 false, CallArgs, ObjCTypes)
5096 : EmitMessageSend(CGF, ResultType, Sel,
5097 Receiver, CGF.getContext().getObjCIdType(),
5098 false, CallArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005099}
5100
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005101llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005102CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005103 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5104
Daniel Dunbardfff2302009-03-02 05:18:14 +00005105 if (!GV) {
Owen Anderson1c431b32009-07-08 19:05:04 +00005106 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
5107 false, llvm::GlobalValue::ExternalLinkage,
5108 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005109 }
5110
5111 return GV;
5112}
5113
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005114llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005115 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005116 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
5117
5118 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005119 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005120 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005121 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005122 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5123 false, llvm::GlobalValue::InternalLinkage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005124 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005125 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005126 Entry->setAlignment(
5127 CGM.getTargetData().getPrefTypeAlignment(
5128 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005129 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
5130 UsedGlobals.push_back(Entry);
5131 }
5132
5133 return Builder.CreateLoad(Entry, false, "tmp");
5134}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005135
Daniel Dunbar11394522009-04-18 08:51:00 +00005136llvm::Value *
5137CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
5138 const ObjCInterfaceDecl *ID) {
5139 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
5140
5141 if (!Entry) {
5142 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5143 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5144 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005145 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5146 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar11394522009-04-18 08:51:00 +00005147 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005148 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005149 Entry->setAlignment(
5150 CGM.getTargetData().getPrefTypeAlignment(
5151 ObjCTypes.ClassnfABIPtrTy));
5152 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005153 UsedGlobals.push_back(Entry);
5154 }
5155
5156 return Builder.CreateLoad(Entry, false, "tmp");
5157}
5158
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005159/// EmitMetaClassRef - Return a Value * of the address of _class_t
5160/// meta-data
5161///
5162llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5163 const ObjCInterfaceDecl *ID) {
5164 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5165 if (Entry)
5166 return Builder.CreateLoad(Entry, false, "tmp");
5167
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005168 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005169 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005170 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005171 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005172 llvm::GlobalValue::InternalLinkage,
5173 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005174 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005175 Entry->setAlignment(
5176 CGM.getTargetData().getPrefTypeAlignment(
5177 ObjCTypes.ClassnfABIPtrTy));
5178
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005179 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005180 UsedGlobals.push_back(Entry);
5181
5182 return Builder.CreateLoad(Entry, false, "tmp");
5183}
5184
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005185/// GetClass - Return a reference to the class for the given interface
5186/// decl.
5187llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5188 const ObjCInterfaceDecl *ID) {
5189 return EmitClassRef(Builder, ID);
5190}
5191
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005192/// Generates a message send where the super is the receiver. This is
5193/// a message send to self with special delivery semantics indicating
5194/// which class's method should be called.
5195CodeGen::RValue
5196CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
5197 QualType ResultType,
5198 Selector Sel,
5199 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005200 bool isCategoryImpl,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005201 llvm::Value *Receiver,
5202 bool IsClassMessage,
5203 const CodeGen::CallArgList &CallArgs) {
5204 // ...
5205 // Create and init a super structure; this is a (receiver, class)
5206 // pair we will pass to objc_msgSendSuper.
5207 llvm::Value *ObjCSuper =
5208 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
5209
5210 llvm::Value *ReceiverAsObject =
5211 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5212 CGF.Builder.CreateStore(ReceiverAsObject,
5213 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
5214
5215 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005216 llvm::Value *Target;
5217 if (IsClassMessage) {
5218 if (isCategoryImpl) {
5219 // Message sent to "super' in a class method defined in
5220 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005221 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005222 Target = CGF.Builder.CreateStructGEP(Target, 0);
5223 Target = CGF.Builder.CreateLoad(Target);
5224 }
5225 else
5226 Target = EmitMetaClassRef(CGF.Builder, Class);
5227 }
5228 else
Daniel Dunbar11394522009-04-18 08:51:00 +00005229 Target = EmitSuperClassRef(CGF.Builder, Class);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005230
Mike Stumpf5408fe2009-05-16 07:57:57 +00005231 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5232 // ObjCTypes types.
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005233 const llvm::Type *ClassTy =
5234 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5235 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5236 CGF.Builder.CreateStore(Target,
5237 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
5238
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005239 return (LegacyDispatchedSelector(Sel))
5240 ? EmitLegacyMessageSend(CGF, ResultType,EmitSelector(CGF.Builder, Sel),
5241 ObjCSuper, ObjCTypes.SuperPtrCTy,
5242 true, CallArgs,
5243 ObjCTypes)
5244 : EmitMessageSend(CGF, ResultType, Sel,
5245 ObjCSuper, ObjCTypes.SuperPtrCTy,
5246 true, CallArgs);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005247}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005248
5249llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
5250 Selector Sel) {
5251 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
5252
5253 if (!Entry) {
5254 llvm::Constant *Casted =
5255 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5256 ObjCTypes.SelectorPtrTy);
5257 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005258 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005259 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00005260 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005261 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005262 UsedGlobals.push_back(Entry);
5263 }
5264
5265 return Builder.CreateLoad(Entry, false, "tmp");
5266}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005267/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
5268/// objc_assign_ivar (id src, id *dst)
5269///
5270void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
5271 llvm::Value *src, llvm::Value *dst)
5272{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005273 const llvm::Type * SrcTy = src->getType();
5274 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005275 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005276 assert(Size <= 8 && "does not support size > 8");
5277 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5278 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005279 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5280 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005281 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5282 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005283 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005284 src, dst, "assignivar");
5285 return;
5286}
5287
5288/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5289/// objc_assign_strongCast (id src, id *dst)
5290///
5291void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
5292 CodeGen::CodeGenFunction &CGF,
5293 llvm::Value *src, llvm::Value *dst)
5294{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005295 const llvm::Type * SrcTy = src->getType();
5296 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005297 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005298 assert(Size <= 8 && "does not support size > 8");
5299 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5300 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005301 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5302 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005303 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5304 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005305 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005306 src, dst, "weakassign");
5307 return;
5308}
5309
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005310void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
5311 CodeGen::CodeGenFunction &CGF,
5312 llvm::Value *DestPtr,
5313 llvm::Value *SrcPtr,
5314 unsigned long size) {
5315 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5316 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
5317 llvm::Value *N = llvm::ConstantInt::get(ObjCTypes.LongTy, size);
5318 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
5319 DestPtr, SrcPtr, N);
5320 return;
5321}
5322
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005323/// EmitObjCWeakRead - Code gen for loading value of a __weak
5324/// object: objc_read_weak (id *src)
5325///
5326llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
5327 CodeGen::CodeGenFunction &CGF,
5328 llvm::Value *AddrWeakObj)
5329{
Eli Friedman8339b352009-03-07 03:57:15 +00005330 const llvm::Type* DestTy =
5331 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005332 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005333 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005334 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005335 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005336 return read_weak;
5337}
5338
5339/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5340/// objc_assign_weak (id src, id *dst)
5341///
5342void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
5343 llvm::Value *src, llvm::Value *dst)
5344{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005345 const llvm::Type * SrcTy = src->getType();
5346 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005347 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005348 assert(Size <= 8 && "does not support size > 8");
5349 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5350 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005351 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5352 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005353 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5354 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005355 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005356 src, dst, "weakassign");
5357 return;
5358}
5359
5360/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5361/// objc_assign_global (id src, id *dst)
5362///
5363void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
5364 llvm::Value *src, llvm::Value *dst)
5365{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005366 const llvm::Type * SrcTy = src->getType();
5367 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005368 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005369 assert(Size <= 8 && "does not support size > 8");
5370 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5371 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005372 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5373 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005374 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5375 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005376 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005377 src, dst, "globalassign");
5378 return;
5379}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005380
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005381void
5382CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5383 const Stmt &S) {
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005384 bool isTry = isa<ObjCAtTryStmt>(S);
5385 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
5386 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005387 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005388 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005389 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005390 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
5391
5392 // For @synchronized, call objc_sync_enter(sync.expr). The
5393 // evaluation of the expression must occur before we enter the
5394 // @synchronized. We can safely avoid a temp here because jumps into
5395 // @synchronized are illegal & this will dominate uses.
5396 llvm::Value *SyncArg = 0;
5397 if (!isTry) {
5398 SyncArg =
5399 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
5400 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005401 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005402 }
5403
5404 // Push an EH context entry, used for handling rethrows and jumps
5405 // through finally.
5406 CGF.PushCleanupBlock(FinallyBlock);
5407
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005408 CGF.setInvokeDest(TryHandler);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005409
5410 CGF.EmitBlock(TryBlock);
5411 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
5412 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
5413 CGF.EmitBranchThroughCleanup(FinallyEnd);
5414
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005415 // Emit the exception handler.
5416
5417 CGF.EmitBlock(TryHandler);
5418
5419 llvm::Value *llvm_eh_exception =
5420 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
5421 llvm::Value *llvm_eh_selector_i64 =
5422 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64);
5423 llvm::Value *llvm_eh_typeid_for_i64 =
5424 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64);
5425 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5426 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow");
5427
5428 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
5429 SelectorArgs.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005430 SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005431
5432 // Construct the lists of (type, catch body) to handle.
Daniel Dunbarede8de92009-03-06 00:01:21 +00005433 llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005434 bool HasCatchAll = false;
5435 if (isTry) {
5436 if (const ObjCAtCatchStmt* CatchStmt =
5437 cast<ObjCAtTryStmt>(S).getCatchStmts()) {
5438 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005439 const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Steve Naroff7ba138a2009-03-03 19:52:17 +00005440 Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody()));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005441
5442 // catch(...) always matches.
Steve Naroff7ba138a2009-03-03 19:52:17 +00005443 if (!CatchDecl) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005444 // Use i8* null here to signal this is a catch all, not a cleanup.
Owen Anderson69243822009-07-13 04:10:07 +00005445 llvm::Value *Null = VMContext.getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005446 SelectorArgs.push_back(Null);
5447 HasCatchAll = true;
5448 break;
5449 }
5450
Steve Naroff14108da2009-07-10 23:34:53 +00005451 if (CatchDecl->getType()->isObjCIdType() ||
Daniel Dunbarede8de92009-03-06 00:01:21 +00005452 CatchDecl->getType()->isObjCQualifiedIdType()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005453 llvm::Value *IDEHType =
5454 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5455 if (!IDEHType)
5456 IDEHType =
Owen Anderson1c431b32009-07-08 19:05:04 +00005457 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
5458 false,
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005459 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00005460 0, "OBJC_EHTYPE_id");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005461 SelectorArgs.push_back(IDEHType);
5462 HasCatchAll = true;
5463 break;
5464 }
5465
5466 // All other types should be Objective-C interface pointer types.
Steve Naroff14108da2009-07-10 23:34:53 +00005467 const ObjCObjectPointerType *PT =
5468 CatchDecl->getType()->getAsObjCObjectPointerType();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005469 assert(PT && "Invalid @catch type.");
Steve Naroff14108da2009-07-10 23:34:53 +00005470 const ObjCInterfaceType *IT = PT->getInterfaceType();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005471 assert(IT && "Invalid @catch type.");
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005472 llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005473 SelectorArgs.push_back(EHType);
5474 }
5475 }
5476 }
5477
5478 // We use a cleanup unless there was already a catch all.
5479 if (!HasCatchAll) {
5480 SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
Daniel Dunbarede8de92009-03-06 00:01:21 +00005481 Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005482 }
5483
5484 llvm::Value *Selector =
5485 CGF.Builder.CreateCall(llvm_eh_selector_i64,
5486 SelectorArgs.begin(), SelectorArgs.end(),
5487 "selector");
5488 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005489 const ParmVarDecl *CatchParam = Handlers[i].first;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005490 const Stmt *CatchBody = Handlers[i].second;
5491
5492 llvm::BasicBlock *Next = 0;
5493
5494 // The last handler always matches.
5495 if (i + 1 != e) {
5496 assert(CatchParam && "Only last handler can be a catch all.");
5497
5498 llvm::BasicBlock *Match = CGF.createBasicBlock("match");
5499 Next = CGF.createBasicBlock("catch.next");
5500 llvm::Value *Id =
5501 CGF.Builder.CreateCall(llvm_eh_typeid_for_i64,
5502 CGF.Builder.CreateBitCast(SelectorArgs[i+2],
5503 ObjCTypes.Int8PtrTy));
5504 CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id),
5505 Match, Next);
5506
5507 CGF.EmitBlock(Match);
5508 }
5509
5510 if (CatchBody) {
5511 llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end");
5512 llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler");
5513
5514 // Cleanups must call objc_end_catch.
5515 //
Mike Stumpf5408fe2009-05-16 07:57:57 +00005516 // FIXME: It seems incorrect for objc_begin_catch to be inside this
5517 // context, but this matches gcc.
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005518 CGF.PushCleanupBlock(MatchEnd);
5519 CGF.setInvokeDest(MatchHandler);
5520
5521 llvm::Value *ExcObject =
Chris Lattner8a569112009-04-22 02:15:23 +00005522 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005523
5524 // Bind the catch parameter if it exists.
5525 if (CatchParam) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005526 ExcObject =
5527 CGF.Builder.CreateBitCast(ExcObject,
5528 CGF.ConvertType(CatchParam->getType()));
5529 // CatchParam is a ParmVarDecl because of the grammar
5530 // construction used to handle this, but for codegen purposes
5531 // we treat this as a local decl.
5532 CGF.EmitLocalBlockVarDecl(*CatchParam);
5533 CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005534 }
5535
5536 CGF.ObjCEHValueStack.push_back(ExcObject);
5537 CGF.EmitStmt(CatchBody);
5538 CGF.ObjCEHValueStack.pop_back();
5539
5540 CGF.EmitBranchThroughCleanup(FinallyEnd);
5541
5542 CGF.EmitBlock(MatchHandler);
5543
5544 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5545 // We are required to emit this call to satisfy LLVM, even
5546 // though we don't use the result.
5547 llvm::SmallVector<llvm::Value*, 8> Args;
5548 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005549 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005550 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
5551 0));
5552 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5553 CGF.Builder.CreateStore(Exc, RethrowPtr);
5554 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5555
5556 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5557
5558 CGF.EmitBlock(MatchEnd);
5559
5560 // Unfortunately, we also have to generate another EH frame here
5561 // in case this throws.
5562 llvm::BasicBlock *MatchEndHandler =
5563 CGF.createBasicBlock("match.end.handler");
5564 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattner8a569112009-04-22 02:15:23 +00005565 CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005566 Cont, MatchEndHandler,
5567 Args.begin(), Args.begin());
5568
5569 CGF.EmitBlock(Cont);
5570 if (Info.SwitchBlock)
5571 CGF.EmitBlock(Info.SwitchBlock);
5572 if (Info.EndBlock)
5573 CGF.EmitBlock(Info.EndBlock);
5574
5575 CGF.EmitBlock(MatchEndHandler);
5576 Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5577 // We are required to emit this call to satisfy LLVM, even
5578 // though we don't use the result.
5579 Args.clear();
5580 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005581 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005582 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
5583 0));
5584 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5585 CGF.Builder.CreateStore(Exc, RethrowPtr);
5586 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5587
5588 if (Next)
5589 CGF.EmitBlock(Next);
5590 } else {
5591 assert(!Next && "catchup should be last handler.");
5592
5593 CGF.Builder.CreateStore(Exc, RethrowPtr);
5594 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5595 }
5596 }
5597
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005598 // Pop the cleanup entry, the @finally is outside this cleanup
5599 // scope.
5600 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5601 CGF.setInvokeDest(PrevLandingPad);
5602
5603 CGF.EmitBlock(FinallyBlock);
5604
5605 if (isTry) {
5606 if (const ObjCAtFinallyStmt* FinallyStmt =
5607 cast<ObjCAtTryStmt>(S).getFinallyStmt())
5608 CGF.EmitStmt(FinallyStmt->getFinallyBody());
5609 } else {
5610 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
5611 // @synchronized.
Chris Lattnerbbccd612009-04-22 02:38:11 +00005612 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005613 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005614
5615 if (Info.SwitchBlock)
5616 CGF.EmitBlock(Info.SwitchBlock);
5617 if (Info.EndBlock)
5618 CGF.EmitBlock(Info.EndBlock);
5619
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005620 // Branch around the rethrow code.
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005621 CGF.EmitBranch(FinallyEnd);
5622
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005623 CGF.EmitBlock(FinallyRethrow);
Chris Lattner8a569112009-04-22 02:15:23 +00005624 CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005625 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005626 CGF.Builder.CreateUnreachable();
5627
5628 CGF.EmitBlock(FinallyEnd);
5629}
5630
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005631/// EmitThrowStmt - Generate code for a throw statement.
5632void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
5633 const ObjCAtThrowStmt &S) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005634 llvm::Value *Exception;
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005635 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005636 Exception = CGF.EmitScalarExpr(ThrowExpr);
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005637 } else {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005638 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
5639 "Unexpected rethrow outside @catch block.");
5640 Exception = CGF.ObjCEHValueStack.back();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005641 }
5642
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005643 llvm::Value *ExceptionAsObject =
5644 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
5645 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
5646 if (InvokeDest) {
5647 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattnerbbccd612009-04-22 02:38:11 +00005648 CGF.Builder.CreateInvoke(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005649 Cont, InvokeDest,
5650 &ExceptionAsObject, &ExceptionAsObject + 1);
5651 CGF.EmitBlock(Cont);
5652 } else
Chris Lattnerbbccd612009-04-22 02:38:11 +00005653 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005654 CGF.Builder.CreateUnreachable();
5655
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005656 // Clear the insertion point to indicate we are in unreachable code.
5657 CGF.Builder.ClearInsertionPoint();
5658}
Daniel Dunbare588b992009-03-01 04:46:24 +00005659
5660llvm::Value *
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005661CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
5662 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00005663 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00005664
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005665 // If we don't need a definition, return the entry if found or check
5666 // if we use an external reference.
5667 if (!ForDefinition) {
5668 if (Entry)
5669 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00005670
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005671 // If this type (or a super class) has the __objc_exception__
5672 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00005673 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005674 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005675 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005676 llvm::GlobalValue::ExternalLinkage,
5677 0,
5678 (std::string("OBJC_EHTYPE_$_") +
Owen Anderson1c431b32009-07-08 19:05:04 +00005679 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005680 }
5681
5682 // Otherwise we need to either make a new entry or fill in the
5683 // initializer.
5684 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005685 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00005686 std::string VTableName = "objc_ehtype_vtable";
5687 llvm::GlobalVariable *VTableGV =
5688 CGM.getModule().getGlobalVariable(VTableName);
5689 if (!VTableGV)
Owen Anderson1c431b32009-07-08 19:05:04 +00005690 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
5691 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00005692 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00005693 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00005694
5695 llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2);
5696
5697 std::vector<llvm::Constant*> Values(3);
5698 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
5699 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005700 Values[2] = GetClassGlobal(ClassName);
Daniel Dunbare588b992009-03-01 04:46:24 +00005701 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
5702
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005703 if (Entry) {
5704 Entry->setInitializer(Init);
5705 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00005706 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005707 llvm::GlobalValue::WeakAnyLinkage,
5708 Init,
5709 (std::string("OBJC_EHTYPE_$_") +
Owen Anderson1c431b32009-07-08 19:05:04 +00005710 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005711 }
5712
Daniel Dunbar04d40782009-04-14 06:00:08 +00005713 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005714 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005715 Entry->setAlignment(8);
5716
5717 if (ForDefinition) {
5718 Entry->setSection("__DATA,__objc_const");
5719 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
5720 } else {
5721 Entry->setSection("__DATA,__datacoal_nt,coalesced");
5722 }
Daniel Dunbare588b992009-03-01 04:46:24 +00005723
5724 return Entry;
5725}
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005726
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005727/* *** */
5728
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005729CodeGen::CGObjCRuntime *
5730CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00005731 return new CGObjCMac(CGM);
5732}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005733
5734CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005735CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005736 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005737}